This is a first baby step toward implementing the more complete API
for the game which we defined today, and which can be found here:
https://git.cworth.org/git/empires-api
In this commit we also introduce a 'Game' class to encapsulate all
data necessary for the game, (so the 'players' array we had before,
but also the 'next_player_id' which we need now).
+class Game {
+ constructor() {
+ this.players = [];
+ this.next_player_id = 1;
+ }
+
+ add_player(name, character) {
+ this.players.push({id: this.next_player_id,
+ name: name,
+ character: character})
+ this.next_player_id++;
+ }
+}
+
+game = new Game();
app.use(body_parser.urlencoded({ extended: false }));
app.use(body_parser.json());
app.use(body_parser.urlencoded({ extended: false }));
app.use(body_parser.json());
});
app.get('/players', function (request, response) {
});
app.get('/players', function (request, response) {
- response.send(players);
+ response.send(game.players);
});
app.post('/register', function (request, response) {
});
app.post('/register', function (request, response) {
- players.push({name: request.body.name,
- character: request.body.character});
+ game.add_player(request.body.name, request.body.character);