make multi-user more reliable

This commit is contained in:
Dirk Sohler 2017-02-28 19:38:24 +01:00
parent 4d04eb7b8a
commit c31f0c1570

@ -8,7 +8,7 @@ end
_xtend.v.xtimer = {
startDate = {},
playerData = {},
interval = _xtend.g('xtimer_update_interval'),
placeholder = _xtend.g('xtimer_placeholder'),
font_color = _xtend.g('xtimer_font_color'),
@ -31,7 +31,7 @@ _xtend.v.xtimer = {
minetest.register_on_joinplayer(function(player)
minetest.after(1, function(player)
local playerName = player:get_player_name()
local hud_id = player:hud_add({
local _hudID = player:hud_add({
hud_elem_type = 'text',
position = _xtend.v.xtimer.location.position,
alignment = _xtend.v.xtimer.location.alignment,
@ -39,32 +39,51 @@ minetest.register_on_joinplayer(function(player)
text = _xtend.v.xtimer.placeholder,
number = '0x'.._xtend.v.xtimer.font_color
})
_xtend.v.xtimer.startDate[playerName] = os.time()
minetest.after(5, _xtimer_changeText, player, hud_id)
_xtend.v.xtimer.playerData[playerName] = {
startTime = os.time(),
hudID = _hudID
}
minetest.after(5, _xtimer_changeText, player)
end, player)
end)
function _xtimer_changeText(player, hud_id)
minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name()
_xtend.v.xtimer.playerData[playerName] = nil
end)
function _xtimer_changeText(player)
-- Save the game-detected locale. Fun fact: changing the locale on runtime
-- from within a mod causes the UI to glitch out completely.
local currentLocale = os.setlocale(nil)
os.setlocale(_xtend.v.xtimer.format.locale)
local playerName = player:get_player_name()
if playerName == '' then return end
local startTime = _xtend.v.xtimer.playerData[playerName].startTime
local hudID = _xtend.v.xtimer.playerData[playerName].hudID
local time = 24*60*minetest.get_timeofday()
local h = tostring((math.floor(time/60) % 60))
local m = tostring((math.floor(time) % 60))
local __c = 0
for x,y in pairs(_xtend.v.xtimer.playerData) do
__c = __c + 1
end
print(__c..' '..playerName)
local res = _xtend.v.xtimer.format.output:gsub('(+.)', {
['+s'] = os.date(_xtend.v.xtimer.format.start, _xtend.v.xtimer.startDate[playerName]),
['+s'] = os.date(_xtend.v.xtimer.format.start, startTime),
['+c'] = os.date(_xtend.v.xtimer.format.current),
['+r'] = os.date('!'.._xtend.v.xtimer.format.runtime:gsub('(+.)', {
['+h'] = '%H',
['+m'] = '%M',
['+s'] = '%S'
}), os.time() - _xtend.v.xtimer.startDate[playerName]),
}), os.time() - startTime),
['+i'] = _xtend.v.xtimer.format.ingame:gsub('(+.)', {
['+h'] = string.rep('0', 2-#h)..h,
['+m'] = string.rep('0', 2-#m)..m
@ -74,6 +93,6 @@ function _xtimer_changeText(player, hud_id)
os.setlocale(currentLocale) -- Restore game-detected locale
player:hud_change(hud_id, 'text', res)
minetest.after(_xtend.v.xtimer.interval,_xtimer_changeText,player,hud_id)
player:hud_change(hudID, 'text', res)
minetest.after(_xtend.v.xtimer.interval, _xtimer_changeText, player)
end