mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 03:25:23 +09:00
Cleaned up use of the Game module
This commit is contained in:
@@ -20,7 +20,6 @@ see ./expcore/commands.lua for more details
|
||||
]]
|
||||
|
||||
local Commands = require 'expcore.commands' --- @dep expcore.commands
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
|
||||
-- luacheck:ignore 212/player
|
||||
Commands.add_parse('boolean',function(input, player)
|
||||
@@ -94,7 +93,7 @@ end)
|
||||
|
||||
Commands.add_parse('player',function(input, player, reject)
|
||||
if not input then return end -- nil check
|
||||
local input_player = Game.get_player_from_any(input)
|
||||
local input_player = game.players[input]
|
||||
if not input_player then
|
||||
return reject{'expcore-commands.reject-player',input}
|
||||
else
|
||||
|
||||
@@ -120,17 +120,17 @@ local function assign_group(player)
|
||||
end
|
||||
|
||||
Event.add(defines.events.on_player_joined_game,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
assign_group(player)
|
||||
end)
|
||||
|
||||
Event.add(defines.events.on_player_promoted,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
assign_group(player)
|
||||
end)
|
||||
|
||||
Event.add(defines.events.on_player_demoted,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
assign_group(player)
|
||||
end)
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
local Gui = require 'expcore.gui' --- @dep expcore.gui
|
||||
local Roles = require 'expcore.roles' --- @dep expcore.roles
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Reports = require 'modules.control.reports' --- @dep modules.control.reports
|
||||
local Warnings = require 'modules.control.warnings' --- @dep modules.control.warnings
|
||||
local Jail = require 'modules.control.jail' --- @dep modules.control.jail
|
||||
@@ -31,7 +30,7 @@ end
|
||||
-- gets the action player and a coloured name for the action to be used on
|
||||
local function get_action_player_name(player)
|
||||
local selected_player_name = SelectedPlayer:get(player)
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
local selected_player_color = format_chat_player_name(selected_player)
|
||||
return selected_player_name, selected_player_color
|
||||
end
|
||||
@@ -64,7 +63,7 @@ end
|
||||
local goto_player = new_button('utility/export',{'player-list.goto-player'})
|
||||
:on_click(function(player)
|
||||
local selected_player_name = get_action_player_name(player)
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if not player.character or not selected_player.character then
|
||||
player.print({'expcore-commands.reject-player-alive'},Colors.orange_red)
|
||||
else
|
||||
@@ -77,7 +76,7 @@ end)
|
||||
local bring_player = new_button('utility/import',{'player-list.bring-player'})
|
||||
:on_click(function(player)
|
||||
local selected_player_name = get_action_player_name(player)
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if not player.character or not selected_player.character then
|
||||
player.print({'expcore-commands.reject-player-alive'},Colors.orange_red)
|
||||
else
|
||||
@@ -90,7 +89,7 @@ end)
|
||||
local kill_player = new_button('utility/too_far',{'player-list.kill-player'})
|
||||
:on_click(function(player)
|
||||
local selected_player_name = get_action_player_name(player)
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if selected_player.character then
|
||||
selected_player.character.die()
|
||||
else
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
-- @addon Advanced-Start
|
||||
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local config = require 'config.advanced_start' --- @dep config.advanced_start
|
||||
local items = config.items
|
||||
|
||||
Event.add(defines.events.on_player_created, function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
-- game init settings
|
||||
if event.player_index == 1 then
|
||||
player.force.friendly_fire = config.friendly_fire
|
||||
|
||||
@@ -10,7 +10,7 @@ local send_text = Game.print_player_floating_text -- (player_index, text, color)
|
||||
|
||||
Event.add(defines.events.on_console_chat, function(event)
|
||||
if not event.player_index or event.player_index < 1 then return end
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
-- Some basic sanity checks
|
||||
if not player then return end
|
||||
|
||||
@@ -9,7 +9,7 @@ local config = require 'config.chat_reply' --- @dep config.chat_reply
|
||||
Event.add(defines.events.on_console_chat, function(event)
|
||||
local player_index = event.player_index
|
||||
if not player_index or player_index < 1 then return end
|
||||
local player = Game.get_player_by_index(player_index)
|
||||
local player = game.players[player_index]
|
||||
local message = event.message:lower():gsub("%s+", "")
|
||||
local allowed = true
|
||||
if config.command_admin_only and not player.admin then allowed = false end
|
||||
|
||||
@@ -95,7 +95,7 @@ end
|
||||
-- When the first player is created this will create all compilatrons that are resisted in the config
|
||||
Event.add(defines.events.on_player_created, function(event)
|
||||
if event.player_index ~= 1 then return end
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
for location in pairs(locations) do
|
||||
Public.spawn_compilatron(player.surface, location)
|
||||
end
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
-- @addon Death-Logger
|
||||
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local config = require 'config.death_logger' --- @dep config.death_logger
|
||||
local format_time, move_items = _C.format_time, _C.move_items --- @dep expcore.common
|
||||
@@ -17,7 +16,7 @@ end)
|
||||
|
||||
--- Creates a new death marker and saves it to the given death
|
||||
local function create_map_tag(death)
|
||||
local player = Game.get_player_from_any(death.player_name)
|
||||
local player = game.players[death.player_name]
|
||||
local message = player.name..' died'
|
||||
if config.include_time_of_death then
|
||||
local time = format_time(death.time_of_death, {hours=true, minutes=true, string=true})
|
||||
@@ -59,7 +58,7 @@ end
|
||||
|
||||
-- when a player dies a new death is added to the records and a map marker is made
|
||||
Event.add(defines.events.on_player_died, function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
local corpse = player.surface.find_entity('character-corpse', player.position)
|
||||
if config.use_chests_as_bodies then
|
||||
local items = corpse.get_inventory(defines.inventory.character_corpse).get_contents()
|
||||
|
||||
@@ -8,7 +8,7 @@ local write_json, format_time = _C.write_json, _C.format_time --- @dep expcore.c
|
||||
local config = require 'config.discord_alerts' --- @dep config.discord_alerts
|
||||
|
||||
local function get_player_name(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
return player.name, event.by_player_name
|
||||
end
|
||||
|
||||
@@ -189,7 +189,7 @@ end
|
||||
if config.player_bans then
|
||||
Event.add(defines.events.on_player_banned, function(event)
|
||||
if event.by_player then
|
||||
local by_player = Game.get_player_by_index(event.by_player)
|
||||
local by_player = game.players[event.by_player]
|
||||
emit_event{
|
||||
title='Banned',
|
||||
description='A player has been banned',
|
||||
@@ -202,7 +202,7 @@ if config.player_bans then
|
||||
end)
|
||||
Event.add(defines.events.on_player_unbanned, function(event)
|
||||
if event.by_player then
|
||||
local by_player = Game.get_player_by_index(event.by_player)
|
||||
local by_player = game.players[event.by_player]
|
||||
emit_event{
|
||||
title='Un-Banned',
|
||||
description='A player has been un-banned',
|
||||
@@ -241,7 +241,7 @@ if config.player_kicks then
|
||||
Event.add(defines.events.on_player_kicked, function(event)
|
||||
if event.by_player then
|
||||
local player_name = get_player_name(event)
|
||||
local by_player = Game.get_player_by_index(event.by_player)
|
||||
local by_player = game.players[event.by_player]
|
||||
emit_event{
|
||||
title='Kick',
|
||||
description='A player has been kicked',
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
-- @addon Scorched-Earth
|
||||
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local print_grid_value, clear_flying_text = _C.print_grid_value, _C.clear_flying_text --- @dep expcore.common
|
||||
local config = require 'config.scorched_earth' --- @dep config.scorched_earth
|
||||
@@ -94,7 +93,7 @@ end
|
||||
|
||||
-- When the player changes position the tile will have a chance to downgrade, debug check is here
|
||||
Event.add(defines.events.on_player_changed_position, function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
local surface = player.surface
|
||||
local position = player.position
|
||||
local strength = get_tile_strength(surface, position)
|
||||
@@ -133,7 +132,7 @@ end)
|
||||
|
||||
-- Used as a way to access the global table
|
||||
return function(player_name, state)
|
||||
local player = Game.get_player_from_any(player_name)
|
||||
local player = game.players[player_name]
|
||||
clear_flying_text(player.surface)
|
||||
debug_players[player_name] = state
|
||||
end
|
||||
@@ -134,7 +134,7 @@ end)
|
||||
|
||||
Event.add(defines.events.on_player_created, function(event)
|
||||
if event.player_index ~= 1 then return end
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local player = game.players[event.player_index]
|
||||
local p = {x=0, y=0}
|
||||
local s = player.surface
|
||||
spawn_base(s, p)
|
||||
|
||||
@@ -19,7 +19,7 @@ Event.add(defines.events.on_marked_for_deconstruction, function(event)
|
||||
-- Check which type of decon a player is allowed
|
||||
local index = event.player_index
|
||||
if chache[index] == nil then
|
||||
local player = Game.get_player_by_index(index)
|
||||
local player = game.players[index]
|
||||
if Roles.player_allowed(player, 'fast-tree-decon') then chache[index] = 'fast'
|
||||
elseif Roles.player_allowed(player, 'standard-decon') then chache[index] = 'standard'
|
||||
else chache[index] = player.force end
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
local Gui = require 'expcore.gui' --- @dep expcore.gui
|
||||
local Roles = require 'expcore.roles' --- @dep expcore.roles
|
||||
local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local config = require 'config.gui.player_list_actions' --- @dep config.gui.player_list_actions
|
||||
local Colors = require 'utils.color_presets' --- @dep utils.color_presets
|
||||
@@ -115,7 +114,7 @@ Gui.element(function(event_trigger, parent, player_data)
|
||||
end)
|
||||
:on_click(function(player, element, event)
|
||||
local selected_player_name = element.caption
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if event.button == defines.mouse_button_type.left then
|
||||
-- LMB will open the map to the selected player
|
||||
local position = selected_player.position
|
||||
@@ -177,7 +176,7 @@ local function update_action_bar(element)
|
||||
element.visible = false
|
||||
|
||||
else
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if not selected_player.connected then
|
||||
-- If the player is offline then reest stores
|
||||
element.visible = false
|
||||
@@ -357,7 +356,7 @@ end)
|
||||
|
||||
--- When a player leaves only remove they entry
|
||||
Event.add(defines.events.on_player_left_game, function(event)
|
||||
local remove_player = Game.get_player_by_index(event.player_index)
|
||||
local remove_player = game.players[event.player_index]
|
||||
for _, player in pairs(game.connected_players) do
|
||||
local frame = Gui.get_left_element(player, player_list_container)
|
||||
local scroll_table = frame.container.scroll.table
|
||||
@@ -390,7 +389,7 @@ Event.add(Roles.events.on_role_unassigned, redraw_player_list)
|
||||
|
||||
--- When the action player is changed the action bar will update
|
||||
SelectedPlayer:on_update(function(player_name, selected_player)
|
||||
local player = Game.get_player_from_any(player_name)
|
||||
local player = game.players[player_name]
|
||||
local frame = Gui.get_left_element(player, player_list_container)
|
||||
local scroll_table = frame.container.scroll.table
|
||||
update_action_bar(frame.container.action_bar)
|
||||
@@ -410,13 +409,13 @@ end)
|
||||
|
||||
--- When the action name is changed the reason input will update
|
||||
SelectedAction:on_update(function(player_name, selected_action)
|
||||
local player = Game.get_player_from_any(player_name)
|
||||
local player = game.players[player_name]
|
||||
local frame = Gui.get_left_element(player, player_list_container)
|
||||
local element = frame.container.reason_bar
|
||||
if selected_action then
|
||||
-- if there is a new value then check the player is still online
|
||||
local selected_player_name = SelectedPlayer:get(player_name)
|
||||
local selected_player = Game.get_player_from_any(selected_player_name)
|
||||
local selected_player = game.players[selected_player_name]
|
||||
if selected_player.connected then
|
||||
element.visible = true
|
||||
else
|
||||
|
||||
@@ -1,64 +1,39 @@
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local Color = require 'utils.color_presets' --- @dep utils.color_presets
|
||||
local pairs = pairs
|
||||
|
||||
local Color = require 'utils.color_presets' --- @dep utils.color_presets
|
||||
local Game = {}
|
||||
|
||||
local bad_name_players = {}
|
||||
Global.register(
|
||||
bad_name_players,
|
||||
function(tbl)
|
||||
bad_name_players = tbl
|
||||
end
|
||||
)
|
||||
--[[ Note to readers
|
||||
Game.get_player_from_name was removed because game.players[name] works without any edge cases
|
||||
always true: game.players[name].name == name
|
||||
|
||||
Game.get_player_by_index was added originally as a workaround for the following edge case:
|
||||
player with index of 5 and name of "Cooldude2606"
|
||||
player with index of 10 and name of "5"
|
||||
game.players[5].name == "5"
|
||||
|
||||
Discovered the following logic:
|
||||
all keys are first converted to string and search against player names
|
||||
if this fails it attempts to convert it to a number and search against player indexes
|
||||
sometimes fails: game.players[index].index == index
|
||||
|
||||
Game.get_player_by_index was removed after the above logic was corrected to the following:
|
||||
when a key is a number it is searched against player indexes, and only their indexes
|
||||
when a key is a string it is searched against player names, and then against their indexes
|
||||
always true: game.players[name].name == name; game.players[index].index == index
|
||||
|
||||
--[[
|
||||
Due to a bug in the Factorio api the following expression isn't guaranteed to be true.
|
||||
game.players[player.index] == player
|
||||
get_player_by_index(index) will always return the correct player.
|
||||
When looking up players by name or iterating through all players use game.players instead.
|
||||
]]
|
||||
function Game.get_player_by_index(index)
|
||||
local p = game.players[index]
|
||||
|
||||
if not p then
|
||||
return nil
|
||||
end
|
||||
if p.index == index then
|
||||
return p
|
||||
end
|
||||
|
||||
p = bad_name_players[index]
|
||||
if p then
|
||||
if p.valid then
|
||||
return p
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
for k, v in pairs(game.players) do
|
||||
if k == index then
|
||||
bad_name_players[index] = v
|
||||
return v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns a valid LuaPlayer if given a number, string, or LuaPlayer. Returns nil otherwise.
|
||||
-- obj <number|string|LuaPlayer>
|
||||
function Game.get_player_from_any(obj)
|
||||
local o_type = type(obj)
|
||||
local p
|
||||
if o_type == 'number' then
|
||||
p = Game.get_player_by_index(obj)
|
||||
elseif o_type == 'string' then
|
||||
local o_type, p = type(obj)
|
||||
if o_type == 'table' then
|
||||
p = obj
|
||||
elseif o_type == 'string' or o_type == 'number' then
|
||||
p = game.players[obj]
|
||||
elseif o_type == 'table' and obj.valid and obj.is_player() then
|
||||
return obj
|
||||
end
|
||||
|
||||
if p and p.valid then
|
||||
if p and p.valid and p.is_player() then
|
||||
return p
|
||||
end
|
||||
end
|
||||
@@ -95,16 +70,18 @@ function Game.print_floating_text(surface, position, text, color)
|
||||
end
|
||||
|
||||
--[[
|
||||
Creates a floating text entity at the player location with the specified color in {r, g, b} format.
|
||||
Creates a floating text entity at the player location with the specified color and offset.
|
||||
Example: "+10 iron" or "-10 coins"
|
||||
|
||||
@param text String to display
|
||||
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
|
||||
@param x_offset number the x offset for the floating text
|
||||
@param y_offset number the y offset for the floating text
|
||||
|
||||
@return the created entity
|
||||
]]
|
||||
function Game.print_player_floating_text_position(player_index, text, color, x_offset, y_offset)
|
||||
local player = Game.get_player_by_index(player_index)
|
||||
function Game.print_player_floating_text_position(player, text, color, x_offset, y_offset)
|
||||
player = Game.get_player_from_any(player)
|
||||
if not player or not player.valid then
|
||||
return
|
||||
end
|
||||
@@ -113,8 +90,17 @@ function Game.print_player_floating_text_position(player_index, text, color, x_o
|
||||
return Game.print_floating_text(player.surface, {x = position.x + x_offset, y = position.y + y_offset}, text, color)
|
||||
end
|
||||
|
||||
function Game.print_player_floating_text(player_index, text, color)
|
||||
Game.print_player_floating_text_position(player_index, text, color, 0, -1.5)
|
||||
--[[
|
||||
Creates a floating text entity at the player location with the specified color in {r, g, b} format.
|
||||
Example: "+10 iron" or "-10 coins"
|
||||
|
||||
@param text String to display
|
||||
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
|
||||
|
||||
@return the created entity
|
||||
]]
|
||||
function Game.print_player_floating_text(player, text, color)
|
||||
Game.print_player_floating_text_position(player, text, color, 0, -1.5)
|
||||
end
|
||||
|
||||
return Game
|
||||
return Game
|
||||
Reference in New Issue
Block a user