mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-05 22:53:39 +02:00
Remove corpse pings + Refactor redmew_settings.
This commit is contained in:
parent
45e52d9ff0
commit
f6ea2cbabc
@ -1,18 +1,8 @@
|
||||
local Event = require 'utils.event'
|
||||
local Settings = require 'utils.redmew_settings'
|
||||
local CorpseUtil = require 'features.corpse_util'
|
||||
|
||||
local Public = {}
|
||||
|
||||
local ping_own_death_name = 'death_corpse_tags.ping_own_death'
|
||||
local ping_other_death_name = 'death_corpse_tags.ping_other_death'
|
||||
|
||||
Public.ping_own_death_name = ping_own_death_name
|
||||
Public.ping_other_death_name = ping_other_death_name
|
||||
|
||||
Settings.register(ping_own_death_name, Settings.types.boolean, true, 'death_corpse_tags.ping_own_death')
|
||||
Settings.register(ping_other_death_name, Settings.types.boolean, false, 'death_corpse_tags.ping_other_death')
|
||||
|
||||
local function player_died(event)
|
||||
local player_index = event.player_index
|
||||
local player = game.get_player(player_index)
|
||||
@ -63,27 +53,6 @@ local function player_died(event)
|
||||
return
|
||||
end
|
||||
|
||||
if Settings.get(player_index, ping_own_death_name) then
|
||||
player.print({
|
||||
'death_corpse_tags.own_corpse_location',
|
||||
string.format('%.1f', position.x),
|
||||
string.format('%.1f', position.y),
|
||||
player.surface.name
|
||||
})
|
||||
end
|
||||
|
||||
for _, other_player in pairs(player.force.players) do
|
||||
if other_player ~= player and Settings.get(other_player.index, ping_other_death_name) then
|
||||
other_player.print({
|
||||
'death_corpse_tags.other_corpse_location',
|
||||
player.name,
|
||||
string.format('%.1f', position.x),
|
||||
string.format('%.1f', position.y),
|
||||
player.surface.name
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
CorpseUtil.add_tag(tag, player_index, tick, true)
|
||||
end
|
||||
|
||||
|
@ -2,7 +2,6 @@ local Declare = require 'utils.test.declare'
|
||||
local EventFactory = require 'utils.test.event_factory'
|
||||
local Assert = require 'utils.test.assert'
|
||||
local Helper = require 'utils.test.helper'
|
||||
local Settings = require 'utils.redmew_settings'
|
||||
local CorpseUtil = require 'features.corpse_util'
|
||||
local DeathCorpseTags = require 'features.death_corpse_tags'
|
||||
|
||||
@ -36,17 +35,6 @@ Declare.module({'features', 'death_corpse_tags'}, function()
|
||||
teardown()
|
||||
end)
|
||||
|
||||
local function change_settings_for_test(context, key, value)
|
||||
local player_index = context.player.index
|
||||
|
||||
local current_value = Settings.get(player_index, key)
|
||||
Settings.set(player_index, key, value)
|
||||
|
||||
context:add_teardown(function()
|
||||
Settings.set(player_index, key, current_value)
|
||||
end)
|
||||
end
|
||||
|
||||
local function fake_death(player, has_items)
|
||||
local surface = player.surface
|
||||
local position = player.position
|
||||
@ -70,176 +58,6 @@ Declare.module({'features', 'death_corpse_tags'}, function()
|
||||
return EventFactory.on_player_died(player.index)
|
||||
end
|
||||
|
||||
declare_test('ping player corpse location when died', function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
|
||||
local actual_text
|
||||
|
||||
Helper.modify_lua_object(context, player, 'print', function(text)
|
||||
actual_text = text
|
||||
end)
|
||||
|
||||
Helper.modify_lua_object(context, game, 'get_player', function()
|
||||
return player
|
||||
end)
|
||||
|
||||
local event = fake_death(player, true)
|
||||
|
||||
-- Act.
|
||||
DeathCorpseTags._player_died(event)
|
||||
|
||||
-- Assert.
|
||||
local expected = {'death_corpse_tags.own_corpse_location', '0.0', '0.0', player.surface.name}
|
||||
Assert.table_equal(expected, actual_text)
|
||||
end)
|
||||
|
||||
declare_test('ping other player corpse location when other player died', function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
local force = player.force
|
||||
|
||||
local actual_text
|
||||
|
||||
local second_player = {
|
||||
index = 2,
|
||||
valid = true,
|
||||
name = 'second_player',
|
||||
surface = player.surface,
|
||||
force = force,
|
||||
print = function()
|
||||
end,
|
||||
position = EventFactory.position({1, 1})
|
||||
}
|
||||
|
||||
change_settings_for_test(context, DeathCorpseTags.ping_other_death_name, true)
|
||||
|
||||
Helper.modify_lua_object(context, game, 'get_player', function(index)
|
||||
if index == player.index then
|
||||
return player
|
||||
end
|
||||
|
||||
if index == second_player.index then
|
||||
return second_player
|
||||
end
|
||||
end)
|
||||
|
||||
Helper.modify_lua_object(context, force, 'players', {player, second_player})
|
||||
|
||||
Helper.modify_lua_object(context, player, 'print', function(text)
|
||||
actual_text = text
|
||||
end)
|
||||
|
||||
local event = fake_death(second_player, true)
|
||||
|
||||
-- Act.
|
||||
DeathCorpseTags._player_died(event)
|
||||
|
||||
-- Assert.
|
||||
local expected = {'death_corpse_tags.other_corpse_location', second_player.name, '1.0', '1.0', player.surface.name}
|
||||
Assert.table_equal(expected, actual_text)
|
||||
end)
|
||||
|
||||
declare_test('do not ping player corpse location when died and setting disabled', function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
change_settings_for_test(context, DeathCorpseTags.ping_own_death_name, false)
|
||||
|
||||
local actual_text
|
||||
|
||||
Helper.modify_lua_object(context, player, 'print', function(text)
|
||||
actual_text = text
|
||||
end)
|
||||
|
||||
Helper.modify_lua_object(context, game, 'get_player', function()
|
||||
return player
|
||||
end)
|
||||
|
||||
local event = fake_death(player, true)
|
||||
|
||||
-- Act.
|
||||
DeathCorpseTags._player_died(event)
|
||||
|
||||
-- Assert.
|
||||
Assert.is_nil(actual_text)
|
||||
end)
|
||||
|
||||
declare_test('do not ping other player corpse location when other player died and settings disabled',
|
||||
function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
local force = player.force
|
||||
|
||||
local actual_text
|
||||
|
||||
local second_player = {
|
||||
index = 2,
|
||||
valid = true,
|
||||
name = 'second_player',
|
||||
surface = player.surface,
|
||||
force = force,
|
||||
print = function()
|
||||
end,
|
||||
position = EventFactory.position({1, 1})
|
||||
}
|
||||
|
||||
change_settings_for_test(context, DeathCorpseTags.ping_other_death_name, false)
|
||||
|
||||
Helper.modify_lua_object(context, game, 'get_player', function(index)
|
||||
if index == player.index then
|
||||
return player
|
||||
end
|
||||
|
||||
if index == second_player.index then
|
||||
return second_player
|
||||
end
|
||||
end)
|
||||
|
||||
Helper.modify_lua_object(context, force, 'players', {player, second_player})
|
||||
|
||||
Helper.modify_lua_object(context, player, 'print', function(text)
|
||||
actual_text = text
|
||||
end)
|
||||
|
||||
local event = fake_death(second_player, true)
|
||||
|
||||
-- Act.
|
||||
DeathCorpseTags._player_died(event)
|
||||
|
||||
-- Assert.
|
||||
Assert.is_nil(actual_text)
|
||||
end)
|
||||
|
||||
declare_test('do not ping other player corpse location for self', function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
local force = player.force
|
||||
|
||||
local actual_text
|
||||
|
||||
change_settings_for_test(context, DeathCorpseTags.ping_own_death_name, false)
|
||||
change_settings_for_test(context, DeathCorpseTags.ping_other_death_name, true)
|
||||
|
||||
Helper.modify_lua_object(context, game, 'get_player', function()
|
||||
return player
|
||||
end)
|
||||
|
||||
Helper.modify_lua_object(context, player, 'force', force)
|
||||
Helper.modify_lua_object(context, force, 'players', {player})
|
||||
|
||||
Helper.modify_lua_object(context, player, 'print', function(text)
|
||||
actual_text = text
|
||||
end)
|
||||
|
||||
local event = fake_death(player, true)
|
||||
|
||||
-- Act.
|
||||
DeathCorpseTags._player_died(event)
|
||||
|
||||
-- Assert.
|
||||
Assert.is_nil(actual_text)
|
||||
end)
|
||||
|
||||
declare_test('corpse removed and empty message when corpse is empty', function(context)
|
||||
-- Arrange.
|
||||
local player = context.player
|
||||
|
@ -182,10 +182,6 @@ instructions=Select a brush tile to replace [item=refined-concrete] and [item=re
|
||||
no_place_landfill=Coloured concrete can not be placed on landfill tiles.
|
||||
|
||||
[death_corpse_tags]
|
||||
ping_own_death=Ping the location when you die.
|
||||
ping_other_death=Ping the location when other players die.
|
||||
own_corpse_location=[color=red][Corpse][/color] Your corpse is located at [gps=__1__,__2__,__3__]
|
||||
other_corpse_location=[color=red][Corpse][/color] __1__'s corpse is located at [gps=__2__,__3__,__4__]
|
||||
empty_corpse=[color=red][Corpse][/color]Your corpse was empty and has been removed.
|
||||
|
||||
[dump_offline_inventories]
|
||||
|
@ -1,30 +1,13 @@
|
||||
local Global = require 'utils.global'
|
||||
local Event = require 'utils.event'
|
||||
local error = error
|
||||
local pairs = pairs
|
||||
local format = string.format
|
||||
local tostring = tostring
|
||||
local type = type
|
||||
local raise_event = script.raise_event
|
||||
|
||||
--- Contains a set of callables that will attempt to sanitize and transform the input
|
||||
local settings_type = require 'resources.setting_types'
|
||||
local settings = {}
|
||||
local memory = {}
|
||||
local missing_setting = {
|
||||
data_transformation = {
|
||||
toScalar = function(input)
|
||||
if type(input) ~= 'table' then
|
||||
return input
|
||||
end
|
||||
|
||||
return tostring(input)
|
||||
end,
|
||||
sanitizer = function (input)
|
||||
return true, input
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
Global.register(memory, function (tbl) memory = tbl end)
|
||||
|
||||
@ -98,7 +81,7 @@ end
|
||||
function Public.validate(name, value)
|
||||
local setting = settings[name]
|
||||
if not setting then
|
||||
return format('Setting "%s" does not exist.', name)
|
||||
return nil
|
||||
end
|
||||
|
||||
local success, sanitized_value = setting.data_transformation.sanitizer(value)
|
||||
@ -118,9 +101,16 @@ end
|
||||
---@param name string
|
||||
---@param value any
|
||||
function Public.set(player_index, name, value)
|
||||
local player_settings = memory[player_index]
|
||||
if not player_settings then
|
||||
player_settings = {}
|
||||
memory[player_index] = player_settings
|
||||
end
|
||||
|
||||
local setting = settings[name]
|
||||
if not setting then
|
||||
setting = missing_setting
|
||||
player_settings[name] = value
|
||||
return
|
||||
end
|
||||
|
||||
local data_transformation = setting.data_transformation
|
||||
@ -130,12 +120,6 @@ function Public.set(player_index, name, value)
|
||||
error(format('Setting "%s" failed: %s', name, sanitized_value), 2)
|
||||
end
|
||||
|
||||
local player_settings = memory[player_index]
|
||||
if not player_settings then
|
||||
player_settings = {}
|
||||
memory[player_index] = player_settings
|
||||
end
|
||||
|
||||
local old_value = player_settings[name]
|
||||
player_settings[name] = sanitized_value
|
||||
|
||||
@ -182,7 +166,7 @@ end
|
||||
function Public.toScalar(name, raw_value)
|
||||
local setting = settings[name]
|
||||
if not setting then
|
||||
setting = missing_setting
|
||||
return nil
|
||||
end
|
||||
|
||||
return setting.data_transformation.toScalar(raw_value)
|
||||
@ -191,49 +175,7 @@ end
|
||||
---Returns a table of all settings for a given player in a key => value setup
|
||||
---@param player_index number
|
||||
function Public.all(player_index)
|
||||
local player_settings = memory[player_index] or {}
|
||||
local output = {}
|
||||
for name, data in pairs(settings) do
|
||||
local setting_value = player_settings[name]
|
||||
if setting_value == nil then
|
||||
output[name] = data.default
|
||||
else
|
||||
output[name] = setting_value
|
||||
end
|
||||
end
|
||||
|
||||
-- not all settings might be mapped, edge-case is triggered when the
|
||||
-- server contains settings that are not known in this instance
|
||||
for name, value in pairs(player_settings) do
|
||||
if output[name] == nil then
|
||||
output[name] = value
|
||||
end
|
||||
end
|
||||
|
||||
return output
|
||||
end
|
||||
|
||||
---Removes a value for a setting for a given name, giving it the default value.
|
||||
---
|
||||
---@param player_index number
|
||||
---@param name string
|
||||
function Public.unset(player_index, name)
|
||||
local player_settings = memory[player_index]
|
||||
if not player_settings then
|
||||
player_settings = {}
|
||||
memory[player_index] = player_settings
|
||||
end
|
||||
|
||||
local old_value = player_settings[name]
|
||||
player_settings[name] = nil
|
||||
|
||||
raise_event(Public.events.on_setting_set, {
|
||||
setting_name = name,
|
||||
old_value = old_value,
|
||||
new_value = nil,
|
||||
player_index = player_index,
|
||||
value_changed = old_value ~= nil
|
||||
})
|
||||
return memory[player_index] or {}
|
||||
end
|
||||
|
||||
---Returns the full settings data, note that this is a reference, do not modify
|
||||
|
Loading…
Reference in New Issue
Block a user