-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverService.js
96 lines (78 loc) · 2.5 KB
/
serverService.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
const GameService = require('./gameService');
const SupabaseService = require('./supabaseService');
class ServerService {
constructor () {
this._games = {};
this._database = new SupabaseService();
this._database.init();
}
create = (rules) => {
if (this._games[`${rules.room}`]) {
throw new Error('Room already in use');
}
this._games[`${rules.room}`] = new GameService(rules);
}
remove = (room) => {
delete this._games[`${room}`];
}
join = async (room, player) => {
if (!this._games[`${room}`]) {
const error = new Error('The room doesn\'t exist');
error.isFull = false;
throw error;
}
if (!this._games[`${room}`].rules.public && !await this._database.check_friendship(this._games[`${room}`].admin, player)) {
return;
}
this._games[`${room}`].join(player);
}
leave = (room, player) => {
if (this._games[`${room}`]) {
this._games[`${room}`].leave(player);
} else {
throw new Error('The room doesn\'t exist');
}
}
abort = (room, player) => {
this.is_admin(room, player);
this.remove(room);
}
is_admin = (room, player) => {
if (!this._games[`${room}`]) {
throw new Error('The room doesn\'t exist');
} else if (!this._games[`${room}`].is_admin(player)) {
throw new Error('You don\'t have the permission for this action');
}
}
ready = (room, player) => {
this.is_admin(room, player);
return this._games[`${room}`].ready();
}
eat = (room, player, is_special) => {
if (this._games[`${room}`]) {
return this._games[`${room}`].eat(player, is_special);
} else {
throw new Error('The room doesn\'t exist');
}
}
end = (room, player) => {
if (this._games[`${room}`]) {
return this._games[`${room}`].end(player);
} else {
throw new Error('The room doesn\'t exist');
}
}
count = () => Object.keys(this._games).length;
save_game = (room) => {
let game;
if (game = this._games[`${room}`]) {
return this._database.create_game(game.array_players, game.rules, room);
} else {
throw new Error('The room doesn\'t exist');
}
}
exists = (room) => {
return this._games[`${room}`] ?? false
}
}
module.exports = ServerService;