-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
205 lines (189 loc) · 5.05 KB
/
Copy pathserver.js
File metadata and controls
205 lines (189 loc) · 5.05 KB
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
import express from "express";
import http from "http";
import createGame from "./game.js";
import socketIo from "socket.io";
const app = express();
const server = http.Server(app);
const io = socketIo(server);
app.use(express.static("public"));
app.use("/jquery", express.static("./node_modules/jquery/dist/"));
server.listen(5000);
const game = createGame(
onGameStarted,
onGameFinished,
onPlayersUpdated,
onRoundStarted,
onRoundFinished,
onTrickStarted,
onTrickFinished,
onPlayerPlayed,
onNextPlayerTurn
);
const socketMap = {};
io.on("connection", (socket) => {
const userId = socket.id;
console.log(`> User connected: ${userId}`);
socket.on("enter_game", (data) => {
const playerName = data.name;
socketMap[socket.id] = socket;
console.log(data, "entrou no jogo");
const res = game.addPlayer(socket.id, data.name);
if (res.error) {
return;
}
socket.emit("entered_game");
socket.on("disconnect", (data) => {
delete socketMap[socket.id];
game.removePlayer(socket.id);
console.log(playerName, "saiu do jogo");
});
});
socket.on("start_game", (data) => {
if (game.state.started) return;
game.start();
});
socket.on("play_card", (data, callback) => {
const result = game.playCard(socket.id, data.cardIdx);
callback(result)
});
});
function onPlayersUpdated() {
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
io.sockets.emit("players_updated", game.state.players);
}
}
function onGameStarted() {
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
socketMap[sendingPlayer.id].emit("game_started", game.state.players);
}
setInterval(()=>{sendGameState()}, 3000)
}
function sendGameState(){
const currentState = game.getCurrentState()
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
socketMap[sendingPlayer.id].emit("game_state", currentState);
}
}
function onRoundStarted(round) {
for (let i = 0; i < game.state.players.length; i++) {
const player = game.state.players[i];
if (player.robot) {
continue;
}
const roundNumber = round.number;
const players = game.state.players.map((x, idx) => ({
...x,
cards: round.playersHands[idx].length,
}));
const playerHand = round.playersHands[i];
socketMap[player.id].emit("round_started", {
roundInfo: {
number: roundNumber,
name: round.name || "Rodada " + roundNumber,
},
playerHand,
players,
});
}
}
function onRoundFinished(round) {
for (let i = 0; i < game.state.players.length; i++) {
const player = game.state.players[i];
if (player.robot) {
continue;
}
const roundNumber = round.number;
const players = game.state.players.map((x, idx) => ({
...x,
cards: round.playersHands[idx].length,
}));
const playerHand = round.playersHands[i];
socketMap[player.id].emit("round_finished", {
roundInfo: {
number: roundNumber,
name: round.name || "Rodada " + roundNumber,
},
playerHand,
players,
});
}
}
function onTrickStarted(trick) {
for (let i = 0; i < game.state.players.length; i++) {
const player = game.state.players[i];
if (player.robot) {
continue;
}
const trickNumber = trick.number;
const starterPlayerIdx = trick.starterPlayerIdx;
socketMap[player.id].emit("trick_started", {
trickNumber,
starterPlayerIdx,
});
}
}
function onTrickFinished(trick) {
for (let i = 0; i < game.state.players.length; i++) {
const player = game.state.players[i];
if (player.robot) {
continue;
}
const trickNumber = trick.number;
const winnerPlayerIdx = trick.winnerIdx;
socketMap[player.id].emit("trick_finished", {
trickNumber,
winnerPlayerIdx,
});
}
}
function onPlayerPlayed(player, card) {
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
socketMap[sendingPlayer.id].emit("player_played_card", {
player,
card,
});
}
}
function onNextPlayerTurn(playerIdx){
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
socketMap[sendingPlayer.id].emit("next_player_turn", {
playerIdx,
});
}
game.autoPlayIfBot();
}
function onGameFinished() {
const result = {
players: game.state.players,
pontuation: game.state.players.map(
(x) => new { name: x.name, points: x.points }()
),
};
for (let i = 0; i < game.state.players.length; i++) {
const sendingPlayer = game.state.players[i];
if (sendingPlayer.robot) {
continue;
}
socketMap[sendingPlayer.id].emit("game_finished", result);
}
}