-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (77 loc) · 2.77 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
const coc = require("./clash.js")
class ClashClient {
/**
* {@link createClashClient}
*
* @param cookie cookie returned from successful login
* @param userId user id of bot user
*/
constructor(cookie, userId) {
this.lastGameHandle = null
this.cookie = cookie
this.userId = userId
}
/**
* Creates a private game and returns info for the game
* Use Result#publicHandle to get the game's handle
* @link getClashLinkFromHandle
*
* @param languages Array of names of allowed languages - ALL are allowed if null; [Rust, Python, Java...etc]
* @param modes Array of allowed modes - ALL are allowed if null; [FASTEST, SHORTEST, REVERSE]
* @returns {Promise<String>} The handle for the created Game
*
*/
async createPrivateGame(languages, modes) {
let game = coc.createPrivateGame(this.cookie, this.userId, languages, modes)
game.then(res => this.lastGameHandle = res[`publicHandle`])
return game
}
/**
* Gets info about a clash like players in the game, started time...ect...
* @param handle The handle of the game, possibly from {@link createPrivateGame}
* @returns {Promise<{...}>} Info about the clash
*/
async getClashByHandle(handle) {
return coc.getClashByHandle(this.cookie, handle)
}
/**
* Finds array of current public pending games normally shown in the home screen of the clash of code
* Each of the elements of the returned array contains info about the clash.
* Use Result#publicHandle to get the game's handle
* @returns {Promise<{...}>}
*/
async getPublicPendingGame() {
return coc.getPublicPendingGame(this.cookie)
}
/**
* Attempts to launch last game.
*
* This limitation of launching last game exists since the bot must be in the game
* and be the game's owner at present to launch the game
* @return true, if successful;
*/
async launchLastGame() {
if (this.lastGameHandle == null) return false
return coc.launchGame(this.cookie, this.userId, this.lastGameHandle)
}
}
/**
* Logs in with specified credentials
* @param email
* @param password
* @return {Promise<ClashClient>} Client that allows managing games
*/
async function createClashClient(email, password) {
let result = await coc.login(email, password)
return new ClashClient(result[`cookie`], result['uid'])
}
/**
* Given the handle (publicHandle) of a game, returns the link to be used to enter the game
* @param handle
* @return {string}
*/
function getClashLinkFromHandle(handle) {
return "https://www.codingame.com/clashofcode/clash/"+ handle
}
exports.createClashClient = createClashClient
exports.getClashLinkFromHandle = getClashLinkFromHandle