Store PvP states before tournaments

master
upsilon 2017-03-19 16:45:14 +01:00
parent 36427abd6a
commit 84719c1f2e
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C
3 changed files with 60 additions and 50 deletions

View File

@ -25,9 +25,11 @@ Some additional commands are only executable by the players possessing the `tour
```lua
-- Enabling/disabling PvP:
pvpplus.pvp_set(player_name, state)
pvpplus.pvp_enable(player_name)
pvpplus.pvp_disable(player_name)
pvpplus.pvp_toggle(player_name)
pvpplus.is_pvp(player_name)
-- PvP tournaments:
pvpplus.engage_player(player_name) -- Engage a player for the next tournament

View File

@ -4,70 +4,68 @@ local pvptable = {}
-- Public table, containing global functions
pvpplus = {}
function pvpplus.pvp_enable(player_name)
function pvpplus.pvp_set(player_name, state)
if type(state) ~= "boolean" then
return false, "The state parameter has to be a boolean."
end
if pvpplus.is_playing_tournament(player_name) then
return false, "PvP state cannot be changed while playing a tournament."
end
local player = minetest.get_player_by_name(player_name)
pvptable[player_name] = true
minetest.chat_send_player(player_name, "You PvP has been enabled")
local enabled_disabled = (state and "enabled") or "disabled"
player:hud_remove(pvpdisabled)
player:hud_remove(nopvppic)
minetest.chat_send_player(player_name, "Your PvP has been " .. enabled_disabled)
pvpenabled = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = "PvP is enabled for you!",
number = 0xFF0000 -- Red
})
pvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x=-210, y = 20},
scale = {x = 1, y = 1},
text = "pvp.png"
})
player:hud_remove((state and pvpdisabled) or pvpenabled)
player:hud_remove((state and nopvppic) or pvppic)
if state then
pvpenabled = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = "PvP is enabled for you!",
number = 0xFF0000 -- Red
})
pvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x=-210, y = 20},
scale = {x = 1, y = 1},
text = "pvp.png"
})
else
pvpdisabled = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = "PvP is disabled for you!",
number = 0x7DC435
})
nopvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x = -210, y = 20},
scale = {x = 1, y = 1},
text = "nopvp.png"
})
end
return true
end
function pvpplus.pvp_enable(player_name)
return pvpplus.pvp_set(player_name, true)
end
function pvpplus.pvp_disable(player_name)
if pvpplus.is_playing_tournament(player_name) then
return false, "PvP state cannot be changed while playing a tournament."
end
player = minetest.get_player_by_name(player_name)
pvptable[player_name] = false
minetest.chat_send_player(player_name, "Your PvP has been disabled")
player:hud_remove(pvpenabled)
player:hud_remove(pvppic)
pvpdisabled = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = "PvP is disabled for you!",
number = 0x7DC435
})
nopvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x = -210, y = 20},
scale = {x = 1, y = 1},
text = "nopvp.png"
})
return true
return pvpplus.pvp_set(player_name, false)
end
function pvpplus.pvp_toggle(playername)
@ -78,6 +76,10 @@ function pvpplus.pvp_toggle(playername)
end
end
function pvpplus.is_pvp(playername)
return pvptable[playername] or false
end
unified_inventory.register_button("pvp", {
type = "image",
image = "pvp.png",

View File

@ -8,6 +8,7 @@ local tournament = {
running_tournament = false,
teleport_immediately = false,
engaged_players = {},
previous_pvp_states = {},
players = {},
sent_damages = {},
received_damages = {},
@ -68,6 +69,7 @@ function pvpplus.start_tournament(starter_name)
local chat_message = "PVP TOURNAMENT BEGINS! Started by " .. starter_name .. "\nEngaged players: "
for player, _ in pairs(tournament.engaged_players) do
-- Enable PvPs
tournament.previous_pvp_states[player] = pvpplus.is_pvp(player)
pvpplus.pvp_enable(player)
-- Move to the playing table
tournament.players[player] = true
@ -145,6 +147,9 @@ function pvpplus.stop_tournament()
to_player = name,
gain = 1.0,
})
-- Set PvP to the state it had before the tournament
pvpplus.pvp_set(name, tournament.previous_pvp_states[name])
end
table.sort(rating, function(a, b) return a.score > b.score end)
@ -198,6 +203,7 @@ function pvpplus.stop_tournament()
engaging_players = false,
running_tournament = false,
engaged_players = {},
previous_pvp_states = {},
players = {},
sent_damages = {},
received_damages = {},