-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
277 lines (256 loc) · 6.76 KB
/
server.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
'use strict';
const Hapi = require('hapi');
const Joi = require('joi');
// const Client = require('./client')
const Player = require('./domain/player');
const Match = require('./domain/match');
const randomName = require('./util/random-name.js');
const server = new Hapi.Server();
server.connection({
port: 5000
});
let players = [];
let matches = [];
server.route([
{
method: 'POST',
path: '/battleship/api/player',
config: {
description: 'Takes an ip and port (optionally a name) and returns a registered player with playerId',
notes: ['Validate that it receives valid inputs and returns a guid, generate a name if not given'],
validate: {
payload: {
name: Joi.string(),
ip: Joi.string().required(),
port: Joi.string().required()
}
},
response: {
schema: {
id: Joi.string().guid().required(),
name: Joi.string().required(),
ip: Joi.string().required(),
port: Joi.string().required(),
wins: Joi.number().integer().required(),
losses: Joi.number().integer().required()
}
}
},
handler: function (request, reply) {
let newPlayer = new Player(request.payload.name, request.payload.ip, request.payload.port);
players.push(newPlayer);
reply({
id: newPlayer.id,
name: newPlayer.name,
ip: newPlayer.ip,
port: newPlayer.port,
wins: newPlayer.wins,
losses: newPlayer.losses
});
}
}, {
method: 'GET',
path: '/battleship/api/players',
config: {
description: 'Return a list of players'
},
handler: function (request, reply) {
reply({
players: players,
});
}
}, {
method: 'GET',
path: '/battleship/api/player/{playerId}',
config: {
description: 'Return a player'
},
handler: function (request, reply) {
let player = players.find(p => p.id === request.params.playerId);
reply({
player: player,
});
}
}, {
method: 'DELETE',
path: '/battleship/api/player/{playerId}',
config: {
description: 'Delete a player defined by a playerId',
validate: {
params: {
playerId: Joi.string().guid().required()
}
},
},
handler: function (request, reply) {
let index = players.findIndex( p => p.id === request.params.playerId)
if(index != -1){
players.splice(index,1);
reply('player deleted');
} else {
reply('player not found');
}
}
}, {
method: 'POST',
path: '/battleship/api/match',
config: {
description: 'Takes two registered players ids and spawns a match for them, returning the results',
notes: ['uses guids for the players and match ids, monitor play with local game board to determine victor'],
validate: {
payload: {
player1Id: Joi.string().guid().required(),
player2Id: Joi.string().guid().required()
}
},
response: {
schema: {
matchResults: Joi.object(),
message: Joi.string()
}
}
},
handler: function (request, reply) {
// extract players from payload playerId's
// and create a match
let player1 = players.find(p => p.id === request.payload.player1Id);
let player2 = players.find(p => p.id === request.payload.player2Id);
if (player1 === undefined) {
player1 = new Player(randomName());
players.push(player1);
}
if (player2 === undefined) {
player2 = new Player(randomName());
players.push(player2);
}
let newMatch = new Match(player1, player2);
matches.push(newMatch);
// then play the match until there is a winner
newMatch.play();
// then reply with the matchResults
reply({
matchResults: newMatch,
message: newMatch.winner.name + ' won'
});
}
}, {
method: 'GET',
path: '/battleship/api/init',
handler: function (request, reply) {
let player1 = new Player(randomName());
players.push(player1);
let player2 = new Player(randomName());
players.push(player2);
let newMatch = new Match(player1, player2);
matches.push(newMatch);
// then play the match until there is a winner
newMatch.play();
reply({
match: newMatch,
});
}
}, {
method: 'GET',
path: '/battleship/api/matches',
config: {
description: 'Return a list of matches'
},
handler: function (request, reply) {
reply({
matches: matches,
});
}
}, {
method: 'GET',
path: '/battleship/api/match/{matchId}',
config: {
description: 'Return a match'
},
handler: function (request, reply) {
let match = matches.find(m => m.id === request.params.matchId);
reply({
match: match,
});
}
}, {
method: 'DELETE',
path: '/battleship/api/match/{matchId}',
config: {
description: 'Receives a matchId to delete and make inactive',
notes: ['Validate that the matchId was valid and the game was active'],
validate: {
params: {
matchId: Joi.string().guid().required()
}
}
},
handler: function (request, reply) {
let index = matches.findIndex(p => p.id === request.params.matchId)
if (index != -1) {
matches.splice(index, 1);
reply('match deleted');
} else {
reply('match not found');
}
}
}, {
method: 'GET',
path: '/lobby',
handler: function (request, reply) {
reply.view('lobby', {
players: players,
matches: matches
});
}
}, {
method: 'GET',
path: '/match/{matchId}',
handler: function (request, reply) {
let match = matches.find(m => m.id === request.params.matchId);
reply.view('match', {
match: match
});
}
}, {
method: 'GET',
path: '/{params*}',
config: {
description: 'Default route for any get handlers, point the user to docs for reference information',
},
handler: function (request, reply) {
reply('Battleship Game Server ' + server.info.uri + '/docs for details')
}
}
]);
server.register([
require('vision'),
require('inert'),
require('lout'),
{
register: require('good'),
options: {
reporters: {
reporter: [{module: 'good-console'}, 'stdout']
}
}
}
], (err) => {
if (err) {
return console.error(err);
}
server.views({
engines: {
hbs: require('handlebars')
},
relativeTo: __dirname,
path: './views',
isCached: false
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Battleship Server running at:', server.info.uri);
});
});
module.exports = server;