Move files to exp_legacy

This commit is contained in:
Cooldude2606
2024-09-23 15:55:28 +01:00
parent 446e87b610
commit 65145b5d34
266 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
--- Stores if you use alt mode or not and auto applies it
-- @data Alt-View
local Event = require 'utils.event' ---@dep utils.event
--- Stores the visible state of alt mode
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local UsesAlt = PlayerData.Settings:combine('UsesAlt')
UsesAlt:set_default(false)
UsesAlt:set_metadata{
stringify = function(value) return value and 'Visible' or 'Hidden' end
}
--- When your data loads apply alt view if you have it enabled
UsesAlt:on_load(function(player_name, uses_alt)
local player = game.players[player_name]
player.game_view_settings.show_entity_info = uses_alt or false
end)
--- When alt view is toggled update this
Event.add(defines.events.on_player_toggled_alt_mode, function(event)
local player = game.players[event.player_index]
UsesAlt:set(player, player.game_view_settings.show_entity_info)
end)

View File

@@ -0,0 +1,103 @@
--[[-- Commands Module - Bonus
- Adds a command that allows players to have increased stats
@data Bonus
]]
local Roles = require 'expcore.roles' --- @dep expcore.roles
local Event = require 'utils.event' --- @dep utils.event
local config = require 'config.bonus' --- @dep config.bonuses
local Commands = require 'expcore.commands' --- @dep expcore.commands
require 'config.expcore.command_general_parse'
-- Stores the bonus for the player
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local PlayerBonus = PlayerData.Settings:combine('Bonus')
PlayerBonus:set_default(0)
PlayerBonus:set_metadata{
permission = 'command/bonus',
stringify = function(value)
if not value or value == 0 then
return 'None set'
end
return value
end
}
--- Apply a bonus to a player
local function apply_bonus(player, stage)
if not player.character then
return
end
for k, v in pairs(config.player_bonus) do
player[k] = v.value * stage / 10
if v.combined_bonus then
for i=1, #v.combined_bonus, 1 do
player[v.combined_bonus[i]] = v.value * stage / 10
end
end
end
end
--- When store is updated apply new bonus to the player
PlayerBonus:on_update(function(player_name, player_bonus)
apply_bonus(game.players[player_name], player_bonus or 0)
end)
--- Changes the amount of bonus you receive
-- @command bonus
-- @tparam number amount range 0-10 the increase for your bonus
Commands.new_command('bonus', {'expcom-bonus.description'}, 'Changes the amount of bonus you receive')
:add_param('amount', 'integer-range', 0, 10)
:register(function(player, amount)
if not Roles.player_allowed(player, 'command/bonus') then
Commands.print{'expcom-bonus.perm', 1}
return
end
PlayerBonus:set(player, amount)
Commands.print{'expcom-bonus.set', amount}
end)
--- When a player respawns re-apply bonus
Event.add(defines.events.on_player_respawned, function(event)
local player = game.players[event.player_index]
apply_bonus(player, PlayerBonus:get(player))
end)
--- Remove bonus if a player no longer has access to the command
local function role_update(event)
local player = game.players[event.player_index]
if not Roles.player_allowed(player, 'command/bonus') then
apply_bonus(player, 0)
end
end
--- When a player dies allow them to have instant respawn
Event.add(defines.events.on_player_died, function(event)
local player = game.players[event.player_index]
if Roles.player_has_flag(player, 'instant-respawn') then
player.ticks_to_respawn = 120
end
end)
Event.add(defines.events.on_player_created, function(event)
if event.player_index ~= 1 then
return
end
for k, v in pairs(config.force_bonus) do
game.players[event.player_index].force[k] = v.value
end
for k, v in pairs(config.surface_bonus) do
game.players[event.player_index].surface[k] = v.value
end
end)
Event.add(Roles.events.on_role_assigned, role_update)
Event.add(Roles.events.on_role_unassigned, role_update)

View File

@@ -0,0 +1,43 @@
--- Greets players on join
-- @data Greetings
local config = require 'config.join_messages' --- @dep config.join_messages
local Commands = require 'expcore.commands' ---@dep expcore.commands
require 'config.expcore.command_general_parse'
--- Stores the join message that the player have
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local CustomMessages = PlayerData.Settings:combine('JoinMessage')
CustomMessages:set_metadata{
permission = 'command/join-message'
}
--- When a players data loads show their message
CustomMessages:on_load(function(player_name, player_message)
local player = game.players[player_name]
local custom_message = player_message or config[player_name]
if custom_message then
game.print(custom_message, player.color)
else
player.print{'join-message.greet', {'links.discord'}}
end
end)
--- Set your custom join message
-- @command join-message
-- @tparam string message The custom join message that will be used
Commands.new_command('join-message', {'expcom-join-message.description-msg'}, 'Sets your custom join message')
:add_param('message', false, 'string-max-length', 255)
:enable_auto_concat()
:register(function(player, message)
if not player then return end
CustomMessages:set(player, message)
return {'join-message.message-set'}
end)
Commands.new_command('join-message-clear', {'expcom-join-message.description-clr'}, 'Clear your join message')
:register(function(player)
if not player then return end
CustomMessages:remove(player)
return {'join-message.message-cleared'}
end)

View File

@@ -0,0 +1,34 @@
--- Stores the language used to join the server
-- @data Language
local Event = require 'utils.event' ---@dep utils.event
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local LocalLanguage = PlayerData.Statistics:combine('LocalLanguage')
LocalLanguage:set_default("Unknown")
--- Creates translation request on_load of a player
LocalLanguage:on_load(function(player_name, _)
local player = game.players[player_name]
player.request_translation({"language.local-language"})
end)
--- Resolves translation request for language setting
Event.add(defines.events.on_string_translated, function(event)
-- Check if event.localised_string is a table
-- Check if the translation request was for language setting
if type(event.localised_string) ~= "table" or event.localised_string[1] ~= "language.local-language" then
return
end
-- Check if the translation request was succesful
local player = game.players[event.player_index]
if not event.translated then
player.print("Could not detect your language settings")
-- Raise error
return
end
-- Change LocalLanguage value for the player to the recognized one
local language = event.result
LocalLanguage:set(player, language)
end)

View File

@@ -0,0 +1,97 @@
local Commands = require 'expcore.commands' --- @dep expcore.commands
local config = require 'config.personal_logistic' --- @dep config.personal-logistic
local pl = {}
function pl.pl(type, target, amount)
local c
local s
if type == 'p' then
c = target.clear_personal_logistic_slot
s = target.set_personal_logistic_slot
elseif type == 's' then
c = target.clear_vehicle_logistic_slot
s = target.set_vehicle_logistic_slot
else
return
end
for _, v in pairs(config.request) do
c(v.key)
end
if (amount < 0) then
return
end
local stats = target.force.item_production_statistics
for k, v in pairs(config.request) do
local v_min = math.ceil(v.min * amount)
local v_max = math.ceil(v.max * amount)
if v.stack and v.stack ~= 1 and v.type ~= 'weapon' then
v_min = math.floor(v_min / v.stack) * v.stack
v_max = math.ceil(v_max / v.stack) * v.stack
end
if v.upgrade_of == nil then
if v.type then
if stats.get_input_count(k) < config.production_required[v.type] then
if v_min > 0 then
if v_min == v_max then
v_min = math.floor((v_max * 0.5) / v.stack) * v.stack
end
else
v_min = 0
end
end
end
s(v.key, {name=k, min=v_min, max=v_max})
else
if v.type then
if stats.get_input_count(k) >= config.production_required[v.type] then
s(v.key, {name=k, min=v_min, max=v_max})
local vuo = v.upgrade_of
while vuo do
s(config.request[vuo].key, {name=vuo, min=0, max=0})
vuo = config.request[vuo].upgrade_of
end
else
s(v.key, {name=k, min=0, max=v_max})
end
end
end
end
end
Commands.new_command('personal-logistic', {'expcom-personal-logistics'}, 'Set Personal Logistic (-1 to cancel all) (Select spidertron to edit spidertron)')
:add_param('amount', 'integer-range', -1, 10)
:add_alias('pl')
:register(function(player, amount)
if player.force.technologies['logistic-robotics'].researched then
if player.selected then
if player.selected.name == 'spidertron' then
pl.pl('s', player.selected, amount / 10)
return Commands.success
end
else
pl.pl('p', player, amount / 10)
return Commands.success
end
else
player.print('Personal Logistic not researched')
end
end)
return pl

View File

@@ -0,0 +1,62 @@
--- Gives players random colours when they join, also applies preset colours to those who have them
-- @data Player-Colours
local Event = require 'utils.event' --- @dep utils.event
local Colours = require 'utils.color_presets' --- @dep utils.color_presets
local config = require 'config.preset_player_colours' --- @dep config.preset_player_colours
--- Stores the colour that the player wants
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local PlayerColours = PlayerData.Settings:combine('Colour')
PlayerColours:set_metadata{
stringify = function(value)
if not value then return 'None set' end
local c = value[1]
return string.format('Red: %d Green: %d Blue: %d', c[1], c[2], c[3])
end
}
--- Used to compact player colours to take less space
local floor = math.floor
local function compact(colour)
return {
floor(colour.r * 255),
floor(colour.g * 255),
floor(colour.b * 255)
}
end
--- Returns a colour that is a bit lighter than the one given
local function lighten(c)
return {r = 255 - (255 - c.r) * 0.5, g = 255 - (255 - c.g) * 0.5, b = 255 - (255 - c.b) * 0.5, a = 255}
end
--- When your data loads apply the players colour, or a random on if none is saved
PlayerColours:on_load(function(player_name, player_colour)
if not player_colour then
local preset = config.players[player_name]
if preset then
player_colour = {preset, lighten(preset)}
else
local colour_name = 'white'
while config.disallow[colour_name] do
colour_name = table.get_random_dictionary_entry(Colours, true)
end
player_colour = {Colours[colour_name], lighten(Colours[colour_name])}
end
end
local player = game.players[player_name]
player.color = player_colour[1]
player.chat_color = player_colour[2]
end)
--- Save the players color when they use the color command
Event.add(defines.events.on_console_command, function(event)
if event.command ~= 'color' then return end
if event.parameters == '' then return end
if not event.player_index then return end
local player = game.players[event.player_index]
if not player or not player.valid then return end
PlayerColours:set(player, {compact(player.color), compact(player.chat_color)})
end)

View File

@@ -0,0 +1,67 @@
--[[-- Commands Module - Quickbar
- Adds a command that allows players to load Quickbar presets
@data Quickbar
]]
local Commands = require 'expcore.commands' --- @dep expcore.commands
local config = require 'config.preset_player_quickbar' --- @dep config.preset_player_quickbar
--- Stores the quickbar filters for a player
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local PlayerFilters = PlayerData.Settings:combine('QuickbarFilters')
PlayerFilters:set_metadata{
permission = 'command/save-quickbar',
stringify = function(value)
if not value then return 'No filters set' end
local count = 0
for _ in pairs(value) do count = count + 1 end
return count..' filters set'
end
}
--- Loads your quickbar preset
PlayerFilters:on_load(function(player_name, filters)
if not filters then filters = config[player_name] end
if not filters then return end
local player = game.players[player_name]
for i, item_name in pairs(filters) do
if item_name ~= nil and item_name ~= '' then
player.set_quick_bar_slot(i, item_name)
end
end
end)
local ignoredItems = {
["blueprint"] = true,
["blueprint-book"] = true,
["deconstruction-planner"] = true,
["spidertron-remote"] = true,
["upgrade-planner"] = true
}
--- Saves your quickbar preset to the script-output folder
-- @command save-quickbar
Commands.new_command('save-quickbar', {'expcom-quickbar.description'}, 'Saves your quickbar preset items to file')
:add_alias('save-toolbar')
:register(function(player)
local filters = {}
for i = 1, 100 do
local slot = player.get_quick_bar_slot(i)
-- Need to filter out blueprint and blueprint books because the slot is a LuaItemPrototype and does not contain a way to export blueprint data
if slot ~= nil then
local ignored = ignoredItems[slot.name]
if ignored ~= true then
filters[i] = slot.name
end
end
end
if next(filters) then
PlayerFilters:set(player, filters)
else
PlayerFilters:remove(player)
end
return {'quickbar.saved'}
end)

View File

@@ -0,0 +1,189 @@
local Event = require 'utils.event' ---@dep utils.event
local Global = require 'utils.global' ---@dep utils.global
local config = require 'config.statistics' ---@dep config.statistics
local format_time = _C.format_time
local floor = math.floor
local afk_required = 5*3600 -- 5 minutes
--- Stores players who have been created, required to avoid loss of data
local new_players = {}
Global.register(new_players, function(tbl)
new_players = tbl
end)
--- Stores the statistics on a player
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local AllPlayerData = PlayerData.All
local Statistics = PlayerData.Statistics
Statistics:set_metadata{
display_order = config.display_order
}
--- Update your statistics with any which happened before the data was valid
Statistics:on_load(function(player_name, player_statistics)
local existing_data = AllPlayerData:get(player_name)
if existing_data and existing_data.valid then return end
local counters = config.counters
-- Merge all data from before you data loaded
for key, value in pairs(Statistics:get(player_name, {})) do
if config[key] or counters[key] then
if not player_statistics[key] then
player_statistics[key] = value
else
player_statistics[key] = player_statistics[key] + value
end
end
end
-- Increment your maps played if this is your first time on this map
if new_players[player_name] then
new_players[player_name] = nil
local ctn = player_statistics.MapsPlayed
player_statistics.MapsPlayed = ctn and ctn + 1 or 1
end
return player_statistics
end)
--- Used to format time in minute format
local function format_minutes(value)
return format_time(value*3600, {
long = true,
hours = true,
minutes = true
})
end
--- Used to format time into a clock
local function format_clock(value)
return format_time(value*3600, {
hours=true,
minutes=true,
seconds=false,
time=false,
string=true
})
end
--- Add MapsPlayed if it is enabled
if config.MapsPlayed then
Statistics:combine('MapsPlayed')
Event.add(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
new_players[player.name] = true
end)
end
--- Add Playtime and AfkTime if it is enabled
if config.Playtime or config.AfkTime then
local playtime, afk_time
if config.Playtime then playtime = Statistics:combine('Playtime') playtime:set_metadata{stringify=format_minutes, stringify_short=format_clock} end
if config.AfkTime then afk_time = Statistics:combine('AfkTime') afk_time:set_metadata{stringify=format_minutes, stringify_short=format_clock} end
Event.on_nth_tick(3600, function()
if game.tick == 0 then return end
for _, player in pairs(game.connected_players) do
if playtime then playtime:increment(player) end
if afk_time and player.afk_time > afk_required then afk_time:increment(player) end
end
end)
end
--- Add DistanceTravelled if it is enabled
if config.DistanceTravelled then
local stat = Statistics:combine('DistanceTravelled')
stat:set_metadata{unit=' tiles'}
Event.add(defines.events.on_player_changed_position, function(event)
local player = game.players[event.player_index]
if not player.valid or not player.connected or player.afk_time > afk_required then return end
stat:increment(player)
end)
end
--- Add MachinesRemoved and TreesDestroyed and config.OreMined if it is enabled
if config.MachinesRemoved or config.TreesDestroyed or config.OreMined then
local machines, trees, ore
if config.MachinesRemoved then machines = Statistics:combine('MachinesRemoved') end
if config.TreesDestroyed then trees = Statistics:combine('TreesDestroyed') end
if config.OreMined then ore = Statistics:combine('OreMined') end
local function on_event(event)
if not event.player_index then return end -- Check player is valid
local player = game.players[event.player_index]
if not player.valid or not player.connected then return end
local entity = event.entity -- Check entity is valid
if not entity.valid then return end
if entity.type == 'resource' then ore:increment(player)
elseif entity.type == 'tree' then trees:increment(player)
elseif entity.force == player.force then machines:increment(player) end
end
Event.add(defines.events.on_marked_for_deconstruction, on_event)
Event.add(defines.events.on_player_mined_entity, on_event)
end
--- Add DamageDealt if it is enabled
if config.DamageDealt then
local stat = Statistics:combine('DamageDealt')
Event.add(defines.events.on_entity_damaged, function(event)
local character = event.cause -- Check character is valid
if not character or not character.valid or character.type ~= 'character' then return end
local player = character.player -- Check player is valid
if not player.valid or not player.connected then return end
local entity = event.entity -- Check entity is valid
if not entity.valid or entity.force == player.force or entity.force.name == 'neutral' then return end
stat:increment(player, floor(event.final_damage_amount))
end)
end
--- Add Kills if it is enabled
if config.Kills then
local stat = Statistics:combine('Kills')
Event.add(defines.events.on_entity_died, function(event)
local character = event.cause -- Check character is valid
if not character or not character.valid or character.type ~= 'character' then return end
local player = character.player -- Check player is valid
if not player or not player.valid or not player.connected then return end
local entity = event.entity -- Check entity is valid
if not entity.valid or entity.force == player.force or entity.force.name == 'neutral' then return end
stat:increment(player)
end)
end
--- Add RocketsLaunched if it is enabled
if config.RocketsLaunched then
local stat = Statistics:combine('RocketsLaunched')
Event.add(defines.events.on_rocket_launched, function(event)
local silo = event.rocket_silo -- Check silo is valid
if not silo or not silo.valid then return end
local force = silo.force -- Check force is valid
if not force or not force.valid then return end
for _, player in pairs(force.connected_players) do
stat:increment(player)
end
end)
end
--- Add RocketsLaunched if it is enabled
if config.ResearchCompleted then
local stat = Statistics:combine('ResearchCompleted')
Event.add(defines.events.on_research_finished, function(event)
local research = event.research -- Check research is valid
if event.by_script or not research or not research.valid then return end
local force = research.force -- Check force is valid
if not force or not force.valid then return end
for _, player in pairs(force.connected_players) do
stat:increment(player)
end
end)
end
--- Add all the remaining statistics from the config
for statistic, event_name in pairs(config.counters) do
local stat = Statistics:combine(statistic)
Event.add(event_name, function(event)
if not event.player_index then return end
local player = game.players[event.player_index]
if not player.valid or not player.connected then return end
stat:increment(player)
end)
end

View File

@@ -0,0 +1,90 @@
--[[-- Commands Module - Tag
- Adds a command that allows players to have a custom tag after their name
@data Tag
]]
local Commands = require 'expcore.commands' --- @dep expcore.commands
local Roles = require 'expcore.roles' --- @dep expcore.roles
require 'config.expcore.command_general_parse'
require 'config.expcore.command_role_parse'
require 'config.expcore.command_color_parse'
--- Stores the tag for a player
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local PlayerTags = PlayerData.Settings:combine('Tag')
local PlayerTagColors = PlayerData.Settings:combine('TagColor')
PlayerTags:set_metadata{
permission = 'command/tag'
}
PlayerTagColors:set_metadata{
permission = 'command/tag-color'
}
local set_tag = function (player, tag, color)
if tag == nil or tag == '' then
player.tag = ''
elseif color then
player.tag = '- [color='.. color ..']'..tag..'[/color]'
else
player.tag = '- '..tag
end
end
--- When your tag is updated then apply the changes
PlayerTags:on_update(function(player_name, player_tag)
local player = game.players[player_name]
local player_tag_color = PlayerTagColors:get(player)
set_tag(player, player_tag, player_tag_color)
end)
--- When your tag color is updated then apply the changes
PlayerTagColors:on_update(function(player_name, player_tag_color)
local player = game.players[player_name]
local player_tag = PlayerTags:get(player)
set_tag(player, player_tag, player_tag_color)
end)
--- Sets your player tag.
-- @command tag
-- @tparam string tag the tag that will be after the name, there is a max length
Commands.new_command('tag', {'expcom-tag.description'}, 'Sets your player tag.')
:add_param('tag', false, 'string-max-length', 20)
:enable_auto_concat()
:register(function(player, tag)
PlayerTags:set(player, tag)
end)
--- Sets your player tag color.
-- @command tag
-- @tparam string color name.
Commands.new_command('tag-color', 'Sets your player tag color.')
:add_param('color', false, 'color')
:enable_auto_concat()
:register(function(player, color)
PlayerTagColors:set(player, color)
end)
--- Clears your tag. Or another player if you are admin.
-- @command tag-clear
-- @tparam[opt=self] LuaPlayer player the player to remove the tag from, nil will apply to self
Commands.new_command('tag-clear', {'expcom-tag.description-clear'}, 'Clears your tag. Or another player if you are admin.')
:add_param('player', true, 'player-role')
:set_defaults{player=function(player)
return player -- default is the user using the command
end}
:register(function(player, action_player)
if action_player.index == player.index then
-- no player given so removes your tag
PlayerTags:remove(action_player)
elseif Roles.player_allowed(player, 'command/clear-tag/always') then
-- player given and user is admin so clears that player's tag
PlayerTags:remove(action_player)
else
-- user is not admin and tried to clear another users tag
return Commands.error{'expcore-commands.unauthorized'}
end
end)