yl_snowball/chatcommands.lua

83 lines
2.6 KiB
Lua

local function _key_participation(player_name)
return string.format("opt_out?%s", player_name)
end
local does_participate = {}
local function set_player_participation(player_name, participates)
local key = _key_participation(player_name)
if participates then
yl_snowball.modstorage:set_string(key, "")
does_participate[player_name] = true
return true
else
yl_snowball.modstorage:set_string(key, "false")
does_participate[player_name] = false
-- if was in a game, leave
local game = yl_snowball.games_by_player[player_name]
if game then
game:leave(player_name)
end
return false
end
end
local function get_player_participation(player_name)
local cached = does_participate[player_name]
if cached == true then
return true
elseif cached == false then
return false
end
local key = _key_participation(player_name)
local val = yl_snowball.modstorage:get_string(key)
if val == "" then
does_participate[player_name] = true
return true
elseif val == "false" then
does_participate[player_name] = false
return false
end
return true
end
core.register_chatcommand(
"snowball",
{
--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|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
set_player_participation(player_name, false)
return true, "You opt-out from snowball games."
elseif param == "opt-in" then
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
return false, "Please specify 'opt-in' or 'opt-out' argument."
end
}
)
yl_snowball.get_player_participation = get_player_participation
yl_snowball.set_player_participation = set_player_participation