Added player bonus

This commit is contained in:
Cooldude2606
2020-05-28 22:52:15 +01:00
parent 6a5d5f02d9
commit bfcb229185
2 changed files with 22 additions and 26 deletions

View File

@@ -11,12 +11,11 @@ local Store = require 'expcore.store' --- @dep expcore.store
local config = require 'config.bonuses' --- @dep config.bonuses
require 'config.expcore.command_general_parse'
-- Store bonus percentages keyed by player name
local bonus_store = Store.register(function(player)
return player.name
end)
--- Stores the bonus for the player
local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data
local PlayerBonus = PlayerData.Settings:combine('Bonus')
-- Apply a bonus amount to a player
--- Apply a bonus amount to a player
local function apply_bonus(player, amount)
if not amount then return end
for bonus, min_max in pairs(config) do
@@ -25,6 +24,11 @@ local function apply_bonus(player, amount)
end
end
--- When store is updated apply new bonus to the player
PlayerBonus.on_update(function(player_name, player_bonus)
apply_bonus(game.player[player_name], player_bonus or 0)
end)
--- Changes the amount of bonus you receive
-- @command bonus
-- @tparam number amount range 0-50 the percent increase for your bonus
@@ -32,41 +36,32 @@ 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, percent)
PlayerBonus:set(player, percent)
Commands.print{'expcom-bonus.set', amount}
Commands.print({'expcom-bonus.wip'}, 'orange')
end)
-- When store is updated apply new bonus to the player
Store.watch(bonus_store, function(value, category)
local player = Game.get_player_from_any(category)
apply_bonus(player, value)
end)
-- When a player respawns re-apply bonus
--- When a player respawns re-apply bonus
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)
apply_bonus(player, value)
local player = game.players[event.player_index]
apply_bonus(player, PlayerBonus:get(player))
end)
-- When a player dies allow them to have instant respawn
--- When a player dies allow them to have instant respawn
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]
if Roles.player_has_flag(player, 'instance-respawn') then
player.ticks_to_respawn = 120
end
end)
-- Remove bonus if a player no longer has access to the command
--- Remove bonus if a player no longer has access to the command
local function role_update(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
if not Roles.player_allowed(player, 'command/bonus') then
Store.clear(bonus_store, player)
PlayerBonus:remove(player)
end
end
Event.add(Roles.events.on_role_assigned, role_update)
Event.add(Roles.events.on_role_unassigned, role_update)
return bonus_store
Event.add(Roles.events.on_role_unassigned, role_update)

View File

@@ -1,8 +1,9 @@
--- Greets players on join
-- @addon greetings
local Commands = require 'expcore.commands' ---@dep expcore.commands
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
@@ -23,7 +24,7 @@ end)
-- @command join-message
-- @tparam string message The custom join message that will be used
Commands.new_command('join-message', 'Sets your custom join message')
:add_param('message', false)
:add_param('message', false, 'string-max-length', 255)
:enable_auto_concat()
:register(function(player, message)
if not player then return end