format file for better readability

This commit is contained in:
Juraj Vajda 2018-01-13 12:52:24 -05:00
parent 47fe7b7b0f
commit ded84548a5
2 changed files with 110 additions and 71 deletions

4
README
View File

@ -1,5 +1,7 @@
OLD PLAYER OLD PLAYER
minetest 0.4.14 minetest 0.4.14+
Modified by SaKeL for Survival X server.
Keep only those players that really did something, like getting some materials (can adjust what in settings). Keep only those players that really did something, like getting some materials (can adjust what in settings).
Removes all player data of players that dont really play and come to just check out the server. Removes all player data of players that dont really play and come to just check out the server.

173
init.lua
View File

@ -1,58 +1,65 @@
-- OLD PLAYER : keep only serious players data on server -- OLD PLAYER : keep only serious players data on server
--(c) 2015-2016 rnd --(c) 2015-2016 rnd
--
-- modified by SaKeL for Survival X server
-- - Linux support
-- - Protector (redo) support (@todo)
oldplayer = {} oldplayer = {}
-- SETTINGS -- SETTINGS
oldplayer.requirement = { "default:stick 1", "default:steel_ingot 1" }
oldplayer.welcome = "*** IMPORTANT *** Please have at least 1 STICK and 1 STEEL INGOT in your inventory when leaving the game to register as serious player. If not, your player data will be deleted!"
oldplayer.requirement = {"default:dirt 1", "default:steel_ingot 1"}; -- CORE
oldplayer.welcome = "*** IMPORTANT *** please have at least 1 dirt and 1 steel ingot in your inventory when leaving to register as serious player. If not, your player data will be deleted."; oldplayer.players = {}
local worldpath = minetest.get_worldpath()
-- END OF SETTINGS
oldplayer.players = {};
local worldpath = minetest.get_worldpath();
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name(); if name == nil then return end local name = player:get_player_name()
if name == nil then return end
-- read player inventory data -- read player inventory data
local inv = player:get_inventory(); local inv = player:get_inventory()
local isoldplayer = inv:get_stack("oldplayer", 1):get_count(); local isoldplayer = inv:get_stack("oldplayer", 1):get_count()
inv:set_size("oldplayer", 2); inv:set_size("oldplayer", 2)
local ip = minetest.get_player_ip(name); if not ip then return end
local ip = minetest.get_player_ip(name)
if not ip then return end
inv:set_stack("oldplayer", 2, ItemStack("IP".. ip)) -- string.gsub(ip,".","_"))); inv:set_stack("oldplayer", 2, ItemStack("IP".. ip)) -- string.gsub(ip,".","_")));
if isoldplayer > 0 then if isoldplayer > 0 then
oldplayer.players[name] = 1 oldplayer.players[name] = 1
minetest.chat_send_player(name, "#OLDPLAYER: welcome back"); minetest.chat_send_player(name, "#OLDPLAYER: Welcome back!")
else else
local privs = minetest.get_player_privs(name); local privs = minetest.get_player_privs(name)
if privs.kick then if privs.kick then
inv:set_stack("oldplayer", 1, ItemStack("oldplayer")); inv:set_stack("oldplayer", 1, ItemStack("oldplayer"))
minetest.chat_send_player(name, "#OLDPLAYER: welcome moderator. setting as old player."); minetest.chat_send_player(name, "#OLDPLAYER: welcome moderator. Setting as old player.");
oldplayer.players[name] = 1 oldplayer.players[name] = 1
else else
oldplayer.players[name] = 0 oldplayer.players[name] = 0
local form = "size [6,2] textarea[0,0;6.6,3.5;help;OLDPLAYER WELCOME;".. oldplayer.welcome.."]" local form = "size [6,2] textarea[0,0;6.6,3.5;help;OLDPLAYER WELCOME;".. oldplayer.welcome.."]"
minetest.show_formspec(name, "oldplayer:welcome", form) minetest.show_formspec(name, "oldplayer:welcome", form)
-- minetest.chat_send_player(name, oldplayer.welcome); -- minetest.chat_send_player(name, oldplayer.welcome)
end end
end end
end) end)
minetest.register_on_leaveplayer(function(player, timed_out) minetest.register_on_leaveplayer(function(player, timed_out)
local name = player:get_player_name(); if name == nil then return end local name = player:get_player_name()
if name == nil then return end
if oldplayer.players[name] == 1 then return end -- already old, do nothing if oldplayer.players[name] == 1 then return end -- already old, do nothing
local delete = false; -- should we delete player? local delete = false -- should we delete player?
-- read player inventory data -- read player inventory data
local inv = player:get_inventory(); local inv = player:get_inventory()
-- does player have all the required items in inventory? -- does player have all the required items in inventory?
for _,item in pairs(oldplayer.requirement) do for _,item in pairs(oldplayer.requirement) do
@ -62,94 +69,121 @@ minetest.register_on_leaveplayer(function(player, timed_out)
end end
if not delete then -- set up oldplayer inventory so we know player is old next time if not delete then -- set up oldplayer inventory so we know player is old next time
inv:set_size("oldplayer", 2); inv:set_size("oldplayer", 2)
inv:set_stack("oldplayer", 1, ItemStack("oldplayer")); inv:set_stack("oldplayer", 1, ItemStack("oldplayer"))
else -- delete player profile
local filename = worldpath .. "\\players\\" .. name; else -- delete player profile
local filename = worldpath.."/players/"..name
-- PROBLEM: deleting doesnt always work? seems minetest itself is saving stuff. -- PROBLEM: deleting doesnt always work? seems minetest itself is saving stuff.
-- so we wait a little and then delete -- so we wait a little and then delete
minetest.after(10,function() minetest.after(10, function()
print("[oldplayer] removing player filename " .. filename) print("[oldplayer] removing player filename " .. filename)
local err,msg = os.remove(filename) local err,msg = os.remove(filename)
if err==nil then
print ("[oldplayer] error removing player data " .. filename .. " error message: " .. msg) if err == nil then
print ("[oldplayer] Error while removing player data " .. filename .. " error message: " .. msg)
end end
-- TO DO: how to remove players from auth.txt easily without editing file manually like below -- TO DO: how to remove players from auth.txt easily without editing file manually like below
end); end)
end end
end end
) )
-- delete file if not old player -- delete file if not old player
local function remove_non_old_player_file(name) local function remove_non_old_player_file(name)
local filename = worldpath.."\\players\\"..name; local filename = worldpath.."/players/"..name
local f=io.open(filename,"r") local f = io.open(filename, "r")
local s = f:read("*all"); f:close(); local s = f:read("*all")
if string.find(s,"Item oldplayer") then return false else os.remove(filename) return true end
f:close()
if string.find(s,"Item oldplayer") then
return false
else
os.remove(filename)
return true
end
end end
-- deletes data with no corresponding playerfiles from auth.txt and creates auth_new.txt -- deletes data with no corresponding playerfiles from auth.txt and creates auth_new.txt
local function player_file_exists(name) local function player_file_exists(name)
local f=io.open(worldpath.."\\players\\"..name,"r") local f = io.open(worldpath.."/players/"..name, "r")
if f~=nil then io.close(f) return true else return false end
if not f then
return false
else
io.close(f)
return true
end
end end
local function remove_missing_players_from_auth() local function remove_missing_players_from_auth()
local playerfilelist = minetest.get_dir_list(worldpath.."/players", false)
local f = io.open(worldpath.."/auth.txt", "r")
local playerfilelist = minetest.get_dir_list(worldpath.."\\players", false);
local f = io.open(worldpath.."\\auth.txt", "r");
if not f then return end
local s = f:read("*a");f:close();
local p1,p2;
f = io.open(worldpath.."\\auth_new.txt", "w");
if not f then return end if not f then return end
local playerlist = {}; local s = f:read("*a")
for _,name in ipairs(playerfilelist) do f:close()
playerlist[name]=true;
local p1, p2
f = io.open(worldpath.."/auth_new.txt", "w")
if not f then return end
local playerlist = {}
for _, name in ipairs(playerfilelist) do
print("name: ", name)
playerlist[name] = true
end end
local i=0; local i = 0
local j=0; local k=0; local j = 0
local name; local k = 0
local count = 0; local name
local count = 0
-- parse through auth and remove missing players data -- parse through auth and remove missing players data
while j do while j do
j=string.find(s,":",i); j = string.find(s, ":", i)
if j then if j then
if i ~= 1 then if i ~= 1 then
name = string.sub(s,i+1,j-1) name = string.sub(s, i + 1, j - 1)
else else
name = string.sub(s,1,j-1) name = string.sub(s, 1, j - 1)
end end
if j then if j then
k=string.find(s,"\n",i+1); k = string.find(s, "\n", i + 1)
if not k then if not k then
j = nil j = nil
if playerlist[name] then if playerlist[name] then
f:write(string.sub(s,i+1)) f:write(string.sub(s, i + 1))
else
count = count+1
end
else
if playerlist[name] then
f:write(string.sub(s,i+1,k))
else else
count = count + 1 count = count + 1
end end
i=k;
else
if playerlist[name] then
f:write(string.sub(s, i + 1, k))
else
count = count + 1
end
i = k
end end
end end
end end
end end
f:close();
print("#OLD PLAYER : removed " .. count .. " entries from auth.txt. Replace auth.txt with auth_new.txt"); f:close()
print("#OLD PLAYER : Removed "..count.." entries from auth.txt. Replace auth.txt with auth_new.txt")
end end
local function remove_non_old_player_files() local function remove_non_old_player_files()
@ -164,4 +198,7 @@ local function remove_non_old_player_files()
print("#OLD PLAYER: removed " .. count .. " non oldplayer player files"); print("#OLD PLAYER: removed " .. count .. " non oldplayer player files");
end end
minetest.register_on_shutdown(function() remove_non_old_player_files();remove_missing_players_from_auth() end) minetest.register_on_shutdown(function()
remove_non_old_player_files()
remove_missing_players_from_auth()
end)