From 7ef2771ef8099aaef87ee1900d8eb8e834000d06 Mon Sep 17 00:00:00 2001 From: Kent Rasmussen Date: Fri, 19 Apr 2024 20:11:36 +0900 Subject: [PATCH] add timeout to `use` (#120) * add timeout to `use` * refactor: use Math.min directly * fix: remove eval It doesn't look good in the UI, we can assume that the current timeout is already shown in the UI --- node/server.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/node/server.js b/node/server.js index 8a611ed..dcb6f23 100644 --- a/node/server.js +++ b/node/server.js @@ -9745,25 +9745,26 @@ function init_io() { ); }); socket.on("use", function (data) { - var player = players[socket.id]; + const player = players[socket.id]; if (!player) { return; } if (data.item == "hp" || data.item == "mp") { - if (player.last.potion && mssince(player.last.potion) < 0) { - return fail_response("not_ready"); + if (player.last.potion) { + const ms = -mssince(player.last.potion); + if (ms > 0) { + return fail_response("not_ready", { ms: ms }); + } } player.last.potion = future_ms(4000); if (data.item == "hp") { - player.hp += 50; + player.hp = Math.min(player.hp + 50, player.max_hp); disappearing_text(socket, player, "+50", { color: "green", xy: 1, s: "hp", nohp: 1 }); } if (data.item == "mp") { - player.mp += 100; + player.mp = Math.min(player.mp + 100, player.max_mp); disappearing_text(socket, player, "+100", { color: "#006AA9", xy: 1, s: "mp", nomp: 1 }); } - player.hp = min(player.hp, player.max_hp); - player.mp = min(player.mp, player.max_mp); // calculate_player_stats(player); [22/11/16] player.cid++; player.u = true;