-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
144 lines (119 loc) · 4.13 KB
/
app.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
const express = require('express')
const http = require('http')
const websocket = require('ws')
const indexRouter = require('./routes/index')
const messages = require('./public/js/messages')
const gameStatus = require('./statTracker')
const Game = require('./game')
const port = process.argv[2]
const app = express()
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/public'))
app.get('/play', indexRouter)
app.get('/', indexRouter)
const server = http.createServer(app)
const wss = new websocket.Server({ server })
const websockets = {}
let currentGame = Game(gameStatus.gamesInitialized++)
let connectionID = 0
wss.on('connection', function connection(ws) {
const con = ws
con.id = connectionID++
const playerType = currentGame.addPlayer(con)
websockets[con.id] = currentGame
console.log(`Player ${con.id} placed in game ${currentGame.getGameID()} as ${playerType}`)
/*
* inform the client about its assigned player type
*/
if (playerType === 'w') {
con.send(JSON.stringify(messages.O_PLAYER_W))
} else {
con.send(JSON.stringify(messages.O_PLAYER_B))
con.send(JSON.stringify(messages.O_GAME_START))
currentGame.getPlayerW().send(JSON.stringify(messages.O_GAME_START))
}
/*
* once we have two players, there is no way back;
* a new game object is created;
* if a player now leaves, the game is aborted (player is not preplaced)
*/
if (currentGame.hasTwoConnectedPlayers()) {
currentGame = Game(gameStatus.gamesInitialized++)
}
/*
* message coming in from a player:
* 1. determine the game object
* 2. determine the opposing player OP
* 3. send the message to OP
*/
con.on('message', (message) => {
const oMsg = JSON.parse(message.toString())
const gameObj = websockets[con.id]
const player = gameObj.determinePlayer(con)
console.log(`message ${JSON.stringify(oMsg.type)} received`)
if (oMsg.type === messages.T_MOVE) {
console.log(`player ${player} moved: ${JSON.stringify(oMsg.data)}`)
if (gameObj.move(player, oMsg.data.from, oMsg.data.to))
player === 'w' ? gameObj.getPlayerB().send(message.toString()) : gameObj.getPlayerW().send(message.toString())
if (/[wbd]/.test(gameObj.getGameState())) {
let endMessage = messages.O_GAME_OVER
endMessage.data = gameObj.getGameState()
gameStatus.gamesCompleted++
gameObj.getPlayerW().send(JSON.stringify(endMessage))
gameObj.getPlayerB().send(JSON.stringify(endMessage))
}
}
if (oMsg.type === messages.T_GAME_OVER) {
gameObj.setGameState(oMsg.data)
gameObj.getPlayerW().send(message)
gameObj.getPlayerB().send(message)
//game was won by somebody, update statistics
gameStatus.gamesAborted++
}
})
con.on('close', function (code) {
/*
* code 1001 means almost always closing initiated by the client;
* source: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
*/
console.log(`${con.id} disconnected ...`)
if (code === 1001) {
/*
* if possible, abort the game; if not, the game is already completed
*/
const gameObj = websockets[con.id]
if (gameObj === null) return
console.log(websockets)
if (!/[wbd]/.test(gameObj.getGameState())) {
gameObj.setGameState(gameObj.determinePlayer(con) === 'w' ? 'b' : 'w')
gameStatus.gamesAborted++
}
/*
* determine whose connection remains open;
* close it
*/
try {
let msg = messages.O_GAME_OVER
msg.data = 'w'
gameObj.getPlayerW().send(JSON.stringify(msg))
gameObj.getPlayerW().close()
gameObj.setPlayerW(null)
console.log('Player W closed')
} catch (e) {
console.log("Player W didn't close: " + e)
}
try {
let msg = messages.O_GAME_OVER
msg.data = 'b'
gameObj.getPlayerB().send(JSON.stringify(msg))
gameObj.getPlayerB().close()
gameObj.setPlayerB(null)
console.log('Player B closed')
} catch (e) {
console.log("Player B didn't close: " + e)
}
delete websockets[con.id]
}
})
})
server.listen(port)