Added top-climber
parent
aaf4043ed8
commit
fa2b3928d4
128
init.lua
128
init.lua
|
@ -1,17 +1,21 @@
|
|||
-- ordered list: { name: '', xp: 0 }
|
||||
|
||||
-- Variable, tables and settingdata
|
||||
|
||||
local xp_to_show = tonumber(minetest.settings:get("xp_to_show")) or 10000
|
||||
local timer_time = tonumber(minetest.settings:get("timer")) or 60
|
||||
local rounding = minetest.settings:get_bool("rounding")
|
||||
|
||||
print(xp_to_show)
|
||||
print(timer_time)
|
||||
print(rounding)
|
||||
local reset_timer_time = tonumber(minetest.settings:get("reset_timer")) or 604800
|
||||
local rounding = minetest.settings:get_bool("rounding")
|
||||
|
||||
xp_highscores = {}
|
||||
xp_highscores.highscore = {}
|
||||
xp_highscores.highscores_climber = {}
|
||||
|
||||
local storage = minetest.get_mod_storage()
|
||||
local fname = minetest.get_worldpath().."/highscores.txt"
|
||||
local fname_climber = minetest.get_worldpath().."/highscores_climber.txt"
|
||||
|
||||
-- Files and storage
|
||||
|
||||
local write_file = function()
|
||||
local f = io.open(fname, "w")
|
||||
|
@ -20,6 +24,18 @@ local write_file = function()
|
|||
io.close(f)
|
||||
end
|
||||
|
||||
local write_file_climber = function()
|
||||
local f = io.open(fname_climber, "w")
|
||||
local data_string = minetest.serialize(xp_highscores.highscores_climber)
|
||||
f:write(data_string)
|
||||
io.close(f)
|
||||
end
|
||||
|
||||
local reset_climber = function()
|
||||
xp_highscores.highscores_climber = {}
|
||||
write_file_climber()
|
||||
end
|
||||
|
||||
local f = io.open(fname, "r")
|
||||
if f then -- file exists
|
||||
local data_string = f:read("*all")
|
||||
|
@ -29,6 +45,19 @@ else
|
|||
write_file()
|
||||
end
|
||||
|
||||
local f = io.open(fname_climber, "r")
|
||||
if f then -- file exists
|
||||
local data_string = f:read("*all")
|
||||
xp_highscores.highscores_climber = minetest.deserialize(data_string)
|
||||
io.close(f)
|
||||
else
|
||||
write_file_climber()
|
||||
end
|
||||
|
||||
if storage:get_float("reset_timer") == nil then storage:set_float("reset_timer", 0) end
|
||||
|
||||
-- functions
|
||||
|
||||
-- Don't try to much here seems that xp above a number(more then 200 million) not more working correct
|
||||
local function round(num)
|
||||
if num < 1000 then
|
||||
|
@ -80,6 +109,59 @@ local update_highscore = function()
|
|||
end
|
||||
end
|
||||
|
||||
xp_redo.add_xp = function(playername, xp)
|
||||
-- Needed for xp_redo
|
||||
local player = minetest.get_player_by_name(playername)
|
||||
if player == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local currentXp = xp_redo.get_xp(playername)
|
||||
|
||||
local sumXp = currentXp + xp
|
||||
if sumXp < 0 then
|
||||
sumXp = 0
|
||||
end
|
||||
|
||||
player:get_meta():set_int("xp", sumXp)
|
||||
|
||||
xp_redo.run_hook("xp_change", { playername, sumXp })
|
||||
|
||||
local previousRank = xp_redo.get_rank(currentXp)
|
||||
local currentRank = xp_redo.get_rank(sumXp)
|
||||
if currentRank and currentRank.xp > previousRank.xp then
|
||||
-- level up
|
||||
xp_redo.run_hook("rank_change", { playername, sumXp, currentRank })
|
||||
|
||||
local state = player:get_meta():get(xp_redo.HUD_DISPLAY_STATE_NAME)
|
||||
if state and state == "on" then
|
||||
level_up(player, currentRank)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Needed for this mod
|
||||
local name = playername
|
||||
local found = false
|
||||
for _,entry in pairs(xp_highscores.highscores_climber) do
|
||||
if entry.name == name then
|
||||
-- connected player already exists in highscore, update value
|
||||
entry.xp = entry.xp+xp
|
||||
found = true
|
||||
end
|
||||
end
|
||||
|
||||
if not found then
|
||||
-- create new entry
|
||||
table.insert(xp_highscores.highscores_climber, { name=name, xp=xp })
|
||||
end
|
||||
table.sort(xp_highscores.highscores_climber, function(a,b) return a.xp > b.xp end)
|
||||
-- Maybe lag, but if a crash come that would be a problem to update all some secounds
|
||||
write_file_climber()
|
||||
-- returning
|
||||
return sumXp
|
||||
end
|
||||
|
||||
-- Updater
|
||||
|
||||
local timer = 0
|
||||
|
@ -92,6 +174,14 @@ minetest.register_globalstep(function(dtime)
|
|||
end
|
||||
end)
|
||||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
storage:set_float("reset_timer", storage:get_float("reset_timer")+dtime)
|
||||
if storage:get_float("reset_timer") >= reset_timer_time then
|
||||
reset_climber()
|
||||
storage:set_float("reset_timer", 0)
|
||||
end
|
||||
end)
|
||||
|
||||
-- Formspec
|
||||
|
||||
local function playerhighscore_lister()
|
||||
|
@ -108,13 +198,35 @@ local function playerhighscore_lister()
|
|||
return string, number
|
||||
end
|
||||
|
||||
local function showxpform(pname, selected_idx, table)
|
||||
string, number = playerhighscore_lister()
|
||||
local function playerhighscoreclimber_lister()
|
||||
local string = ""
|
||||
local number = 1
|
||||
for _,entry in pairs(xp_highscores.highscores_climber) do
|
||||
if rounding then
|
||||
string = string..tostring(number)..". "..entry.name .. ": " .. round(entry.xp) .. ","
|
||||
else
|
||||
string = string..tostring(number)..". "..entry.name .. ": " .. tostring(entry.xp) .. ","
|
||||
end
|
||||
number = number + 1
|
||||
end
|
||||
return string, number
|
||||
end
|
||||
|
||||
local function showxpform(pname, selected_idx)
|
||||
if selected_idx == 1 then
|
||||
string, number = playerhighscore_lister()
|
||||
label = ""
|
||||
elseif selected_idx == 2 then
|
||||
string, number = playerhighscoreclimber_lister()
|
||||
label = "label[0,8;Next Reset in: "..tostring((math.floor((reset_timer_time-storage:get_float("reset_timer"))*10+0.5)/10)).." Secounds]"
|
||||
end
|
||||
|
||||
minetest.show_formspec(pname, "highscores:xp_highscores",
|
||||
"size[8,9]"..
|
||||
"tabheader[0,0;highscores_tab;" .. "Player Highscores,Top climber;"
|
||||
.. tostring(selected_idx) .. ";true;false]"..
|
||||
"table[1,1;6,7;highscores_table;"..string..";1]")
|
||||
"table[1,1;6,7;highscores_table;"..string..";1]"..
|
||||
label)
|
||||
end
|
||||
|
||||
minetest.register_chatcommand("highscores", {
|
||||
|
|
|
@ -4,5 +4,8 @@ xp_to_show (Xp to reach the list) int 10000
|
|||
# How often it update the highscore lists
|
||||
timer (timer) int 60
|
||||
|
||||
#How often the climber-list will reseted in secounds. 86400 = 1 day, 604800 = 1 week.
|
||||
reset_timer (reset_timer) int 604800
|
||||
|
||||
#If xp is over 1000 there stand instead of 1000 1k. At 1000000 1M and above 1000000000 1G
|
||||
rounding (rounding) bool true
|
||||
|
|
Loading…
Reference in New Issue