-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
216 lines (193 loc) · 7.06 KB
/
app.ts
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
import * as express from "express";
import { Socket } from "socket.io";
import redis = require('redis');
class MainApplication {
app;
http;
io;
redisHost = 'redis';
redis: redis.RedisClient;
constructor() {
this.app = express();
this.http = require("http").Server(this.app);
// set up socket.io and bind it to our
// http server.
this.io = require("socket.io")(this.http);
this.app.get("/", (_: any, res: any) => {
res.send({ message: 'DominoParty API... Hello hacker!' });
});
// whenever a user connects on port 3000 via
// a websocket, log that a user has connected
this.io.on("connection", this.onConnection.bind(this));
this.redis = redis.createClient({port:6379, host:this.redisHost});
}
processMessage(message: string, socket: Socket) {
let jsonMsg: { event:string, room: string , data?:any} = JSON.parse(message);
console.log("received message",jsonMsg);
let event = jsonMsg.event;
console.log(event);
switch (event) {
case 'connect':
this.onPlayerConnect(jsonMsg.room, socket);
break;
case 'update':
case 'setup':
this.onPlayerUpdate(jsonMsg.room, jsonMsg.data, socket);
break;
case 'pieceUpdate':
console.log("updating a piece");
this.onPieceUpdate(jsonMsg.room, jsonMsg.data, socket);
break;
}
}
// When a player connects a room, add the player to the room and notify other players
onPlayerConnect(roomName: string, socket: Socket) {
console.log(`Player ${socket.id} connecting to room: ` + roomName);
let roomData: {boardData: any, players: any} = {boardData:{}, players:[]};
// get room data from DB
this.redis.get(roomName, (err, data) => {
if (data){
let room = JSON.parse(data);
// construct response payload with room data
roomData = {
boardData: room.boardData,
players: room.players || []
};
// notify other players of player joining the room
if (room.players) {
let availablePlayers: string[] = [];
room.players.forEach(player => {
if (this.io.sockets.connected[player]) {
availablePlayers.push(player);
this.io.sockets.connected[player].emit('message', { event: 'playerConnected' });
}
});
roomData.players = availablePlayers;
roomData.players.push(socket.id);
this.redis.set(roomName, JSON.stringify(roomData));
console.log(`Players in rom ${roomName} ${availablePlayers}`)
socket.emit('message', { event: 'connected', data: roomData.boardData });
}
}
else{
console.log('Didnt find room in DB, inserting')
this.redis.set(roomName, JSON.stringify({ room: roomName, players: [socket.id] }));
}
});
}
onPieceUpdate(roomName: string,
pieceData: {event:string, piece:string, name:string,
state:{action?:string, x?:number, y?:number, rotation?:number, flipped:boolean}},
socket: Socket) {
// update the room data in the DB
this.redis.get(roomName, (err, data) => {
let room: {
players:any[],
boardData:{ event: string,
tiles:any[],
center:{x:number ,y:number}, trains:any[]}} = JSON.parse(data);
switch(pieceData.piece) {
case 'train':
console.log("looking for train...")
console.log(pieceData);
console.log(room.boardData.trains)
let train = room.boardData.trains.find(train => train.name == pieceData.name);
if(train) {
console.log('updating train ', train);
train.x = pieceData.state.x;
train.y = pieceData.state.y;
}
break;
case 'center':
room.boardData.center.x = pieceData.state.x;
room.boardData.center.y = pieceData.state.y;
break;
case 'tile':
console.log("looking for tile...");
console.log(pieceData);
let tile = room.boardData.tiles.find(tile => tile.name == pieceData.name);
switch(pieceData.state.action) {
case 'move':
console.log(room.boardData.tiles);
if (tile) {
console.log('updating tile', tile);
tile.x = pieceData.state.x;
tile.y = pieceData.state.y;
if (pieceData.state.rotation) {
tile.rotation = pieceData.state.rotation
}
if(pieceData.state.flipped){
tile.flipped = pieceData.state.flipped
}
}
break;
case 'create':
room.boardData.tiles.push({
rotation: pieceData.state.rotation,
x: pieceData.state.x,
y: pieceData.state.y,
flipped: pieceData.state.flipped,
name: pieceData.name
});
break;
case 'destroy':
room.boardData.tiles = room.boardData.tiles.filter(tile => tile.name != pieceData.name);
break;
}
break;
}
room.players = room.players.filter(player => {
if (!this.io.sockets.connected[player]) { return false; }
if (player == socket.id) { return true; }
console.log('Sending message to player ' + player)
this.io.sockets.connected[player].emit('message',
{ event: pieceData.event, data: pieceData });
return true;
});
this.redis.set(roomName, JSON.stringify(room));
});
}
onPlayerUpdate(roomName: string, newBoardData: any, socket: Socket) {
console.log(roomName);
// update the room data in the DB
this.redis.get(roomName, (err, data) => {
let room = JSON.parse(data);
room.boardData = newBoardData;
room.players = room.players.filter(player => {
if (!this.io.sockets.connected[player]) { return false; }
if (player == socket.id) { return true; }
console.log('Sending message to player ' + player)
this.io.sockets.connected[player].emit('message',
{ event: newBoardData['event'], data: newBoardData });
return true;
});
this.redis.set(roomName, JSON.stringify(room));
console.log("Room now has:",room);
});
}
onConnection(socket: Socket) {
console.log("a user connected, id: ", socket.id);
socket.on('disconnect', (reason) => {
this.onDisconnect(socket);
console.log(`User with id ${socket.id} disconencted :(. `, reason);
});
// Main Message Listener
socket.on("message", (message) => {
this.processMessage(message, socket);
});
}
onDisconnect(socket:Socket) {
// get list of players for on user's room
let players: Socket[] = [];
// notify of player leaving
players.forEach((element: Socket) => {
element.emit('message', {event: 'disconnect', player: socket.id})
});
}
listen(port: number) {
this.http.listen(port, () => {
console.log(`listening on *: ${port}`);
});
}
}
new MainApplication().listen(3000);