From 23f95ed41964dff94fb8b4ccb00cd250f63166be Mon Sep 17 00:00:00 2001 From: crabman77 Date: Tue, 30 Jun 2015 23:00:29 +0200 Subject: [PATCH] improved mod news, news becoming too long, the display of formspec was long, there are pages now. (may be replace buttons to the center?) --- mods/news/init.lua | 104 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 24 deletions(-) diff --git a/mods/news/init.lua b/mods/news/init.lua index ab9709d9..3c1a67dd 100755 --- a/mods/news/init.lua +++ b/mods/news/init.lua @@ -1,51 +1,107 @@ 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 - article = "news.txt" -- vu qu'il s'en sert pas - --else - -- article = "news_"..article..".txt" - --end + for i,line in ipairs(commit) do + if string.len(news[nb]) < caracteres_max then + news[nb] = news[nb]..minetest.formspec_escape(line.."\n\n\n") + else + 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;]" formspec = formspec.."background[-0.22,-0.25;13,11;background.jpg]" - if newsfile ~= nil then - local newscontent = newsfile:read("*a") - formspec = formspec.."textarea[.50,.50;12,10;news;;"..minetest.formspec_escape(newscontent).."]" + if page > nb_pages then + page = nb_pages + 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 formspec = formspec.."label[.50,.50;Pas d'article pour le moment]" end - formspec = formspec.."button_exit[5,9.25;2,1;exit;Fermer" - if ( newsfile ~= nil ) then - newsfile:close() - end - return formspec + + formspec = formspec.."button_exit[2,9.25;2,1;exit;Fermer]".. + "button[6,9.25;1,1;page;<<]".. + "button[7,9.25;1,1;page;<]".. + "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 -local function show_formspec(player) - local name = player:get_player_name() - minetest.show_formspec(name,"news",formspec(player)) - minetest.log('action','Showing formspec to '..name) -end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "news:news" then + local name = player:get_player_name() + 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",{ - params = "
", + params = "", description="Montre les news du serveur", func = function (name,params) 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, }) + minetest.register_on_joinplayer(function (player) 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)