-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
233 lines (196 loc) · 5.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const formatMessage = require("./utils/messages");
const {
userJoin,
getUsers,
getCurrentUser,
userLeave,
getRoomUsers,
} = require("./utils/users");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// Set static folder
app.use(express.static("public"));
const botName = "System";
var rollableDice = 5;
var rollAvailable = 3;
var newRoll = [];
var storeDice = [];
var currentTurn = "";
var currentRound = 1;
var startOrigin = "";
// bug
// 1. player cannot use the same name
// 2. fix point calculation full house, large straight (Fixed)
// 3. on small screen select dice bug (maybe uuid line:708, body undifined line:710)
// 4. Randomly Dice not roll for oneplayer.
// Run when client connects
io.on("connection", (socket) => {
socket.on("joinRoom", ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
// Welcome current user
socket.emit("message", formatMessage(botName, "Welcome to Waiting Room!"));
// Broadcast when a user connects
socket.broadcast.to(user.room).emit(
"message",
formatMessage(botName, `${user.name} has joined the chat`)
);
// Send users and room info
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
});
// Return users include points
socket.on("playerPoint", () => {
let users = getUsers();
io.emit("playerPoint", users);
});
// send click pos to all user
socket.on("selectPosition", data => {
io.emit("selectPosition", data);
});
// store dice
socket.on("selectDice", (data) => {
if (data.player_id == socket.id) {
rollableDice--;
storeDice.push(data.dice_value);
}
io.emit("showStoreDice", storeDice);
});
// Listen for Game Start
socket.on("StartGame", (data) => {
rollableDice = 5;
rollAvailable = 3;
newRoll = [];
storeDice = [];
currentTurn = data.player.id;
startOrigin = data.player.id;
currentRound = 1;
io.emit("StartGame", data);
io.emit("SetRound", currentRound);
});
// Listen for Die Rolling
socket.on("rollTheDice", (data) => {
const user = getCurrentUser(socket.id);
newRoll = [];
let faceValue;
for (i = 1; i <= rollableDice; i++) {
faceValue = Math.floor(Math.random() * 6) + 1;
newRoll.push(faceValue);
}
data["player"] = user;
data["storeDice"] = storeDice;
data["newRoll"] = newRoll;
rollAvailable--;
data["rollAvailable"] = rollAvailable;
io.emit("rollTheDice", data);
});
// Save Point and Update Cell
socket.on("savePoint", (data) => {
const user = getCurrentUser(socket.id);
if (data.player_id == user.id) {
user.point[data.type] = data.point;
}
io.emit("savePoint", data);
});
// Reset
socket.on("endTurn", () => {
storeDice = [];
newRoll = [];
rollableDice = 5;
rollAvailable = 3;
let users = getUsers();
let nextPlayer = {};
let nextTurn = "";
for (let i = 0; i < users.length; i++) {
if (users[i].id == currentTurn) {
if (users[i + 1] != undefined) {
nextPlayer = users[i + 1];
nextTurn = users[i + 1].id;
} else {
nextPlayer = users[0];
nextTurn = users[0].id;
}
}
}
currentTurn = nextTurn;
io.emit("endTurn", nextPlayer);
if (currentTurn == startOrigin) {
currentRound++;
io.emit("SetRound", currentRound);
}
});
// power
socket.on("powerUsed", (power) => {
const user = getCurrentUser(socket.id);
switch (power.type) {
case 'addOneDice':
power["newValue"] = Math.floor(Math.random() * 6) + 1;
break;
case 'specialDice':
power["newValue"] = Math.floor(Math.random() * 6) + 1;
break;
}
if (power.player_id == user.id && rollableDice == 5) {
user.power[power.type]--;
io.emit("powerUsed", power);
}
});
// return user and undo rollAvailable
socket.on("powerHandler", (data) => {
const user = getCurrentUser(socket.id);
if (data.player_id == user.id) {
data['player'] = user;
rollAvailable++;
io.emit('powerHandler', data);
}
});
// Save Power Point
socket.on("powerPointHandler", (data) => {
if (data.para == 'specialDice') {
let user = getCurrentUser(socket.id);
user['specialDice'] = data.point;
io.emit("displayChange", {player_id:user.id, change:data.point, type:'green'});
} else if (data.para == 'randomAttack') {
let users = getUsers();
users.forEach(u => {
let change = Math.floor(Math.random() * data.point) + 1;
u['minus'] += change;
io.emit("displayChange", {player_id:u.id, change:change, type:'red'});
});
} else {
// for choseAttack where para is user id
let user = getCurrentUser(data.para);
user['minus'] += data.point;
io.emit("displayChange", {player_id:user.id, change:data.point, type:'red'});
}
io.emit("playerPoint", getUsers());
});
// Listen for chatMessage
socket.on("chatMessage", (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit("message", formatMessage(user.name, msg));
});
// Runs when client disconnects
socket.on("disconnect", () => {
let user = userLeave(socket.id);
if (user) {
io.to(user.room).emit(
"message",
formatMessage(botName, `${user.name} has left the chat`)
);
// Send users and room info
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));