-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclash.js
174 lines (154 loc) · 4.74 KB
/
clash.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
const request = require("request");
const BASE_URL = "https://www.codingame.com/services"
const CODIN_GAME = "https://www.codingame.com/multiplayer/clashofcode"
const BASIC_HEADERS = {
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json;charset=UTF-8",
"sec-ch-ua": "\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"",
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
}
async function login(email, password) {
const loginRequest = {
url: BASE_URL + '/CodinGamer/loginSiteV2',
headers: BASIC_HEADERS,
body: JSON.stringify([
email,
password,
true
]),
referrer: CODIN_GAME,
referrerPolicy: "strict-origin-when-cross-origin",
method: "POST",
mode: "cors"
}
return new Promise((resolve, reject) => {
request(loginRequest, (error, response, body) => {
if (!error && response.statusCode === 200) {
let cookie = response.headers['set-cookie'].join(';')
let user_info = JSON.parse(body.toString())
let uid = user_info['codinGamer']['userId']
resolve({cookie: cookie, uid: uid});
} else {
reject(error);
}
})
})
}
async function createPrivateGame(cookie, userId, languages, modes) {
const gameCreationRequest = {
url: BASE_URL + '/ClashOfCode/createPrivateClash',
headers: {
...BASIC_HEADERS,
"cookie": cookie
},
body: JSON.stringify([
userId,
{ SHORT: true },
languages,
modes,
]),
referrer: CODIN_GAME,
referrerPolicy: "strict-origin-when-cross-origin",
method: "POST",
mode: "cors"
}
return new Promise((resolve, reject) => {
request(gameCreationRequest, (error, response, body) => {
if (!error && response.statusCode === 200) {
let game = JSON.parse(body)
resolve(game);
} else {
reject(error);
}
})
})
}
async function launchGame(cookie, userId, handle) {
const gameLaunchRequest = {
url: BASE_URL + '/ClashOfCode/startClashByHandle',
headers: {
...BASIC_HEADERS,
"cookie": cookie
},
body: JSON.stringify([
userId,
handle
]),
referrer: CODIN_GAME,
referrerPolicy: "strict-origin-when-cross-origin",
method: "POST",
mode: "cors"
}
return new Promise((resolve, reject) => {
request(gameLaunchRequest, (error, response, _) => {
if (!error) {
if (response.statusCode === 204) {
resolve(true);
} else {
resolve(false)
}
} else {
reject(error);
}
})
})
}
async function getClashByHandle(cookie, handle) {
const clashFindRequest = {
url: BASE_URL + '/ClashOfCode/findClashByHandle',
headers: {
...BASIC_HEADERS,
"cookie": cookie
},
body: JSON.stringify([
handle
]),
referrer: CODIN_GAME,
referrerPolicy: "strict-origin-when-cross-origin",
method: "POST",
mode: "cors"
}
return new Promise((resolve, reject) => {
request(clashFindRequest, (error, response, body) => {
if (!error && response.statusCode === 200) {
let clash = JSON.parse(body)
resolve(clash);
} else {
reject(error);
}
})
})
}
async function getPublicPendingGame(cookie) {
const publicPendingGameRequest = {
url: BASE_URL + '/ClashOfCode/findPendingClashes',
headers: {
...BASIC_HEADERS,
"cookie": cookie
},
body: JSON.stringify([]),
referrer: CODIN_GAME,
referrerPolicy: "strict-origin-when-cross-origin",
method: "POST",
mode: "cors"
}
return new Promise((resolve, reject) => {
request(publicPendingGameRequest, (error, response, body) => {
if (!error && response.statusCode === 200) {
let clash = JSON.parse(body)
resolve(clash);
} else {
reject(error);
}
})
})
}
exports.login = login
exports.createPrivateGame = createPrivateGame
exports.getClashByHandle = getClashByHandle
exports.getPublicPendingGame = getPublicPendingGame
exports.launchGame = launchGame