forked from menmaa/titles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (79 loc) · 2.14 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
const fs = require('fs');
const path = require('path');
module.exports = function TeraTitles(dispatch) {
const { UI } = (() => {
try {
return dispatch;
} catch (e) {
console.warn('[titles] ui module not found. UI functionality will be unavailable.');
}
})();
const { command } = dispatch;
let gameId, name, currentTitle;
let playerTitles = {};
try { playerTitles = require('./player.json'); }
catch (e) {
playerTitles = {};
saveTitles();
}
// UI WILL BE FIXED SOON FOR NOW USED IDs FROM titles.json
if(UI) {
ui = UI(dispatch);
ui.use('/titles/', UI.static(__dirname + '/ui'));
ui.get('/titles/api/*', (req, res) => {
let data = req.params[0].split(";");
let request = data.shift();
switch(request) {
case "load": {
const titles = {
currentTitle,
titles: require('./titles.json')
};
return res.status(200).json(titles);
}
case "title": {
let title = parseInt(data[0]);
dispatch.send('S_APPLY_TITLE', 2, { gameId, title });
playerTitles[name] = title;
saveTitles();
currentTitle = title;
return res.status(200).json({ ok: 1 });
}
default:
return res.status(404).send("404");
}
});
}
dispatch.hook('S_LOGIN', 14, (event) => {
({ gameId, name } = event);
if(playerTitles[name])
event.title = playerTitles[name];
currentTitle = event.title;
return true;
});
// REMEMBER TO MOVE .map and .def FILES -> TOOLBOX -> DATA TY
dispatch.hook('C_APPLY_TITLE', 1, event => {
playerTitles[name] = undefined;
saveTitles();
currentTitle = event.id;
});
command.add('title', (arg) => {
if(arg) {
let title = parseInt(arg);
dispatch.send('S_APPLY_TITLE', 3, { gameId, title });
playerTitles[name] = title;
saveTitles();
currentTitle = title;
command.message(' Title changed to ID: ' + title);
} else if(UI) {
dispatch.ui.open('/titles/');
} else {
command.message(' UI unavailable. Title ID required.');
}
});
function saveTitles() {
fs.writeFile(path.join(__dirname, 'player.json'), JSON.stringify(playerTitles, null, 4), (err) => {
if(err) console.warn(err);
});
}
}