|
| 1 | +local Gui = require 'utils.gui' |
| 2 | +local Rank = require 'features.rank_system' |
| 3 | +local ENTITIES = prototypes.entity |
| 4 | +local ITEMS = prototypes.item |
| 5 | +local TILES = prototypes.tile |
| 6 | + |
| 7 | +local Public = { |
| 8 | + main_frame_name = Gui.uid_name(), |
| 9 | + close_button_name = Gui.uid_name(), |
| 10 | +} |
| 11 | + |
| 12 | +local function get_sprite_from_name(name) |
| 13 | + if ITEMS[name] then |
| 14 | + return 'item.'..name |
| 15 | + elseif TILES[name] then |
| 16 | + return 'tile.'..name |
| 17 | + elseif ENTITIES[name] then |
| 18 | + return 'entity.'..name |
| 19 | + end |
| 20 | + return 'virtual-signal.signal-deny' |
| 21 | +end |
| 22 | + |
| 23 | +local function get_tooltip_from_name(name) |
| 24 | + if ITEMS[name] then |
| 25 | + return ITEMS[name].localised_name |
| 26 | + elseif TILES[name] then |
| 27 | + return TILES[name].localised_name |
| 28 | + elseif ENTITIES[name] then |
| 29 | + return ENTITIES[name].localised_name |
| 30 | + end |
| 31 | + return name |
| 32 | +end |
| 33 | + |
| 34 | +Public.get_player_inventory = function(player) |
| 35 | + local results = {} |
| 36 | + |
| 37 | + for k = 1, player.get_max_inventory_index() do |
| 38 | + local inventory = player.get_inventory(k) |
| 39 | + if (inventory and inventory.valid) then |
| 40 | + for _, stack in pairs(inventory.get_contents()) do |
| 41 | + results[stack.name] = (results[stack.name] or 0) + stack.count |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + return results |
| 47 | +end |
| 48 | + |
| 49 | +Public.get_main_frame = function(player, selected) |
| 50 | + local frame = player.gui.screen[Public.main_frame_name] |
| 51 | + if frame and frame.valid then |
| 52 | + Gui.destroy(frame) |
| 53 | + end |
| 54 | + |
| 55 | + frame = player.gui.screen.add { |
| 56 | + type = 'frame', |
| 57 | + name = Public.main_frame_name, |
| 58 | + direction = 'vertical', |
| 59 | + } |
| 60 | + Gui.set_style(frame, { maximal_height = 930 }) |
| 61 | + |
| 62 | + do -- Header |
| 63 | + local header = frame.add { type = 'flow', direction = 'horizontal' } |
| 64 | + Gui.set_style(header, { horizontal_spacing = 8, vertical_align = 'center', bottom_padding = 4 }) |
| 65 | + |
| 66 | + local label = header.add { type = 'label', caption = 'Player inventory', style = 'frame_title' } |
| 67 | + label.drag_target = frame |
| 68 | + |
| 69 | + local dragger = header.add { type = 'empty-widget', style = 'draggable_space_header' } |
| 70 | + dragger.drag_target = frame |
| 71 | + Gui.set_style(dragger, { height = 24, horizontally_stretchable = true }) |
| 72 | + |
| 73 | + local button = header.add { |
| 74 | + type = 'sprite-button', |
| 75 | + name = Public.close_button_name, |
| 76 | + sprite = 'utility/close', |
| 77 | + clicked_sprite = 'utility/close_black', |
| 78 | + style = 'close_button', |
| 79 | + tooltip = {'gui.close-instruction'} |
| 80 | + } |
| 81 | + Gui.set_data(button, frame) |
| 82 | + end |
| 83 | + |
| 84 | + do -- Content |
| 85 | + local inner = frame.add { type = 'frame', style = 'inside_deep_frame', direction = 'vertical' } |
| 86 | + local online = inner |
| 87 | + .add { type = 'frame', style = 'subheader_frame'} |
| 88 | + .add { type = 'label', caption = ('Online: %d/%d'):format(#game.connected_players, #game.players), style = 'subheader_label' } |
| 89 | + Gui.set_style(online.parent, { horizontally_stretchable = true }) |
| 90 | + |
| 91 | + local grid = inner |
| 92 | + .add { type = 'scroll-pane', style = 'naked_scroll_pane' } |
| 93 | + .add { type = 'table', style = 'table_with_selection', column_count = 4 } |
| 94 | + |
| 95 | + local function make_rank(name) |
| 96 | + local label = grid.add { type = 'label', caption = Rank.get_player_rank_name(name) } |
| 97 | + Gui.set_style(label, { font_color = Rank.get_player_rank_color(name) }) |
| 98 | + end |
| 99 | + |
| 100 | + local function make_status(target) |
| 101 | + grid.add { |
| 102 | + type = 'label', |
| 103 | + caption = target.connected and 'Online' or 'Offline', |
| 104 | + style = target.connected and 'bold_green_label' or 'bold_red_label', |
| 105 | + } |
| 106 | + end |
| 107 | + |
| 108 | + local function make_inventory(target) |
| 109 | + local list = grid |
| 110 | + .add { type = 'scroll-pane', style = 'naked_scroll_pane' } |
| 111 | + .add { type = 'table', column_count = 10, style = 'filter_slot_table' } |
| 112 | + list.parent.horizontal_scroll_policy = 'never' |
| 113 | + |
| 114 | + for name, count in pairs(Public.get_player_inventory(target)) do |
| 115 | + list.add{ |
| 116 | + type = 'sprite-button', |
| 117 | + style = 'slot_button', |
| 118 | + sprite = get_sprite_from_name(name), |
| 119 | + tooltip = get_tooltip_from_name(name), |
| 120 | + number = count, |
| 121 | + } |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + grid.add { type = 'label', caption = 'Name' } |
| 126 | + grid.add { type = 'label', caption = 'Rank' } |
| 127 | + grid.add { type = 'label', caption = 'Status' } |
| 128 | + grid.add { type = 'label', caption = 'Inventory' } |
| 129 | + |
| 130 | + local player_list = selected and { selected } or game.players |
| 131 | + for _, p in pairs(player_list) do |
| 132 | + grid.add { type = 'label', caption = p.name } |
| 133 | + make_rank(p.name) |
| 134 | + make_status(p) |
| 135 | + make_inventory(p) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + frame.force_auto_center() |
| 140 | + player.opened = frame |
| 141 | + return frame |
| 142 | +end |
| 143 | + |
| 144 | +Gui.on_click(Public.close_button_name, function(event) |
| 145 | + Gui.destroy(Gui.get_data(event.element)) |
| 146 | +end) |
| 147 | + |
| 148 | +Gui.on_custom_close(Public.main_frame_name, function(event) |
| 149 | + Gui.destroy(event.element) |
| 150 | +end) |
| 151 | + |
| 152 | +return Public |
0 commit comments