Commit 57d97b02 authored by JoyJ's avatar JoyJ

update diceroll & dc-random

parent d13087a5
ygopro.ctos_follow_before('CHAT', false, async function (buffer, info, client, server, datas) { ygopro.ctos_follow_before(
var room = ROOM_all[client.rid]; "CHAT",
if (room.rolls == undefined) false,
{ async function (buffer, info, client, server, datas) {
room.rolls = new Object(); var room = ROOM_all[client.rid];
} if (room.rolls == undefined) {
room.rolls = new Object();
}
var msg = _.trim(info.msg); var msg = _.trim(info.msg);
if (msg.substring(0, 5) == "/dice") if (msg.substring(0, 5) == "/dice") {
{ var diceresult = Math.floor(Math.random() * 6) + 1;
var diceresult = Math.floor(Math.random()*6) + 1; var dicemessage = client.name + "投掷1d6并掷出了" + diceresult + "";
var dicemessage = client.name + "投掷1d6并掷出了" + diceresult + ""; ygopro.stoc_send_chat_to_room(
ygopro.stoc_send_chat_to_room(room, dicemessage, ygopro.constants.COLORS.PINK); room,
return; dicemessage,
ygopro.constants.COLORS.PINK
);
return;
} }
if (msg.substring(0, 5) == "/coin") if (msg.substring(0, 5) == "/coin") {
{ var coinresult = Math.floor(Math.random() * 2) == 1 ? "正面" : "反面";
var coinresult = Math.floor(Math.random()*2) == 1 ? "正面" : "反面"; var coinmessage = client.name + "投掷硬币并掷出了" + coinresult;
var coinmessage = client.name + "投掷硬币并掷出了" + coinresult; ygopro.stoc_send_chat_to_room(
ygopro.stoc_send_chat_to_room(room, coinmessage, ygopro.constants.COLORS.BLUE); room,
return; coinmessage,
ygopro.constants.COLORS.BLUE
);
return;
} }
if (msg.substring(0, 5) != "/roll") { if (msg.substring(0, 5) != "/roll") {
return; return;
} }
var regex = /[0-9]*d[0-9]+/; var regex = /[0-9]*d[0-9]+/;
var found = regex.exec(msg); var regex2 = /[0-9]*d[0-9]+[\+][0-9]+/;
var dicenum = 1; var regex2m = /[0-9]*d[0-9]+[\-][0-9]+/;
var dicemax = 100; var found2 = regex2.exec(msg);
if (found) var found2m = regex2m.exec(msg);
{ var dicenum = 1;
found = found.toString(); var dicemax = 100;
var split = found.split('d'); var diceadd = 0;
dicenum = split[0]; if (found2) {
dicemax = split[1]; found2 = found2.toString();
} var split = found2.split("+");
if (dicenum > 100) diceadd = split[1];
dicenum = 100; msg = split[0];
if (dicenum < 1) }
dicenum = 1; if (found2m) {
if (dicemax > 60000) found2m = found2m.toString();
dicemax = 60000; var split = found2.split("-");
if (dicemax < 1) diceadd = -split[1];
dicemax = 1; msg = split[0];
var result = 0; }
for (var i=0;i<dicenum;i++) var found = regex.exec(msg);
{ if (found) {
result += Math.floor(Math.random()*dicemax) + 1; found = found.toString();
} var split = found.split("d");
var name = client.name; dicenum = split[0];
var message = name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + ""; dicemax = split[1];
log.warn(message); }
if (room.rolls[name] == undefined) if (dicenum > 100) dicenum = 100;
{ if (dicenum < 1) dicenum = 1;
if (dicenum == 1 || dicemax == 100) if (dicemax > 60000) dicemax = 60000;
{ if (dicemax < 1) dicemax = 1;
room.rolls[name] = result; if (diceadd > 1000000) diceadd = 1000000;
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.YELLOW); if (diceadd < -1000000) diceadd = -1000000;
} var result = 0;
else for (var i = 0; i < dicenum; i++) {
{ result += Math.floor(Math.random() * dicemax) + 1;
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.BABYBLUE); }
} result += diceadd;
} var name = client.name;
else var message =
{ name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + "";
message = message + ";首次1d100投掷为" + room.rolls[name] + ""; log.warn(message);
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.BABYBLUE); if (room.rolls[client.ip] == undefined) {
} if (dicenum == 1 && dicemax == 100 && diceadd == 0) {
room.rolls[client.ip] = result;
ygopro.stoc_send_chat_to_room(
room,
message,
ygopro.constants.COLORS.YELLOW
);
} else {
ygopro.stoc_send_chat_to_room(
room,
message,
ygopro.constants.COLORS.BABYBLUE
);
}
} else {
message = message + ";首次1d100投掷为" + room.rolls[client.ip] + "";
ygopro.stoc_send_chat_to_room(
room,
message,
ygopro.constants.COLORS.BABYBLUE
);
}
return true; return true;
}); }
\ No newline at end of file );
ygopro.ctos_follow_after("UPDATE_DECK", true, async (buffer, info, client, server, datas) => { ygopro.ctos_follow_after(
"UPDATE_DECK",
true,
async (buffer, info, client, server, datas) => {
var room = ROOM_all[client.rid]; var room = ROOM_all[client.rid];
if (!room) { if (!room) {
return null; return null;
} }
if (room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN) { if (room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN) {
return null; return null;
} }
if (client.is_local) { if (client.is_local) {
return null; return null;
} }
var room_parameters = room.name.split('#', 2)[0].split(/[,,]/); //房间名的参数 var room_parameters = room.name.split("#", 2)[0].split(/[,,]/); //房间名的参数
var MirrorMode = false; var MirrorMode = false;
for(var parameter of room_parameters) { for (var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") { //关键字[MM] if (parameter.toUpperCase() == "MM") {
MirrorMode = true; //关键字[MM]
break; MirrorMode = true;
} break;
}
} }
var deckindex = global.dc_decks_index; var deckindex = global.dc_decks_index;
if (MirrorMode) { if (MirrorMode) {
if (room.mm_deck == undefined) { if (room.mm_deck == undefined) {
global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0;
}
room.mm_deck = global.dc_decks_main[deckindex];
room.mm_side = global.dc_decks_side[deckindex];
room.mm_md5 = global.dc_decks_md5[deckindex];
MirrorMode=false;
}
client.main = room.mm_deck;
client.side = room.mm_side;
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = room.mm_md5;
}
else {
global.dc_decks_index++; global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) { if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0; global.dc_decks_index = 0;
} }
client.main = global.dc_decks_main[deckindex]; room.mm_deck = global.dc_decks_main[deckindex];
client.side = global.dc_decks_side[deckindex]; room.mm_side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side); room.mm_md5 = global.dc_decks_md5[deckindex];
client.deckMd5 = global.dc_decks_md5[deckindex]; MirrorMode = false;
}
client.main = room.mm_deck;
client.side = room.mm_side;
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = room.mm_md5;
} else {
global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0;
}
client.main = global.dc_decks_main[deckindex];
client.side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = global.dc_decks_md5[deckindex];
} }
ygopro.ctos_send(server, "UPDATE_DECK", { ygopro.ctos_send(server, "UPDATE_DECK", {
mainc: client.main.length, mainc: client.main.length,
sidec: client.side.length, sidec: client.side.length,
deckbuf: compat_deckbuf deckbuf: compat_deckbuf,
}); });
if (MirrorMode) { if (MirrorMode) {
return true; return true;
} }
var sql = "SELECT * FROM RandomDecks AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM RandomDecks)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;" var sql =
"SELECT * FROM RandomDecks AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM RandomDecks)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;";
global.mysqldb.query(sql, null, function(err, result) {
if (err) { global.mysqldb.query(sql, null, function (err, result) {
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql); if (err) {
log.info(err); global.mysqldb = mysql.createConnection(
return; global.settings.modules.cloud_replay.mysql
} );
if (result == null || result.length == 0) { log.info(err);
return null; return;
} }
var buff_main = []; if (result == null || result.length == 0) {
var buff_side = []; return null;
var cards = result[0].content.split(/[\r\n\t ]+/); }
var side = false; var buff_main = [];
var valid = false; var buff_side = [];
for (var i = 0; i < cards.length; i++) { var cards = result[0].content.split(/[\r\n\t ]+/);
var code = parseInt(cards[i].trim()); var side = false;
if (!isNaN(code)) { var valid = false;
if (!side) { for (var i = 0; i < cards.length; i++) {
buff_main.push(code); var code = parseInt(cards[i].trim());
valid = true; if (!isNaN(code)) {
} if (!side) {
else { buff_main.push(code);
buff_side.push(code); valid = true;
} } else {
} buff_side.push(code);
else if (cards[i].substring(0, 5) == "!side") { }
side = true; } else if (cards[i].substring(0, 5) == "!side") {
} side = true;
}
if (valid) {
dc_decks_main[deckindex] = buff_main;
dc_decks_side[deckindex] = buff_side;
dc_decks_md5[deckindex] = result[0].md5;
} }
}
if (valid) {
dc_decks_main[deckindex] = buff_main;
dc_decks_side[deckindex] = buff_side;
dc_decks_md5[deckindex] = result[0].md5;
}
}); });
return true; return true;
}); }
);
ygopro.ctos_follow_before('CHAT', true, async function(buffer, info, client, server, datas) { ygopro.ctos_follow_before("CHAT", true, async function (
room = ROOM_all[client.rid]; buffer,
if (!room) { info,
return; client,
} server,
if (!client.deckMd5) { datas
return; ) {
} room = ROOM_all[client.rid];
var md5 = client.deckMd5; if (!room) {
if (room.duel_stage != ygopro.constants.DUEL_STAGE.DUELING) { return;
return; }
if (!client.deckMd5) {
return;
}
var md5 = client.deckMd5;
if (room.duel_stage != ygopro.constants.DUEL_STAGE.DUELING) {
return;
}
var msg = _.trim(info.msg);
if (msg.substring(0, 7) != "/report") {
return;
}
msg = msg.substring(7);
msg = _.trim(msg);
if (msg.length < 2) {
ygopro.stoc_send_chat(
client,
"请以【/report 举报原因】的格式写明举报原因",
ygopro.constants.COLORS.YELLOW
);
return;
}
//"CREATE TABLE DCReportServer(reporterIp varchar(255),reportDate DateTime)";
var sql =
"SELECT * FROM `DCReportServer` WHERE reporterIp=? AND reportDate > date_sub(now(), interval 5 minute)";
sqlParams = [client.ip];
mysqldb.query(sql, sqlParams, function (err, result) {
if (err) {
mysql.createConnection(settings.modules.cloud_replay.mysql);
log.info(err);
ygopro.stoc_send_chat(
client,
"服务器出现异常,请重试",
ygopro.constants.COLORS.RED
);
return;
} }
var msg = _.trim(info.msg); if (result.length > 0) {
if (msg.substring(0, 7) != "/report") { ygopro.stoc_send_chat(
return; client,
} "服务器端的举报在5分钟内只能进行一次,请稍后再试",
msg = msg.substring(7); ygopro.constants.COLORS.YELLOW
msg = _.trim(msg); );
if (msg.length < 2) { return;
ygopro.stoc_send_chat(client, "请以【/report 举报原因】的格式写明举报原因", ygopro.constants.COLORS.YELLOW);
return;
} }
//"CREATE TABLE DCReportServer(reporterIp varchar(255),reportDate DateTime)"; sql = "INSERT INTO `DCReportServer` VALUES(?,now())";
var sql = "SELECT * FROM `DCReportServer` WHERE reporterIp=? AND reportDate > date_sub(now(), interval 5 minute)";
sqlParams = [client.ip]; sqlParams = [client.ip];
mysqldb.query(sql, sqlParams, function(err, result) { mysqldb.query(sql, sqlParams, function (err, result) {
if (err) {
global.mysqldb = mysql.createConnection(
global.settings.modules.cloud_replay.mysql
);
log.info(err);
ygopro.stoc_send_chat(
client,
"出现异常,请重试",
ygopro.constants.COLORS.RED
);
return;
}
sql = "SELECT * FROM DCDeckNoChangeList WHERE deckMd5=?";
sqlParams = [md5];
mysqldb.query(sql, sqlParams, function (err, result) {
if (err) { if (err) {
mysql.createConnection(settings.modules.cloud_replay.mysql); global.mysqldb = mysql.createConnection(
log.info(err); global.settings.modules.cloud_replay.mysql
ygopro.stoc_send_chat(client, "服务器出现异常,请重试", ygopro.constants.COLORS.RED); );
return; log.info(err);
ygopro.stoc_send_chat(
client,
"例外库无法检索卡组,提交失败",
ygopro.constants.COLORS.RED
);
return;
} }
if (result.length > 0) { if (result.length > 0) {
ygopro.stoc_send_chat(client, "服务器端的举报在5分钟内只能进行一次,请稍后再试", ygopro.constants.COLORS.YELLOW); ygopro.stoc_send_chat(
return; client,
"此卡组已被判定为无需修改,提交失败",
ygopro.constants.COLORS.RED
);
return;
} }
sql = "INSERT INTO `DCReportServer` VALUES(?,now())"; msg =
sqlParams = [client.ip]; new Date().toString() +
mysqldb.query(sql, sqlParams, function(err, result) { " - 由服务器端提交(提交者:" +
if (err) { client.name +
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql); "\r\n\r\n" +
log.info(err); msg;
ygopro.stoc_send_chat(client, "出现异常,请重试", ygopro.constants.COLORS.RED); sql = "INSERT INTO DCReport VALUES(?,?,?,'')";
return; sqlParams = [md5, client.ip, msg];
} mysqldb.query(sql, sqlParams, function (err, result) {
sql = "SELECT * FROM DCDeckNoChangeList WHERE deckMd5=?"; if (err) {
sqlParams = [md5]; global.mysqldb = mysql.createConnection(
mysqldb.query(sql, sqlParams, function(err, result) { global.settings.modules.cloud_replay.mysql
if (err) { );
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql); log.info(err);
log.info(err); ygopro.stoc_send_chat(
ygopro.stoc_send_chat(client, "例外库无法检索卡组,提交失败", ygopro.constants.COLORS.RED); client,
return; "提交失败",
} ygopro.constants.COLORS.RED
if (result.length > 0) { );
ygopro.stoc_send_chat(client, "此卡组已被判定为无需修改,提交失败", ygopro.constants.COLORS.RED); return;
return; }
} ygopro.stoc_send_chat(
msg = (new Date().toString()) + " - 由服务器端提交(提交者:" + client.name + "\r\n\r\n" + msg; client,
sql = "INSERT INTO DCReport VALUES(?,?,?,'')"; "提交成功,感谢您的举报。\n举报ID为" +
sqlParams = [md5, client.ip, msg]; md5 +
mysqldb.query(sql, sqlParams, function(err, result) { ",可用编年史客户端查看并修改。",
if (err) { ygopro.constants.COLORS.YELLOW
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql); );
log.info(err);
ygopro.stoc_send_chat(client, "提交失败", ygopro.constants.COLORS.RED);
return;
}
ygopro.stoc_send_chat(client, "提交成功,感谢您的举报。\n举报ID为"+md5+",可用编年史客户端查看并修改。", ygopro.constants.COLORS.YELLOW);
});
});
}); });
});
}); });
return true; });
return true;
}); });
ygopro.stoc_follow_after("DUEL_START", false, (buffer, info, client, server, datas) => { ygopro.stoc_follow_after(
"DUEL_START",
false,
(buffer, info, client, server, datas) => {
var room = ROOM_all[client.rid]; var room = ROOM_all[client.rid];
room.mm_deck = null; room.mm_deck = null;
}); }
);
ygopro.stoc_follow_after("CHANGE_SIDE", true, (buffer, info, client, server, datas) => { ygopro.stoc_follow_after(
"CHANGE_SIDE",
true,
(buffer, info, client, server, datas) => {
var room = ROOM_all[client.rid]; var room = ROOM_all[client.rid];
if (!room) { if (!room) {
return false; return false;
} }
if (client.is_local) { if (client.is_local) {
return false; return false;
} }
var room_parameters = room.name.split('#', 2)[0].split(/[,,]/); //房间名的参数 var room_parameters = room.name.split("#", 2)[0].split(/[,,]/); //房间名的参数
var MirrorMode = false; var MirrorMode = false;
for(var parameter of room_parameters) { for (var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") { //关键字[MM] if (parameter.toUpperCase() == "MM") {
MirrorMode = true; //关键字[MM]
break; MirrorMode = true;
} break;
}
} }
var deckindex = global.dc_decks_index; var deckindex = global.dc_decks_index;
if (MirrorMode) { if (MirrorMode) {
if (room.mm_deck == null) { if (room.mm_deck == null) {
global.dc_decks_index++; global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0;
}
room.mm_deck = global.dc_decks_main[deckindex];
room.mm_side = global.dc_decks_side[deckindex];
room.mm_md5 = global.dc_decks_md5[deckindex];
MirrorMode=false;
}
client.main = room.mm_deck;
client.side = room.mm_side;
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = room.mm_md5;
}
else {
global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) { if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0; global.dc_decks_index = 0;
} }
client.main = global.dc_decks_main[deckindex]; room.mm_deck = global.dc_decks_main[deckindex];
client.side = global.dc_decks_side[deckindex]; room.mm_side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side); room.mm_md5 = global.dc_decks_md5[deckindex];
client.deckMd5 = global.dc_decks_md5[deckindex]; MirrorMode = false;
}
client.main = room.mm_deck;
client.side = room.mm_side;
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = room.mm_md5;
} else {
global.dc_decks_index++;
if (global.dc_decks_index == global.dc_decks_index_max) {
global.dc_decks_index = 0;
}
client.main = global.dc_decks_main[deckindex];
client.side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = global.dc_decks_md5[deckindex];
} }
ygopro.ctos_send(server, "UPDATE_DECK", { ygopro.ctos_send(server, "UPDATE_DECK", {
mainc: client.main.length, mainc: client.main.length,
sidec: client.side.length, sidec: client.side.length,
deckbuf: compat_deckbuf deckbuf: compat_deckbuf,
}); });
if (MirrorMode) { if (MirrorMode) {
return true; return true;
} }
var sql = "SELECT * FROM RandomDecks AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM RandomDecks)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;" var sql =
"SELECT * FROM RandomDecks AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM RandomDecks)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;";
global.mysqldb.query(sql, null, function(err, result) {
if (err) { global.mysqldb.query(sql, null, function (err, result) {
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql); if (err) {
log.info(err); global.mysqldb = mysql.createConnection(
return; global.settings.modules.cloud_replay.mysql
} );
if (result == null || result.length == 0) { log.info(err);
return null; return;
} }
var buff_main = []; if (result == null || result.length == 0) {
var buff_side = []; return null;
var cards = result[0].content.split(/[\r\n\t ]+/); }
var side = false; var buff_main = [];
var valid = false; var buff_side = [];
for (var i = 0; i < cards.length; i++) { var cards = result[0].content.split(/[\r\n\t ]+/);
var code = parseInt(cards[i].trim()); var side = false;
if (!isNaN(code)) { var valid = false;
if (!side) { for (var i = 0; i < cards.length; i++) {
buff_main.push(code); var code = parseInt(cards[i].trim());
valid = true; if (!isNaN(code)) {
} if (!side) {
else { buff_main.push(code);
buff_side.push(code); valid = true;
} } else {
} buff_side.push(code);
else if (cards[i].substring(0, 5) == "!side") { }
side = true; } else if (cards[i].substring(0, 5) == "!side") {
} side = true;
}
if (valid) {
dc_decks_main[deckindex] = buff_main;
dc_decks_side[deckindex] = buff_side;
dc_decks_md5[deckindex] = result[0].md5;
} }
}
if (valid) {
dc_decks_main[deckindex] = buff_main;
dc_decks_side[deckindex] = buff_side;
dc_decks_md5[deckindex] = result[0].md5;
}
}); });
return true; return true;
}); }
\ No newline at end of file );
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