Add HUD support

This commit is contained in:
rubenwardy 2015-07-16 18:01:31 +01:00
parent 211d1d2dec
commit 061e0beeb9
4 changed files with 125 additions and 33 deletions

View File

@ -5,6 +5,11 @@ Created by [rubenwardy](http://rubenwardy.com)
Copyright (c) 2015, no rights reserved Copyright (c) 2015, no rights reserved
Licensed under WTFPL or CC0 (you choose) Licensed under WTFPL or CC0 (you choose)
# Settings
* vote.maximum_active - maximum votes running at a time, votes are queued if it
reaches this. Defaults to 1.
# Example # Example
```lua ```lua

48
hudkit.lua Normal file
View File

@ -0,0 +1,48 @@
-- HudKit, by rubenwardy
-- License: Either WTFPL or CC0, you can choose.
local function hudkit()
return {
players = {},
add = function(self, player, id, def)
local name = player:get_player_name()
local elements = self.players[name]
if not elements then
self.players[name] = {}
elements = self.players[name]
end
elements[id] = player:hud_add(def)
end,
exists = function(self, player, id)
local elements = self.players[player:get_player_name()]
return elements and elements[id]
end,
change = function(self, player, id, stat, value)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
player:hud_change(elements[id], stat, value)
return true
end,
remove = function(self, player, id)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
player:hud_remove(elements[id])
elements[id] = nil
return true
end
}
end
return hudkit

105
init.lua
View File

@ -4,7 +4,7 @@ vote = {
} }
function vote.new_vote(creator, voteset) function vote.new_vote(creator, voteset)
local max_votes = tonumber(minetest.setting_get("vote_maximum_active")) or 1 local max_votes = tonumber(minetest.setting_get("vote.maximum_active")) or 1
if #vote.active < max_votes then if #vote.active < max_votes then
vote.start_vote(voteset) vote.start_vote(voteset)
@ -50,30 +50,8 @@ function vote.start_vote(voteset)
end) end)
end end
-- Send notification to players -- Show HUD a.s.a.p.
local players = minetest.get_connected_players() vote.update_all_hud()
for _, player in pairs(players) do
local name = player:get_player_name()
if not voteset.can_vote or
voteset:can_vote(name) then
local nextvote = vote.get_next_vote(name)
if nextvote == voteset then
minetest.chat_send_player(name,
"Vote started: " .. voteset.description)
if voteset.help then
minetest.chat_send_player(name, voteset.help)
end
else
minetest.chat_send_player(name,
"A new vote started, please respond to this one first:")
minetest.chat_send_player(name,
"Next vote: " .. nextvote.description)
if nextvote.help then
minetest.chat_send_player(name, nextvote.help)
end
end
end
end
end end
function vote.end_vote(voteset) function vote.end_vote(voteset)
@ -88,7 +66,6 @@ function vote.end_vote(voteset)
return return
end end
local result = nil local result = nil
if voteset.on_decide then if voteset.on_decide then
result = voteset:on_decide(voteset.results) result = voteset:on_decide(voteset.results)
@ -109,11 +86,15 @@ function vote.end_vote(voteset)
voteset:on_result(result, voteset.results) voteset:on_result(result, voteset.results)
end end
local max_votes = tonumber(minetest.setting_get("vote_maximum_active")) or 1 local max_votes = tonumber(minetest.setting_get("vote.maximum_active")) or 1
if #vote.active < max_votes and #vote.queue > 0 then if #vote.active < max_votes and #vote.queue > 0 then
local nextvote = table.remove(vote.queue, 1) local nextvote = table.remove(vote.queue, 1)
vote.start_vote(nextvote) vote.start_vote(nextvote)
else
-- Update HUD a.s.a.p.
vote.update_all_hud()
end end
end end
function vote.get_next_vote(name) function vote.get_next_vote(name)
@ -155,15 +136,72 @@ function vote.vote(voteset, name, value)
voteset:on_vote(name, value) voteset:on_vote(name, value)
end end
vote.check_vote(voteset) vote.check_vote(voteset)
end
local nextvote = vote.get_next_vote(name) local hudkit = dofile(minetest.get_modpath("vote") .. "/hudkit.lua")
if nextvote then vote.hud = hudkit()
minetest.chat_send_player(name, "Next vote: " .. nextvote.description) function vote.update_hud(player)
if nextvote.help then local name = player:get_player_name()
minetest.chat_send_player(name, nextvote.help) local voteset = vote.get_next_vote(name)
if not voteset then
vote.hud:remove(player, "vote:desc")
vote.hud:remove(player, "vote:bg")
vote.hud:remove(player, "vote:help")
return
end
if not vote.hud:exists(player, "vote:bg") then
vote.hud:add(player, "vote:bg", {
hud_elem_type = "image",
position = {x = 1, y = 0.5},
scale = {x = 1, y = 1},
text = "vote_background.png",
offset = {x=-100, y = 10},
number = 0xFFFFFF
})
end
if vote.hud:exists(player, "vote:desc") then
vote.hud:change(player, "vote:desc", "text", voteset.description .. "?")
else
vote.hud:add(player, "vote:desc", {
hud_elem_type = "text",
position = {x = 1, y = 0.5},
scale = {x = 100, y = 100},
text = voteset.description .. "?",
offset = {x=-100, y = 0},
number = 0xFFFFFF
})
end
if voteset.help then
if vote.hud:exists(player, "vote:help") then
vote.hud:change(player, "vote:help", "text", voteset.help)
else
vote.hud:add(player, "vote:help", {
hud_elem_type = "text",
position = {x = 1, y = 0.5},
scale = {x = 100, y = 100},
text = voteset.help,
offset = {x=-100, y = 20},
number = 0xFFFFFF
})
end end
else
vote.hud:remove(player, "vote:help")
end end
end end
minetest.register_on_leaveplayer(function(player)
vote.hud.players[player:get_player_name()] = nil
end)
function vote.update_all_hud()
local players = minetest.get_connected_players()
for _, player in pairs(players) do
vote.update_hud(player)
end
minetest.after(5, vote.update_all_hud)
end
minetest.after(5, vote.update_all_hud)
minetest.register_chatcommand("yes", { minetest.register_chatcommand("yes", {
privs = { privs = {
@ -244,10 +282,11 @@ minetest.register_chatcommand("vote_kick", {
if not minetest.get_player_by_name(param) then if not minetest.get_player_by_name(param) then
minetest.chat_send_player(name, "There is no player called '" .. minetest.chat_send_player(name, "There is no player called '" ..
param .. "'") param .. "'")
return
end end
vote.new_vote(name, { vote.new_vote(name, {
description = "Kick player " .. param, description = "Kick " .. param,
help = "/yes, /no or /abstain", help = "/yes, /no or /abstain",
name = param, name = param,
time = 60, time = 60,

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 B