1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Merge branch 'player-list-rewrite' of https://github.com/grilledham/RedMew

This commit is contained in:
grilledham 2018-06-30 11:00:06 +01:00
commit 72ee343b04
11 changed files with 1447 additions and 496 deletions

View File

@ -1,4 +1,4 @@
_DEBUG = false
_DEBUG = true
global.scenario = {}
global.spys = {"valansch", "air20"}

View File

@ -6,7 +6,6 @@ require 'custom_commands'
require 'base_data'
require 'train_station_names'
require 'nuke_control'
require 'walk_distance'
require 'follow'
require 'autodeconstruct'
require 'corpse_util'

View File

@ -61,9 +61,56 @@ local function teleport_location(cmd)
game.player.teleport(pos)
end
local function kill()
if game.player and game.player.character then
game.player.character.die()
local function do_fish_kill(player)
local c = player.character
if not c then
return false
end
local e = player.surface.create_entity {name = 'fish', position = player.position}
c.die(player.force, e)
return true
end
local function kill(cmd)
local player = game.player
local param = cmd.parameter
local target
if param then
target = game.players[param]
if not target then
player_print(table.concat {"Sorry, player '", param, "' was not found."})
return
end
end
if not target and player then
if not do_fish_kill(player) then
player_print("Sorry, you don't have a character to kill.")
end
elseif player then
if target == player then
if not do_fish_kill(player) then
player_print("Sorry, you don't have a character to kill.")
end
elseif target and player.admin then
if not do_fish_kill(player) then
player_print(table.concat {"'Sorry, '", target.name, "' doesn't have a character to kill."})
end
else
player_print("Sorry you don't have permission to use the kill command on other players.")
end
elseif target then
if not do_fish_kill(target) then
player_print(table.concat {"'Sorry, '", target.name, "' doesn't have a character to kill."})
end
else
if param then
player_print(table.concat {"Sorry, player '", param, "' was not found."})
else
player_print('Usage: /kill <player>')
end
end
end

View File

@ -19,6 +19,7 @@ make pet follow you moar
local Event = require 'utils.event'
local Token = require 'utils.global_token'
local Task = require 'utils.Task'
local PlayerStats = require 'player_stats'
local market_items = require 'resources.market_items'
@ -103,37 +104,26 @@ local fish_market_bonus_message = {
local total_fish_market_bonus_messages = #fish_market_bonus_message
if not global.fish_market_fish_caught then
global.fish_market_fish_caught = {}
end
if not global.fish_market_fish_spent then
global.fish_market_fish_spent = {}
end
local function fish_earned_index(player_index, amount)
local player = game.players[player_index]
player.insert {name = 'raw-fish', count = amount}
if global.fish_market_fish_caught[player_index] then
global.fish_market_fish_caught[player_index] = global.fish_market_fish_caught[player_index] + amount
else
global.fish_market_fish_caught[player_index] = amount
end
local x = global.fish_market_fish_caught[player_index] % 70
if x == 0 then
local z = math.random(1, total_fish_market_bonus_messages)
player.print(fish_market_bonus_message[z])
end
end
local function fish_earned(event, amount)
fish_earned_index(event.player_index, amount)
local player_index = event.player_index
local fish = PlayerStats.get_fish_earned(player_index)
fish = fish + amount
PlayerStats.set_fish_earned(player_index, fish)
if fish % 70 == 0 then
local player = game.players[player_index]
if player and player.valid then
local message = fish_market_bonus_message[math.random(total_fish_market_bonus_messages)]
player.print(message)
end
end
end
local function pre_player_mined_item(event)
if event.entity.type == 'simple-entity' then -- Cheap check for rock, may have other side effects
fish_earned(event, 10)
return
end
if event.entity.type == 'tree' then
@ -218,7 +208,7 @@ local function reset_player_runningspeed(player)
global.player_speed_boost_records[player.index] = nil
end
local function boost_player_runningspeed(player)
local function boost_player_runningspeed(player, market)
if global.player_speed_boost_records == nil then
global.player_speed_boost_records = {}
end
@ -242,7 +232,7 @@ local function boost_player_runningspeed(player)
game.print(string.format(boost_msg[global.player_speed_boost_records[player.index].boost_lvl], player.name))
if global.player_speed_boost_records[player.index].boost_lvl >= 4 then
reset_player_runningspeed(player)
player.character.die()
player.character.die(player.force, market)
end
end
@ -251,7 +241,7 @@ local function reset_player_miningspeed(player)
global.player_mining_boost_records[player.index] = nil
end
local function boost_player_miningspeed(player)
local function boost_player_miningspeed(player, market)
if global.player_mining_boost_records == nil then
global.player_mining_boost_records = {}
end
@ -275,7 +265,7 @@ local function boost_player_miningspeed(player)
game.print(string.format(boost_msg[global.player_mining_boost_records[player.index].boost_lvl], player.name))
if global.player_mining_boost_records[player.index].boost_lvl >= 4 then
reset_player_miningspeed(player)
player.character.die()
player.character.die(player.force, market)
end
end
@ -291,16 +281,17 @@ local function market_item_purchased(event)
-- cost
local market_item = market.get_market_items()[offer_index]
local fish_cost = market_item.price[1].amount * event.count
global.fish_market_fish_spent[player_index] = global.fish_market_fish_spent[player_index] + fish_cost
PlayerStats.change_fish_spent(player_index, fish_cost)
if event.offer_index == 1 then -- Temporary speed bonus
local player = game.players[player_index]
boost_player_runningspeed(player)
boost_player_runningspeed(player, market)
end
if event.offer_index == 2 then -- Temporary mining bonus
local player = game.players[player_index]
boost_player_miningspeed(player)
boost_player_miningspeed(player, market)
end
--[[

File diff suppressed because it is too large Load Diff

424
player_list2.lua Normal file
View File

@ -0,0 +1,424 @@
--[[
Hello there!
This will add a player list with "ranks" to your server.
Oh.. and you can also "poke" a player.
pokemessages = 80% by redlabel
To install, add: require "player_list"
to your scenario control.lua.
---MewMew---
things to do (maybe)
make it sorted by time played
--]]
local Event = require "utils.event"
local symbol_asc = ""
local symbol_desc = ""
local pokemessages = require "resources.poke_messages"
local function on_player_joined_game(event)
local player = game.players[event.player_index]
if not global.poke_spam_protection then global.poke_spam_protection = {} end
global.poke_spam_protection[event.player_index] = game.tick
if not global.player_list_pokes_counter then global.player_list_pokes_counter = {} end
if not global.scenario.variables.player_deaths[player.name] then global.scenario.variables.player_deaths[player.name] = 0 end
if not global.fish_market_fish_caught[event.player_index] then global.fish_market_fish_caught[event.player_index] = 0 end
if not global.fish_market_fish_spent[event.player_index] then global.fish_market_fish_spent[event.player_index] = 0 end
if player.gui.top.player_list_button == nil then
local button = player.gui.top.add({ type = "sprite-button", name = "player_list_button", sprite = "item/heavy-armor" })
button.style.minimal_height = 38
button.style.minimal_width = 38
button.style.top_padding = 2
button.style.left_padding = 4
button.style.right_padding = 4
button.style.bottom_padding = 2
end
end
local function get_formatted_playtime(x)
local y = x / 216000
y = tostring(y)
local h = ""
for i=1,10,1 do
local z = string.sub(y, i, i)
if z == "." then
break
else
h = h .. z
end
end
local m = x % 216000
m = m / 3600
m = math.floor(m)
m = tostring(m)
if h == "0" then
local str = m .. " minutes"
return str
else
local str = h .. " hours "
str = str .. m
str = str .. " minutes"
return str
end
end
local function get_rank(player)
local m = player.online_time / 3600
local ranks = {
"item/iron-axe","item/burner-mining-drill","item/burner-inserter","item/stone-furnace","item/light-armor","item/steam-engine",
"item/inserter", "item/transport-belt", "item/underground-belt", "item/splitter","item/assembling-machine-1","item/long-handed-inserter","item/electronic-circuit","item/electric-mining-drill",
"item/heavy-armor","item/steel-furnace","item/steel-axe","item/gun-turret","item/fast-transport-belt", "item/fast-underground-belt", "item/fast-splitter","item/assembling-machine-2","item/fast-inserter","item/radar","item/filter-inserter",
"item/defender-capsule","item/pumpjack","item/chemical-plant","item/solar-panel","item/advanced-circuit","item/modular-armor","item/accumulator", "item/construction-robot",
"item/distractor-capsule","item/stack-inserter","item/electric-furnace","item/express-transport-belt","item/express-underground-belt", "item/express-splitter","item/assembling-machine-3","item/processing-unit","item/power-armor","item/logistic-robot","item/laser-turret",
"item/stack-filter-inserter","item/destroyer-capsule","item/power-armor-mk2","item/flamethrower-turret","item/beacon",
"item/steam-turbine","item/centrifuge","item/nuclear-reactor"
}
--52 ranks
local time_needed = 15 -- in minutes between rank upgrades
m = m / time_needed
m = math.floor(m)
m = m + 1
if m > #ranks then m = #ranks end
return ranks[m]
end
local function get_sorted_list(sort_by)
local player_list = {}
for i, player in pairs(game.connected_players) do
player_list[i] = {}
player_list[i].rank = get_rank(player)
player_list[i].name = player.name
player_list[i].played_time = get_formatted_playtime(player.online_time)
player_list[i].played_ticks = player.online_time
if not global.player_list_pokes_counter[player.index] then global.player_list_pokes_counter[player.index] = 0 end
player_list[i].pokes = global.player_list_pokes_counter[player.index]
player_list[i].player_index = player.index
end
for i = #player_list, 1, -1 do
for i2 = #player_list, 1, -1 do
if sort_by == "pokes_asc" then
if player_list[i].pokes > player_list[i2].pokes then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "pokes_desc" then
if player_list[i].pokes < player_list[i2].pokes then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "time_played_asc" then
if player_list[i].played_ticks > player_list[i2].played_ticks then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "time_played_desc" then
if player_list[i].played_ticks < player_list[i2].played_ticks then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "distance_asc" then
if global.player_walk_distances[player_list[i].name] > global.player_walk_distances[player_list[i2].name] then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "distance_desc" then
if global.player_walk_distances[player_list[i].name] < global.player_walk_distances[player_list[i2].name] then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "name_asc" then
if player_list[i].name > player_list[i2].name then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
if sort_by == "name_desc" then
if player_list[i].name < player_list[i2].name then
local a = player_list[i]
local b = player_list[i2]
player_list[i] = b
player_list[i2] = a
end
end
end
end
return player_list
end
local function player_list_show(player, sort_by)
local frame = player.gui.left["player-list-panel"]
if frame then frame.destroy() end
local frame = player.gui.left.add { type = "frame", name = "player-list-panel", direction = "vertical" }
frame.style.minimal_width = 700
frame.style.top_padding = 8
frame.style.left_padding = 8
frame.style.right_padding = 8
frame.style.bottom_padding = 8
local player_list_panel_header_table = frame.add { type = "table", name = "player_list_panel_header_table", column_count = 7 }
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_1", caption = " " .. #game.connected_players }
label.style.font = "default-game"
label.style.font_color = { r=0.00, g=0.00, b=0.00}
label.style.minimal_width = 35
local str = ""
if sort_by == "name_asc" then str = symbol_asc .. " " end
if sort_by == "name_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_2", caption = str .. "Players online" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 160
label.style.maximal_width = 160
str = ""
if sort_by == "time_played_asc" then str = symbol_asc .. " " end
if sort_by == "time_played_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_3", caption = str .. "Time" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 130
label.style.maximal_width = 130
str = ""
if sort_by == "distance_asc" then str = symbol_asc .. " " end
if sort_by == "distance_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_4", caption = str .. "Walked" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 100
label.style.maximal_width = 100
str = ""
if sort_by == "fish_asc" then str = symbol_asc .. " " end
if sort_by == "fish_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_fish", caption = str .. "Fish" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 80
label.style.maximal_width = 80
str = ""
if sort_by == "deaths_asc" then str = symbol_asc .. " " end
if sort_by == "deaths_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_deaths", caption = str .. "Deaths" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 80
label.style.maximal_width = 80
str = ""
if sort_by == "pokes_asc" then str = symbol_asc .. " " end
if sort_by == "pokes_desc" then str = symbol_desc .. " " end
local label = player_list_panel_header_table.add { type = "label", name = "player_list_panel_header_5", caption = str .. "Poke" }
label.style.font_color = { r=0.98, g=0.66, b=0.22}
label.style.minimal_width = 35
local player_list_panel_table = frame.add { type = "scroll-pane", name = "scroll_pane", direction = "vertical", horizontal_scroll_policy = "never", vertical_scroll_policy = "auto"}
player_list_panel_table.style.maximal_height = 650
player_list_panel_table = player_list_panel_table.add { type = "table", name = "player_list_panel_table", column_count = 7 }
local player_list = get_sorted_list(sort_by)
for i = 1, #player_list, 1 do
local sprite = player_list_panel_table.add { type = "sprite", name = "player_rank_sprite_" .. i, sprite = player_list[i].rank }
sprite.style.minimal_width = 35
local label = player_list_panel_table.add { type = "label", name = "player_list_panel_player_names_" .. i, caption = player_list[i].name }
label.style.font = "default"
label.style.font_color = {
r = .4 + game.players[player_list[i].player_index].color.r * 0.6,
g = .4 + game.players[player_list[i].player_index].color.g * 0.6,
b = .4 + game.players[player_list[i].player_index].color.b * 0.6,
}
label.style.minimal_width = 160
label.style.maximal_width = 160
local label = player_list_panel_table.add { type = "label", name = "player_list_panel_player_time_played_" .. i, caption = player_list[i].played_time }
label.style.minimal_width = 130
label.style.maximal_width = 130
local label = player_list_panel_table.add { type = "label", name = "player_list_panel_player_distance_" .. i, caption = math.round(global.player_walk_distances[player_list[i].name]/1000, 1) .. " km" }
label.style.minimal_width = 100
label.style.maximal_width = 100
local label = player_list_panel_table.add { type = "label", name = "player_list_panel_player_fish" .. i, caption = global.fish_market_fish_caught[player_list[i].player_index] .. " / " .. global.fish_market_fish_spent[player_list[i].player_index] }
label.style.minimal_width = 80
label.style.maximal_width = 80
local label = player_list_panel_table.add { type = "label", name = "player_list_panel_player_deaths" .. i, caption = global.scenario.variables.player_deaths[player_list[i].name] }
label.style.minimal_width = 80
label.style.maximal_width = 80
local flow = player_list_panel_table.add { type = "flow", name = "button_flow_" .. i, direction = "horizontal" }
flow.add { type = "label", name = "button_spacer_" .. i, caption = "" }
local button = flow.add { type = "button", name = "poke_player_" .. player_list[i].name, caption = player_list[i].pokes }
button.style.font = "default"
label.style.font_color = { r=0.83, g=0.83, b=0.83}
button.style.minimal_height = 30
button.style.minimal_width = 30
button.style.maximal_height = 30
button.style.maximal_width = 30
button.style.top_padding = 0
button.style.left_padding = 0
button.style.right_padding = 0
button.style.bottom_padding = 0
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 == "player_list_button") then
if player.gui.left["player-list-panel"] then
player.gui.left["player-list-panel"].destroy()
else
player_list_show(player,"time_played_desc")
end
return
end
if (name == "player_list_panel_header_2") then
if string.find(event.element.caption, symbol_desc) then
player_list_show(player,"name_asc")
else
player_list_show(player,"name_desc")
end
return
end
if (name == "player_list_panel_header_3") then
if string.find(event.element.caption, symbol_desc) then
player_list_show(player,"time_played_asc")
else
player_list_show(player,"time_played_desc")
end
return
end
if (name == "player_list_panel_header_4") then
if string.find(event.element.caption, symbol_desc) then
player_list_show(player,"distance_asc")
else
player_list_show(player,"distance_desc")
end
return
end
if (name == "player_list_panel_header_5") then
if string.find(event.element.caption, symbol_desc) then
player_list_show(player,"pokes_asc")
else
player_list_show(player,"pokes_desc")
end
return
end
--Poke other players
if event.element.type == "button" then
local x = string.find(name, "poke_player_")
if x ~= nil then
local y = string.len(event.element.name)
local poked_player = string.sub(event.element.name, 13, y)
if player.name ~= poked_player then
local x = global.poke_spam_protection[event.element.player_index] + 240
if x < game.tick then
local str = ">> "
str = str .. player.name
str = str .. " has poked "
str = str .. poked_player
str = str .. " with "
local z = math.random(1,#pokemessages)
str = str .. pokemessages[z]
str = str .. " <<"
game.print(str)
global.poke_spam_protection[event.element.player_index] = game.tick
local p = game.players[poked_player]
global.player_list_pokes_counter[p.index] = global.player_list_pokes_counter[p.index] + 1
end
end
end
end
end
function 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
local function log_on_player_died_debug(str, event)
local cause = event.cause or {name = "no cause"}
game.write_file("on_player_died_debug", game.tick .. " (" .. game.players[event.player_index].name .. ", cause: " .. cause.name .. ") " .. str .. "\n", true, 0)
end
local function player_list_on_player_died(event)
log_on_player_died_debug("entry", event)
local player = game.players[event.player_index]
log_on_player_died_debug("player", event)
if not global.scenario.variables.player_deaths[player.name] then
log_on_player_died_debug("if", event)
global.scenario.variables.player_deaths[player.name] = 0
log_on_player_died_debug("deaths zero", event)
end
log_on_player_died_debug("deaths ++", event)
global.scenario.variables.player_deaths[player.name] = global.scenario.variables.player_deaths[player.name] + 1
log_on_player_died_debug("exit", event)
end
Event.on_nth_tick(720, on_12_seconds)
Event.add(defines.events.on_player_joined_game, on_player_joined_game)
Event.add(defines.events.on_gui_click, on_gui_click)
Event.add(defines.events.on_player_died, player_list_on_player_died)

142
player_stats.lua Normal file
View File

@ -0,0 +1,142 @@
local Event = require 'utils.event'
local Global = require 'utils.global'
local player_last_position = {}
local player_walk_distances = {}
local player_fish_earned = {}
local player_fish_spent = {}
local player_deaths = {}
local total_players = {0}
Global.register(
{
player_last_position = player_last_position,
player_walk_distances = player_walk_distances,
player_fish_earned = player_fish_earned,
player_fish_spent = player_fish_spent,
player_deaths = player_deaths,
total_players = total_players
},
function(tbl)
player_last_position = tbl.player_last_position
player_walk_distances = tbl.player_walk_distances
player_fish_earned = tbl.player_fish_earned
player_fish_spent = tbl.player_fish_spent
player_deaths = tbl.player_deaths
total_players = tbl.total_players
end
)
local function player_created(event)
local index = event.player_index
player_last_position[index] = game.players[index].position
player_walk_distances[index] = 0
player_fish_earned[index] = 0
player_fish_spent[index] = 0
player_deaths[index] = {causes = {}, count = 0}
total_players[1] = total_players[1] + 1
end
local function get_cause_name(cause)
if cause then
local name = cause.name
if name == 'player' then
local player = cause.player
if player and player.valid then
return player.name
end
else
return name
end
end
return 'No cause'
end
local function player_died(event)
local player_index = event.player_index
local cause = get_cause_name(event.cause)
local data = player_deaths[player_index]
data.count = data.count + 1
local causes = data.causes
local cause_count = causes[cause] or 0
causes[cause] = cause_count + 1
end
local function picked_up_item(event)
local stack = event.item_stack
if stack.name == 'raw-fish' then
local player_index = event.player_index
player_fish_earned[player_index] = player_fish_earned[player_index] + stack.count
end
end
local function tick()
for _, p in ipairs(game.connected_players) do
if (p.afk_time < 30 or p.walking_state.walking) and p.vehicle == nil then
local index = p.index
local last_pos = player_last_position[index]
local pos = p.position
local d_x = last_pos.x - pos.x
local d_y = last_pos.y - pos.y
player_walk_distances[index] = player_walk_distances[index] + math.sqrt(d_x * d_x + d_y * d_y)
player_last_position[index] = pos
end
end
end
Event.add(defines.events.on_player_created, player_created)
Event.add(defines.events.on_player_died, player_died)
Event.add(defines.events.on_picked_up_item, picked_up_item)
Event.on_nth_tick(62, tick)
local Public = {}
function Public.get_walk_distance(player_index)
return player_walk_distances[player_index]
end
function Public.get_fish_earned(player_index)
return player_fish_earned[player_index]
end
function Public.set_fish_earned(player_index, value)
player_fish_earned[player_index] = value
end
function Public.change_fish_earned(player_index, amount)
player_fish_earned[player_index] = player_fish_earned[player_index] + amount
end
function Public.get_fish_spent(player_index)
return player_fish_spent[player_index]
end
function Public.set_fish_spent(player_index, value)
player_fish_spent[player_index] = value
end
function Public.change_fish_spent(player_index, amount)
player_fish_spent[player_index] = player_fish_spent[player_index] + amount
end
function Public.get_death_count(player_index)
return player_deaths[player_index].count
end
-- Returns a dictionary of casue_name -> count
function Public.get_all_death_counts_by_casue(player_index)
return player_deaths[player_index].causes or {}
end
function Public.get_total_player_count()
return total_players[1]
end
return Public

View File

@ -0,0 +1,54 @@
return {
'item/iron-axe',
'item/burner-mining-drill',
'item/burner-inserter',
'item/stone-furnace',
'item/light-armor',
'item/steam-engine',
'item/inserter',
'item/transport-belt',
'item/underground-belt',
'item/splitter',
'item/assembling-machine-1',
'item/long-handed-inserter',
'item/electronic-circuit',
'item/electric-mining-drill',
'item/heavy-armor',
'item/steel-furnace',
'item/steel-axe',
'item/gun-turret',
'item/fast-transport-belt',
'item/fast-underground-belt',
'item/fast-splitter',
'item/assembling-machine-2',
'item/fast-inserter',
'item/radar',
'item/filter-inserter',
'item/defender-capsule',
'item/pumpjack',
'item/chemical-plant',
'item/solar-panel',
'item/advanced-circuit',
'item/modular-armor',
'item/accumulator',
'item/construction-robot',
'item/distractor-capsule',
'item/stack-inserter',
'item/electric-furnace',
'item/express-transport-belt',
'item/express-underground-belt',
'item/express-splitter',
'item/assembling-machine-3',
'item/processing-unit',
'item/power-armor',
'item/logistic-robot',
'item/laser-turret',
'item/stack-filter-inserter',
'item/destroyer-capsule',
'item/power-armor-mk2',
'item/flamethrower-turret',
'item/beacon',
'item/steam-turbine',
'item/centrifuge',
'item/nuclear-reactor'
}

View File

@ -1,43 +1,49 @@
global.regulars = require "resources.regulars"
local Event = require "utils.event"
local Utils = require "utils.utils"
global.regulars = require 'resources.regulars'
local Event = require 'utils.event'
local Utils = require 'utils.utils'
local Module = {}
local function update_file()
local file = "regulars.lua"
game.write_file(file, "{", false, 0)
local line = ""
for player_name,_ in pairs(global.regulars) do
line = string.format('["%s"] = true,\n', player_name)
game.write_file(file, line, true, 0)
local data = {'{\n'}
for player_name, _ in pairs(global.regulars) do
table.insert(data, '["')
table.insert(data, player_name)
table.insert(data, '"] = true,\n')
end
game.write_file(file, "}", true, 0)
table.insert(data, '}')
game.write_file('regulars.lua', table.concat(data), false, 0)
end
Module.is_regular = function(player_name)
Module.is_regular =
function(player_name)
return Utils.cast_bool(global.regulars[player_name] or global.regulars[player_name:lower()]) --to make it backwards compatible
end
Module.add_regular = function(player_name)
local actor = Utils.get_actor()
if Module.is_regular(player_name) then player_print(player_name .. " is already a regular.")
if Module.is_regular(player_name) then
player_print(player_name .. ' is already a regular.')
else
if game.players[player_name] then
player_name = game.players[player_name].name
game.print(actor .. " promoted " .. player_name .. " to regular.")
game.print(actor .. ' promoted ' .. player_name .. ' to regular.')
global.regulars[player_name] = true
update_file()
else
player_print(player_name .. " does not exist.")
player_print(player_name .. ' does not exist.')
end
end
end
Module.remove_regular = function(player_name)
Module.remove_regular =
function(player_name)
local actor = Utils.get_actor()
if game.players[player_name] then
player_name = game.players[player_name].name
if Module.is_regular(player_name) then game.print(player_name .. " was demoted from regular by " .. actor .. ".") end
if Module.is_regular(player_name) then
game.print(player_name .. ' was demoted from regular by ' .. actor .. '.')
end
global.regulars[player_name] = nil
global.regulars[player_name:lower()] = nil --backwards compatible
update_file()
@ -45,19 +51,31 @@ Module.remove_regular = function(player_name)
end
Module.print_regulars = function()
for k,_ in pairs(global.regulars) do
for k, _ in pairs(global.regulars) do
player_print(k)
end
end
function Module.get_rank(player)
if player.admin then
return 'Admin'
elseif Module.is_regular(player.name) then
return 'Regular'
else
return 'Guest'
end
end
Event.add(defines.events.on_player_joined_game, function(event)
Event.add(
defines.events.on_player_joined_game,
function(event)
local correctCaseName = game.players[event.player_index].name
if global.regulars[correctCaseName:lower()] and not global.regulars[correctCaseName] then
global.regulars[correctCaseName:lower()] = nil
global.regulars[correctCaseName] = true
update_file()
end
end)
end
)
return Module

View File

@ -42,6 +42,31 @@ function Gui.remove_data_recursivly(element)
end
end
function Gui.remove_children_data(element)
local children = element.children
if not children then
return
end
for _, child in ipairs(children) do
if child.valid then
Gui.set_data(child, nil)
Gui.remove_children_data(child)
end
end
end
function Gui.destroy(element)
Gui.remove_data_recursivly(element)
element.destroy()
end
function Gui.clear(element)
Gui.remove_children_data(element)
element.clear()
end
local function handler_factory(event_id)
local handlers

View File

@ -1,35 +0,0 @@
local Event = require "utils.event"
function on_second()
local last_positions = global.scenario.variables.player_positions
local d_x = 0
local d_y = 0
for _,v in pairs(game.players) do
if (v.afk_time < 30 or v.walking_state.walking) and v.vehicle == nil then
if last_positions[v.name] then
d_x = last_positions[v.name].x - v.position.x
d_y = last_positions[v.name].y - v.position.y
global.player_walk_distances[v.name] = global.player_walk_distances[v.name] + math.sqrt(d_x*d_x + d_y*d_y)
global.scenario.variables.player_positions[v.name] = v.position
end
end
end
end
local function get_player_positions()
local pos = {}
for _,v in pairs(game.players) do
pos[v.name] = v.position
end
return pos
end
local function init_player_position(event)
local player = game.players[event.player_index]
global.scenario.variables.player_positions[player.name] = player.position
if not global.player_walk_distances[player.name] then
global.player_walk_distances[player.name] = 0
end
end
Event.on_nth_tick(62, on_second)
Event.add(defines.events.on_player_joined_game, init_player_position)