-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
164 lines (151 loc) · 4.42 KB
/
client.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
'use strict';
const Hapi = require('hapi');
const Joi = require('joi');
const Targeter = require('./domain/targeter');
const server = new Hapi.Server();
server.connection({port: 4000});
let Targeters = new Map();
server.route([
{
method: 'POST',
path: '/battleship/game',
config: {
description: 'Creates a game and returns a gameId and a grid with ships on it to the referee',
tags: ['api'],
notes: ['use a guid for the gameId', 'validate the game board'],
response: {
schema: {
gameId: Joi.string().guid().required(),
grid: Joi.string().length(100)
}
}
},
handler: function (request, reply) {
let newTargeter = new Targeter();
Targeters.set(newTargeter.id, newTargeter);
reply({
gameId: newTargeter.id,
grid: newTargeter.getGrid()
});
}
}, {
method: 'POST',
path: '/battleship/game/{gameId}/shot',
config: {
description: 'Takes a gameId and returns a shot in the form A2 (where A=Row, 2=Col)',
notes: ['Validate that it receives an active gameId and that the row and col are between A-J 1-10'],
validate: {
params: {
gameId: Joi.string().guid().required()
}
},
response: {
schema: {
letter: Joi.string().valid('A','B','C','D','E','F','G','H','I','J').required(),
number: Joi.number().integer().min(1).max(10).required()
}
}
},
handler: function (request, reply) {
let shot = Targeters.get(request.params.gameId).targetMyShot();
reply({
letter: shot.row,
number: shot.col
});
}
}, {
method: 'POST',
path: '/battleship/game/{gameId}/shot-result/{shotresult}',
config: {
description: 'Takes a gameId and the results of our last shot, 0 miss, 1 hit, 2 sunk',
notes: ['Validate that it receives an active gameId and that the result is 0, 1 or 2'],
validate: {
params: {
gameId: Joi.string().guid().required(),
shotresult: Joi.number().integer().min(0).max(2).required()
}
}
},
handler: function (request, reply) {
Targeters.get(request.params.gameId).recordMyShotResult(request.params.shotresult);
reply(
'shot result accepted'
);
}
}, {
method: 'POST',
path: '/battleship/game/{gameId}/receive-shot/{letter}/{number}',
config: {
description: 'Receive a shot with a gameId composed of a letter and number.' +
'Return 0 for a miss, 1 for a hit and 2 for sunk',
notes: ['Validate that it receives an active gameId, a leter from A-J and a number from 1-10'],
validate: {
params: {
gameId: Joi.string().guid().required(),
letter: Joi.string().valid('A','B','C','D','E','F','G','H','I','J').required(),
number: Joi.number().integer().min(1).max(10).required()
}
}
},
handler: function (request, reply) {
reply({
// 0 miss, 1 hit, 2 sunk
result: Targeters.get(request.params.gameId).receiveShot(request.params.letter, request.params.number)
});
}
}, {
method: 'DELETE',
path: '/battleship/game/{gameId}',
config: {
description: 'Receives a gameId to delete and make inactive',
notes: ['Validate that the gameId was valid and the game was active'],
validate: {
params: {
gameId: Joi.string().guid().required()
}
}
},
handler: function (request, reply) {
if(Targeters.has(request.params.gameId)){
Targeters.clear(request.params.gameId);
reply('game deleted');
} else {
reply('game not found');
}
}
}, {
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 Client ' + 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.start((err) => {
if (err) {
throw err;
}
console.log('Battleship client running at:', server.info.uri);
});
});
module.exports = server;