-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
101 lines (92 loc) · 2.78 KB
/
Copy pathgame.js
File metadata and controls
101 lines (92 loc) · 2.78 KB
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
'use strict';
/**
* Game Class Module
* @module game
* @exports Game - Game class
*/
const events = require('./events.js');
const emitWrapper = require('./lib/emit_wrapper.js');
/**
* Game Class
* @class Game
*/
class Game {
/**
* Constructor
* @constructor
* @param {*} id - Game id
* @param {*} io - Socket server instance
*/
constructor(id, io) {
this.io = io;
this.id = id;
// Chris - Here's a basic setup for stacks to use during the game.
//Morgana - switched it over to being an object and using dynamic amounts.
this.stacks = {
a: this.generateRandomAmount(),
b: this.generateRandomAmount(),
c: this.generateRandomAmount(),
};
this.players = [];
this.timeLeft = 20;
this.countdown = null;
// Chris - a tally to keep track of how many items are left game-wide accross all stacks.
// When this is zero, game is over.
this.totalItemsRemaining = 0;
this.decrement = this.decrement.bind(this);
}
/**
* Decrement
* @method decrement
* @param {*} player - Player socket id
* Decrements the timeLeft variable and emits an event to the player
* Emits game over events if the time funs out
* Triggered at some interval in the socket server
*/
decrement(player) {
this.timeLeft = this.timeLeft - 1;
if (this.timeLeft === -1) {
console.log('Ran out of time!!');
emitWrapper(this.io, events.message, 'Time\'s up!!');
emitWrapper(this.io, events.gameOver, 'Game Over!', `${this.players[0]}`);
emitWrapper(this.io, events.gameOver, 'Game Over!', `${this.players[1]}`);
clearInterval(this.countdown);
this.countdown = null;
this.timeLeft = 20;
}
}
/**
* Generate Random Ammount
* @method generateRandomAmount
* Generates a random number between 5 and 25
*/
generateRandomAmount() {
return Math.floor(Math.random() * 20) + 5;
}
/**
* Tally Remaining
* @method _tallyTotalItemsRemaining
* Tallies the total amount in all stacks
* Updates the totalItemsRemaining property
*/
_tallyTotalItemsRemaining() {
let total = 0;
for(let i in this.stacks) {
total += this.stacks[i];
}
this.totalItemsRemaining = total;
}
// Chris - This applies the player's move to the stack they selected, and updates totalRemainingItems.
/**
* Take from Stack
* @method takeItemsFromStack
* @param {String} stackChoice - Stack to remove items from
* @param {Number} numberToTake - Amount to remove
* Given a stack name and an amount, removes the amount from the stack and runs the _tallyTotalRemaining method
*/
takeItemsFromStack (stackChoice, numberToTake) {
this.stacks[stackChoice] = this.stacks[stackChoice] - numberToTake;
this._tallyTotalItemsRemaining();
}
}
module.exports = Game;