From d7befd931b863f036b1b2a69c1979e281b741fa3 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 2 Jun 2019 17:19:52 +0100 Subject: [PATCH] Added bonus --- config/_file_loader.lua | 1 + config/bonuses.lua | 9 ++++++ config/roles.lua | 7 +++++ expcore/commands.lua | 11 ++++++-- modules/commands/bonus.lua | 56 +++++++++++++++++++++++++++++++++++++- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 config/bonuses.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 23eb99dd..f1342f8a 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -23,6 +23,7 @@ return { 'modules.commands.spawn', 'modules.commands.warnings', 'modules.commands.find', + 'modules.commands.bonus', -- QoL Addons 'modules.addons.chat-popups', 'modules.addons.damage-popups', diff --git a/config/bonuses.lua b/config/bonuses.lua new file mode 100644 index 00000000..d74ddfbf --- /dev/null +++ b/config/bonuses.lua @@ -0,0 +1,9 @@ +--- Lists all bonuses which can be used, name followed by min max +return { + character_mining_speed_modifier={0,3}, + character_crafting_speed_modifier={0,3}, + character_running_speed_modifier={0,3}, + character_build_distance_bonus={0,20}, + character_reach_distance_bonus={0,20}, + character_inventory_slots_bonus={0,200} +} \ No newline at end of file diff --git a/config/roles.lua b/config/roles.lua index 9c6594d4..c42e8e6f 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -34,6 +34,7 @@ Roles.new_role('System','SYS') :set_flag('is_admin') :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_allow_all() Roles.new_role('Senior Administrator','SAdmin') @@ -41,6 +42,7 @@ Roles.new_role('Senior Administrator','SAdmin') :set_flag('is_admin') :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_parent('Administrator') :allow{ 'command/interface', @@ -54,6 +56,7 @@ Roles.new_role('Administrator','Admin') :set_flag('is_admin') :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_parent('Moderator') :allow{ } @@ -64,6 +67,7 @@ Roles.new_role('Moderator','Mod') :set_flag('is_admin') :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_parent('Trainee') :allow{ 'command/assign-role', @@ -76,6 +80,7 @@ Roles.new_role('Moderator','Mod') 'command/clear-warnings', 'command/clear-temp-ban', 'command/clear-inventory', + 'command/bonus', 'gui/rocket-info/toggle-active', 'gui/rocket-info/remote_launch', } @@ -106,6 +111,7 @@ Roles.new_role('Sponsor','Spon') :set_custom_color{r=247,g=246,b=54} :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_parent('Pay to Win') :allow{ } @@ -115,6 +121,7 @@ Roles.new_role('Pay to Win','P2W') :set_custom_color{r=238,g=172,b=44} :set_flag('is_spectator') :set_flag('report-immune') +:set_flag('instance-respawn') :set_parent('Donator') :allow{ 'gui/rocket-info/toggle-active', diff --git a/expcore/commands.lua b/expcore/commands.lua index feb40942..ffa13acd 100644 --- a/expcore/commands.lua +++ b/expcore/commands.lua @@ -427,7 +427,7 @@ end --- Adds a new param to the command this will be displayed in the help and used to parse the input -- @tparam string name the name of the new param that is being added to the command --- @tparam[opt=true] boolean optional is this param required for this command, these must be after all required params +-- @tparam[opt=false] boolean optional is this param required for this command, these must be after all required params -- @tparam[opt=pass function through] ?string|function parse this function will take the input and return a new (or same) value -- @param[opt] ... extra args you want to pass to the parse function; for example if the parse is general use -- parse param - input: string - the input given by the user for this param @@ -436,12 +436,17 @@ end -- parse return - the value that will be passed to the command callback, must not be nil and if reject then command is not run -- @treturn Commands._prototype pass through to allow more functions to be called function Commands._prototype:add_param(name,optional,parse,...) - if optional ~= false then optional = true end + local parse_args = {...} + if type(optional) ~= 'boolean' then + parse_args = {parse,...} + parse = optional + optional = false + end parse = parse or function(string) return string end self.params[name] = { optional=optional, parse=parse, - parse_args={...} + parse_args=parse_args } self.max_param_count = self.max_param_count+1 if not optional then diff --git a/modules/commands/bonus.lua b/modules/commands/bonus.lua index e5023f03..2e02b706 100644 --- a/modules/commands/bonus.lua +++ b/modules/commands/bonus.lua @@ -1,4 +1,58 @@ local Commands = require 'expcore.commands' +local Roles = require 'expcore.roles' +local Event = require 'utils.event' +local Game = require 'utils.game' +local Store = require 'expcore.store' +local config = require 'config.bonuses' require 'config.expcore-commands.parse_general' -Commands.new_command('bonus','Changes the amount of bonus you receive') \ No newline at end of file +local bonus_store = +Store.register(function(value,category) + local player = Game.get_player_from_any(category) + for bonus,min_max in pairs(config) do + local increase = min_max[2]*value + player[bonus] = min_max[1]+increase + end +end) + +Commands.new_command('bonus','Changes the amount of bonus you receive') +:add_param('amount','integer-range',0,50) +:register(function(player,amount) + local percent = amount/100 + Store.set(bonus_store,player.name,percent) +end) + +Event.add(defines.events.on_player_respawned,function(event) + local player = Game.get_player_by_index(event.player_index) + local value = Store.get(bonus_store,player.name) + if value then + for bonus,min_max in pairs(config) do + local increase = min_max[2]*value + player[bonus] = min_max[1]+increase + end + end +end) + +Event.add(defines.events.on_pre_player_died,function(event) + local player = Game.get_player_by_index(event.player_index) + if Roles.player_has_flag(player,'instance-respawn') then + player.ticks_to_respawn = 120 + -- manually dispatch death event because it is not fired when ticks_to_respawn is set pre death + Event.dispatch{ + name=defines.events.on_player_died, + tick=event.tick, + player_index=event.player_index, + cause = event.cause + } + end +end) + +local function role_update(event) + local player = Game.get_player_by_index(event.player_index) + if not Roles.player_allowed(player,'command/bonus') then + Store.clear(bonus_store,player.name) + end +end + +Event.add(Roles.player_role_assigned,role_update) +Event.add(Roles.player_role_unassigned,role_update) \ No newline at end of file