-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketFunc.js
349 lines (317 loc) · 11 KB
/
socketFunc.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"use strict";
var userList = require(__dirname+'/private/users.js');
var unlogUserList = Object.keys(userList);
var Room = require(__dirname+'/room.js');
var User = require(__dirname+'/user.js');
var g = require(__dirname+'/global.js');
var rooms = g.rooms;
var pages = g.pages;
const stage = g.stage;
const ROOMAMOUNT = g.ROOMAMOUNT;
var generalStatus = g.generalStatus;
function sf(socket){
// socket.username
socket.paging = "login"; // login, waiting, battle
socket.room = -1;
socket.asking = false;
console.log(`${new Date().toLocaleString()} a user connected`);
// now unavailable
socket.on('register', register);
/* two types: log in and change room
* data: username, password, room, type{new, change}
* emit: userjoined, verified, otherReady, generateCard
*/
socket.on('verifyWaiting', verify);
socket.on('generateCard', generate);
socket.on('ready', onReady);
socket.on('nextStage', nextStage);
socket.on('useCard', useCard);
socket.on('buyCard', buyCard);
socket.on('disconnect', disconnect);
}
function register(data){
//socket.paging
if(this.paging !== 'login') return;
this.emit('registered',{
valid: false,
errorcode: 1
});
return;
}
function verify(data){
// socket.username
// socket.paging
// socket.room
// rooms[],unlogUserList[],userList
if(typeof(data) != "object"
|| ( rooms[data.room.toString()] !== undefined
&& rooms[data.room.toString()].host !== undefined
&& rooms[data.room.toString()].paging === "battle")
|| data.room >= ROOMAMOUNT ){
this.emit('verified',{
valid: false,
errorcode: 1,
from: data.type,
});
return;
}
if(data.type === "new"){//log in
if(this.paging != 'login') return;
console.log(`${new Date().toLocaleString()} attempt to login: ${data.username}`);
console.log(`inputed password: ${data.password}`);
console.log(`valid password: ${userList[data.username]}`);
if(unlogUserList.includes(data.username)
&& data.password === userList[data.username]){
unlogUserList.remove(data.username);
}// check if valid
else { // login failed
this.emit('verified',{
valid: false,
errorcode: 2
});
return;
}
console.log(`${data.username} logged in`);
this.username = data.username;
this.paging = 'waiting';
}
else if(data.type === "change"){
this.leave(this.room);
delete rooms[this.room].users[this.username];
rooms[this.room].sockets.remove(this);
this.to(this.room).emit('user left',{
username: this.username,
});
if(Object.keys(rooms[this.room].users).length === 0){
delete rooms[this.room];
}
console.log(`${new Date().toLocaleString()} ${this.username} changed room from ${this.room} to ${data.room.toString()}`);
}
else return;
this.room = data.room.toString();
this.join(this.room);
if(rooms[this.room] === undefined
|| rooms[this.room].users === undefined
|| Object.keys(rooms[this.room].users).length === 0
|| rooms[this.room].host === undefined){ // initialize
rooms[this.room] = new Room({host:this.username, no: this.room});
}
let room = rooms[this.room];
room.users[this.username] = new User({state:"waiting",room:this.room,socket:this});
room.sockets.push(this);
room.show();
this.to(this.room).emit('user joined',{
username: this.username,
});
this.emit('verified',{
valid: true,
users: Object.keys(room.users),
page: data.type === "new"? pages[1] : undefined,
roomhost: rooms[this.room].host
});
for(let user in room.users)
this.emit('otherReady',{
name: user,
prepared: room.users[user].state === 'prepared',
already: false
});
if(room.supply !== undefined){
this.emit('generateCard',room.supply);
}
}
function generate(data){ // emit generateCard
if(typeof(this.room) === 'undefined' || typeof(rooms[this.room]) === 'undefined')return;
if(rooms[this.room].paging !== "waiting") return;
if(this.username !== rooms[this.room].host){
return;
}
console.log(`${new Date().toLocaleString()} room#${this.room} sending card..`);
if(data.index !== undefined){
rooms[this.room].changeCard(data.limit, data.index);
}
else{
rooms[this.room].generateCard(data.limit, data.type);
}
for(let username in rooms[this.room].users){
rooms[this.room].users[username].state = "waiting";
}
this.emit('generateCard',rooms[this.room].supply);
this.to(this.room).emit('generateCard',rooms[this.room].supply);
}
function onReady(prepared){ //emit otherReady, startGame
if(rooms[this.room].paging !== "waiting" || prepared === undefined) return;
let room = rooms[this.room];
let user = room.users[this.username];
if(this.username !== room.host){
if(user.state === 'waiting')
user.state = 'prepared';
else user.state = 'waiting';
}
console.log(user.state);
let ppusers = 0;// prepared users
for(let userName in room.users){
ppusers += room.users[userName].state === 'prepared' ? 1 : 0;
}
console.log(ppusers);
if(this.username === room.host){
if(Object.keys(room.users).length > 1
&& ppusers >= Object.keys(room.users).length - 1
&& room.supply !== undefined){
room.initialGame();
return;
}
}
this.to(this.room).emit('otherReady',{
name: this.username,
prepared: prepared,
already: ppusers >= Object.keys(room.users).length - 1
});
}
async function nextStage(){
if(this.paging !== "battle") return;
let room = rooms[this.room];
let user = room.users[this.username];
if(this.username !== room.nowPlayer) return;
console.log("in nextstage");
room.nowStage ++;
// Action
if(room.nowStage % 3 === 0){
[user.money, user.action, user.buy, user.actionUsed] = [0, 1, 1, 0];
if(user.extraTurn === true){
user.extraTurn = 'done';
}
else {
user.extraTurn = false;
room.nowPlayerPoint ++;
}
room.nowPlayer = room.userOrder[room.nowPlayerPoint % room.userOrder.length];
let np = room.users[room.nowPlayer];
[np.money, np.action, np.buy, np.actionUsed] = [0, 1, 1, 0];
room.users[room.nowPlayer].gained = {supply:[],basic:[]};
room.users[room.nowPlayer].duration.forEach((card) => {
room.users[room.nowPlayer].actionArea.push(card);
card.duration(room.users[room.nowPlayer],card);
np.socket.emit("statusUpdate",{
usingCard: card,
});
np.socket.to(np.socket.room).emit("statusUpdate",{
usingCard: card,
});
});
room.users[room.nowPlayer].duration = [];
generalStatus(room.users[room.nowPlayer].socket);
}
// Buy
if(room.nowStage % 3 === 1){
}
// Cleanup
if(room.nowStage % 3 == 2){
// user.actionArea = user.actionArea.filter(card => typeof(card.duration) === 'undefined');
user.actionArea.forEach((card,index)=>{
card.used = false;
});
user.drop('all','actionArea');
user.drop('all','hand');
if(user.extraTurn === true){
user.draw(3);
}
else {
user.draw(5);
};
}
console.log(`${new Date().toLocaleString()} in room ${this.room}`);
console.log(`in stage ${room.nowStage}`);
console.log(`in player ${room.nowPlayer}`);
generalStatus(this, room.nowStage % 3 === 0);
}
async function useCard(index){
if(this.paging !== "battle" || index === undefined) return;
let room = rooms[this.room];
let user = room.users[this.username];
if(this.username !== room.nowPlayer
|| user.hand[index] === undefined
|| user.hand[index].used
|| user.aCardUsing) return;
let usingCard = user.hand[index];
console.log(`${new Date().toLocaleString()} in room#${this.room}`);
console.log("in using card:");
console.log(usingCard.name.ch, usingCard.no, usingCard.used);
if(usingCard.types.includes('行动') && stage[room.nowStage % 3] !== 'Action'
|| usingCard.types.includes('行动') && user.action < 1
|| usingCard.types.includes('资源') && stage[room.nowStage % 3] !== 'Buy'
|| typeof(usingCard.use) !== "function") return;
usingCard.used = true;
user.aCardUsing = true;
if(typeof(usingCard.duration) === 'undefined')user.actionArea.push(usingCard);
user.hand.splice(index,1);//affect state update
if(usingCard.types.includes('行动')) {
user.action -= 1;
user.actionUsed ++;
}
this.emit("statusUpdate",{
usingCard: usingCard,
nowAction:user.action,
});
this.to(this.room).emit("statusUpdate",{
usingCard: usingCard,
nowAction:user.action,
});
await usingCard.use(user,usingCard);
console.log("used");
user.aCardUsing = false;
generalStatus(this);
}
async function buyCard(data){
if(this.paging !== "battle" || data === undefined
|| data.src !== "supply" && data.src !== "basic") return;
let room = rooms[this.room];
let user = room.users[this.username];
let index = data.index;
let src = data.src;
if(this.username != room.nowPlayer) return;
if(room[src][index] === undefined
|| user.money < room[src][index].cost
|| user.buy < 1
|| room[src + "Remain"][index] < 1) return;
user.money -= room[src][index].cost;
user.buy -= 1;
console.log(`${new Date().toLocaleString()} in room ${this.room}`);
console.log("in buying Card");
await user.gainCard("drops", src, index,'buy');
}
function disconnect(){ //emit userleft
console.log(new Date().toLocaleString());
if(this.paging === 'login'){
console.log('a user disconnected');
}
else if(this.paging === 'result'){
console.log(this.username + ' disconnected');
unlogUserList.push(this.username);
}
else if(this.paging === 'battle'){
let room = rooms[this.room];
console.log(this.username + ' disconnected');
room.endGame();
room.show();
unlogUserList.push(this.username);
this.to(this.room).emit('user left',{
username: this.username
});
}
else {
let room = rooms[this.room];
console.log(this.username + ' disconnected');
rooms[this.room].sockets.remove(this);
this.leave(this.room);
delete rooms[this.room].users[this.username];
if(rooms[this.room].host === this.username){
rooms[this.room].host = Object.keys(rooms[this.room].users)[0];
}
room.show();
unlogUserList.push(this.username);
this.to(this.room).emit('user left',{
username: this.username
});
}
}
module.exports = sf;