-
Notifications
You must be signed in to change notification settings - Fork 1
/
pepeplanet.js
159 lines (121 loc) · 3.98 KB
/
pepeplanet.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
import gbxremote from 'gbxremote';
import _ from 'lodash';
import config from './config.js';
import log from './utils/log.js';
import server from './utils/server.js';
import runningMessage from './utils/runningMessage.js';
import callbacksList from './callbacks/index.js';
import generateUI from './UI/generateUI.js';
import playerdb from './db/players.js';
import db from './db/database.js';
/**
* Author Esvalirion (https://github.com/Esvalirion)
*/
const pepeplanet = {
client: null,
players: {},
addPlayerToPool(login, nickName) {
if (this.players[login]) return;
this.players[login] = {
nickName,
wayPoints: [],
};
},
async renderNewUI(client) {
const playerList = await client.query('GetPlayerList', [1000, 0]);
playerList.forEach(async ({ Login, playerID, NickName }) => {
if (playerID === 0) return;
this.addPlayerToPool(Login, NickName);
try {
const { IPAddress } = await client.query('GetDetailedPlayerInfo', [Login]);
const isPlayerExist = await playerdb.existPlayer(Login);
if (!isPlayerExist) {
await playerdb.upsertPlayer(Login, NickName, IPAddress);
}
generateUI(Login, client);
} catch (err) {
log.red('Something went wrong in renderNewUI');
log.red(err);
}
});
},
async startCallbackListening() {
this.client.on('callback', (method, params) => {
let callbackFn = _.get(callbacksList, method.split('.')[1]);
let attrs = params;
if (method.split('.')[1] === 'ModeScriptCallbackArray') {
if (params[0].split('.')[1] === 'Event') {
callbackFn = callbacksList.Event[params[0].split('.')[2]];
} else {
callbackFn = _.get(callbacksList, params[0].split('.')[1]);
}
attrs = JSON.parse(params[1][0]);
}
if (!callbackFn) {
// log.red(`Unregistered method ${method} detected, captain!`);
// log.white(JSON.stringify(params));
// log.white('----------');
return;
}
// log.green('Registered method, captain!');
// log.white(method);
// log.white(JSON.stringify(params));
// log.white('----------');
callbackFn(attrs, this.client);
});
},
async triggerModeScript(client) {
try {
await client.query('TriggerModeScriptEventArray', ['XmlRpc.EnableCallbacks', ['true']]);
} catch (err) {
log.red('TriggerModeScriptEventArray failed');
log.red(err);
process.exit(1);
}
log.green('Script callbacks enabled ...');
log.green(runningMessage);
this.client = client;
server.log('$0f0How it feels, pepegas, pepeplanet is here!');
this.startCallbackListening();
this.renderNewUI(client);
},
async enableCallbacks(client) {
try {
await client.query('EnableCallbacks', [true]);
} catch (err) {
log.red('EnableCallbacks failed');
log.red(err);
process.exit(1);
}
log.green('Callbacks enabled ...');
this.triggerModeScript(client);
},
async authenticate(client) {
// https://doc.maniaplanet.com/dedicated-server/references/xml-rpc-callbacks
// https://doc.maniaplanet.com/dedicated-server/references/xml-rpc-methods
await client.query('SetApiVersion', ['2019-03-02']);
try {
await client.query('Authenticate', [config.trackmania.login, config.trackmania.password]);
} catch (err) {
log.red('Authenticate failed');
log.red(err);
process.exit(1);
}
log.green('Authenticated ...');
this.enableCallbacks(client);
},
connect() {
const client = gbxremote.createClient(config.trackmania.port, config.trackmania.host);
db.mysqlCheckAndDeployTables();
client.on('error', (err) => {
log.red('Could not connect to server');
log.red(err);
// TODO: add restart tmserver
process.exit(1);
});
log.green('Connected ...');
client.on('connect', () => this.authenticate(client));
},
};
pepeplanet.connect();
export default pepeplanet;