diff --git a/chatcommands.lua b/chatcommands.lua index ed25a2b..3301da8 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -18,7 +18,7 @@ local function set_player_participation(player_name, participates) -- if was in a game, leave local game = yl_snowball.games_by_player[player_name] if game then - game:on_leaveplayer(player_name) + game:leave(player_name) end return false end @@ -50,16 +50,25 @@ core.register_chatcommand( { --params = "opt-out|opt-in|hud", --description = "Opt-out or opt-in from the snowball games. Hide/show game HUD", - params = "opt-out|opt-in", - description = "Opt-out or opt-in from the snowball games.", + params = "opt-out|opt-in|leave", + description = "Opt-out or opt-in from the snowball games. Or just leave the current one.", privs = {}, func = function(player_name, param) if param == "opt-out" then - local res = set_player_participation(player_name, false) + set_player_participation(player_name, false) return true, "You opt-out from snowball games." + elseif param == "opt-in" then - local res = set_player_participation(player_name, true) + set_player_participation(player_name, true) return true, "You opt-in into snowball games." + + elseif param == "leave" then + local game = yl_snowball.games_by_player[player_name] + if game then + game:leave(player_name) + return true, "You've left the current game." + end + return false, "You're not participating in any ongoing games." -- elseif param == "hud" then -- return false, "Not implemented yet, sorry." end diff --git a/game.lua b/game.lua index e24266c..075afaf 100644 --- a/game.lua +++ b/game.lua @@ -44,7 +44,7 @@ core.register_on_leaveplayer( if player_name ~= "" then local game = games_by_player[player_name] if game then - game:on_leaveplayer(player_name) + game:leave(player_name) end games_by_player[player_name] = nil end @@ -165,13 +165,24 @@ end function Game:add_player(player_name, game_player) - if not game_player then - game_player = GamePlayer(player_name) + if self.players[player_name] then + if game_player and game_player.active then + self.players[player_name] = game_player + games_by_player[player_name] = self + end + -- Do nothing here: player merged their old scores from the + -- game there weren't active in + else + if (not game_player) or (not game_player.active) then + game_player = GamePlayer(player_name) + end + self.players[player_name] = game_player + games_by_player[player_name] = self + + -- this is a new player, increase player count + self.player_num = self.player_num + 1 end - self.players[player_name] = game_player - games_by_player[player_name] = self - self.player_num = self.player_num + 1 -end + end function Game:get_player(player_name) @@ -179,9 +190,9 @@ function Game:get_player(player_name) end -function Game:on_leaveplayer(player_name) +function Game:leave(player_name) game_hud:set_enabled(core.get_player_by_name(player_name), false) - -- TODO set active = false + self.players[player_name].active = false games_by_player[player_name] = nil end @@ -226,17 +237,15 @@ function Game:stop() } if self.player_num > 1 then - for p_name, player in pairs(self.players) do - if get_player_participation(p_name) then - core.chat_send_player(p_name, table.concat(lines, "\n")) + for player_name, player in pairs(self.players) do + if games_by_player[player_name] then + core.chat_send_player(player_name, table.concat(lines, "\n")) + player:quit() + games_by_player[player_name] = nil end end end - for player_name, player in pairs(self.players) do - player:quit() - games_by_player[player_name] = nil - end active_games[self] = nil end @@ -276,6 +285,7 @@ function snowgame.register_shot(shooter_name) -- started shooting first time, started a game s_game = Game() s_game:add_player(shooter_name) + games_by_player[shooter_name] = s_game end s_game:register_shot(shooter_name) end