-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (147 loc) · 3.9 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class User {
name;
money;
constructor(name, money) {
this.name = name;
this.money = money;
}
play(gameMachine, moneyForGame) {
this.money -= moneyForGame;
this.money += gameMachine.play(moneyForGame);
}
}
class SuperUser extends User {
constructor(name, money) {
super(name, money);
}
_casinos = [];
get casinosList() {
return this._casinos;
}
getCasino(name) {
return this.casinosList.find((casino) => casino.casinoName === name);
}
createCasino(name, money) {
this._casinos.push(new Casino(name, money));
}
}
class Casino {
_name;
_money;
_gameMachines = [];
constructor(name, money) {
this._name = name;
this._money = money;
}
get casinoName() {
return this._name
}
createGameMachine(money, name) {
// reduce all Casino money because we add this money to GameMachine
this._money -= money;
this._gameMachines.push(new GameMachine(money, name));
}
getGameMachine(name) {
return this._gameMachines.find(
(gameMashine) => gameMashine.machineName === name
);
}
removeGameMachine(name) {
const selectedGameMachine = this._gameMachines.find(
(gameMachine) => gameMachine.machineName === name
);
if (selectedGameMachine) {
const machineMoney = selectedGameMachine.removeAllMoney();
this.gameMachines = this._gameMachines.filter(
(gameMachine) => gameMachine.machineName !== name
);
this.gameMachines = this._gameMachines.map((gameMachine) => {
gameMachine.addMoney(machineMoney / this.gameMachines.length);
return gameMachine;
});
} else {
throw new Error("No gameMachine found");
}
}
get getMoney() {
// get money from all gameMachines and money that left in casino
let allMoney = 0;
this._gameMachines.forEach((gameMachine) => {
allMoney += gameMachine.moneyCount;
});
return allMoney + this._money;
}
get getMachineCount() {
return this._gameMachines.length;
}
}
class GameMachine {
_name;
_money;
get machineName() {
return this._name;
}
constructor(money, name) {
this._name = name;
this._money = money;
}
get moneyCount() {
return this._money;
}
removeMoney(moneyCount) {
if (moneyCount <= this._money) {
this._money -= moneyCount;
}
}
removeAllMoney() {
const allMoney = this.moneyCount;
this._money = 0;
return allMoney;
}
addMoney(moneyCount) {
this._money += moneyCount;
}
play(userMoney) {
this._money += userMoney;
if (userMoney * 2 > this._money || userMoney * 3 > this._money) {
this._money -= userMoney;
return userMoney;
}
const randomNumber = Math.floor(Math.random() * (999 - 100 + 1) + 100);
var counts = {};
let winningMoney = 0;
randomNumber
.toString()
.split("")
.forEach((x) => {
counts[x] = (counts[x] || 0) + 1;
});
// Just for 3 digits number
Object.keys(counts).forEach((keys) => {
if (counts[keys] === 3) {
winningMoney = userMoney * 3;
return;
} else if (counts[keys] === 2) {
winningMoney = userMoney * 2;
return;
}
});
this._money -= winningMoney;
return winningMoney;
}
}
const superAdmin = new SuperUser("superUser", 500);
superAdmin.createCasino("Casino1", 200);
superAdmin.getCasino("Casino1").createGameMachine(70, "gameMachine1");
superAdmin.getCasino("Casino1").createGameMachine(70, "gameMachine2");
const selectedGameMachine = superAdmin
.getCasino("Casino1")
.getGameMachine("gameMachine1");
selectedGameMachine.addMoney(130);
const user1 = new User("Petro", 50);
user1.play(selectedGameMachine, 30);
superAdmin.getCasino("Casino1").removeGameMachine("gameMachine1");
const machineCount = superAdmin.getCasino("Casino1").getMachineCount;
const selectedMachineMoneyCount = superAdmin
.getCasino("Casino1")
.getGameMachine("gameMachine2").moneyCount;