forked from rickypeng99/yugioh_web_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (187 loc) · 5.64 KB
/
index.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 io = require("socket.io")(4001, {
// handling CORS
cors: {
origin: '*',
}
});
const matched_ids = {}
const match_meta = {}
io.on("connection", socket => {
// either with send()
socket.send(`Hello ${socket.id} from the server!`);
// handle the event sent with socket.send()
socket.on("message", (data) => {
console.log(data);
});
socket.on("exchange_deck", (data) => {
sending_deck(socket, data)
})
/**
* monster related
*/
socket.on("summon", (data) => {
sending_summon(socket, data)
})
socket.on("attack_start", (data) => {
sending_attack_start(socket, data)
})
socket.on("attack_ack", (data) => {
sending_attack_ack(socket, data)
})
socket.on("change_phase", (data) => {
sending_change_phase(socket, data)
})
// socket.on("tribute", (data) => {
// sending_tribute(socket, data)
// })
socket.on("move_card_to_graveyard", (data) => {
sending_move_card_to_graveyard(socket, data)
})
socket.on('activate_effect', (data) => {
const opponent = matched_ids[socket.id]
const unique_id = get_match_unique_id(socket.id, opponent)
data.owner = socket.id
if (match_meta[unique_id].effect.chain_stack) {
match_meta[unique_id].effect.chain_stack.push(data)
} else {
match_meta[unique_id].effect.chain_stack = [data]
}
sending_card_activate(socket, data)
})
socket.on('effect_ack', (data) => {
const opponent = matched_ids[socket.id]
const unique_id = get_match_unique_id(socket.id, opponent)
console.log("ack " + socket.id)
if (match_meta[unique_id].effect.ack) {
match_meta[unique_id].effect.ack[socket.id] = true
if (match_meta[unique_id].effect.ack[opponent]) {
match_meta[unique_id].effect.ack = {}
// send operation
sending_card_operate(unique_id)
}
} else {
match_meta[unique_id].effect.ack = {}
match_meta[unique_id].effect.ack[socket.id] = true
// TODO: send topmost card back
sending_opponent_effect_ack(socket, data)
}
})
socket.on('card_finish_operate', (data) => {
// sending_opponent_card_operate(socket, data)
const opponent = matched_ids[socket.id]
const unique_id = get_match_unique_id(socket.id, opponent)
if (match_meta[unique_id].effect.chain_stack.length > 0) {
sending_card_operate(unique_id)
} else {
// reset the current effect chainings to default
match_meta[unique_id].effect = {}
sending_final_ack(socket, data)
}
})
seeking_match(socket)
});
// SENDING functions
const sending_move_card_to_graveyard = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_move_card_to_graveyard", {
data: data
})
}
// const sending_tribute = (socket, data) => {
// io.to(matched_ids[socket.id]).emit("opponent_tribute", {
// data: data
// })
// }
const sending_change_phase = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_change_phase", {
data: data
})
}
const sending_deck = (socket, data) => {
io.to(matched_ids[socket.id]).emit("receive_deck", {
deck: data.deck,
})
}
const sending_summon = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_summon", {
data: data
})
}
const sending_attack_start = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_attack_start", {
data: data
})
}
const sending_attack_ack = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_attack_ack", {
data: data
})
}
const sending_card_activate = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_card_activate", {
data: data
})
}
const sending_opponent_effect_ack = (socket, data) => {
io.to(matched_ids[socket.id]).emit("opponent_effect_ack", {
data: data
})
}
const sending_card_operate = (mactch_id) => {
const topMost = match_meta[mactch_id].effect.chain_stack.pop()
console.log(topMost)
io.to(topMost.owner).emit("card_operate", {
data: topMost
})
}
// const sending_opponent_card_operate = (socket, data) => {
// io.to(matched_ids[socket.id]).emit("opponent_card_operate", {
// data: data
// })
// }
// all chainings within this round of effect's activation is finished
const sending_final_ack = (socket, data) => {
io.to(socket.id).emit("final_ack", {
data: data
})
io.to(matched_ids[socket.id]).emit("final_ack", {
data: data
})
}
const seeking_match = (socket) => {
let has_space = false
for (const key of Object.keys(matched_ids)) {
if (!matched_ids[key]) {
has_space = true
matched_ids[key] = socket.id
matched_ids[socket.id] = key
// a dictionary to record info for the match
match_meta[get_match_unique_id(key, socket.id)] = {
effect: {} // to deal with current effect chainings
}
//decides who starts first
const both_players = [socket.id, key]
const player_starts = both_players[Math.floor(Math.random() * both_players.length)];
//notify both players that they are matched with each other
io.to(key).emit("matched", {
my_id: key,
opponent: socket.id,
player_starts: player_starts
})
io.to(socket.id).emit("matched", {
my_id: socket.id,
opponent: key,
player_starts: player_starts
})
break
}
}
if (!has_space) {
// start to wait for a potential match
matched_ids[socket.id] = false
}
}
const get_match_unique_id = (player1, player2) => {
let players = [player1, player2]
players.sort((a, b) => a.localeCompare(b))
return players.join('-')
}