]> git.lizzy.rs Git - playerlist.git/blob - init.lua
Globalize playerlist table
[playerlist.git] / init.lua
1 playerlist = {
2         huds = {}
3 }
4
5 function playerlist.iterator()
6         return ipairs(minetest.get_connected_players())
7 end
8
9 function playerlist.count()
10         return #minetest.get_connected_players()
11 end
12
13 controls.register_on_press(function(user, key)
14         if key == "sneak" then
15                 local user_name = user:get_player_name()
16                 local huds = {user:hud_add({
17                         hud_elem_type = "image",
18                         position = {x = 0.5, y = 0},
19                         offset = {x = 0, y = 20},
20                         text = "playerlist_background.png",
21                         alignment = {x = 0, y = 1},
22                         scale = {x = 400, y = playerlist.count() * 18 + 8},
23                         number = 0xFFFFFF,
24                 })}
25                 for i, player, color, text in playerlist.iterator() do
26                         local name = player:get_player_name()
27                         local ping = math.max(1, math.ceil(4 - minetest.get_player_information(name).avg_rtt * 50))                     
28                         table.insert(huds, user:hud_add({
29                                 hud_elem_type = "text",
30                                 position = {x = 0.5, y = 0},
31                                 offset = {x = 0, y = 23 + (i - 1) * 18},
32                                 text = text or name,
33                                 alignment = {x = 0, y = 1},
34                                 scale = {x = 100, y = 100},
35                                 number = color or 0xFFFFFF,
36                         }))
37                         table.insert(huds, user:hud_add({
38                                 hud_elem_type = "image",
39                                 position = {x = 0.5, y = 0},
40                                 offset = {x = -195, y = 20 + (i - 1) * 18},
41                                 text = "server_ping_" .. ping .. ".png",
42                                 alignment = {x = 1, y = 1},
43                                 scale = {x = 1.5, y = 1.5},
44                                 number = 0xFFFFFF,
45                         }))
46                 end
47                 playerlist.huds[user_name] = huds
48         end
49 end)
50
51 controls.register_on_release(function(user, key)
52         if key == "sneak" and user then
53                 for _, id in pairs(playerlist.huds[user:get_player_name()]) do
54                         user:hud_remove(id)
55                 end
56         end
57 end)
58