make game participation out-out via chatcommand

This commit is contained in:
whosit 2024-11-23 21:37:08 +03:00
parent ea0d0f4511
commit ad31c53291
4 changed files with 100 additions and 22 deletions

64
chatcommands.lua Normal file
View File

@ -0,0 +1,64 @@
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
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",
privs = {},
func = function(player_name, param)
if param == "opt-out" then
local res = 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)
return true, "You opt-in into snowball games."
elseif param == "hud" then
end
end
}
)
yl_snowball.get_player_participation = get_player_participation
yl_snowball.set_player_participation = set_player_participation

View File

@ -5,6 +5,7 @@ local Object = dofile(modpath .. DIR_DELIM .. "classic.lua")
local snowgame = {}
local get_player_participation = yl_snowball.get_player_participation
yl_snowball.GAME_TIMEOUT = 10 -- FIXME increase
@ -207,14 +208,17 @@ end
function Game:stop()
-- TODO show only top + your own scores
local lasted = self.time_last_action - self.time_started
local lines = {
f("* Snowball Game Ended (lasted %ds)", lasted),
self:get_info_string(),
}
for p_name, player in pairs(self.players) do
-- TODO show only top + your own scores
local lasted = self.time_last_action - self.time_started
local lines = {
f("* Snowball Game Ended (lasted %ds)", lasted),
self:get_info_string(),
}
core.chat_send_player(p_name, table.concat(lines, "\n"))
if get_player_participation(p_name) then
core.chat_send_player(p_name, table.concat(lines, "\n"))
end
end
for player_name, player in pairs(self.players) do

View File

@ -17,6 +17,7 @@ yl_snowball.information = {
}
dofile(MODPATH .. "chatcommands.lua")
dofile(MODPATH .. "internal.lua")
dofile(MODPATH .. "game.lua")

View File

@ -3,6 +3,11 @@ local v_rotate_around_axis = vector.rotate_around_axis
local v_multiply = vector.multiply
local v_dot = vector.dot
local modstorage = core.get_mod_storage()
yl_snowball.modstorage = modstorage
local get_player_participation = yl_snowball.get_player_participation
yl_snowball.COOLDOWN = 0.1 -- TODO make configurable
@ -190,24 +195,28 @@ local SNOWBALL_DEF = {
spawn_snow_puff(intersection_point, intersection_normal)
self.object:remove()
--ballistics.on_hit_node_hit_sound_play(self, node_pos, node, above_pos, intersection_point, intersection_normal, box_id)
--ballistics.on_hit_object_hit_sound_play(self, target, intersection_point, intersection_normal, box_id)
if target:is_player() then
ballistics.on_hit_object_hit_sound_play(self, target, intersection_point, intersection_normal, box_id)
if futil.is_player(target) then
local target_name = target:get_player_name()
-- TODO or check if opted-out here?
yl_snowball.snowgame.register_hit(self._source_player_name, target_name)
local source = self._source_obj
if futil.is_player(source) then
local sound_name = "y_bows_arrow_successful_hit"
core.sound_play({ name = sound_name }, {
gain = 0.7,
to_player = source:get_player_name(),
})
local take_hit_sound_name = "default_snow_footstep"
core.sound_play({ name = take_hit_sound_name }, {
gain = 0.7,
to_player = target_name,
})
local source_name = source:get_player_name()
local source_participates = get_player_participation(source_name)
local target_participates = get_player_participation(target_name)
if source_participates and target_participates then
yl_snowball.snowgame.register_hit(source_name, target_name)
local sound_name = "y_bows_arrow_successful_hit"
core.sound_play({ name = sound_name }, {
gain = 0.7,
to_player = source_name,
})
local take_hit_sound_name = "default_snow_footstep"
core.sound_play({ name = take_hit_sound_name }, {
gain = 0.7,
to_player = target_name,
})
end
end
end
return true