merge changes from skyblock branch
parent
c2c9f4585c
commit
a3b81882a3
314
init.lua
314
init.lua
|
@ -7,7 +7,7 @@ skin_db.active = {}
|
|||
skin_db.inactive = {}
|
||||
skin_db.skin = {}
|
||||
|
||||
minetest.register_privilege("moderator_skins", "Moderator skins access")
|
||||
minetest.register_privilege("moderator_skins", "Access Moderator skins")
|
||||
|
||||
local ie = minetest.request_insecure_environment()
|
||||
|
||||
|
@ -30,8 +30,7 @@ local S = {
|
|||
armor = minetest.get_modpath("3d_armor")
|
||||
}
|
||||
|
||||
local db = _sql.open(S.WP.."/skin_db.sqlite") -- connection
|
||||
|
||||
local db = _sql.open(S.WP.."/skin_db.sqlite") -- database connection
|
||||
|
||||
local function getos()
|
||||
-- is popen supported?
|
||||
|
@ -50,23 +49,21 @@ local function getos()
|
|||
return "unknown"
|
||||
end
|
||||
|
||||
local function active_index(name)
|
||||
local result
|
||||
for i,v in ipairs(skin_db.active) do
|
||||
if v.name == name then result = i end
|
||||
end
|
||||
return result or 1
|
||||
end
|
||||
|
||||
-- Create db:exec wrapper for error reporting
|
||||
|
||||
local function db_exec(stmt)
|
||||
if db:exec(stmt) ~= _sql.OK then
|
||||
minetest.log("info", "Sqlite ERROR: ", db:errmsg())
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
################
|
||||
### Database ###
|
||||
################
|
||||
]]
|
||||
|
||||
|
||||
local create_db = [[
|
||||
CREATE TABLE IF NOT EXISTS player (id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(32), skin_id INTEGER);
|
||||
|
@ -77,23 +74,28 @@ active BOOLEAN);
|
|||
]]
|
||||
db_exec(create_db)
|
||||
|
||||
|
||||
------------
|
||||
-- Queries
|
||||
------------
|
||||
|
||||
local function get_player_record(name)
|
||||
local query = ([[
|
||||
SELECT * FROM player WHERE name = '%s' LIMIT 1;
|
||||
]]):format(name)
|
||||
for row in db:nrows(query) do
|
||||
local it, state = db:nrows(query)
|
||||
local row = it(state)
|
||||
if row then
|
||||
return row
|
||||
end
|
||||
end
|
||||
|
||||
local function get_skin(id)
|
||||
local query = ([[
|
||||
SELECT * FROM skins WHERE id = '%i' LIMIT 1;
|
||||
SELECT * FROM skins WHERE id = '%i' AND active = "true" LIMIT 1;
|
||||
]]):format(id)
|
||||
for row in db:nrows(query) do
|
||||
local it, state = db:nrows(query)
|
||||
local row = it(state)
|
||||
if row then
|
||||
return row
|
||||
end
|
||||
end
|
||||
|
@ -125,7 +127,9 @@ local function get_skin_id(name)
|
|||
local query = ([[
|
||||
SELECT id FROM skins WHERE name = '%s';
|
||||
]]):format(name)
|
||||
for row in db:nrows(query) do
|
||||
local it, state = db:nrows(query)
|
||||
local row = it(state)
|
||||
if row then
|
||||
return row.id
|
||||
end
|
||||
end
|
||||
|
@ -136,12 +140,17 @@ local function check_skins(filename)
|
|||
FROM skins
|
||||
WHERE filename = '%s' LIMIT 1;
|
||||
]]):format(filename)
|
||||
for row in db:nrows(query) do
|
||||
local it, state = db:nrows(query)
|
||||
local row = it(state)
|
||||
if row then
|
||||
return row
|
||||
end
|
||||
end
|
||||
|
||||
------------
|
||||
-- Inserts
|
||||
------------
|
||||
|
||||
local function create_player_record(name, skin_id)
|
||||
local stmt = ([[
|
||||
INSERT INTO player (
|
||||
|
@ -173,7 +182,10 @@ local function create_skin_record(filename, name, author, license, mod, admin, p
|
|||
db_exec(stmt)
|
||||
end
|
||||
|
||||
------------
|
||||
-- Updates
|
||||
------------
|
||||
|
||||
local function update_player_record(name, skin_id)
|
||||
local stmt = ([[
|
||||
UPDATE player SET skin_id = '%i' WHERE name = '%s';
|
||||
|
@ -186,6 +198,7 @@ local function update_skin_active(skin_id, status)
|
|||
UPDATE skins SET active = '%s' WHERE id = '%i';
|
||||
]]):format(status, skin_id)
|
||||
db_exec(stmt)
|
||||
get_active() -- udate cache
|
||||
end
|
||||
|
||||
local function update_skin_record(data)
|
||||
|
@ -200,11 +213,30 @@ local function update_skin_record(data)
|
|||
db_exec(stmt)
|
||||
end
|
||||
|
||||
--[[
|
||||
#################
|
||||
## Formspecs ##
|
||||
#################
|
||||
]]
|
||||
----------------
|
||||
-- player skin
|
||||
----------------
|
||||
|
||||
-- Update
|
||||
local function update_player_skin(player)
|
||||
|
||||
if not player then return end
|
||||
local name = player:get_player_name()
|
||||
local file_name = skin_db.skin[name].filename
|
||||
|
||||
-- 3d_armor mod?
|
||||
if S.armor then
|
||||
armor.textures[name].skin = file_name
|
||||
armor:set_player_armor(player)
|
||||
else
|
||||
-- Set player texture
|
||||
player:set_properties({textures = {file_name},})
|
||||
end
|
||||
end
|
||||
|
||||
-------------
|
||||
-- formspecs
|
||||
-------------
|
||||
|
||||
local state = {}
|
||||
|
||||
|
@ -213,12 +245,12 @@ local function get_context(name)
|
|||
|
||||
if not tbl then -- initialise?
|
||||
tbl = {
|
||||
event = "",
|
||||
event = "", -- identifier
|
||||
page = 1, -- admin gui inactive
|
||||
max = 1,
|
||||
i = 1, -- active list index
|
||||
o = -1, -- inactive list index
|
||||
id = 1,
|
||||
max = 1, -- max pages
|
||||
i = 1, -- active page index
|
||||
o = -1, -- inactive page index
|
||||
id = 1, -- inactive list index
|
||||
list = {},
|
||||
preview = 1
|
||||
}
|
||||
|
@ -230,6 +262,7 @@ end
|
|||
-- Selection
|
||||
skin_db.formspec = {}
|
||||
|
||||
-- Preview and selection
|
||||
skin_db.formspec.main = function(name)
|
||||
|
||||
local formspec = ""
|
||||
|
@ -261,30 +294,44 @@ skin_db.formspec.main = function(name)
|
|||
and record.moderator == "true"
|
||||
and record.private == "false" then -- hub mod
|
||||
table.insert(context.list, record)
|
||||
elseif record.private == name then -- private
|
||||
elseif record.private == "true" and
|
||||
record.name == name then -- private
|
||||
table.insert(context.list, record)
|
||||
elseif record.admin == "false"
|
||||
and record.moderator == "false"
|
||||
and record.private == "false" then -- player
|
||||
elseif record.admin == "false" and
|
||||
record.moderator == "false" and
|
||||
record.private == "false" then -- player
|
||||
table.insert(context.list, record)
|
||||
end
|
||||
end
|
||||
|
||||
-- apply filtered skins
|
||||
formspec = formspec.. "textlist[0.5,4.5;6.8,4;sel;"
|
||||
|
||||
for i,v in ipairs(context.list) do
|
||||
formspec = formspec .. v.name..","
|
||||
-- set metadata for correct index
|
||||
if i == context.preview then
|
||||
meta = {
|
||||
name = v.name,
|
||||
author = v.author,
|
||||
license = v.license
|
||||
}
|
||||
if #context.list > 0 then
|
||||
for i,v in ipairs(context.list) do
|
||||
formspec = formspec .. v.name..","
|
||||
-- set metadata for correct index
|
||||
if i == context.preview then
|
||||
meta = {
|
||||
name = v.name,
|
||||
author = v.author,
|
||||
license = v.license
|
||||
}
|
||||
end
|
||||
end
|
||||
else
|
||||
-- player doesn't have priv, set default
|
||||
local v = skin_db.active[1]
|
||||
formspec = formspec .. v.name..","
|
||||
meta = {
|
||||
name = v.name,
|
||||
author = v.author,
|
||||
license = v.license
|
||||
}
|
||||
table.insert(context.list, v)
|
||||
v = skin_db.active[26]
|
||||
table.insert(context.list, v)
|
||||
end
|
||||
|
||||
|
||||
-- Remove unwanted final comma
|
||||
formspec = formspec:sub(1, (formspec:len() - 1))
|
||||
formspec = formspec..";"..context.preview..";true]"
|
||||
|
@ -307,7 +354,6 @@ skin_db.formspec.main = function(name)
|
|||
|
||||
return formspec
|
||||
end
|
||||
|
||||
-- Management
|
||||
skin_db.formspec.admin = function(name)
|
||||
|
||||
|
@ -315,6 +361,7 @@ skin_db.formspec.admin = function(name)
|
|||
local context = get_context(name)
|
||||
local bgimg = ""
|
||||
local privs = minetest.get_player_privs(name)
|
||||
local entries_max = 100
|
||||
|
||||
if not privs.server then return "" end
|
||||
if default and default.gui_bg_img then
|
||||
|
@ -329,26 +376,25 @@ skin_db.formspec.admin = function(name)
|
|||
.."textlist[0.5,0.5;4,7.6;skin_db:out;" -- inactive list
|
||||
|
||||
if #skin_db.inactive > 0 then -- content?
|
||||
-- calculate pages
|
||||
-- calculate pages; 100 entries/page
|
||||
local min,max,pmax
|
||||
pmax = math.floor(#skin_db.inactive / 100)
|
||||
-- add a page?
|
||||
if pmax * 100 < #skin_db.inactive then pmax = pmax+1 end
|
||||
-- store page count
|
||||
context.max = pmax
|
||||
-- initialise the limits for the current page
|
||||
if context.page == 1 then
|
||||
min = 1
|
||||
max = 100
|
||||
else
|
||||
min = context.page * 100 - 99
|
||||
if context.page ~= pmax then
|
||||
max = min + 99
|
||||
else
|
||||
local leftover = #skin_db.inactive - ((pmax - 1)*100)
|
||||
max = min + leftover - 1
|
||||
end
|
||||
pmax = math.floor(#skin_db.inactive / entries_max)
|
||||
-- overflow requires additional page
|
||||
if pmax * entries_max < #skin_db.inactive then
|
||||
pmax = pmax + 1
|
||||
end
|
||||
-- set page count in context
|
||||
context.max = pmax
|
||||
-- initialise limits of current page
|
||||
min = (context.page * entries_max) - (entries_max - 1)
|
||||
if context.page ~= pmax then -- max entries
|
||||
max = min + entries_max - 1
|
||||
else
|
||||
-- sum = inactive - (pages-1) * entries per page
|
||||
local leftover = #skin_db.inactive - ((pmax - 1) * entries_max)
|
||||
max = min + leftover - 1
|
||||
end
|
||||
|
||||
-- add names
|
||||
for i = min, max do
|
||||
formspec = formspec..skin_db.inactive[i].name..","
|
||||
|
@ -422,28 +468,49 @@ skin_db.formspec.admin = function(name)
|
|||
return formspec
|
||||
end
|
||||
|
||||
-- Update player skin
|
||||
local function update_player_skin(player)
|
||||
-----------
|
||||
-- Import
|
||||
-----------
|
||||
|
||||
if not player then return end
|
||||
local name = player:get_player_name()
|
||||
local file_name = skin_db.skin[name].filename
|
||||
local function bdb()
|
||||
|
||||
-- 3d_armor mod?
|
||||
if S.armor then
|
||||
armor.textures[name].skin = file_name
|
||||
armor:set_player_armor(player)
|
||||
else
|
||||
-- Set player texture
|
||||
player:set_properties({textures = {file_name},})
|
||||
local tdir = S.MP.."/textures"
|
||||
local mdir = S.MP.."/meta"
|
||||
local p = ie.io.popen('ls -1v '..tdir)
|
||||
local mfn = ""
|
||||
|
||||
for file in p:lines() do
|
||||
if file:find(".png") and
|
||||
not file:find("_preview") and
|
||||
not file:find("skin_db") then
|
||||
local result = check_skins(file) -- prevent duplication
|
||||
if not result then
|
||||
mfn = file:gsub(".png", ".txt")
|
||||
local f = ie.io.open(mdir.."/"..mfn, "r")
|
||||
if f then
|
||||
local cont = {}
|
||||
for line in f:lines() do
|
||||
cont[#cont+1] = line
|
||||
end
|
||||
f:close()
|
||||
local name = cont[1]
|
||||
local author = cont[2]
|
||||
local license = cont[3]
|
||||
name = name:gsub("%(", " ")
|
||||
name = name:gsub("%)", "")
|
||||
name = name:gsub(",", "")
|
||||
create_skin_record(file, name, author, license, nil, nil, nil, nil, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
p:close()
|
||||
update_skin_active(1, 'true')
|
||||
end
|
||||
|
||||
--[[
|
||||
###################
|
||||
## Inventories ##
|
||||
###################
|
||||
]]
|
||||
---------------
|
||||
-- inventory
|
||||
---------------
|
||||
|
||||
-- register sfinv tab when inv+ not active
|
||||
if S.ui then
|
||||
|
@ -494,16 +561,11 @@ elseif S.sfinv and not S.invplus then
|
|||
})
|
||||
end
|
||||
|
||||
--------------
|
||||
-- callbacks
|
||||
--------------
|
||||
|
||||
--[[
|
||||
#################
|
||||
## Callbacks ##
|
||||
#################
|
||||
]]
|
||||
|
||||
|
||||
-- formspecs
|
||||
|
||||
-- formspec handler
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
|
||||
if formname ~= "skin_db:main" and
|
||||
|
@ -531,11 +593,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
context.o = ev_out.index
|
||||
context.i = -1
|
||||
context.event = "inactive"
|
||||
if context.page > 1 then
|
||||
context.id = context.o + (context.page * 100) - 100
|
||||
else
|
||||
context.id = context.o
|
||||
end
|
||||
context.id = context.o + (context.page * 100) - 100
|
||||
-- update preview
|
||||
minetest.show_formspec(name, "skin_db:admin",
|
||||
skin_db.formspec.admin(name))
|
||||
|
@ -543,14 +601,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
|
||||
if ev_out.type == "DCL" then
|
||||
-- make skin active
|
||||
local name_ = skin_db.inactive[context.id].name
|
||||
update_skin_active(skin_db.inactive[context.id].id, 'true')
|
||||
get_inactive()
|
||||
get_active()
|
||||
context.o = -1
|
||||
context.i = active_index(name_)
|
||||
context.event = "active"
|
||||
minetest.show_formspec(name, "skin_db:admin",
|
||||
skin_db.formspec.admin(name))
|
||||
if S.ui then
|
||||
unified_inventory.set_inventory_formspec(player, "craft")
|
||||
end
|
||||
end
|
||||
|
||||
if ev_in.type == "CHG" then
|
||||
|
@ -567,19 +624,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
update_skin_active(skin_db.active[context.i].id, 'false')
|
||||
context.i = 1
|
||||
context.event = "active"
|
||||
get_inactive()
|
||||
get_active()
|
||||
-- update form
|
||||
minetest.show_formspec(name, "skin_db:admin",
|
||||
skin_db.formspec.admin(name))
|
||||
if S.ui then
|
||||
unified_inventory.set_inventory_formspec(player, "craft")
|
||||
end
|
||||
end
|
||||
|
||||
if fields.upd then
|
||||
bdb()
|
||||
get_active()
|
||||
get_active()
|
||||
-- update preview
|
||||
minetest.show_formspec(name, "skin_db:admin",
|
||||
skin_db.formspec.admin(name))
|
||||
|
@ -613,7 +665,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
pname ~= "" and fields.private == "true" then
|
||||
update = true
|
||||
else -- not allowed
|
||||
-- switch and force update
|
||||
-- switch and set context
|
||||
context.event = "inactive"
|
||||
context.i = -1
|
||||
context.o = 1
|
||||
|
@ -624,7 +676,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
update_skin_record(skin_db.active[context.i])
|
||||
end
|
||||
elseif context.event == "inactive" then
|
||||
-- switch and force update
|
||||
-- switch and set context
|
||||
context.event = "active"
|
||||
context.i = 1
|
||||
context.o = -1
|
||||
|
@ -708,9 +760,14 @@ minetest.register_on_joinplayer(function(player)
|
|||
local name = player:get_player_name()
|
||||
local playerdata = get_player_record(name)
|
||||
|
||||
if playerdata then -- record?
|
||||
-- cache
|
||||
skin_db.skin[name] = get_skin(playerdata.skin_id)
|
||||
if playerdata then
|
||||
local skin = get_skin(playerdata.skin_id)
|
||||
if skin then
|
||||
skin_db.skin[name] = skin
|
||||
else
|
||||
skin_db.skin[name] = get_skin(1)
|
||||
update_player_record(name, 1)
|
||||
end
|
||||
else -- initialise default skin
|
||||
create_player_record(name, 1)
|
||||
skin_db.skin[name] = get_skin(1)
|
||||
|
@ -729,43 +786,6 @@ minetest.register_on_leaveplayer(function(player)
|
|||
skin_db.skin[name] = nil
|
||||
end)
|
||||
|
||||
local function bdb()
|
||||
|
||||
local tdir = S.MP.."/textures"
|
||||
local mdir = S.MP.."/meta"
|
||||
local p = ie.io.popen('ls -1v '..tdir)
|
||||
local mfn = ""
|
||||
|
||||
for file in p:lines() do
|
||||
if file:find(".png") and
|
||||
not file:find("_preview") and
|
||||
not file:find("skin_db") then
|
||||
local result = check_skins(file) -- prevent duplication
|
||||
if not result then
|
||||
mfn = file:gsub(".png", ".txt")
|
||||
local f = ie.io.open(mdir.."/"..mfn, "r")
|
||||
if f then
|
||||
local cont = {}
|
||||
for line in f:lines() do
|
||||
cont[#cont+1] = line
|
||||
end
|
||||
f:close()
|
||||
local name = cont[1]
|
||||
local author = cont[2]
|
||||
local license = cont[3]
|
||||
name = name:gsub("%(", " ")
|
||||
name = name:gsub("%)", "")
|
||||
name = name:gsub(",", "")
|
||||
create_skin_record(file, name, author, license, nil, nil, nil, nil, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
p:close()
|
||||
update_skin_active(1, 'true')
|
||||
end
|
||||
--bdb()
|
||||
|
||||
minetest.register_chatcommand("build_db", {
|
||||
description = "",
|
||||
params = "",
|
||||
|
@ -773,6 +793,6 @@ minetest.register_chatcommand("build_db", {
|
|||
func = function(name)
|
||||
minetest.chat_send_player(name, "building database...")
|
||||
bdb()
|
||||
minetest.chat_send_player(name, "db data inserted...")
|
||||
minetest.chat_send_player(name, "building db completed!")
|
||||
end,
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue