summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
fbb338a)
Specifically, the checks for whether the player who submitted a move
belongs on a team and belongs to the team that has the next move.
These checks belong in the generic Game class so that future games
won't have to maintain their own copies of these implementations.
Previously, we were using the presence of the "add_move" property on a
game class to determine whether to add the "/move" route. Now that we
are adding add_move to the base class, we have to check specifically
whether the child class has its own add_move (with
hasOwnProperty('add_move')) to know whether the "/move" route should
be added.
this.players = [];
this.next_player_id = 1;
this.teams = [];
this.players = [];
this.next_player_id = 1;
this.teams = [];
+ this.state = {
+ team_to_play: ""
+ };
/* Send a comment to every connected client every 15 seconds. */
setInterval(() => {this.broadcast_string(":");}, 15000);
/* Send a comment to every connected client every 15 seconds. */
setInterval(() => {this.broadcast_string(":");}, 15000);
+ /* Just performs some checks for whether a move is definitely not
+ * legal (such as not the player's turn). A child class is expected
+ * to override this (and call super.add_move early!) to implement
+ * the actual logic for a move. */
+ add_move(player, move) {
+ /* Cannot move if you are not on a team. */
+ if (player.team === "")
+ {
+ return { legal: false,
+ message: "You must be on a team to take a turn" };
+ }
+
+ /* Cannot move if it's not this player's team's turn. */
+ if (player.team !== this.state.team_to_play)
+ {
+ return { legal: false,
+ message: "It's not your turn to move" };
+ }
+
+ return { legal: true };
+ }
+
add_player(session, connection) {
/* First see if we already have a player object for this session. */
const existing = this.players[session.id];
add_player(session, connection) {
/* First see if we already have a player object for this session. */
const existing = this.players[session.id];
/* Further, add some routes conditionally depending on whether the
* engine provides specific, necessary methods for the routes. */
/* Further, add some routes conditionally depending on whether the
* engine provides specific, necessary methods for the routes. */
- if (engine.prototype.add_move) {
+
+ /* Note: We have to use hasOwnProperty here since the base Game
+ * class has a geeric add_move function, and we don't want that to
+ * have any influence on our decision. Only if the child has
+ * overridden that do we want to create a "/move" route. */
+ if (engine.prototype.hasOwnProperty("add_move")) {
router.post('/move', (request, response) => {
const game = request.game;
const move = request.body.move;
router.post('/move', (request, response) => {
const game = request.game;
const move = request.body.move;
/* Returns true if move was legal and added, false otherwise. */
add_move(player, square) {
/* Returns true if move was legal and added, false otherwise. */
add_move(player, square) {
- /* Cannot move if you are not on a team. */
- if (player.team === "")
- {
- return { legal: false,
- message: "You must be on a team to take a turn" };
- }
+ const result = super.add_move(player, square);
- /* Cannot move if it's not this player's team's turn. */
- if (player.team !== this.state.team_to_play)
- {
- return { legal: false,
- message: "It's not your turn to move" };
- }
+ /* If the generic Game class can reject this move, then we don't
+ * need to look at it any further. */
+ if (! result.legal)
+ return result;
/* Cannot move to an occupied square. */
if (this.state.board[square])
/* Cannot move to an occupied square. */
if (this.state.board[square])