From bbbcd5f38db4cc534e11bfeae15ea502b279d731 Mon Sep 17 00:00:00 2001 From: Valansch Date: Fri, 21 Jul 2017 01:29:49 +0200 Subject: [PATCH 1/4] fixed issue where the tag window wouldn't open if a player had a custom tag. Also implemented /tag --- band.lua | 133 ++++++++++++++++---------------------------- config.lua | 4 ++ custom_commands.lua | 21 +++++++ 3 files changed, 72 insertions(+), 86 deletions(-) diff --git a/band.lua b/band.lua index 140dcdc3..c4f747f1 100644 --- a/band.lua +++ b/band.lua @@ -28,7 +28,7 @@ local expand_band_gui local local_role local function create_band_gui(event) - local player = game.players[event.player_index] + local player = game.players[event.player_index] global.band_last_change[event.player_index] = game.tick if player.gui.top.band_toggle_btn == nil then local button = player.gui.top.add { name = "band_toggle_btn", type = "sprite-button", caption = "Tag", style = "dialog_button_style" } @@ -48,17 +48,17 @@ end -- Make a list of random names with roles local function test_fake_players( t ) local limit = math.random(20,120) - + local roles_ind = {} for role in pairs(roles) do table.insert( roles_ind, role) end - + for i = 1,limit do local rolei = math.random(1, #roles_ind) local role = roles_ind[rolei] - table.insert(t[role], + table.insert(t[role], { "Fake#" .. (tostring(math.random())):sub(3,-math.random(1,10)), { @@ -79,17 +79,17 @@ local function subgui_update_role_counter(gui_parent, count_diff) local count_label if gui_parent["role_counter"] then count_label = gui_parent["role_counter"] - else + else count_label = gui_parent.add {type = "label", caption = 0, name = "role_counter", single_line = false} count_label.style.font = "default-small-bold" - count_label.style.maximal_height = 10 + count_label.style.maximal_height = 10 count_label.style.top_padding = 0 count_label.style.left_padding = 0 count_label.style.right_padding = 0 count_label.style.bottom_padding = 0 count_label.style.font_color = {r=.55,g=.55,b=.55} end - + local new_count = tonumber(count_label.caption) + count_diff count_label.caption = new_count if new_count == 0 then @@ -126,7 +126,7 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) local frame = player.gui.left["band_panel"] if (frame) then frame.destroy() - + if player.gui.center["textfield_item_icon_frame"] then player.gui.center["textfield_item_icon_frame"].destroy() end @@ -138,16 +138,15 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) end return end - - local player_role = player.tag:sub(2,-2) + local player_role = player.tag:sub(2,-2) -- Will be filled: { roleN = {{name,color},...} , ...} local players_by_role = {} - + for role in pairs(roles) do players_by_role[role] = {} end - + if right_click then for _, oplayer in pairs(game.players) do local prole = oplayer.tag:sub(2,-2) @@ -162,24 +161,24 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) else for _, oplayer in pairs(game.connected_players) do local prole = oplayer.tag:sub(2,-2) - if prole ~= "" then + if players_by_role[prole] ~= nil then table.insert( players_by_role[prole], {oplayer.name, oplayer.color}) end end end - + if dev_addfakes then test_fake_players(players_by_role) end - + player.gui.top.band_toggle_btn.tooltip = "" - + local button--reusable variable :D local frame = player.gui.left.add { type = "frame", direction = "vertical", name = "band_panel", caption = "Choose your role:"} frame.style.font = "default-listbox" frame.style.font_color = { r=0.98, g=0.66, b=0.22} - + if dev_icons then local choose local chooselist = frame.add { type = "flow", direction = "horizontal" } @@ -194,25 +193,25 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) choose.style.bottom_padding = 2 end end - + local scroll = frame.add{type = "scroll-pane", name = "scroll", horizontal_scroll_policy = "never", vertical_scroll_policy = "auto"} scroll.style.maximal_height = 600 scroll.style.minimal_width = 250 scroll.style.bottom_padding = 10 - + local table_roles = scroll.add{type = "table", name = "table_roles", colspan = 2} table_roles.style.horizontal_spacing = 15 table_roles.style.vertical_spacing = 4 - - + + local name_label local pname local pcolor - + local show_role_tooltip = math.random() > .5 - + for role, role_icons in pairs(roles) do - + local role_line = table_roles.add { type = "flow", direction = "horizontal" } button = role_line.add { type = "sprite-button", sprite = get_random_from_table(role_icons), name = "band_role_"..role, style = "recipe_slot_button_style"} @@ -223,14 +222,14 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) if show_role_tooltip and role_icons.tooltip then button.tooltip = get_random_from_table( role_icons.tooltip ) end - + local role_cap_line = role_line.add { type = "flow", name = "role_cap_line", direction = "horizontal" } role_cap_line.style.max_on_row = 1 - + local role_label = role_cap_line.add { type = "label", caption = role, single_line = true} -- role_label.style.minimal_width = 0 role_label.style.minimal_height = 0 - role_label.style.maximal_height = 12 + role_label.style.maximal_height = 12 role_label.style.top_padding = 0 role_label.style.left_padding = 0 role_label.style.right_padding = 0 @@ -239,7 +238,7 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) if role == player_role then role_label.style.font_color = {r=.7,g=1,b=.7} end - + subgui_update_role_counter(role_cap_line, #players_by_role[role]) local list_players = table_roles.add { type = "flow", direction = "horizontal" } @@ -255,7 +254,7 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) end end end - + local close_btn_flow = frame.add { type = "flow", direction = "horizontal" } button = close_btn_flow.add { type = "button", caption = "Close", name = "band_close" } button.style.font = "default-bold" @@ -265,7 +264,7 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) button.style.left_padding = 2 button.style.right_padding = 2 button.style.bottom_padding = 0 - + button = close_btn_flow.add { type = "button", caption = "Clear tag", name = "band_clear" } button.style.font = "default-bold" button.style.font_color = {r=1, g=.7, b=.7} @@ -292,7 +291,7 @@ local function print_role_change(name, role) str = str .. " (["..local_role.."] squad will miss you)." end]]-- end - + if str then str = str:gsub('%%name', name) game.print(str) @@ -304,54 +303,17 @@ local function update_player_role(player, role) global.update_player_name = player global.update_player_role_name = role - + if global.update_player_role_name then global.update_player_name.tag = "[" .. global.update_player_role_name .. "]" else global.update_player_name.tag = "" end - - -- update other player gui (counter & label) - --[[ - for _,cplayer in pairs( game.connected_players ) do - if cplayer.gui.left.band_panel then - local troles = cplayer.gui.left.band_panel.scroll.table_roles - if local_role then - local player_label = troles.children[roles[local_role].index*2]["list_players_" .. cplayer.name] - if player_label then - player_label.destroy() - end - end - - if global.update_player_role_name then - subgui_add_player_label( troles.children[roles[global.update_player_role_name].index*2], cplayer.name, cplayer.color) - subgui_update_role_counter( troles.children[roles[global.update_player_role_name].index*2 - 1].role_cap_line, 1) - end - - if local_role then - subgui_update_role_counter( troles.children[roles[local_role].index*2 - 1].role_cap_line, -1) - end - end - end - - - -- update local player gui (role label color) - local troles_local = player.gui.left.band_panel.scroll.table_roles - - if local_role then - troles_local.children[roles[local_role].index*2 - 1].children[2].children[1].style.font_color = {r=1,g=1,b=1} - end - - if global.update_player_role_name then - troles_local.children[roles[global.update_player_role_name].index*2 - 1].children[2].children[1].style.font_color = {r=.7,g=1,b=.7} - end ---]] - print_role_change(global.update_player_name.name, global.update_player_role_name) expand_band_gui(player) expand_band_gui(player) - + local_role = role end @@ -359,11 +321,11 @@ local function on_gui_click(event) if not (event and event.element and event.element.valid) then return end local player = game.players[event.element.player_index] local name = event.element.name - + if (name == "band_toggle_btn") then --player, dev_icons, dev_addfakes - expand_band_gui(player, - player.admin and event.control, + expand_band_gui(player, + player.admin and event.control, player.admin and event.alt, event.button == defines.mouse_button_type.right) end @@ -372,18 +334,17 @@ local function on_gui_click(event) expand_band_gui(player) return end - + if (name == "band_clear") then - + update_player_role(player, nil) - + player.gui.top.band_toggle_btn.caption = "Tag" player.gui.top.band_toggle_btn.tooltip = "" player.gui.top.band_toggle_btn.sprite = "" - -- expand_band_gui(player) return end - + --role button clicked if name:find("band_role_") == 1 then if not player.admin and event.tick - global.band_last_change[event.player_index] < option_band_change_interval then @@ -392,20 +353,20 @@ local function on_gui_click(event) end local _,role_ind_start = name:find("band_role_") local name_role = name:sub(role_ind_start + 1) - + if player.tag:find(name_role) then -- current tag = new tag return end - + for role, role_icons in pairs(roles) do if (name_role == role) then global.band_last_change[event.player_index] = event.tick - + player.gui.top.band_toggle_btn.caption = "" player.gui.top.band_toggle_btn.sprite = event.element.sprite --get_random_from_table(role_icons) - - update_player_role(player, role) + + update_player_role(player, role) -- expand_band_gui(player) end end @@ -424,18 +385,18 @@ local function on_gui_elem_changed(event) if event.element.elem_type and event.element.elem_value then local frame = player.gui.center.add{ type = "frame", name = "textfield_item_icon_frame", caption = "SpritePath"} frame.style.minimal_width = 310 - local textfield + local textfield -- if type(event.element.elem_value ) == 'table' then -- textfield = frame.add { name = "textfield_item_icon", type = "textfield", text = "virtual-signal/" .. event.element.elem_value.name } -- else textfield = frame.add { name = "textfield_item_icon", type = "textfield", text = event.element.elem_type .. "/" .. event.element.elem_value } -- end - + --buggy textfield.style.minimal_width = 300 end end - + end Event.register(defines.events.on_gui_elem_changed, on_gui_elem_changed) diff --git a/config.lua b/config.lua index 8a920bcb..a493bea4 100644 --- a/config.lua +++ b/config.lua @@ -12,6 +12,10 @@ Event.register(-1, function() } global.scenario.variables.regulars = { helpower2 = "", + rorror = "", + Medival3 = "", + dpoba = "", + settdigger = "" } global.scenario.variables.player_positions = {} global.scenario.variables.player_walk_distances = {} diff --git a/custom_commands.lua b/custom_commands.lua index 54e92d8b..5a31acbb 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -242,6 +242,26 @@ local function afk() end end + +local function tag(cmd) + if not game.player.admin then + cant_run(cmd.name) + return + end + local params = {} + for param in string.gmatch(cmd.parameter, "%w+") do table.insert(params, param) end + if #params ~= 2 then + game.player.print("Two arguments expect failed. Usage: Sets a players tag.") + elseif game.players[params[1]] == nil then + game.player.print("Player does not exist.") + else + game.players[params[1]].tag = "[" .. params[2] .. "]" + game.print(params[1] .. " joined [" .. params[2] .. "].") + end +end + + + commands.add_command("kill", "Will kill you.", kill) commands.add_command("detrain", " - Kicks the player off a train. (Admins and moderators)", detrain) commands.add_command("tpplayer", " - Teleports you to the player. (Admins and moderators)", teleport_player) @@ -256,3 +276,4 @@ commands.add_command("regular", ', Change regular stat commands.add_command("mods", 'Prints a list of game mods.', print_mods) commands.add_command("mod", ', Changes moderator status of a player. (Admins only)', mod) commands.add_command("afktime", 'Shows how long players have been afk.', afk) +commands.add_command("tag", ' Sets a players tag. (Admins only)', tag) From 8be41df66ca7a9dc49b023298e9a15b490bb37e3 Mon Sep 17 00:00:00 2001 From: Valansch Date: Fri, 21 Jul 2017 02:26:36 +0200 Subject: [PATCH 2/4] implemented dynamic tags and fixed market null pointer --- band.lua | 17 ++++++++- fish_market.lua | 97 +++++++++++++++++++------------------------------ 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/band.lua b/band.lua index c4f747f1..09ab6d2b 100644 --- a/band.lua +++ b/band.lua @@ -20,6 +20,7 @@ do end end +local custom_roles = {} local expand_band_gui @@ -143,10 +144,23 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) -- Will be filled: { roleN = {{name,color},...} , ...} local players_by_role = {} + for role in pairs(roles) do players_by_role[role] = {} end + for _,role in pairs(custom_roles) do + roles[role] = nil + end + for _,cplayer in pairs(game.players) do + local role = cplayer.tag:sub(2,-2) + if role ~= "" and roles[role] == nil then + players_by_role[role] = {} + table.insert(custom_roles, role) + roles[role] = {"item/iron-stick",tooltip = {"I'm sure he does something"},verbs = {"enlarged"}} + end + end + if right_click then for _, oplayer in pairs(game.players) do local prole = oplayer.tag:sub(2,-2) @@ -161,13 +175,12 @@ expand_band_gui = function(player, dev_icons, dev_addfakes, right_click) else for _, oplayer in pairs(game.connected_players) do local prole = oplayer.tag:sub(2,-2) - if players_by_role[prole] ~= nil then + if prole ~= "" then table.insert( players_by_role[prole], {oplayer.name, oplayer.color}) end end end - if dev_addfakes then test_fake_players(players_by_role) end diff --git a/fish_market.lua b/fish_market.lua index f5dc6323..c949ba91 100644 --- a/fish_market.lua +++ b/fish_market.lua @@ -287,70 +287,47 @@ if not global.pet_command_rotation then global.pet_command_rotation = 1 end local function on_tick(event) - if game.tick % 1000 == 0 then - if global.player_speed_boost_records then - for k,v in pairs(global.player_speed_boost_records) do - if game.tick - v.start_tick > 3000 then - reset_player_runningspeed(game.players[k]) - end - end - end - end - if game.tick % 200 == 0 then - for _, pets in pairs(global.player_pets) do - local player = game.players[pets.owner] - if pcall(function () local x = pets.entity.name end) then - if global.pet_command_rotation % 15 == 0 then - local surface = game.surfaces[1] - local pet_pos = pets.entity.position - local pet_name = pets.entity.name - local pet_direction = pets.entity.direction - pets.entity.destroy() - pets.entity = surface.create_entity {name=pet_name, position=pet_pos, direction=pet_direction, force="player"} - end - if global.pet_command_rotation % 2 == 1 then - pets.entity.set_command({type=defines.command.go_to_location, destination=player.position,distraction=defines.distraction.none}) - else - local fake_pos = pets.entity.position - pets.entity.set_command({type=defines.command.go_to_location, destination=fake_pos,distraction=defines.distraction.none}) - end - else - global.player_pets[pets.id] = nil - local str = player.name .. "´s pet died ;_;" - game.print(str) - end - end - global.pet_command_rotation = global.pet_command_rotation + 1 + + if game.tick % 1000 == 0 then + if global.player_speed_boost_records then + for k,v in pairs(global.player_speed_boost_records) do + if game.tick - v.start_tick > 3000 then + reset_player_runningspeed(game.players[k]) + end + end + end + end + + if global.player_pets then + for _, pets in pairs(global.player_pets) do + local player = game.players[pets.owner] + if pcall(function () local x = pets.entity.name end) then + if global.pet_command_rotation % 15 == 0 then + local surface = game.surfaces[1] + local pet_pos = pets.entity.position + local pet_name = pets.entity.name + local pet_direction = pets.entity.direction + pets.entity.destroy() + pets.entity = surface.create_entity {name=pet_name, position=pet_pos, direction=pet_direction, force="player"} + end + if global.pet_command_rotation % 2 == 1 then + pets.entity.set_command({type=defines.command.go_to_location, destination=player.position,distraction=defines.distraction.none}) + else + local fake_pos = pets.entity.position + pets.entity.set_command({type=defines.command.go_to_location, destination=fake_pos,distraction=defines.distraction.none}) + end + else + global.player_pets[pets.id] = nil + local str = player.name .. "´s pet died ;_;" + game.print(str) + end + end + global.pet_command_rotation = global.pet_command_rotation + 1 + end end end ---[[ -function help() - local infotext = global.player_pets[1].entity.help() - player = game.players[1] - player.gui.left.direction = "horizontal" - local frame = player.gui.left.add { type = "frame", name = "info_panel"} - frame.style.top_padding = 20 - frame.style.left_padding = 20 - frame.style.right_padding = 20 - frame.style.bottom_padding = 20 - local info_table = frame.add { type = "table", colspan = 1, name = "info_table" } - local headline_label = info_table.add { type = "label", name = "headline_label", caption = "redmew fishy info" } - headline_label.style.font = "default-listbox" - headline_label.style.font_color = { r=0.98, g=0.66, b=0.22} - - - local text_box = info_table.add { type = "text-box", text = infotext, name = "text_box" } - text_box.read_only = true - text_box.selectable = true - text_box.word_wrap = false - text_box.style.right_padding = 5 - text_box.style.top_padding = 5 - text_box.style.left_padding = 5 - text_box.style.bottom_padding = 5 -end ---]] Event.register(defines.events.on_preplayer_mined_item, preplayer_mined_item) Event.register(defines.events.on_entity_died, fish_drop_entity_died) From 5f96759833c092c6295795a0897752b39ed953d2 Mon Sep 17 00:00:00 2001 From: Valansch Date: Fri, 21 Jul 2017 13:05:26 +0200 Subject: [PATCH 3/4] Made player distance persistent --- walk_distance.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/walk_distance.lua b/walk_distance.lua index 5fabe042..61769dda 100644 --- a/walk_distance.lua +++ b/walk_distance.lua @@ -29,7 +29,9 @@ end local function init_player_position(event) local player = game.players[event.player_index] global.scenario.variables.player_positions[player.name] = player.position - global.scenario.variables.player_walk_distances[player.name] = 0 + if not global.scenario.variables.player_walk_distances[player.name] then + global.scenario.variables.player_walk_distances[player.name] = 0 + end end Event.register(defines.events.on_tick, on_tick) From 93d59ec14bfaed5abc71a19b915cc04ba954830b Mon Sep 17 00:00:00 2001 From: Valansch Date: Fri, 21 Jul 2017 18:43:34 +0200 Subject: [PATCH 4/4] removed all on_tick event handlers and replaced it with designated file --- control.lua | 1 + fish_market.lua | 9 +- on_tick.lua | 16 ++++ pet.lua | 58 ++++--------- player_list.lua | 32 +++---- poll.lua | 211 +++++++++++++++++++++++----------------------- walk_distance.lua | 9 +- 7 files changed, 156 insertions(+), 180 deletions(-) create mode 100644 on_tick.lua diff --git a/control.lua b/control.lua index c2e3b294..d930d818 100644 --- a/control.lua +++ b/control.lua @@ -16,6 +16,7 @@ require "map_layout" require "custom_commands" require "nuke_control" require "walk_distance" +require "on_tick" diff --git a/fish_market.lua b/fish_market.lua index c949ba91..21b5113c 100644 --- a/fish_market.lua +++ b/fish_market.lua @@ -285,11 +285,11 @@ end if not global.pet_command_rotation then global.pet_command_rotation = 1 end -local function on_tick(event) +function fish_market_on_180_ticks() - if game.tick % 200 == 0 then - - if game.tick % 1000 == 0 then + if game.tick % 180 == 0 then + + if game.tick % 900 == 0 then if global.player_speed_boost_records then for k,v in pairs(global.player_speed_boost_records) do if game.tick - v.start_tick > 3000 then @@ -332,4 +332,3 @@ end Event.register(defines.events.on_preplayer_mined_item, preplayer_mined_item) Event.register(defines.events.on_entity_died, fish_drop_entity_died) Event.register(defines.events.on_market_item_purchased, market_item_purchased) -Event.register(defines.events.on_tick, on_tick) diff --git a/on_tick.lua b/on_tick.lua new file mode 100644 index 00000000..403ea38e --- /dev/null +++ b/on_tick.lua @@ -0,0 +1,16 @@ +local function on_tick() + if game.tick % 60 == 0 then + poll_on_second() + walk_distance_on_second() + if game.tick % 120 == 0 then + pet_on_120_ticks() + if game.tick % 1200 == 0 then + player_list_on_12_seconds() + end + elseif game.tick % 180 == 0 then + fish_market_on_180_ticks() + end + end +end + +Event.register(defines.events.on_tick, on_tick) diff --git a/pet.lua b/pet.lua index 7a729575..a4b35238 100644 --- a/pet.lua +++ b/pet.lua @@ -1,8 +1,8 @@ --[[local function on_player_joined_game(event) local player = game.players[event.player_index] - + if player.gui.top.pet_button == nil then - local button = player.gui.top.add({ type = "sprite-button", name = "pet_button", sprite = "entity/small-biter" }) + local button = player.gui.top.add({ type = "sprite-button", name = "pet_button", sprite = "entity/small-biter" }) button.style.minimal_height = 38 button.style.minimal_width = 38 button.style.top_padding = 0 @@ -13,8 +13,8 @@ end local function show_pet_panel(player) - local frame = player.gui.left.add { type = "frame", name = "pet-panel", direction = "vertical" } - + local frame = player.gui.left.add { type = "frame", name = "pet-panel", direction = "vertical" } + pet_table = frame.add { type = "table", name = "pet_panel_table", colspan = 2 } pet_table.add({ type = "sprite-button", name = "pet_button", sprite = "entity/small-biter" }) end @@ -25,19 +25,19 @@ function pet(player, entity_name) else player = game.players[player] end - if not entity_name then + if not entity_name then entity_name = "small-biter" end if not global.player_pets then global.player_pets = {} end - + local surface = game.surfaces[1] - + local pos = player.position pos.y = pos.y - 2 - + local x = 1 x = x + #global.player_pets - + global.player_pets[x] = {} global.player_pets[x].entity = surface.create_entity {name=entity_name, position=pos, force="player"} global.player_pets[x].owner = player.index @@ -45,44 +45,18 @@ function pet(player, entity_name) end -local function on_tick() - if game.tick % 120 == 0 then - for _, pets in pairs(global.player_pets) do - local player = game.players[pets.owner] - if pcall(function () local x = pets.entity.name end) then - pets.entity.set_command({type=defines.command.go_to_location, destination=player.position,distraction=defines.distraction.none}) - else - global.player_pets[pets.id] = nil - local str = player.name .. "´s pet died ;_;" - game.print(str) --- game.print(pets.id) - end - end - end -end ---[[ -local function try() - local x = global.player_pets[1].entity.name -end - -function test() +function pet_on_120_ticks() for _, pets in pairs(global.player_pets) do - local str = " ID=" - str = str .. pets.id - if pcall(function () local x = global.player_pets[pets.id].entity.name end) then - str = str .. pets.entity.name + local player = game.players[pets.owner] + if pcall(function () local x = pets.entity.name end) then + pets.entity.set_command({type=defines.command.go_to_location, destination=player.position,distraction=defines.distraction.none}) else - str = str .. "entity.. HAS.... NOOO... NAAAAAAMEEE" + global.player_pets[pets.id] = nil + local str = player.name .. "´s pet died ;_;" + game.print(str) end - - str = str .. " ownerID=" - str = str .. pets.owner - - game.print(str) end end -]]-- Event.register(defines.events.on_gui_click, on_gui_click) Event.register(defines.events.on_player_joined_game, on_player_joined_game) -Event.register(defines.events.on_tick, on_tick) \ No newline at end of file diff --git a/player_list.lua b/player_list.lua index 71f366b9..9ffacac4 100644 --- a/player_list.lua +++ b/player_list.lua @@ -351,26 +351,22 @@ local function on_gui_click(event) end - -local function on_tick() - if game.tick % 1200 == 0 then - for _,player in pairs(game.connected_players) do - if player.gui.left["player-list-panel"] then - local sort_method - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_2.caption, symbol_desc) then sort_method = "name_desc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_2.caption, symbol_asc) then sort_method = "name_asc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_3.caption, symbol_desc) then sort_method = "time_played_desc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_3.caption, symbol_asc) then sort_method = "time_played_asc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_4.caption, symbol_desc) then sort_method = "distance_desc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_4.caption, symbol_asc) then sort_method = "distance_asc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_5.caption, symbol_desc) then sort_method = "pokes_desc" end - if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_5.caption, symbol_asc) then sort_method = "pokes_asc" end - player.gui.left["player-list-panel"].destroy() - player_list_show(player,sort_method) - end +function player_list_on_12_seconds() + for _,player in pairs(game.connected_players) do + if player.gui.left["player-list-panel"] then + local sort_method + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_2.caption, symbol_desc) then sort_method = "name_desc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_2.caption, symbol_asc) then sort_method = "name_asc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_3.caption, symbol_desc) then sort_method = "time_played_desc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_3.caption, symbol_asc) then sort_method = "time_played_asc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_4.caption, symbol_desc) then sort_method = "distance_desc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_4.caption, symbol_asc) then sort_method = "distance_asc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_5.caption, symbol_desc) then sort_method = "pokes_desc" end + if string.find(player.gui.left["player-list-panel"].player_list_panel_header_table.player_list_panel_header_5.caption, symbol_asc) then sort_method = "pokes_asc" end + player.gui.left["player-list-panel"].destroy() + player_list_show(player,sort_method) end end end -Event.register(defines.events.on_tick, on_tick) Event.register(defines.events.on_player_joined_game, on_player_joined_game) Event.register(defines.events.on_gui_click, on_gui_click) diff --git a/poll.lua b/poll.lua index 29ba1865..ba0eb9b4 100644 --- a/poll.lua +++ b/poll.lua @@ -1,11 +1,11 @@ ---------------------------------------------------------------------------------------------------------------------------------------- --- Create Polls for your Factory Workers --- by MewMew -- with some help from RedLabel, Klonan, Morcup, BrainClot +-- Create Polls for your Factory Workers +-- by MewMew -- with some help from RedLabel, Klonan, Morcup, BrainClot ---------------------------------------------------------------------------------------------------------------------------------------- local function create_poll_gui(event) local player = game.players[event.player_index] - + if player.gui.top.poll == nil then local button = player.gui.top.add { name = "poll", type = "sprite-button", sprite = "item/programmable-speaker" } button.style.font = "default-bold" @@ -19,32 +19,32 @@ local function create_poll_gui(event) end local function poll_show(player) - - player.gui.left.direction = "horizontal" - local frame = player.gui.left.add { type = "frame", name = "poll-panel", direction = "vertical" } + + player.gui.left.direction = "horizontal" + local frame = player.gui.left.add { type = "frame", name = "poll-panel", direction = "vertical" } frame.add { type = "table", name = "poll_panel_table", colspan = 2 } - + local poll_panel_table = frame.poll_panel_table - + if not (global.poll_question == "") then - + local str = "Poll #" .. global.score_total_polls_created .. ":" if global.score_total_polls_created > 1 then local x = game.tick x = ((x / 60) / 60) / 60 - x = global.score_total_polls_created / x + x = global.score_total_polls_created / x x = round(x, 0) str = str .. " (Polls/hour: " str = str .. x str = str .. ")" end - + poll_panel_table.add { type = "label", caption = str, single_line = false, name = "poll_number_label" } poll_panel_table.poll_number_label.style.font_color = { r=0.75, g=0.75, b=0.75} - poll_panel_table.add { type = "label"} + poll_panel_table.add { type = "label"} --poll_panel_table.add { caption = "----------------------------", type = "label" } - --poll_panel_table.add { type = "label" } + --poll_panel_table.add { type = "label" } poll_panel_table.add { type = "label", caption = global.poll_question, single_line = false, name = "question_label" } poll_panel_table.question_label.style.maximal_width = 208 poll_panel_table.question_label.style.maximal_height = 170 @@ -52,61 +52,61 @@ local function poll_show(player) poll_panel_table.question_label.style.font_color = { r=0.98, g=0.66, b=0.22} poll_panel_table.add { type = "label" } end - + local y = 1 while (y < 4) do - if not (global.poll_answers[y] == "") then - + if not (global.poll_answers[y] == "") then + local z = tostring(y) - + poll_panel_table.add { type = "label", caption = global.poll_answers[y], single_line = false, name = "answer_label_" .. z } local answer_label = poll_panel_table["answer_label_" .. z] answer_label.style.maximal_width = 208 answer_label.style.minimal_width = 208 answer_label.style.maximal_height = 165 answer_label.style.font = "default" - - local answerbutton = poll_panel_table.add { type = "button", caption = global.poll_button_votes[y], name = "answer_button_" .. z } + + local answerbutton = poll_panel_table.add { type = "button", caption = global.poll_button_votes[y], name = "answer_button_" .. z } answerbutton.style.font = "default-listbox" end y = y + 1 end - + frame.add { type = "table", name = "poll_panel_button_table", colspan = 3 } local poll_panel_button_table = frame.poll_panel_button_table poll_panel_button_table.add { type = "button", caption = "New Poll", name = "new_poll_assembler_button" } - - + + global.poll_panel_creation_time[player.index] = game.tick - + local str = "Hide (" .. global.poll_duration_in_seconds str = str .. ")" - - + + poll_panel_button_table.add { type = "button", caption = str, name = "poll_hide_button" } - - poll_panel_button_table.poll_hide_button.style.minimal_width = 70 + + poll_panel_button_table.poll_hide_button.style.minimal_width = 70 poll_panel_button_table.new_poll_assembler_button.style.font = "default-bold" poll_panel_button_table.new_poll_assembler_button.style.minimal_height = 38 - poll_panel_button_table.poll_hide_button.style.font = "default-bold" - poll_panel_button_table.poll_hide_button.style.minimal_height = 38 - poll_panel_button_table.add { type = "checkbox", caption = "Show Polls", state = global.autoshow_polls_for_player[player.name], name = "auto_show_polls_checkbox" } + poll_panel_button_table.poll_hide_button.style.font = "default-bold" + poll_panel_button_table.poll_hide_button.style.minimal_height = 38 + poll_panel_button_table.add { type = "checkbox", caption = "Show Polls", state = global.autoshow_polls_for_player[player.name], name = "auto_show_polls_checkbox" } end local function poll(player) - + local frame = player.gui.left["poll-assembler"] frame = frame.table_poll_assembler - + global.poll_question = "" global.poll_question = frame.textfield_question.text if (global.poll_question == "") then return end - - + + global.poll_answers = {"","",""} global.poll_answers[1] = frame.textfield_answer_1.text global.poll_answers[2] = frame.textfield_answer_2.text @@ -114,79 +114,79 @@ local function poll(player) if (global.poll_answers[3] .. global.poll_answers[2] .. global.poll_answers[1] == "") then return end - + local msg = player.name msg = msg .. " has created a new Poll!" - + global.score_total_polls_created = global.score_total_polls_created + 1 - + local frame = player.gui.left["poll-assembler"] frame.destroy() - - global.poll_voted = nil + + global.poll_voted = nil global.poll_voted = {} global.poll_button_votes = {0,0,0} - + local x = 1 - + while (game.players[x] ~= nil) do - + local player = game.players[x] - - local frame = player.gui.left["poll-panel"] - + + local frame = player.gui.left["poll-panel"] + if (frame) then frame.destroy() end - + if (global.autoshow_polls_for_player[player.name] == true) then poll_show(player) end - + player.print(msg) - + x = x + 1 end - - + + --------------------- -- data for score.lua --------------------- --global.score_total_polls_created = global.score_total_polls_created + 1 --refresh_score() - + end local function poll_refresh() - + local x = 1 - + while (game.players[x] ~= nil) do - + local player = game.players[x] - - if (player.gui.left["poll-panel"]) then + + if (player.gui.left["poll-panel"]) then local frame = player.gui.left["poll-panel"] frame = frame.poll_panel_table - - if not (frame.answer_button_1 == nil) then + + if not (frame.answer_button_1 == nil) then frame.answer_button_1.caption = global.poll_button_votes[1] end - if not (frame.answer_button_2 == nil) then + if not (frame.answer_button_2 == nil) then frame.answer_button_2.caption = global.poll_button_votes[2] end - if not (frame.answer_button_3 == nil) then + if not (frame.answer_button_3 == nil) then frame.answer_button_3.caption = global.poll_button_votes[3] - end + end end x = x + 1 end - + end -local function poll_assembler(player) - local frame = player.gui.left.add { type = "frame", name = "poll-assembler", caption = "" } +local function poll_assembler(player) + local frame = player.gui.left.add { type = "frame", name = "poll-assembler", caption = "" } local frame_table = frame.add { type = "table", name = "table_poll_assembler", colspan = 2 } frame_table.add { type = "label", caption = "Question:" } frame_table.add { type = "textfield", name = "textfield_question", text = "" } @@ -212,25 +212,25 @@ function poll_sync_for_new_joining_player(event) if not global.poll_duration_in_seconds then global.poll_duration_in_seconds = 99 end if not global.poll_panel_creation_time then global.poll_panel_creation_time = {} end if not global.score_total_polls_created then global.score_total_polls_created = 0 end - - local player = game.players[event.player_index] - + + local player = game.players[event.player_index] + global.autoshow_polls_for_player[player.name] = true - - local frame = player.gui.left["poll-panel"] + + local frame = player.gui.left["poll-panel"] if (frame == nil) then if not (global.poll_question == "") then poll_show(player) end - end - + end + end local function on_gui_click(event) if not (event and event.element and event.element.valid) then return end local player = game.players[event.element.player_index] local name = event.element.name - + if (name == "poll") then local frame = player.gui.left["poll-panel"] if (frame) then @@ -238,13 +238,13 @@ local function on_gui_click(event) else poll_show(player) end - + local frame = player.gui.left["poll-assembler"] if (frame) then frame.destroy() end end - + if (name == "new_poll_assembler_button") then local frame = player.gui.left["poll-assembler"] if (frame) then @@ -253,11 +253,11 @@ local function on_gui_click(event) poll_assembler(player) end end - - if (name == "create_new_poll_button") then + + if (name == "create_new_poll_button") then poll(player) end - + if (name == "poll_hide_button") then local frame = player.gui.left["poll-panel"] if (frame) then @@ -268,58 +268,55 @@ local function on_gui_click(event) frame.destroy() end end - + if (name == "auto_show_polls_checkbox") then - global.autoshow_polls_for_player[player.name] = event.element.state - end - - if global.poll_voted[event.player_index] == nil then - + global.autoshow_polls_for_player[player.name] = event.element.state + end + + if global.poll_voted[event.player_index] == nil then + if(name == "answer_button_1") then global.poll_button_votes[1] = global.poll_button_votes[1] + 1 global.poll_voted[event.player_index] = player.name poll_refresh() end - + if(name == "answer_button_2") then global.poll_button_votes[2] = global.poll_button_votes[2] + 1 global.poll_voted[event.player_index] = player.name poll_refresh() end - + if(name == "answer_button_3") then global.poll_button_votes[3] = global.poll_button_votes[3] + 1 global.poll_voted[event.player_index] = player.name poll_refresh() end - - end + + end end -local function on_tick() - if game.tick % 60 == 0 then - for _, player in pairs(game.connected_players) do - if global.poll_panel_creation_time[player.index] then - local frame = player.gui.left["poll-panel"] - if frame then - local y = (game.tick - global.poll_panel_creation_time[player.index]) / 60 - local y = global.poll_duration_in_seconds - y - y = round(y, 0) - if y <= 0 then - frame.destroy() - global.poll_panel_creation_time[player.index] = nil - else - y = "Hide (" .. y - y = y .. ")" - frame.poll_panel_button_table.poll_hide_button.caption = y - end +function poll_on_second() + for _, player in pairs(game.connected_players) do + if global.poll_panel_creation_time[player.index] then + local frame = player.gui.left["poll-panel"] + if frame then + local y = (game.tick - global.poll_panel_creation_time[player.index]) / 60 + local y = global.poll_duration_in_seconds - y + y = round(y, 0) + if y <= 0 then + frame.destroy() + global.poll_panel_creation_time[player.index] = nil + else + y = "Hide (" .. y + y = y .. ")" + frame.poll_panel_button_table.poll_hide_button.caption = y end end end - end + end end -Event.register(defines.events.on_tick, on_tick) Event.register(defines.events.on_gui_click, on_gui_click) Event.register(defines.events.on_player_joined_game, create_poll_gui) -Event.register(defines.events.on_player_joined_game, poll_sync_for_new_joining_player) \ No newline at end of file +Event.register(defines.events.on_player_joined_game, poll_sync_for_new_joining_player) diff --git a/walk_distance.lua b/walk_distance.lua index 61769dda..b001f39b 100644 --- a/walk_distance.lua +++ b/walk_distance.lua @@ -1,10 +1,4 @@ -local function on_tick() - if game.tick % 60 == 0 then - on_second() - end -end - -function on_second() +function walk_distance_on_second() local last_positions = global.scenario.variables.player_positions local d_x = 0 local d_y = 0 @@ -34,5 +28,4 @@ local function init_player_position(event) end end -Event.register(defines.events.on_tick, on_tick) Event.register(defines.events.on_player_joined_game, init_player_position)