-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-manager.js
executable file
·76 lines (64 loc) · 1.92 KB
/
game-manager.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
/**
* @file Keep tracks of every running games and their states
* @author jojos38
*/
// -------------------- SOME VARIABLES -------------------- //
const ClassicGame = require('gamemodes/classic-game.js');
const logger = require('logger.js');
const tools = require('tools.js');
// -------------------- SOME VARIABLES -------------------- //
class GameManager {
#games = {};
/**
* Starts a new classic game
*/
async startClassicGame(db, guild, channel, userID, lang, args) {
logger.info("Starting new game in channel " + channel.id);
let game = new ClassicGame(this, db, guild, channel, userID, lang)
this.#games[channel.id] = game;
game.preStart(args);
}
/**
* Restores a classic game in the state it was before
* @param {json} rawData - All the data of the game (scores, difficulty etc)
*/
async restoreClassicGame(db, channelID, rawData) {
logger.info("Starting a game in channel " + channelID);
let game = new ClassicGame(this, db)
this.#games[channelID] = game;
game.restoreGameState(rawData);
}
/**
* Returns if a game is running in the given channelID
*/
running(channelID) {
if (this.#games[channelID]) return true;
else return false;
}
/**
* Stop a running game
*/
stopGame(channel, member, reason, lang) {
let game = this.#games[channel.id];
if (game) {
if (!game.isRunning()) { return; }
if (member.user.id == game.getUserID() || member.permissions.has("MANAGE_MESSAGES")) {
game.stop();
tools.sendCatch(channel, lm.getStopEmbed(lang, reason));
logger.info("Game aborted");
} else {
tools.sendCatch(channel, lm.getWrongPlayerStopEmbed(lang));
}
} else {
tools.sendCatch(channel, lm.getNoGameRunningEmbed(lang));
}
}
/**
* Delete a game from the manager
*/
deleteGame(channelID) {
if (delete this.#games[channelID]) logger.info("Deleted game " + channelID);
else logger.warn("Game " + channelID + " not found");
}
}
module.exports = GameManager;