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) {
var room = ROOM_all[client.rid];
if (room.rolls == undefined)
{
room.rolls = new Object();
}
ygopro.ctos_follow_before(
"CHAT",
false,
async function (buffer, info, client, server, datas) {
var room = ROOM_all[client.rid];
if (room.rolls == undefined) {
room.rolls = new Object();
}
var msg = _.trim(info.msg);
if (msg.substring(0, 5) == "/dice")
{
var diceresult = Math.floor(Math.random()*6) + 1;
var dicemessage = client.name + "投掷1d6并掷出了" + diceresult + "";
ygopro.stoc_send_chat_to_room(room, dicemessage, ygopro.constants.COLORS.PINK);
return;
if (msg.substring(0, 5) == "/dice") {
var diceresult = Math.floor(Math.random() * 6) + 1;
var dicemessage = client.name + "投掷1d6并掷出了" + diceresult + "";
ygopro.stoc_send_chat_to_room(
room,
dicemessage,
ygopro.constants.COLORS.PINK
);
return;
}
if (msg.substring(0, 5) == "/coin")
{
var coinresult = Math.floor(Math.random()*2) == 1 ? "正面" : "反面";
var coinmessage = client.name + "投掷硬币并掷出了" + coinresult;
ygopro.stoc_send_chat_to_room(room, coinmessage, ygopro.constants.COLORS.BLUE);
return;
if (msg.substring(0, 5) == "/coin") {
var coinresult = Math.floor(Math.random() * 2) == 1 ? "正面" : "反面";
var coinmessage = client.name + "投掷硬币并掷出了" + coinresult;
ygopro.stoc_send_chat_to_room(
room,
coinmessage,
ygopro.constants.COLORS.BLUE
);
return;
}
if (msg.substring(0, 5) != "/roll") {
return;
return;
}
var regex = /[0-9]*d[0-9]+/;
var found = regex.exec(msg);
var dicenum = 1;
var dicemax = 100;
if (found)
{
found = found.toString();
var split = found.split('d');
dicenum = split[0];
dicemax = split[1];
}
if (dicenum > 100)
dicenum = 100;
if (dicenum < 1)
dicenum = 1;
if (dicemax > 60000)
dicemax = 60000;
if (dicemax < 1)
dicemax = 1;
var result = 0;
for (var i=0;i<dicenum;i++)
{
result += Math.floor(Math.random()*dicemax) + 1;
}
var name = client.name;
var message = name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + "";
log.warn(message);
if (room.rolls[name] == undefined)
{
if (dicenum == 1 || dicemax == 100)
{
room.rolls[name] = 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[name] + "";
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.BABYBLUE);
}
var regex2 = /[0-9]*d[0-9]+[\+][0-9]+/;
var regex2m = /[0-9]*d[0-9]+[\-][0-9]+/;
var found2 = regex2.exec(msg);
var found2m = regex2m.exec(msg);
var dicenum = 1;
var dicemax = 100;
var diceadd = 0;
if (found2) {
found2 = found2.toString();
var split = found2.split("+");
diceadd = split[1];
msg = split[0];
}
if (found2m) {
found2m = found2m.toString();
var split = found2.split("-");
diceadd = -split[1];
msg = split[0];
}
var found = regex.exec(msg);
if (found) {
found = found.toString();
var split = found.split("d");
dicenum = split[0];
dicemax = split[1];
}
if (dicenum > 100) dicenum = 100;
if (dicenum < 1) dicenum = 1;
if (dicemax > 60000) dicemax = 60000;
if (dicemax < 1) dicemax = 1;
if (diceadd > 1000000) diceadd = 1000000;
if (diceadd < -1000000) diceadd = -1000000;
var result = 0;
for (var i = 0; i < dicenum; i++) {
result += Math.floor(Math.random() * dicemax) + 1;
}
result += diceadd;
var name = client.name;
var message =
name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + "";
log.warn(message);
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;
});
\ 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];
if (!room) {
return null;
return null;
}
if (room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN) {
return null;
return null;
}
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;
for(var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") { //关键字[MM]
MirrorMode = true;
break;
}
for (var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") {
//关键字[MM]
MirrorMode = true;
break;
}
}
var deckindex = global.dc_decks_index;
if (MirrorMode) {
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 {
if (room.mm_deck == undefined) {
global.dc_decks_index++;
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];
client.side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = global.dc_decks_md5[deckindex];
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) {
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", {
mainc: client.main.length,
sidec: client.side.length,
deckbuf: compat_deckbuf
mainc: client.main.length,
sidec: client.side.length,
deckbuf: compat_deckbuf,
});
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;"
global.mysqldb.query(sql, null, function(err, result) {
if (err) {
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql);
log.info(err);
return;
}
if (result == null || result.length == 0) {
return null;
}
var buff_main = [];
var buff_side = [];
var cards = result[0].content.split(/[\r\n\t ]+/);
var side = false;
var valid = false;
for (var i = 0; i < cards.length; i++) {
var code = parseInt(cards[i].trim());
if (!isNaN(code)) {
if (!side) {
buff_main.push(code);
valid = true;
}
else {
buff_side.push(code);
}
}
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;
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 = mysql.createConnection(
global.settings.modules.cloud_replay.mysql
);
log.info(err);
return;
}
if (result == null || result.length == 0) {
return null;
}
var buff_main = [];
var buff_side = [];
var cards = result[0].content.split(/[\r\n\t ]+/);
var side = false;
var valid = false;
for (var i = 0; i < cards.length; i++) {
var code = parseInt(cards[i].trim());
if (!isNaN(code)) {
if (!side) {
buff_main.push(code);
valid = true;
} else {
buff_side.push(code);
}
} 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;
}
});
return true;
});
}
);
ygopro.ctos_follow_before('CHAT', true, async function(buffer, info, client, server, datas) {
room = ROOM_all[client.rid];
if (!room) {
return;
}
if (!client.deckMd5) {
return;
}
var md5 = client.deckMd5;
if (room.duel_stage != ygopro.constants.DUEL_STAGE.DUELING) {
return;
ygopro.ctos_follow_before("CHAT", true, async function (
buffer,
info,
client,
server,
datas
) {
room = ROOM_all[client.rid];
if (!room) {
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 (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;
if (result.length > 0) {
ygopro.stoc_send_chat(
client,
"服务器端的举报在5分钟内只能进行一次,请稍后再试",
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)";
sql = "INSERT INTO `DCReportServer` VALUES(?,now())";
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) {
mysql.createConnection(settings.modules.cloud_replay.mysql);
log.info(err);
ygopro.stoc_send_chat(client, "服务器出现异常,请重试", ygopro.constants.COLORS.RED);
return;
global.mysqldb = mysql.createConnection(
global.settings.modules.cloud_replay.mysql
);
log.info(err);
ygopro.stoc_send_chat(
client,
"例外库无法检索卡组,提交失败",
ygopro.constants.COLORS.RED
);
return;
}
if (result.length > 0) {
ygopro.stoc_send_chat(client, "服务器端的举报在5分钟内只能进行一次,请稍后再试", ygopro.constants.COLORS.YELLOW);
return;
ygopro.stoc_send_chat(
client,
"此卡组已被判定为无需修改,提交失败",
ygopro.constants.COLORS.RED
);
return;
}
sql = "INSERT INTO `DCReportServer` VALUES(?,now())";
sqlParams = [client.ip];
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) {
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql);
log.info(err);
ygopro.stoc_send_chat(client, "例外库无法检索卡组,提交失败", ygopro.constants.COLORS.RED);
return;
}
if (result.length > 0) {
ygopro.stoc_send_chat(client, "此卡组已被判定为无需修改,提交失败", ygopro.constants.COLORS.RED);
return;
}
msg = (new Date().toString()) + " - 由服务器端提交(提交者:" + client.name + "\r\n\r\n" + msg;
sql = "INSERT INTO DCReport VALUES(?,?,?,'')";
sqlParams = [md5, client.ip, msg];
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;
}
ygopro.stoc_send_chat(client, "提交成功,感谢您的举报。\n举报ID为"+md5+",可用编年史客户端查看并修改。", ygopro.constants.COLORS.YELLOW);
});
});
msg =
new Date().toString() +
" - 由服务器端提交(提交者:" +
client.name +
"\r\n\r\n" +
msg;
sql = "INSERT INTO DCReport VALUES(?,?,?,'')";
sqlParams = [md5, client.ip, msg];
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;
}
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];
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];
if (!room) {
return false;
return false;
}
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;
for(var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") { //关键字[MM]
MirrorMode = true;
break;
}
for (var parameter of room_parameters) {
if (parameter.toUpperCase() == "MM") {
//关键字[MM]
MirrorMode = true;
break;
}
}
var deckindex = global.dc_decks_index;
if (MirrorMode) {
if (room.mm_deck == null) {
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 (room.mm_deck == null) {
global.dc_decks_index++;
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];
client.side = global.dc_decks_side[deckindex];
var compat_deckbuf = client.main.concat(client.side);
client.deckMd5 = global.dc_decks_md5[deckindex];
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) {
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", {
mainc: client.main.length,
sidec: client.side.length,
deckbuf: compat_deckbuf
mainc: client.main.length,
sidec: client.side.length,
deckbuf: compat_deckbuf,
});
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;"
global.mysqldb.query(sql, null, function(err, result) {
if (err) {
global.mysqldb=mysql.createConnection(global.settings.modules.cloud_replay.mysql);
log.info(err);
return;
}
if (result == null || result.length == 0) {
return null;
}
var buff_main = [];
var buff_side = [];
var cards = result[0].content.split(/[\r\n\t ]+/);
var side = false;
var valid = false;
for (var i = 0; i < cards.length; i++) {
var code = parseInt(cards[i].trim());
if (!isNaN(code)) {
if (!side) {
buff_main.push(code);
valid = true;
}
else {
buff_side.push(code);
}
}
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;
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 = mysql.createConnection(
global.settings.modules.cloud_replay.mysql
);
log.info(err);
return;
}
if (result == null || result.length == 0) {
return null;
}
var buff_main = [];
var buff_side = [];
var cards = result[0].content.split(/[\r\n\t ]+/);
var side = false;
var valid = false;
for (var i = 0; i < cards.length; i++) {
var code = parseInt(cards[i].trim());
if (!isNaN(code)) {
if (!side) {
buff_main.push(code);
valid = true;
} else {
buff_side.push(code);
}
} 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;
}
});
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