1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2025-01-23 16:30:19 +01:00

improved mod news, news becoming too long, the display of formspec was long, there are pages now.

(may be replace buttons to the center?)
This commit is contained in:
crabman77 2015-06-30 23:00:29 +02:00
parent 16c5c0a61f
commit 23f95ed419

View File

@ -1,51 +1,107 @@
local news = {} local news = {}
local caracteres_max = 5000
local pages_players = {}
local path = minetest.get_worldpath() function load_news()
local newsfile = io.open(minetest.get_worldpath().."/news.txt","r")
local content
if newsfile ~= nil then
content = newsfile:read("*a")
newsfile:close()
end
local function formspec(player,article) if content == nil or string.len(content) == 0 then
return
end
local nb = 1
news[nb] = ""
local commit = string.split(content, "\n\n")
--if ( article == "" or article == nil ) then for i,line in ipairs(commit) do
article = "news.txt" -- vu qu'il s'en sert pas if string.len(news[nb]) < caracteres_max then
--else news[nb] = news[nb]..minetest.formspec_escape(line.."\n\n\n")
-- article = "news_"..article..".txt" else
--end nb = nb + 1
news[nb] = minetest.formspec_escape(line.."\n\n\n")
end
end
end
local newsfile = io.open(path.."/"..article,"r") load_news()
local function show_formspec(player, page)
local name = player:get_player_name()
if not name then return end
local nb_pages = #news
local formspec = "size[12,10;]" local formspec = "size[12,10;]"
formspec = formspec.."background[-0.22,-0.25;13,11;background.jpg]" formspec = formspec.."background[-0.22,-0.25;13,11;background.jpg]"
if newsfile ~= nil then if page > nb_pages then
local newscontent = newsfile:read("*a") page = nb_pages
formspec = formspec.."textarea[.50,.50;12,10;news;;"..minetest.formspec_escape(newscontent).."]" elseif page < 1 then
page = 1
end
pages_players[name] = page
if news[page] ~= nil then
formspec = formspec.."textarea[.50,.50;12,10;news;;"..news[page].."]"
else else
formspec = formspec.."label[.50,.50;Pas d'article pour le moment]" formspec = formspec.."label[.50,.50;Pas d'article pour le moment]"
end end
formspec = formspec.."button_exit[5,9.25;2,1;exit;Fermer"
if ( newsfile ~= nil ) then formspec = formspec.."button_exit[2,9.25;2,1;exit;Fermer]"..
newsfile:close() "button[6,9.25;1,1;page;<<]"..
end "button[7,9.25;1,1;page;<]"..
return formspec "button[8,9.25;1,1;none;"..page.."/"..nb_pages.."]"..
"button[9,9.25;1,1;page;>]"..
"button[10,9.25;1,1;page;>>]"
minetest.show_formspec(name,"news:news",formspec)
--minetest.log('action','Showing formspec to '..name.."page "..page)
end end
local function show_formspec(player)
local name = player:get_player_name() minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.show_formspec(name,"news",formspec(player)) if formname == "news:news" then
minetest.log('action','Showing formspec to '..name) local name = player:get_player_name()
end if not name then return end
local page = (pages_players[name] or 1)
if fields["quit"] then
pages_players[name] = nil
return
elseif fields["page"] == "<<" then
page = page - 2
elseif fields["page"] == "<" then
page = page - 1
elseif fields["page"] == ">" then
page = page + 1
elseif fields["page"] == ">>" then
page = page + 2
end
show_formspec(player,page)
end
end)
minetest.register_chatcommand("news",{ minetest.register_chatcommand("news",{
params = "<article>", params = "<page>",
description="Montre les news du serveur", description="Montre les news du serveur",
func = function (name,params) func = function (name,params)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
minetest.show_formspec(name,"news",formspec(player,params)) local page
if tonumber(params) then
page = tonumber(params)
else
page = 1
end
show_formspec(player, page)
end, end,
}) })
minetest.register_on_joinplayer(function (player) minetest.register_on_joinplayer(function (player)
if minetest.get_player_privs(player:get_player_name()).interact == true then if minetest.get_player_privs(player:get_player_name()).interact == true then
minetest.after(6,show_formspec,player) minetest.after(6,show_formspec,player, 1)
end end
end) end)