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(
"CHAT",
false,
async function (buffer, info, client, server, datas) {
var room = ROOM_all[client.rid]; var room = ROOM_all[client.rid];
if (room.rolls == undefined) if (room.rolls == undefined) {
{
room.rolls = new Object(); 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(room, dicemessage, ygopro.constants.COLORS.PINK); ygopro.stoc_send_chat_to_room(
room,
dicemessage,
ygopro.constants.COLORS.PINK
);
return; 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(room, coinmessage, ygopro.constants.COLORS.BLUE); ygopro.stoc_send_chat_to_room(
room,
coinmessage,
ygopro.constants.COLORS.BLUE
);
return; 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 regex2m = /[0-9]*d[0-9]+[\-][0-9]+/;
var found2 = regex2.exec(msg);
var found2m = regex2m.exec(msg);
var dicenum = 1; var dicenum = 1;
var dicemax = 100; var dicemax = 100;
if (found) 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(); found = found.toString();
var split = found.split('d'); var split = found.split("d");
dicenum = split[0]; dicenum = split[0];
dicemax = split[1]; dicemax = split[1];
} }
if (dicenum > 100) if (dicenum > 100) dicenum = 100;
dicenum = 100; if (dicenum < 1) dicenum = 1;
if (dicenum < 1) if (dicemax > 60000) dicemax = 60000;
dicenum = 1; if (dicemax < 1) dicemax = 1;
if (dicemax > 60000) if (diceadd > 1000000) diceadd = 1000000;
dicemax = 60000; if (diceadd < -1000000) diceadd = -1000000;
if (dicemax < 1)
dicemax = 1;
var result = 0; var result = 0;
for (var i=0;i<dicenum;i++) for (var i = 0; i < dicenum; i++) {
{ result += Math.floor(Math.random() * dicemax) + 1;
result += Math.floor(Math.random()*dicemax) + 1;
} }
result += diceadd;
var name = client.name; var name = client.name;
var message = name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + ""; var message =
name + "投掷" + dicenum + "d" + dicemax + "并掷出了" + result + "";
log.warn(message); log.warn(message);
if (room.rolls[name] == undefined) if (room.rolls[client.ip] == undefined) {
{ if (dicenum == 1 && dicemax == 100 && diceadd == 0) {
if (dicenum == 1 || dicemax == 100) room.rolls[client.ip] = result;
{ ygopro.stoc_send_chat_to_room(
room.rolls[name] = result; room,
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.YELLOW); message,
ygopro.constants.COLORS.YELLOW
);
} else {
ygopro.stoc_send_chat_to_room(
room,
message,
ygopro.constants.COLORS.BABYBLUE
);
} }
else } else {
{ message = message + ";首次1d100投掷为" + room.rolls[client.ip] + "";
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.BABYBLUE); ygopro.stoc_send_chat_to_room(
} room,
} message,
else ygopro.constants.COLORS.BABYBLUE
{ );
message = message + ";首次1d100投掷为" + room.rolls[name] + "";
ygopro.stoc_send_chat_to_room(room, message, ygopro.constants.COLORS.BABYBLUE);
} }
return true; return true;
}); }
\ No newline at end of file );
This diff is collapsed.
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