【发布时间】:2020-07-26 05:29:09
【问题描述】:
我有一个 Ruby Discord (discordrb) 机器人,用于管理 D&D 角色。我注意到当多个玩家同时提交相同的命令时,他们各自收到的结果并不独立。一个玩家的请求(为他们的角色分配武器)最终被分配给同时提交相同请求的其他角色。我希望每个请求都按顺序单独执行。如何防止交叉请求?
bot.message(contains:"$Wset") do |event|
inputStr = event.content; # this should contain "$Wset#" where # is a single digit
check_user_or_nick(event); pIndex = nil; #fetch the value of @user & set pIndex
(0..(@player.length-1)).each do |y| #find the @player pIndex within the array using 5 char of @user
if (@player[y][0].index(@user.slice(0,5)) == 0) then pIndex = y; end; #finds player Index Value (integer or nil)
end;
weaponInt = Integer(inputStr.slice(5,1)) rescue false; #will detect integer or non integer input
if (pIndex != nil) && (weaponInt != false) then;
if weaponInt < 6 then;
@player[pIndex][1]=weaponInt;
say = @player[pIndex][0].to_s + " weapon damage has be set to " + @weapon[(@player[pIndex][1])].to_s;
else;
say = "Sorry, $Wset requires this format: $Wset? where ? is a single number ( 0 to 5 )";
end;
else
say = "Sorry, $Wset requires this format: $Wset? where ? is a single number ( 0 to 5 )";
end;
event.respond say;
end;
【问题讨论】:
-
顺便说一句,这里有很多不习惯的东西,例如
then;可以简单地删除,因为它没有任何效果(更广泛地说,分号是 not在 Ruby 中需要)。不过,那是另一天的讨论 - 我会推荐你到rubocop's style guide