Skip to content

Commit 4fd3cb9

Browse files
committed
update osu!stats.js
Update to new script engine
1 parent 452b5d7 commit 4fd3cb9

File tree

1 file changed

+200
-91
lines changed

1 file changed

+200
-91
lines changed

osu!stats.js

Lines changed: 200 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
registerPlugin({
22
name: 'Osu!Stats',
3-
version: '1.0',
3+
version: '1.1',
44
description: 'Osu! account statistics',
55
author: 'NT5',
6-
vars: {
7-
api_key: {
6+
vars: [
7+
{
8+
name: 'api_key',
89
title: 'API KEY (http://osu.ppy.sh/p/api)',
910
type: 'string'
1011
},
11-
text_format: {
12+
{
13+
name: 'text_format',
1214
title: 'Message Format',
1315
type: 'multiline',
1416
placeholder: 'Player: {player_name} - Play Count: {play_count} - Ranked Score: {ranked_score} - Total Score: {total_score} - PP: {pp_raw} - Accuracy: {accuracy}% - Level: {level} - Country: {country}'
1517
},
16-
default_gamemode: {
18+
{
19+
name: 'default_gamemode',
1720
title: 'Default gamemode',
1821
type: 'select',
1922
options: [
@@ -23,8 +26,14 @@ registerPlugin({
2326
'Mania'
2427
]
2528
}
26-
}
29+
]
2730
}, function(sinusbot, config) {
31+
32+
var backend = require('backend');
33+
var engine = require('engine');
34+
var event = require('event');
35+
36+
// String format util
2837
if (!String.prototype.format) {
2938
String.prototype.format = function() {
3039
var str = this.toString();
@@ -40,99 +49,199 @@ registerPlugin({
4049
}
4150
}
4251

43-
function addCommas(nStr) {
44-
nStr += '';
45-
x = nStr.split('.');
46-
x1 = x[0];
47-
x2 = x.length > 1 ? '.' + x[1] : '';
48-
var rgx = /(\d+)(\d{3})/;
49-
while (rgx.test(x1)) {
50-
x1 = x1.replace(rgx, '$1' + ',' + '$2');
51-
}
52-
return x1 + x2;
53-
}
54-
var api_url = "https://osu.ppy.sh/api/get_user?k={0}&u={1}&m={2}";
55-
sinusbot.on('chat', function(ev) {
56-
var send_msg = function(id, mode, msg) {
57-
switch (mode) {
58-
case 1:
59-
sinusbot.chatPrivate(id, msg);
52+
var osu = {
53+
config: {
54+
api: {
55+
url: "https://osu.ppy.sh/api/get_user?k={0}&u={1}&m={2}",
56+
key: config.api_key || false
57+
},
58+
plugin: {
59+
regex: {
60+
cmd: /^!(\w+)\s*(.+)/,
61+
gamemode: /^(?:m|gamemode|mode):(standar|taiko|ctb|mania\*?)\s(.+)/i
62+
},
63+
trigger: 'osu',
64+
gamemode: parseInt(config.default_gamemode),
65+
message: config.text_format || 'Player: {player_name} - Play Count: {play_count} - Ranked Score: {ranked_score} - Total Score: {total_score} - PP: {pp_raw} - Accuracy: {accuracy}% - Level: {level} - Country: {country}'
66+
}
67+
},
68+
msg: function(options) {
69+
options = (typeof options !== "object") ? {} : options;
70+
71+
options.text = options.text || '';
72+
options.mode = options.mode || 0;
73+
options.backend = options.backend || backend;
74+
options.client = options.client || false;
75+
options.channel = options.channel || false;
76+
77+
/*
78+
TODO
79+
- [BUG] Make sure if works in all cases
80+
*/
81+
switch(options.mode) {
82+
case 1: // Private client message
83+
if (options.client) {
84+
options.client.chat(options.text);
85+
} else {
86+
youtube.msg({
87+
text: options.text,
88+
backend: options.backend,
89+
mode: 0
90+
});
91+
}
6092
break;
61-
case 2:
62-
sinusbot.chatChannel(msg);
93+
case 2: // Channel message
94+
if (options.channel) {
95+
options.channel.chat(options.text);
96+
} else {
97+
youtube.msg({
98+
text: options.text,
99+
backend: options.backend,
100+
mode: 0
101+
});
102+
}
63103
break;
64-
default:
65-
sinusbot.chatServer(msg);
104+
default: // Server message
105+
options.backend.chat(options.text);
66106
break;
67-
}
68-
};
69-
var self = sinusbot.getBotId();
70-
if (ev.clientId == self) return;
71-
var cmd, text, re = /^!(\w+)\s*(.+)/;
72-
if ((text = re.exec(ev.msg)) !== null) {
73-
cmd = text[1].toLowerCase();
74-
text = text[2];
75-
if (cmd === 'osu') {
76-
if (text.length > 0) {
77-
if (typeof config.api_key == 'undefined' || config.api_key.length == 0) {
78-
send_msg(ev.clientId, ev.mode, "Invalid API KEY");
107+
}
108+
109+
},
110+
fetch: function(options) {
111+
options = (typeof options !== "object") ? {} : options;
112+
113+
options.api_key = options.api_key || osu.config.api.key;
114+
options.player = options.player || 'peppy';
115+
options.gamemode = options.gamemode || 0;
116+
options.callback = options.callback || function(json) { engine.log(json); };
117+
options.error_callback = options.error_callback || function(error) { engine.log(error); };
118+
119+
/*
120+
TODO
121+
- [ENH] Port to new script engine
122+
*/
123+
sinusbot.http({
124+
method: 'GET',
125+
url: osu.config.api.url.format(options.api_key, options.player, options.gamemode),
126+
headers: {
127+
'Content-Type': 'application/json; charset=UTF-8'
128+
}
129+
}, function(err, res) {
130+
if (err || res.statusCode != 200) {
131+
options.error_callback(err);
132+
} else {
133+
var json = JSON.parse(res.data);
134+
options.callback(json);
135+
}
136+
});
137+
},
138+
callbacks: {
139+
fetch: {
140+
done: function(client, channel, mode, data) {
141+
if (data.length > 0) {
142+
data = data[0];
143+
144+
// Prepare format
145+
var str_vars = {
146+
player_name: data.username,
147+
play_count: osu.util.addCommas(data.playcount),
148+
ranked_score: osu.util.addCommas(Math.floor(data.ranked_score)),
149+
total_score: osu.util.addCommas(Math.floor(data.total_score)),
150+
pp_raw: osu.util.addCommas(Math.floor(data.pp_raw)),
151+
accuracy: Math.floor(data.accuracy),
152+
level: Math.floor(data.level),
153+
country: data.country
154+
};
155+
156+
osu.msg({
157+
text: osu.config.plugin.message.format(str_vars),
158+
channel: channel,
159+
client: client,
160+
mode: mode
161+
});
162+
79163
} else {
80-
var mod_re = /^(?:m|gamemode|mode):(standar|taiko|ctb|mania\*?)\s(.+)/i;
81-
var gamemode, player;
82-
if ((mod_re = mod_re.exec(text)) !== null) {
83-
gamemode = mod_re[1];
84-
player = mod_re[2];
85-
switch (gamemode.toLowerCase()) {
86-
case 'taiko':
87-
gamemode = 1;
88-
break;
89-
case 'ctb':
90-
gamemode = 2;
91-
break;
92-
case 'mania':
93-
gamemode = 3;
94-
break;
95-
default:
96-
gamemode = 0;
97-
break;
98-
}
99-
} else {
100-
gamemode = config.default_gamemode;
101-
player = text;
102-
}
103-
sinusbot.http({
104-
method: 'GET',
105-
url: api_url.format(config.api_key, player, gamemode)
106-
}, function(err, res) {
107-
if (err) {
108-
send_msg(ev.clientId, ev.mode, "API Request error");
109-
} else {
110-
if (res.statusCode == 200) {
111-
var p = JSON.parse(res.data);
112-
if (p.length > 0) {
113-
p = p[0];
114-
var default_str = "Player: {player_name} - Play Count: {play_count} - Ranked Score: {ranked_score} - Total Score: {total_score} - PP: {pp_raw} - Accuracy: {accuracy}% - Level: {level} - Country: {country}",
115-
str_msg;
116-
str_msg = (typeof config.text_format == 'undefined' || config.text_format.length == 0 ? default_str : config.text_format);
117-
send_msg(ev.clientId, ev.mode, str_msg.format({
118-
player_name: p.username,
119-
play_count: addCommas(p.playcount),
120-
ranked_score: addCommas(Math.floor(p.ranked_score)),
121-
total_score: addCommas(Math.floor(p.total_score)),
122-
pp_raw: addCommas(Math.floor(p.pp_raw)),
123-
accuracy: Math.floor(p.accuracy),
124-
level: Math.floor(p.level),
125-
country: p.country
126-
}));
127-
} else {
128-
send_msg(ev.clientId, ev.mode, "{0} Unknown player".format(player));
129-
}
130-
}
131-
}
164+
osu.msg({
165+
text: "Unknown player",
166+
channel: channel,
167+
client: client,
168+
mode: mode
132169
});
133170
}
171+
},
172+
error: function(client, channel, mode, data) {
173+
osu.msg({
174+
text: "Search failed (Bad request)",
175+
channel: channel,
176+
client: client,
177+
mode: mode
178+
});
134179
}
135180
}
181+
},
182+
util: {
183+
addCommas: function(nStr) {
184+
nStr += '';
185+
x = nStr.split('.');
186+
x1 = x[0];
187+
x2 = x.length > 1 ? '.' + x[1] : '';
188+
var rgx = /(\d+)(\d{3})/;
189+
while (rgx.test(x1)) {
190+
x1 = x1.replace(rgx, '$1' + ',' + '$2');
191+
}
192+
return x1 + x2;
193+
}
194+
}
195+
};
196+
197+
event.on('chat', function(ev) {
198+
var client = ev.client;
199+
var channel = ev.channel;
200+
201+
if (client.isSelf()) return;
202+
203+
var cmd, text;
204+
205+
// Regex text: !{command} {text}
206+
if ((text = osu.config.plugin.regex.cmd.exec(ev.text)) !== null) {
207+
cmd = text[1].toLowerCase(); // command trigger
208+
text = text[2]; // args
209+
if (cmd === osu.config.plugin.trigger.toLowerCase() && text.length > 0) {
210+
// trigger command {text}
211+
var gm_regx, gamemode, player;
212+
if ((gm_regx = osu.config.plugin.regex.gamemode.exec(text)) !== null) {
213+
gamemode = gm_regx[1];
214+
player = gm_regx[2];
215+
switch (gamemode.toLowerCase()) {
216+
case 'taiko':
217+
gamemode = 1;
218+
break;
219+
case 'ctb':
220+
gamemode = 2;
221+
break;
222+
case 'mania':
223+
gamemode = 3;
224+
break;
225+
default:
226+
gamemode = 0;
227+
break;
228+
}
229+
} else {
230+
gamemode = config.default_gamemode;
231+
player = text;
232+
}
233+
234+
osu.fetch({
235+
player: player,
236+
gamemode: gamemode,
237+
callback: function(data) {
238+
osu.callbacks.fetch.done(client, channel, ev.mode, data);
239+
},
240+
error_callback: function(data) {
241+
osu.callbacks.fetch.error(client, channel, ev.mode, data);
242+
}
243+
});
244+
}
136245
}
137246
});
138247
});

0 commit comments

Comments
 (0)