-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controller.js
114 lines (91 loc) · 3.65 KB
/
Controller.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
class Controller {
constructor(grid = new Grid("grid"), inter = new Interface()) {
this._interface = inter;
this._game = new Game("unknown", "normal");
this._grid = grid;
this._table = new Table();
this.game.initGame(this.grid, this.interface.timer);
this.addListener(this.interface.difficultySelect, () => {
this.game.changeDifficulty(this.interface.difficultySelect.value);
this.game.initGame(this.grid, this.interface.timer, this.interface.actionsCounter);
this.interface.changeGameBtn("start");
if (document.getElementById('summary-popup')) this.interface.removeGameSummary();
}, "change");
this.addListener(this.interface.nameBtn, () => {
if (this.game.gameState !== 'ongoing' && this.game.gameState !== 'failed') {
if (!this.interface.nameInput.value) {
this.interface.nameInput.style.backgroundColor = 'red';
setTimeout(() => this.interface.nameInput.style.backgroundColor = '#ffffff', 1000);
} else {
this.game.stats.username = this.interface.nameInput.value;
}
}
});
this.addListener(this.interface.gameBtn, () => {
if (this.game.gameState === "init" || this.game.gameState === "stopped")
this.game.startGame(this.grid, this.interface.timer);
else if (this.game.gameState === "ongoing")
this.game.stopGame(this.grid);
this.interface.toggleGameBtn();
if (document.getElementById('summary-popup')) this.interface.removeGameSummary();
this.addListener(this.grid.root, function (e) {
if ((this.game.gameState === "ongoing" || this.game.gameState === 'win' || this.game.gameState === 'failed') && e.target.className === "game-square") {
this.interface.actionsCounter = this.game.stats.actionsCounter;
}
if (this.game.gameState === 'win') {
this.game.stopGame(this.grid);
const result = new Result(
this.game.stats.timerValue,
this.game.stats._getTimerResoult(),
this.game.stats.actionsCounter,
this.game.stats.difficulty,
this.game.stats.username
);
this.table.addSortedResult(result);
this.table.addResultToView(result);
this.interface.addGameSummary(
this.game.stats._getTimerResoult(),
this.game.stats.actionsCounter,
this.game.initGame,
this.grid,
this.interface.timer,
this.interface.actionsCounter,
this.game,
document.getElementById('container'))
}
})
});
this.addListener(this.interface.resetBtn, () => {
this.game.initGame(this.grid, this.interface.timer, this.interface.actionsCounter);
this.interface.changeGameBtn("start");
if (document.getElementById('summary-popup')) this.interface.removeGameSummary();
});
}
get interface() {
return this._interface;
}
set interface(inter) {
return this._interface = inter;
}
get game() {
return this._game;
}
set game(game) {
return this._game = game;
}
get grid() {
return this._grid;
}
set grid(grid) {
return this._grid = grid;
}
get table() {
return this._table;
}
set table(table) {
return this._table = table;
}
addListener(elem, callback, eventType = "click", binded = this) {
elem.addEventListener(eventType, callback.bind(binded));
}
}