aboutsummaryrefslogtreecommitdiff
path: root/gui.lua
blob: 6b60ed3a91d6ba254a1b4a47055f787b3fa0358d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

local FORMNAME = "xban2:main"

local states = { }

local table_insert, table_concat =
      table.insert, table.concat

local ESC = minetest.formspec_escape

local function make_fs(name)
	local state = states[name]
	if not state then return end
	local list, index, filter = state.list, state.index, state.filter
	if index > #list then
		index = #list
	end
	local fs = {
		"size[10,8]",
		"label[0.5,0.6;Filter]",
		"field[1.5,0.5;6,2;filter;;"..ESC(filter).."]",
		"button[7.5,0.5;2,1;search;Search]",
	}
	table_insert(fs,
			("textlist[0.5,2;3,5.5;player;%s;%d;0]"):
			format(table_concat(list, ","), index))
	local record_name = list[index]
	if record_name then
		local record, err = xban.get_record(record_name)
		if record then
			local reclist = { }
			for _, r in ipairs(record) do
				table_insert(reclist, ESC(r))
			end
			table_insert(fs,
					("textlist[4,2;5,5.5;entry;%s;0;0]"):
					format(table_concat(reclist, ",")))
		else
			table_insert(fs,
					"textlist[4,2;5,5.5;entry;"..ESC(err)..";0]")
		end
	end
	fs = table_concat(fs)
	print(fs)
	return fs
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname ~= FORMNAME then return end
	local name = player:get_player_name()
	local state = states[name]
	if fields.player then
		local t = minetest.explode_textlist_event(fields.player)
		if (t.type == "CHG") or (t.type == "DCL") then
			state.index = t.index
			minetest.show_formspec(name, FORMNAME, make_fs(name))
		end
		return
	end
	if fields.search then
		local filter = fields.filter or ""
		state.filter = filter
		local list = { }
		state.list = list
		for k in pairs(minetest.auth_table) do
			if k:find(filter, 1, true) then
				table_insert(list, k)
			end
		end
		table.sort(list)
		minetest.show_formspec(name, FORMNAME, make_fs(name))
	end
end)

minetest.register_chatcommand("xban_gui", {
	description = "Show XBan GUI",
	params = "",
	func = function(name, params)
		local state = states[name]
		if not state then
			state = { index=1, filter="" }
			states[name] = state
			local list = { }
			state.list = list
			for k in pairs(minetest.auth_table) do
				table_insert(list, k)
			end
			table.sort(list)
		end
		minetest.show_formspec(name, FORMNAME, make_fs(name))
	end,
})