-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
141 lines (114 loc) · 5.31 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { PresidentCardGame } = require("./src/CardGames/PresidentCardGame");
const { PresidentPlayer } = require("./src/CardGames/Players/PresidentPlayer");
let express = require("express");
let ws = require("ws");
const path = require("path")
const enableWs = require("express-ws")
const helpers = require("./src/helpers")
let app = express()
app.use(express.static(path.join(__dirname, 'build')))
const Ws_Server = enableWs(app)
let wss = Ws_Server.getWss();
app.get("/", function(req, res){res.sendFile(path.join(__dirname, 'build',"index.html"))})
let games = [];
app.ws("/api", function(ws, req){
ws.on("message", async function (msg){
let msgObj = JSON.parse(msg);
// make sure msgObj has a good shape:
if(!msgObj.hasOwnProperty("data")){
return;
}
if(!msgObj.hasOwnProperty("command")){
return;
}
switch(msgObj.command){
case("createTable"):
// make sure object is in good shape:
if(!msgObj.data.hasOwnProperty("tableName")){
console.log(1)
return;
}
if(!helpers.StringIsAlphaNumeric(msgObj.data.tableName)){
console.log(2)
ws.send(JSON.stringify({state: {error: "Table name not alphanumeric!"}}))
return;
}
// make sure table names do not conflict:
if(false){
console.log(3)
ws.send(JSON.stringify({state: {error: "Table name not available."}}))
return;
}
// make sure there is no table attached to the socket:
if(false){
console.log(4)
ws.send(JSON.stringify({state: {error: "Socket has table. Cannot create new table"}}))
return;
}
let newGame = new PresidentCardGame(msgObj.data.numberOfPlayers, msgObj.data.tableName);
games.push(newGame);
let player = new PresidentPlayer(ws, newGame.deck, newGame.eventReciever);
await newGame.addPlayer(player);
console.log(ws.table)
ws.send(JSON.stringify({state: {msg: "Created Table", data: {
name: newGame.tableName,
playerNames: newGame.players.map((player) => player.name),
tableValue: newGame.tableValue,
president: newGame.president,
vicePresident: newGame.vicePresident,
trash: newGame.trash,
viceTrash: newGame.viceTrash,
turn: newGame.turnIndex,
targetNumberOfPlayers: newGame.targetNumberOfPlayers,
phase: newGame.phase,
AllEligiblePlayersPassed: newGame.AllEligiblePlayersPassed()
}}}))
break;
case("joinTable"):
console.log(msgObj)
// make sure object is in good shape / and is not malicious:
if(!msgObj.data.hasOwnProperty("tableName")){
return;
}
if(!helpers.StringIsAlphaNumeric(msgObj.data.tableName)){
ws.send(JSON.stringify({state: {msg: "error", error: "Table name not alphanumeric!"}}))
return;
}
// make sure table already exists:
let searchedGame = games.filter((elem) => elem.tableName == msgObj.data.tableName)
console.log(games)
if(searchedGame.length == 0){
ws.send(JSON.stringify({state: {msg: "error", error: "Table not found!"}}))
return;
}
// if the target number of players is reached: do not allow more players
if(searchedGame[0].targetNumberOfPlayers == searchedGame[0].players.length){
ws.send(JSON.stringify({state: {msg: "error", error: "Table name not available."}}))
return;
}
let Newplayer = new PresidentPlayer(ws,searchedGame[0].deck,searchedGame[0].eventReciever);
await searchedGame[0].addPlayer(Newplayer);
// if with the newly joined player the target number of players is reached, start the round:
if(searchedGame[0].targetNumberOfPlayers == searchedGame[0].players.length){
searchedGame[0].phase = "round"
let p = searchedGame[0].runGame();
break;
}
//ws.table.broadcastNewState("Player Joined");
//console.log(ws.table)
break;
case("changeName"):
if(!helpers.StringIsAlphaNumeric(msgObj.data)){
ws.send(JSON.stringify({state: {error: "Player name not alphanumeric!"}}))
return;
}
ws.playerName = String(msgObj.data)
ws.send(JSON.stringify({state: {success: "Player name changed to: "+ ws.playerName}}))
break;
}
});
ws.on("close", function(){
})
ws.send("Connection established");
})
app.listen(3000);