-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.min.js
More file actions
1 lines (1 loc) · 3.44 KB
/
index.min.js
File metadata and controls
1 lines (1 loc) · 3.44 KB
1
const app=require("express")(),http=require("http").createServer(app),WebSocket=require("ws"),ws=new WebSocket.Server({port:667});let calcPlayers=()=>{var e=0;return rooms.forEach(({players:t})=>{t.forEach(t=>{e++})}),e};var rooms=[];app.get("/",(e,t)=>{t.send(`${rooms.length} room(s) active. ${calcPlayers()} player(s) active.`)}),app.post("/webhook",(e,t)=>{console.log("received webhook. Restarting now."),process.exit()}),http.listen(666,()=>{console.log("listening on *:666")});class PlayerStatus{}PlayerStatus.BETTING="betting",PlayerStatus.WAITING="waiting",PlayerStatus.LOST="lost",PlayerStatus.WON="won",PlayerStatus.WON_BET="won_bet",PlayerStatus.CONNECTING="connecting",PlayerStatus.DISCONNECTED="disconnected";class Player{constructor(e,t){this.socket=e,this.room=t,this.id=rooms[this.room].players.length||0,this.name="Player",this.status=PlayerStatus.CONNECTING,this.money=1e4,this.bet=0,this.choice=0,this.hasBet=!1,e.send("connected"),console.log("New player is "+this.status),e.on("message",e=>this.handleClientMessage(e)),e.on("close",()=>this.leaveGame())}leaveGame(){console.log("player leaving game");try{rooms[this.room].players.splice(this.id,1)}catch(e){}try{rooms[this.room].players.length<=0&&rooms.splice(rooms.indexOf(rooms[this.room]),1)}catch(e){}}handleClientMessage(e){if("connected"==e)this.setStatus(PlayerStatus.WAITING);else try{"bet"==(e=JSON.parse(e)).type&&e.bet<=this.money&&e.bet>=100&&e.choice<=6&&e.choice>=1&&(this.bet=e.bet,this.choice=e.choice,this.hasBet=!0)}catch(t){console.log("received: %s",e)}}setStatus(e){this.status!=PlayerStatus.LOST&&(this.status=e),console.log(this.status)}resetPlayer(){this.money=1e4,this.nextRound()}nextRound(){this.bet=0,this.choice=0,this.hasBet=!1}}class Room{constructor(e){this.roomCode=e,this.maxPlayers=8,this.minPlayers=2,this.players=[],this.roll=0,this.running=!0,this.hasPlayerJoined=!1,setInterval((e=this)=>{e.hasPlayerJoined&&e.gameLoop()},1e3)}gameLoop(){if(this.players>=this.minPlayers&&this.players<=maxPlayers&&this.running){for(this.players.filter(e=>e.status!=PlayerStatus.LOST).length<=1&&(running=!1);!e();)this.players.forEach(e=>{e.hasBet?e.setStatus(PlayerStatus.WAITING):e.setStatus(PlayerStatus.BETTING)});let t=1+~~(6*Math.random());this.roll=t,this.Update();let s=0,a=[];this.players.filter(e=>e.status!=PlayerStatus.LOST).forEach((e,r)=>{e.choice!=t&&(setPlayerStatus(e,PlayerStatus.LOST_BET),e.money-=e.bet,s+=e.bet,e.money<=0&&setPlayerStatus(PlayerStatus.LOST)),e.choice==t&&(setPlayerStatus(e,PlayerStatus.WON_BET),a.push(e))}),a.forEach(e=>{e.money+=~~(s/a.length)}),this.players.forEach(e=>e.nextRound())}else 0==this.running&&(this.players.forEach(e=>{e.status==PlayerStatus.LOST&&e.resetPlayer()}),running=!0);function e(){return 0==this.players.filter(e=>!e.hasBet).length}this.Update()}addPlayer(e,t){let s=new Player(e,t);this.players.push(s),this.hasPlayerJoined=!0}Update(){let e=[];this.players.forEach(({socket:t,...s}=player)=>{e.push(s)}),this.players.forEach(({socket:t,...s}=player)=>{t.send(JSON.stringify({player:s,roll:this.roll,players:e}))})}}function makeid(e){for(var t="",s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",a=0;a<s.length;a++)t+=s.charAt(Math.floor(Math.random()*s.length));return t}ws.on("connection",e=>{var t;console.log("a user connected"),0==rooms.length||rooms[rooms.length-1].players.length>=rooms[rooms.length-1].maxPlayers?(t=new Room(makeid(5)),rooms.push(t)):t=rooms.filter(e=>e.players.length<e.maxPlayers)[0],t.addPlayer(e,rooms.indexOf(t))});