-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
192 lines (154 loc) · 5.5 KB
/
bot.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'module-alias/register';
import * as request from 'request-promise';
import * as io from 'socket.io-client';
import { table } from 'table';
// types
import { IGameRedisType } from '@Types/GameType';
// services
import MongoService from '@Services/MongoService';
// utils
import { LOGIN_ENDPOINT, GAME_ENDPOINT, GAMES_ENDPOINT } from '@Constants/routs';
import { CREATED_GAME_EVENT_WSS, TURN_GAME_EVENT_WSS, FINISH_GAME_EVENT_WSS } from '@Constants/eventsWSS';
import { SOCKET_PORT, SOCKET_HOST, API_HOST, API_PORT } from '@Config/index';
const API = `${API_HOST}:${API_PORT}`;
const WS_API = `${SOCKET_HOST}:${SOCKET_PORT}`;
const COUNT_GAMES = 10;
class Bot {
countGames = 0;
countGamesMax;
player;
socket;
constructor(Player, games) {
this.countGamesMax = games;
this.socket = io.connect(WS_API, {
autoConnect: true,
query: {
uuid: Player._id,
},
});
this.player = {
_id: Player._id,
username: Player.username,
password: Player.password,
botLevel: Player.botLevel,
token: '',
games: {},
}
this.socket.on(CREATED_GAME_EVENT_WSS, ({ data, clientsID }) => {
if(clientsID.includes(String(this.player._id))) {
this._responseStartGameWSS(data);
}
});
this.socket.on(TURN_GAME_EVENT_WSS, ({data, clientsID}) => {
if(clientsID.includes(String(this.player._id))) {
this._responseTurnGameWSS(data);
}
});
this.socket.on(FINISH_GAME_EVENT_WSS, ({data, clientsID}) => {
if(clientsID.includes(String(this.player._id))) {
this._responseEndGameWSS(data);
}
});
}
public async sleep() { return new Promise(resolve => setTimeout(resolve, Math.floor(Math.random() * (+2000 - +1000)) + +1000)); };
public renderTableInTerminal(turns, p1, p2) {
const getSymbol = (s) => {
if(s === '') return ' ';
return s === String(p1) ? 'X' : 'O';
}
console.log('PLAYER 1 - ', p1);
console.log('PLAYER 2 - ', p2);
const data = [
[getSymbol(turns.c0), getSymbol(turns.c1), getSymbol(turns.c2)],
[getSymbol(turns.c3), getSymbol(turns.c4), getSymbol(turns.c5)],
[getSymbol(turns.c6), getSymbol(turns.c7), getSymbol(turns.c8)],
];
console.log( table(data));
console.log('\n\n');
}
public async _responseStartGameWSS(data: IGameRedisType) {
this.player.games[String(data.gameID)] = data;
if(data.currentPlayerTurn == this.player._id) {
console.log('CREATED_GAME_EVENT_WSS');
this.renderTableInTerminal(data.fieldTurns, data.player1, data.player2);
await this.turnGame(data.gameID);
}
}
public async _responseTurnGameWSS(data: IGameRedisType) {
this.player.games[String(data.gameID)] = data;
if(data.currentPlayerTurn == this.player._id) {
console.log('TURN_GAME_EVENT_WSS');
this.renderTableInTerminal(data.fieldTurns, data.player1, data.player2);
await this.turnGame(data.gameID);
}
}
public async _responseEndGameWSS(data: IGameRedisType) {
this.countGames++;
console.log('FINISH_GAME_EVENT_WSS');
this.renderTableInTerminal(data.fieldTurns, data.player1, data.player2);
console.log('COUNT GAMES', this.countGames);
if(this.countGames < this.countGamesMax) {
await this.startGame();
}
}
private getRandomTurn(turns): number | undefined {
const freeCell = Object.keys(turns).filter(key => !turns[key]).map(cell => cell[1]);
return +freeCell[Math.floor(Math.random() * freeCell.length )]
}
public async getJwtAccess() {
const Auth = await request.post({
method: 'POST',
uri: `${API}${LOGIN_ENDPOINT}`,
body: {
username: this.player.username,
password: this.player.password
},
json: true
});
this.player.token = Auth.data.token;
}
public async startGame() {
await this.sleep();
await request({
method: 'POST',
uri: `${API}${String(GAMES_ENDPOINT)}`,
headers: {
Authorization: this.player.token
},
json: true
});
}
public async turnGame(gameID) {
await this.sleep();
const turnID = this.getRandomTurn(this.player.games[gameID].fieldTurns);
// the moves are over
if(turnID === undefined ) return;
await request({
method: 'PUT',
uri: `${API}${String(GAME_ENDPOINT).replace(':id', gameID)}`,
headers: {
Authorization: this.player.token
},
body: {
turnID
},
json: true
});
}
public getPlayerData() {
return this.player;
}
}
MongoService.connect(async () => {
const DB = MongoService.getModels();
const Users = await DB.Users.find();
const bot1 = await new Bot(Users[0], COUNT_GAMES);
const bot2 = await new Bot(Users[1], COUNT_GAMES);
const bot3 = await new Bot(Users[2], COUNT_GAMES);
await bot1.getJwtAccess();
await bot2.getJwtAccess();
await bot3.getJwtAccess();
await bot1.startGame();
await bot2.startGame();
await bot3.startGame();
});