Commit ec322fef authored by nanahira's avatar nanahira

async for dashboard

parent d6a52232
...@@ -132,11 +132,15 @@ try ...@@ -132,11 +132,15 @@ try
catch e catch e
log.info e unless e.code == 'ENOENT' log.info e unless e.code == 'ENOENT'
setting_save = global.setting_save = (settings) -> setting_save = global.setting_save = (settings, callback) ->
fs.writeFileSync(settings.file, JSON.stringify(settings, null, 2)) if !callback
callback = (err) ->
if(err)
log.warn("setting save fail", err.toString())
fs.writeFile(settings.file, JSON.stringify(settings, null, 2), callback)
return return
setting_change = global.setting_change = (settings, path, val) -> setting_change = global.setting_change = (settings, path, val, callback) ->
# path should be like "modules:welcome" # path should be like "modules:welcome"
log.info("setting changed", path, val) if _.isString(val) log.info("setting changed", path, val) if _.isString(val)
path=path.split(':') path=path.split(':')
...@@ -149,7 +153,7 @@ setting_change = global.setting_change = (settings, path, val) -> ...@@ -149,7 +153,7 @@ setting_change = global.setting_change = (settings, path, val) ->
target=target[key] target=target[key]
key = path.shift() key = path.shift()
target[key] = val target[key] = val
setting_save(settings) setting_save(settings, callback)
return return
# 读取配置 # 读取配置
...@@ -450,20 +454,27 @@ ROOM_connected_ip = global.ROOM_connected_ip = {} ...@@ -450,20 +454,27 @@ ROOM_connected_ip = global.ROOM_connected_ip = {}
ROOM_bad_ip = global.ROOM_bad_ip = {} ROOM_bad_ip = global.ROOM_bad_ip = {}
# ban a user manually and permanently # ban a user manually and permanently
ban_user = global.ban_user = (name) -> ban_user = global.ban_user = (name, callback) ->
settings.ban.banned_user.push(name) settings.ban.banned_user.push(name)
setting_save(settings) setting_save(settings)
bad_ip=0 bad_ip = []
for room in ROOM_all when room and room.established _async.each(ROOM_all.filter((room)->
for player in room.players return room and room.established
if player and (player.name == name or player.ip == bad_ip) ), (room, done)->
_async.each(["players", "watchers"], (player_type, _done)->
_async.each(room[player_type].filter((player)->
return player and (player.name == name or bad_ip.indexOf(player.ip) != -1)
), (player, __done)->
bad_ip = player.ip bad_ip = player.ip
ROOM_bad_ip[bad_ip]=99 ROOM_bad_ip[bad_ip]=99
settings.ban.banned_ip.push(player.ip) settings.ban.banned_ip.push(player.ip)
ygopro.stoc_send_chat_to_room(room, "#{player.name} ${kicked_by_system}", ygopro.constants.COLORS.RED) ygopro.stoc_send_chat_to_room(room, "#{player.name} ${kicked_by_system}", ygopro.constants.COLORS.RED)
CLIENT_send_replays(player, room) CLIENT_send_replays(player, room)
CLIENT_kick(player) CLIENT_kick(player)
continue __done()
, _done)
, done)
, callback)
return return
# automatically ban user to use random duel # automatically ban user to use random duel
...@@ -484,6 +495,28 @@ ROOM_ban_player = global.ROOM_ban_player = (name, ip, reason, countadd = 1)-> ...@@ -484,6 +495,28 @@ ROOM_ban_player = global.ROOM_ban_player = (name, ip, reason, countadd = 1)->
#log.info("banned", name, ip, reason, bannedplayer.count) #log.info("banned", name, ip, reason, bannedplayer.count)
return return
ROOM_kick = (name, callback)->
rooms = ROOM_all.filter((room)->
return room and room.established and (name == "all" or name == room.process_pid.toString() or name == room.name)
)
if !rooms.length
callback(null, false)
_async.each(rooms, (room, done)->
if room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN
room.scores[room.dueling_players[0].name_vpass] = 0
room.scores[room.dueling_players[1].name_vpass] = 0
room.kicked = true
room.send_replays()
room.process.kill()
room.delete()
done()
return
, (err)->
callback(null, true)
return
)
ROOM_player_win = global.ROOM_player_win = (name)-> ROOM_player_win = global.ROOM_player_win = (name)->
if !ROOM_players_scores[name] if !ROOM_players_scores[name]
ROOM_players_scores[name]={win:0, lose:0, flee:0, combo:0} ROOM_players_scores[name]={win:0, lose:0, flee:0, combo:0}
...@@ -2366,7 +2399,7 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server, datas)-> ...@@ -2366,7 +2399,7 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server, datas)->
return return
# 登场台词 # 登场台词
load_dialogues = global.load_dialogues = () -> load_dialogues = global.load_dialogues = (callback) ->
request request
url: settings.modules.dialogues.get url: settings.modules.dialogues.get
json: true json: true
...@@ -2378,6 +2411,8 @@ load_dialogues = global.load_dialogues = () -> ...@@ -2378,6 +2411,8 @@ load_dialogues = global.load_dialogues = () ->
else else
setting_change(dialogues, "dialogues", body) setting_change(dialogues, "dialogues", body)
log.info "dialogues loaded", _.size dialogues.dialogues log.info "dialogues loaded", _.size dialogues.dialogues
if callback
callback(error, body)
return return
return return
...@@ -2824,7 +2859,7 @@ ygopro.stoc_send_random_tip_to_room = (room)-> ...@@ -2824,7 +2859,7 @@ ygopro.stoc_send_random_tip_to_room = (room)->
ygopro.stoc_send_chat_to_room(room, "Tip: " + tips.tips[Math.floor(Math.random() * tips.tips.length)]) ygopro.stoc_send_chat_to_room(room, "Tip: " + tips.tips[Math.floor(Math.random() * tips.tips.length)])
return return
load_tips = global.load_tips = ()-> load_tips = global.load_tips = (callback)->
request request
url: settings.modules.tips.get url: settings.modules.tips.get
json: true json: true
...@@ -2836,6 +2871,8 @@ load_tips = global.load_tips = ()-> ...@@ -2836,6 +2871,8 @@ load_tips = global.load_tips = ()->
else else
setting_change(tips, "tips", body) setting_change(tips, "tips", body)
log.info "tips loaded", tips.tips.length log.info "tips loaded", tips.tips.length
if callback
callback(error, body)
return return
return return
...@@ -3506,13 +3543,13 @@ spawn_windbot = global.spawn_windbot = () -> ...@@ -3506,13 +3543,13 @@ spawn_windbot = global.spawn_windbot = () ->
windbot_process = spawn windbot_bin, windbot_parameters, {cwd: 'windbot'} windbot_process = spawn windbot_bin, windbot_parameters, {cwd: 'windbot'}
windbot_process.on 'error', (err)-> windbot_process.on 'error', (err)->
log.warn 'WindBot ERROR', err log.warn 'WindBot ERROR', err
if windbot_looplimit < 1000 and !rebooted if windbot_looplimit < 1000 and !global.rebooted
windbot_looplimit++ windbot_looplimit++
spawn_windbot() spawn_windbot()
return return
windbot_process.on 'exit', (code)-> windbot_process.on 'exit', (code)->
log.warn 'WindBot EXIT', code log.warn 'WindBot EXIT', code
if windbot_looplimit < 1000 and !rebooted if windbot_looplimit < 1000 and !global.rebooted
windbot_looplimit++ windbot_looplimit++
spawn_windbot() spawn_windbot()
return return
...@@ -3529,7 +3566,7 @@ spawn_windbot = global.spawn_windbot = () -> ...@@ -3529,7 +3566,7 @@ spawn_windbot = global.spawn_windbot = () ->
if settings.modules.windbot.enabled and settings.modules.windbot.spawn if settings.modules.windbot.enabled and settings.modules.windbot.spawn
spawn_windbot() spawn_windbot()
rebooted = false global.rebooted = false
#http #http
if settings.modules.http if settings.modules.http
...@@ -3687,18 +3724,26 @@ if settings.modules.http ...@@ -3687,18 +3724,26 @@ if settings.modules.http
return return
if u.query.stop == 'false' if u.query.stop == 'false'
u.query.stop = false u.query.stop = false
setting_change(settings, 'modules:stop', u.query.stop) setting_change(settings, 'modules:stop', u.query.stop, (err)->
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['stop ok', '" + u.query.stop + "']")) if(err)
response.end(addCallback(u.query.callback, "['stop fail', '" + u.query.stop + "']"))
else
response.end(addCallback(u.query.callback, "['stop ok', '" + u.query.stop + "']"))
)
else if u.query.welcome else if u.query.welcome
if !auth.auth(u.query.username, u.query.pass, "change_settings", "change_welcome") if !auth.auth(u.query.username, u.query.pass, "change_settings", "change_welcome")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
setting_change(settings, 'modules:welcome', u.query.welcome) setting_change(settings, 'modules:welcome', (err)->
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['welcome ok', '" + u.query.welcome + "']")) if(err)
response.end(addCallback(u.query.callback, "['welcome fail', '" + u.query.welcome + "']"))
else
response.end(addCallback(u.query.callback, "['welcome ok', '" + u.query.welcome + "']"))
)
else if u.query.getwelcome else if u.query.getwelcome
if !auth.auth(u.query.username, u.query.pass, "change_settings", "get_welcome") if !auth.auth(u.query.username, u.query.pass, "change_settings", "get_welcome")
...@@ -3713,98 +3758,112 @@ if settings.modules.http ...@@ -3713,98 +3758,112 @@ if settings.modules.http
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
load_tips() load_tips((err)->
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['loading tip', '" + settings.modules.tips.get + "']")) if(err)
response.end(addCallback(u.query.callback, "['tip fail', '" + u.query.loadtips + "']"))
else
response.end(addCallback(u.query.callback, "['tip ok', '" + u.query.loadtips + "']"))
)
else if u.query.loaddialogues else if u.query.loaddialogues
if !auth.auth(u.query.username, u.query.pass, "change_settings", "change_dialogues") if !auth.auth(u.query.username, u.query.pass, "change_settings", "change_dialogues")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
load_dialogues() load_dialogues((err)->
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['loading dialogues', '" + settings.modules.dialogues.get + "']")) if(err)
response.end(addCallback(u.query.callback, "['dialogues fail', '" + u.query.loaddialogues + "']"))
else
response.end(addCallback(u.query.callback, "['dialogues ok', '" + u.query.loaddialogues + "']"))
)
else if u.query.ban else if u.query.ban
if !auth.auth(u.query.username, u.query.pass, "ban_user", "ban_user") if !auth.auth(u.query.username, u.query.pass, "ban_user", "ban_user")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
ban_user(u.query.ban) ban_user(u.query.ban, (err)->
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['ban ok', '" + u.query.ban + "']")) if(err)
response.end(addCallback(u.query.callback, "['ban fail', '" + u.query.ban + "']"))
else
response.end(addCallback(u.query.callback, "['ban ok', '" + u.query.ban + "']"))
)
else if u.query.kick else if u.query.kick
if !auth.auth(u.query.username, u.query.pass, "kick_user", "kick_user") if !auth.auth(u.query.username, u.query.pass, "kick_user", "kick_user")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
kick_room_found = false ROOM_kick(u.query.kick, (err, found)->
for room in ROOM_all when room and room.established and (u.query.kick == "all" or u.query.kick == room.process_pid.toString() or u.query.kick == room.name) response.writeHead(200)
kick_room_found = true if err
if room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN response.end(addCallback(u.query.callback, "['kick fail', '" + u.query.kick + "']"))
room.scores[room.dueling_players[0].name_vpass] = 0 else if found
room.scores[room.dueling_players[1].name_vpass] = 0 response.end(addCallback(u.query.callback, "['kick ok', '" + u.query.kick + "']"))
room.kicked = true else
room.send_replays() response.end(addCallback(u.query.callback, "['room not found', '" + u.query.kick + "']"))
room.process.kill() )
room.delete()
response.writeHead(200)
if kick_room_found
response.end(addCallback(u.query.callback, "['kick ok', '" + u.query.kick + "']"))
else
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.kick + "']"))
else if u.query.death else if u.query.death
if !auth.auth(u.query.username, u.query.pass, "start_death", "start_death") if !auth.auth(u.query.username, u.query.pass, "start_death", "start_death")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
rooms = ROOM_all.filter((room)->
return room and (u.query.death == "all" or u.query.death == room.process_pid.toString() or u.query.death == room.name)
)
death_room_found = false death_room_found = false
for room in ROOM_all when room and (u.query.death == "all" or u.query.death == room.process_pid.toString() or u.query.death == room.name) _async.each(rooms, (room, done)->
if room.start_death() if room.start_death()
death_room_found = true death_room_found = true
response.writeHead(200) done()
if death_room_found , () ->
response.end(addCallback(u.query.callback, "['death ok', '" + u.query.death + "']")) response.writeHead(200)
else if death_room_found
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.death + "']")) response.end(addCallback(u.query.callback, "['death ok', '" + u.query.death + "']"))
else
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.death + "']"))
)
else if u.query.deathcancel else if u.query.deathcancel
if !auth.auth(u.query.username, u.query.pass, "start_death", "cancel_death") if !auth.auth(u.query.username, u.query.pass, "start_death", "cancel_death")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
rooms = ROOM_all.filter((room)->
return room and (u.query.deathcancel == "all" or u.query.deathcancel == room.process_pid.toString() or u.query.deathcancel == room.name)
)
death_room_found = false death_room_found = false
for room in ROOM_all when room and (u.query.deathcancel == "all" or u.query.deathcancel == room.process_pid.toString() or u.query.deathcancel == room.name) _async.each(rooms, (room, done)->
if room.cancel_death() if room.cancel_death()
death_room_found = true death_room_found = true
response.writeHead(200) done()
if death_room_found , () ->
response.end(addCallback(u.query.callback, "['death cancel ok', '" + u.query.deathcancel + "']")) response.writeHead(200)
else if death_room_found
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.deathcancel + "']")) response.end(addCallback(u.query.callback, "['death cancel ok', '" + u.query.deathcancel + "']"))
else
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.deathcancel + "']"))
)
else if u.query.reboot else if u.query.reboot
if !auth.auth(u.query.username, u.query.pass, "stop", "reboot") if !auth.auth(u.query.username, u.query.pass, "stop", "reboot")
response.writeHead(200) response.writeHead(200)
response.end(addCallback(u.query.callback, "['密码错误', 0]")) response.end(addCallback(u.query.callback, "['密码错误', 0]"))
return return
for room in ROOM_all when room ROOM_kick("all", (err, found)->
if room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN global.rebooted = true
room.scores[room.dueling_players[0].name_vpass] = 0 if windbot_process
room.scores[room.dueling_players[1].name_vpass] = 0 windbot_process.kill()
room.kicked = true response.writeHead(200)
room.send_replays() response.end(addCallback(u.query.callback, "['reboot ok', '" + u.query.reboot + "']"))
room.process.kill() process.exit()
room.delete() )
rebooted = true
if windbot_process
windbot_process.kill()
response.writeHead(200)
response.end(addCallback(u.query.callback, "['reboot ok', '" + u.query.reboot + "']"))
throw "rebooted"
else else
response.writeHead(400) response.writeHead(400)
......
// Generated by CoffeeScript 1.12.7 // Generated by CoffeeScript 1.12.7
(function() { (function() {
var CLIENT_get_authorize_key, CLIENT_get_kick_reconnect_target, CLIENT_heartbeat_register, CLIENT_heartbeat_unregister, CLIENT_import_data, CLIENT_is_able_to_kick_reconnect, CLIENT_is_able_to_reconnect, CLIENT_is_banned_by_mc, CLIENT_is_player, CLIENT_kick, CLIENT_kick_reconnect, CLIENT_pre_reconnect, CLIENT_reconnect, CLIENT_reconnect_register, CLIENT_reconnect_unregister, CLIENT_send_pre_reconnect_info, CLIENT_send_reconnect_info, CLIENT_send_replays, Cloud_replay_ids, ROOM_all, ROOM_bad_ip, ROOM_ban_player, ROOM_clear_disconnect, ROOM_connected_ip, ROOM_find_by_name, ROOM_find_by_pid, ROOM_find_by_port, ROOM_find_by_title, ROOM_find_or_create_ai, ROOM_find_or_create_by_name, ROOM_find_or_create_random, ROOM_player_flee, ROOM_player_get_score, ROOM_player_lose, ROOM_player_win, ROOM_players_banned, ROOM_players_oppentlist, ROOM_players_scores, ROOM_unwelcome, ROOM_validate, Room, SERVER_clear_disconnect, SERVER_kick, SOCKET_flush_data, _, _async, addCallback, auth, badwords, ban_user, bunyan, challonge, challonge_cache, challonge_module_name, challonge_queue_callbacks, chat_color, config, cppversion, crypto, date, deck_name_match, default_config, default_data, dialogues, disconnect_list, dns, duel_log, e, exec, execFile, fs, geoip, get_callback, get_memory_usage, http, http_server, https, https_server, import_datas, imported, is_requesting, j, l, len, len1, len2, lflists, list, loadJSON, load_dialogues, load_tips, log, long_resolve_cards, m, memory_usage, merge, moment, net, oldbadwords, oldconfig, olddialogues, oldduellog, oldtips, options, os, path, pgClient, pg_client, pg_query, plugin_filename, plugin_list, plugin_path, real_windbot_server_ip, rebooted, redis, redisdb, ref, ref1, refresh_challonge_cache, release_disconnect, report_to_big_brother, request, requestListener, roomlist, setting_change, setting_save, settings, spawn, spawnSync, spawn_windbot, tips, url, users_cache, wait_room_start, wait_room_start_arena, windbot_looplimit, windbot_process, windbots, ygopro, zlib; var CLIENT_get_authorize_key, CLIENT_get_kick_reconnect_target, CLIENT_heartbeat_register, CLIENT_heartbeat_unregister, CLIENT_import_data, CLIENT_is_able_to_kick_reconnect, CLIENT_is_able_to_reconnect, CLIENT_is_banned_by_mc, CLIENT_is_player, CLIENT_kick, CLIENT_kick_reconnect, CLIENT_pre_reconnect, CLIENT_reconnect, CLIENT_reconnect_register, CLIENT_reconnect_unregister, CLIENT_send_pre_reconnect_info, CLIENT_send_reconnect_info, CLIENT_send_replays, Cloud_replay_ids, ROOM_all, ROOM_bad_ip, ROOM_ban_player, ROOM_clear_disconnect, ROOM_connected_ip, ROOM_find_by_name, ROOM_find_by_pid, ROOM_find_by_port, ROOM_find_by_title, ROOM_find_or_create_ai, ROOM_find_or_create_by_name, ROOM_find_or_create_random, ROOM_kick, ROOM_player_flee, ROOM_player_get_score, ROOM_player_lose, ROOM_player_win, ROOM_players_banned, ROOM_players_oppentlist, ROOM_players_scores, ROOM_unwelcome, ROOM_validate, Room, SERVER_clear_disconnect, SERVER_kick, SOCKET_flush_data, _, _async, addCallback, auth, badwords, ban_user, bunyan, challonge, challonge_cache, challonge_module_name, challonge_queue_callbacks, chat_color, config, cppversion, crypto, date, deck_name_match, default_config, default_data, dialogues, disconnect_list, dns, duel_log, e, exec, execFile, fs, geoip, get_callback, get_memory_usage, http, http_server, https, https_server, import_datas, imported, is_requesting, j, l, len, len1, len2, lflists, list, loadJSON, load_dialogues, load_tips, log, long_resolve_cards, m, memory_usage, merge, moment, net, oldbadwords, oldconfig, olddialogues, oldduellog, oldtips, options, os, path, pgClient, pg_client, pg_query, plugin_filename, plugin_list, plugin_path, real_windbot_server_ip, redis, redisdb, ref, ref1, refresh_challonge_cache, release_disconnect, report_to_big_brother, request, requestListener, roomlist, setting_change, setting_save, settings, spawn, spawnSync, spawn_windbot, tips, url, users_cache, wait_room_start, wait_room_start_arena, windbot_looplimit, windbot_process, windbots, ygopro, zlib;
net = require('net'); net = require('net');
...@@ -130,11 +130,18 @@ ...@@ -130,11 +130,18 @@
} }
} }
setting_save = global.setting_save = function(settings) { setting_save = global.setting_save = function(settings, callback) {
fs.writeFileSync(settings.file, JSON.stringify(settings, null, 2)); if (!callback) {
callback = function(err) {
if (err) {
return log.warn("setting save fail", err.toString());
}
};
}
fs.writeFile(settings.file, JSON.stringify(settings, null, 2), callback);
}; };
setting_change = global.setting_change = function(settings, path, val) { setting_change = global.setting_change = function(settings, path, val, callback) {
var key, target; var key, target;
if (_.isString(val)) { if (_.isString(val)) {
log.info("setting changed", path, val); log.info("setting changed", path, val);
...@@ -151,7 +158,7 @@ ...@@ -151,7 +158,7 @@
key = path.shift(); key = path.shift();
target[key] = val; target[key] = val;
} }
setting_save(settings); setting_save(settings, callback);
}; };
default_config = loadJSON('./data/default_config.json'); default_config = loadJSON('./data/default_config.json');
...@@ -527,29 +534,28 @@ ...@@ -527,29 +534,28 @@
ROOM_bad_ip = global.ROOM_bad_ip = {}; ROOM_bad_ip = global.ROOM_bad_ip = {};
ban_user = global.ban_user = function(name) { ban_user = global.ban_user = function(name, callback) {
var bad_ip, len2, len3, m, n, player, ref2, room; var bad_ip;
settings.ban.banned_user.push(name); settings.ban.banned_user.push(name);
setting_save(settings); setting_save(settings);
bad_ip = 0; bad_ip = [];
for (m = 0, len2 = ROOM_all.length; m < len2; m++) { _async.each(ROOM_all.filter(function(room) {
room = ROOM_all[m]; return room && room.established;
if (room && room.established) { }), function(room, done) {
ref2 = room.players; return _async.each(["players", "watchers"], function(player_type, _done) {
for (n = 0, len3 = ref2.length; n < len3; n++) { return _async.each(room[player_type].filter(function(player) {
player = ref2[n]; return player && (player.name === name || bad_ip.indexOf(player.ip) !== -1);
if (player && (player.name === name || player.ip === bad_ip)) { }), function(player, __done) {
bad_ip = player.ip; bad_ip = player.ip;
ROOM_bad_ip[bad_ip] = 99; ROOM_bad_ip[bad_ip] = 99;
settings.ban.banned_ip.push(player.ip); settings.ban.banned_ip.push(player.ip);
ygopro.stoc_send_chat_to_room(room, player.name + " ${kicked_by_system}", ygopro.constants.COLORS.RED); ygopro.stoc_send_chat_to_room(room, player.name + " ${kicked_by_system}", ygopro.constants.COLORS.RED);
CLIENT_send_replays(player, room); CLIENT_send_replays(player, room);
CLIENT_kick(player); CLIENT_kick(player);
continue; return __done();
} }, _done);
} }, done);
} }, callback);
}
}; };
ROOM_ban_player = global.ROOM_ban_player = function(name, ip, reason, countadd) { ROOM_ban_player = global.ROOM_ban_player = function(name, ip, reason, countadd) {
...@@ -585,6 +591,29 @@ ...@@ -585,6 +591,29 @@
} }
}; };
ROOM_kick = function(name, callback) {
var rooms;
rooms = ROOM_all.filter(function(room) {
return room && room.established && (name === "all" || name === room.process_pid.toString() || name === room.name);
});
if (!rooms.length) {
callback(null, false);
}
return _async.each(rooms, function(room, done) {
if (room.duel_stage !== ygopro.constants.DUEL_STAGE.BEGIN) {
room.scores[room.dueling_players[0].name_vpass] = 0;
room.scores[room.dueling_players[1].name_vpass] = 0;
}
room.kicked = true;
room.send_replays();
room.process.kill();
room["delete"]();
done();
}, function(err) {
callback(null, true);
});
};
ROOM_player_win = global.ROOM_player_win = function(name) { ROOM_player_win = global.ROOM_player_win = function(name) {
if (!ROOM_players_scores[name]) { if (!ROOM_players_scores[name]) {
ROOM_players_scores[name] = { ROOM_players_scores[name] = {
...@@ -2940,7 +2969,7 @@ ...@@ -2940,7 +2969,7 @@
} }
}); });
load_dialogues = global.load_dialogues = function() { load_dialogues = global.load_dialogues = function(callback) {
request({ request({
url: settings.modules.dialogues.get, url: settings.modules.dialogues.get,
json: true json: true
...@@ -2953,6 +2982,9 @@ ...@@ -2953,6 +2982,9 @@
setting_change(dialogues, "dialogues", body); setting_change(dialogues, "dialogues", body);
log.info("dialogues loaded", _.size(dialogues.dialogues)); log.info("dialogues loaded", _.size(dialogues.dialogues));
} }
if (callback) {
callback(error, body);
}
}); });
}; };
...@@ -3567,7 +3599,7 @@ ...@@ -3567,7 +3599,7 @@
} }
}; };
load_tips = global.load_tips = function() { load_tips = global.load_tips = function(callback) {
request({ request({
url: settings.modules.tips.get, url: settings.modules.tips.get,
json: true json: true
...@@ -3580,6 +3612,9 @@ ...@@ -3580,6 +3612,9 @@
setting_change(tips, "tips", body); setting_change(tips, "tips", body);
log.info("tips loaded", tips.tips.length); log.info("tips loaded", tips.tips.length);
} }
if (callback) {
callback(error, body);
}
}); });
}; };
...@@ -4504,14 +4539,14 @@ ...@@ -4504,14 +4539,14 @@
}); });
windbot_process.on('error', function(err) { windbot_process.on('error', function(err) {
log.warn('WindBot ERROR', err); log.warn('WindBot ERROR', err);
if (windbot_looplimit < 1000 && !rebooted) { if (windbot_looplimit < 1000 && !global.rebooted) {
windbot_looplimit++; windbot_looplimit++;
spawn_windbot(); spawn_windbot();
} }
}); });
windbot_process.on('exit', function(code) { windbot_process.on('exit', function(code) {
log.warn('WindBot EXIT', code); log.warn('WindBot EXIT', code);
if (windbot_looplimit < 1000 && !rebooted) { if (windbot_looplimit < 1000 && !global.rebooted) {
windbot_looplimit++; windbot_looplimit++;
spawn_windbot(); spawn_windbot();
} }
...@@ -4530,7 +4565,7 @@ ...@@ -4530,7 +4565,7 @@
spawn_windbot(); spawn_windbot();
} }
rebooted = false; global.rebooted = false;
if (settings.modules.http) { if (settings.modules.http) {
addCallback = function(callback, text) { addCallback = function(callback, text) {
...@@ -4540,7 +4575,7 @@ ...@@ -4540,7 +4575,7 @@
return callback + "( " + text + " );"; return callback + "( " + text + " );";
}; };
requestListener = function(request, response) { requestListener = function(request, response) {
var archive_args, archive_name, archive_process, check, death_room_found, duellog, error, filename, getpath, kick_room_found, len2, len3, len4, len5, len6, len7, m, n, o, p, parseQueryString, pass_validated, player, q, r, ref2, replay, room, roomsjson, u; var archive_args, archive_name, archive_process, check, death_room_found, duellog, error, filename, getpath, len2, len3, m, n, parseQueryString, pass_validated, player, ref2, replay, room, rooms, roomsjson, u;
parseQueryString = true; parseQueryString = true;
u = url.parse(request.url, parseQueryString); u = url.parse(request.url, parseQueryString);
if (u.pathname === '/api/getrooms') { if (u.pathname === '/api/getrooms') {
...@@ -4738,18 +4773,28 @@ ...@@ -4738,18 +4773,28 @@
if (u.query.stop === 'false') { if (u.query.stop === 'false') {
u.query.stop = false; u.query.stop = false;
} }
setting_change(settings, 'modules:stop', u.query.stop); setting_change(settings, 'modules:stop', u.query.stop, function(err) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['stop ok', '" + u.query.stop + "']")); if (err) {
return response.end(addCallback(u.query.callback, "['stop fail', '" + u.query.stop + "']"));
} else {
return response.end(addCallback(u.query.callback, "['stop ok', '" + u.query.stop + "']"));
}
});
} else if (u.query.welcome) { } else if (u.query.welcome) {
if (!auth.auth(u.query.username, u.query.pass, "change_settings", "change_welcome")) { if (!auth.auth(u.query.username, u.query.pass, "change_settings", "change_welcome")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
setting_change(settings, 'modules:welcome', u.query.welcome); setting_change(settings, 'modules:welcome', function(err) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['welcome ok', '" + u.query.welcome + "']")); if (err) {
return response.end(addCallback(u.query.callback, "['welcome fail', '" + u.query.welcome + "']"));
} else {
return response.end(addCallback(u.query.callback, "['welcome ok', '" + u.query.welcome + "']"));
}
});
} else if (u.query.getwelcome) { } else if (u.query.getwelcome) {
if (!auth.auth(u.query.username, u.query.pass, "change_settings", "get_welcome")) { if (!auth.auth(u.query.username, u.query.pass, "change_settings", "get_welcome")) {
response.writeHead(200); response.writeHead(200);
...@@ -4764,124 +4809,119 @@ ...@@ -4764,124 +4809,119 @@
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
load_tips(); load_tips(function(err) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['loading tip', '" + settings.modules.tips.get + "']")); if (err) {
return response.end(addCallback(u.query.callback, "['tip fail', '" + u.query.loadtips + "']"));
} else {
return response.end(addCallback(u.query.callback, "['tip ok', '" + u.query.loadtips + "']"));
}
});
} else if (u.query.loaddialogues) { } else if (u.query.loaddialogues) {
if (!auth.auth(u.query.username, u.query.pass, "change_settings", "change_dialogues")) { if (!auth.auth(u.query.username, u.query.pass, "change_settings", "change_dialogues")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
load_dialogues(); load_dialogues(function(err) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['loading dialogues', '" + settings.modules.dialogues.get + "']")); if (err) {
return response.end(addCallback(u.query.callback, "['dialogues fail', '" + u.query.loaddialogues + "']"));
} else {
return response.end(addCallback(u.query.callback, "['dialogues ok', '" + u.query.loaddialogues + "']"));
}
});
} else if (u.query.ban) { } else if (u.query.ban) {
if (!auth.auth(u.query.username, u.query.pass, "ban_user", "ban_user")) { if (!auth.auth(u.query.username, u.query.pass, "ban_user", "ban_user")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
ban_user(u.query.ban); ban_user(u.query.ban, function(err) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['ban ok', '" + u.query.ban + "']")); if (err) {
return response.end(addCallback(u.query.callback, "['ban fail', '" + u.query.ban + "']"));
} else {
return response.end(addCallback(u.query.callback, "['ban ok', '" + u.query.ban + "']"));
}
});
} else if (u.query.kick) { } else if (u.query.kick) {
if (!auth.auth(u.query.username, u.query.pass, "kick_user", "kick_user")) { if (!auth.auth(u.query.username, u.query.pass, "kick_user", "kick_user")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
kick_room_found = false; ROOM_kick(u.query.kick, function(err, found) {
for (o = 0, len4 = ROOM_all.length; o < len4; o++) { response.writeHead(200);
room = ROOM_all[o]; if (err) {
if (!(room && room.established && (u.query.kick === "all" || u.query.kick === room.process_pid.toString() || u.query.kick === room.name))) { return response.end(addCallback(u.query.callback, "['kick fail', '" + u.query.kick + "']"));
continue; } else if (found) {
} return response.end(addCallback(u.query.callback, "['kick ok', '" + u.query.kick + "']"));
kick_room_found = true; } else {
if (room.duel_stage !== ygopro.constants.DUEL_STAGE.BEGIN) { return response.end(addCallback(u.query.callback, "['room not found', '" + u.query.kick + "']"));
room.scores[room.dueling_players[0].name_vpass] = 0;
room.scores[room.dueling_players[1].name_vpass] = 0;
} }
room.kicked = true; });
room.send_replays();
room.process.kill();
room["delete"]();
}
response.writeHead(200);
if (kick_room_found) {
response.end(addCallback(u.query.callback, "['kick ok', '" + u.query.kick + "']"));
} else {
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.kick + "']"));
}
} else if (u.query.death) { } else if (u.query.death) {
if (!auth.auth(u.query.username, u.query.pass, "start_death", "start_death")) { if (!auth.auth(u.query.username, u.query.pass, "start_death", "start_death")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
rooms = ROOM_all.filter(function(room) {
return room && (u.query.death === "all" || u.query.death === room.process_pid.toString() || u.query.death === room.name);
});
death_room_found = false; death_room_found = false;
for (p = 0, len5 = ROOM_all.length; p < len5; p++) { _async.each(rooms, function(room, done) {
room = ROOM_all[p]; if (room.start_death()) {
if (room && (u.query.death === "all" || u.query.death === room.process_pid.toString() || u.query.death === room.name)) { death_room_found = true;
if (room.start_death()) {
death_room_found = true;
}
} }
} return done();
response.writeHead(200); }, function() {
if (death_room_found) { response.writeHead(200);
response.end(addCallback(u.query.callback, "['death ok', '" + u.query.death + "']")); if (death_room_found) {
} else { return response.end(addCallback(u.query.callback, "['death ok', '" + u.query.death + "']"));
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.death + "']")); } else {
} return response.end(addCallback(u.query.callback, "['room not found', '" + u.query.death + "']"));
}
});
} else if (u.query.deathcancel) { } else if (u.query.deathcancel) {
if (!auth.auth(u.query.username, u.query.pass, "start_death", "cancel_death")) { if (!auth.auth(u.query.username, u.query.pass, "start_death", "cancel_death")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
rooms = ROOM_all.filter(function(room) {
return room && (u.query.deathcancel === "all" || u.query.deathcancel === room.process_pid.toString() || u.query.deathcancel === room.name);
});
death_room_found = false; death_room_found = false;
for (q = 0, len6 = ROOM_all.length; q < len6; q++) { _async.each(rooms, function(room, done) {
room = ROOM_all[q]; if (room.cancel_death()) {
if (room && (u.query.deathcancel === "all" || u.query.deathcancel === room.process_pid.toString() || u.query.deathcancel === room.name)) { death_room_found = true;
if (room.cancel_death()) {
death_room_found = true;
}
} }
} return done();
response.writeHead(200); }, function() {
if (death_room_found) { response.writeHead(200);
response.end(addCallback(u.query.callback, "['death cancel ok', '" + u.query.deathcancel + "']")); if (death_room_found) {
} else { return response.end(addCallback(u.query.callback, "['death cancel ok', '" + u.query.deathcancel + "']"));
response.end(addCallback(u.query.callback, "['room not found', '" + u.query.deathcancel + "']")); } else {
} return response.end(addCallback(u.query.callback, "['room not found', '" + u.query.deathcancel + "']"));
}
});
} else if (u.query.reboot) { } else if (u.query.reboot) {
if (!auth.auth(u.query.username, u.query.pass, "stop", "reboot")) { if (!auth.auth(u.query.username, u.query.pass, "stop", "reboot")) {
response.writeHead(200); response.writeHead(200);
response.end(addCallback(u.query.callback, "['密码错误', 0]")); response.end(addCallback(u.query.callback, "['密码错误', 0]"));
return; return;
} }
for (r = 0, len7 = ROOM_all.length; r < len7; r++) { ROOM_kick("all", function(err, found) {
room = ROOM_all[r]; global.rebooted = true;
if (!(room)) { if (windbot_process) {
continue; windbot_process.kill();
}
if (room.duel_stage !== ygopro.constants.DUEL_STAGE.BEGIN) {
room.scores[room.dueling_players[0].name_vpass] = 0;
room.scores[room.dueling_players[1].name_vpass] = 0;
} }
room.kicked = true; response.writeHead(200);
room.send_replays(); response.end(addCallback(u.query.callback, "['reboot ok', '" + u.query.reboot + "']"));
room.process.kill(); return process.exit();
room["delete"](); });
}
rebooted = true;
if (windbot_process) {
windbot_process.kill();
}
response.writeHead(200);
response.end(addCallback(u.query.callback, "['reboot ok', '" + u.query.reboot + "']"));
throw "rebooted";
} else { } else {
response.writeHead(400); response.writeHead(400);
response.end(); response.end();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment