prevent bad shooters from extending the game indefinitely

This commit is contained in:
whosit 2024-11-26 23:19:06 +03:00
parent fd6e44e29c
commit 212ba9fc85

View File

@ -7,7 +7,13 @@ local snowgame = {}
local get_player_participation = yl_snowball.get_player_participation
yl_snowball.GAME_TIMEOUT = 10 -- FIXME increase
-- game will end after this time when no one is shooting/hitting
yl_snowball.GAME_TIMEOUT = 10
-- If people continue shooting without hitting anyone, game time
-- will be extended, but only up to this time limit
yl_snowball.PLAYER_SUCK_TIME = 30
local active_games = {}
local games_by_player = yl_snowball.games_by_player or {}
@ -109,7 +115,8 @@ local Game = Object:extend()
function Game:new()
local current_time = core.get_gametime()
self.time_started = current_time
self.time_last_action = current_time
self.time_last_shot = current_time
self.time_last_hit = current_time
self.players = {}
self.player_num = 0
active_games[self] = true
@ -119,7 +126,12 @@ end
function Game:get_time_left()
local current_time = core.get_gametime()
local time_left = yl_snowball.GAME_TIMEOUT - (current_time - self.time_last_action)
local time_left
if (self.time_last_shot - self.time_last_hit) < yl_snowball.PLAYER_SUCK_TIME then
time_left = yl_snowball.GAME_TIMEOUT - (current_time - self.time_last_shot)
else
time_left = yl_snowball.GAME_TIMEOUT + yl_snowball.PLAYER_SUCK_TIME - (current_time - self.time_last_hit)
end
return time_left > 0 and time_left or 0
end
@ -175,7 +187,8 @@ function Game:merge(other_game)
self:add_player(name, game_player)
end
self.time_started = math.min(self.time_started, other_game.time_started)
self.time_last_action = math.max(self.time_last_action, other_game.time_last_action)
self.time_last_hit = math.max(self.time_last_hit, other_game.time_last_hit)
self.time_last_shot = math.max(self.time_last_shot, other_game.time_last_shot)
active_games[other_game] = nil
end
@ -188,7 +201,7 @@ function Game:register_hit(shooter_name, target_name)
t_player:on_hit_take()
local current_time = core.get_gametime()
self.time_last_action = current_time
self.time_last_hit = current_time
end
@ -196,13 +209,13 @@ function Game:register_shot(shooter_name)
local s_player = self:get_player(shooter_name)
s_player:on_shoot()
local current_time = core.get_gametime()
self.time_last_action = current_time
self.time_last_shot = current_time
end
function Game:stop()
-- TODO show only top + your own scores
local lasted = self.time_last_action - self.time_started
local lasted = self.time_last_shot - self.time_started
local lines = {
f("* Snowball Game Ended (lasted %ds)", lasted),
self:get_info_string(),
@ -269,7 +282,7 @@ futil.register_globalstep({
func = function(dtime)
local current_time = core.get_gametime()
for game,_ in pairs(active_games) do
if (current_time - game.time_last_action) > yl_snowball.GAME_TIMEOUT then
if game:get_time_left() <= 0 then
game:stop()
end
end