mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-30 04:30:58 +02:00
Merge pull request #674 from plague006/fix/player_rewards
Deal with negative numbers in a sane way in player rewards
This commit is contained in:
commit
61b4d04f00
@ -2,16 +2,31 @@ local Global = require 'utils.global'
|
|||||||
local Game = require 'utils.game'
|
local Game = require 'utils.game'
|
||||||
local PlayerStats = require 'features.player_stats'
|
local PlayerStats = require 'features.player_stats'
|
||||||
local Command = require 'utils.command'
|
local Command = require 'utils.command'
|
||||||
|
|
||||||
local format = string.format
|
local format = string.format
|
||||||
|
local abs = math.abs
|
||||||
|
local concat = table.concat
|
||||||
|
|
||||||
local Public = {}
|
local Public = {}
|
||||||
local reward_token = {global.config.player_rewards.token} or {global.config.market.currency} or {'coin'}
|
local reward_token = {global.config.player_rewards.token} or {global.config.market.currency} or {'coin'}
|
||||||
|
|
||||||
Global.register({
|
Global.register(
|
||||||
reward_token = reward_token,
|
{
|
||||||
}, function (tbl)
|
reward_token = reward_token
|
||||||
reward_token = tbl.reward_token
|
},
|
||||||
end)
|
function(tbl)
|
||||||
|
reward_token = tbl.reward_token
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
--- Returns the single or plural form of the token name
|
||||||
|
local function get_token_plural(quantity)
|
||||||
|
if quantity and quantity > 1 then
|
||||||
|
return concat({reward_token[1], 's'})
|
||||||
|
else
|
||||||
|
return reward_token[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Set the item to use for rewards
|
--- Set the item to use for rewards
|
||||||
-- @param reward string - item name to use as reward
|
-- @param reward string - item name to use as reward
|
||||||
@ -30,9 +45,9 @@ Public.get_reward = function()
|
|||||||
return reward_token[1]
|
return reward_token[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gives the player the quantity of reward
|
--- Gives reward tokens to the player
|
||||||
-- @param player <number|LuaPlayer> the player to reward
|
-- @param player <number|LuaPlayer>
|
||||||
-- @param amount <number> the amount of reward tokens to remove
|
-- @param amount <number> of reward tokens
|
||||||
-- @param message <string> an optional message to send to the affected player
|
-- @param message <string> an optional message to send to the affected player
|
||||||
-- @return <number> indicating how many were inserted or if operation failed
|
-- @return <number> indicating how many were inserted or if operation failed
|
||||||
Public.give_reward = function(player, amount, message)
|
Public.give_reward = function(player, amount, message)
|
||||||
@ -61,9 +76,9 @@ Public.give_reward = function(player, amount, message)
|
|||||||
return coin_difference
|
return coin_difference
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Removes an amount of rewards from the player
|
--- Removes reward tokens from the player
|
||||||
-- @param player <number|LuaPlayer> the player to reward
|
-- @param player <number|LuaPlayer>
|
||||||
-- @param amount <number> the amount of reward tokens to remove
|
-- @param amount <number> of reward tokens
|
||||||
-- @param message <string> an optional message to send to the affected player
|
-- @param message <string> an optional message to send to the affected player
|
||||||
-- @return <number> indicating how many were removed or if operation failed
|
-- @return <number> indicating how many were removed or if operation failed
|
||||||
Public.remove_reward = function(player, amount, message)
|
Public.remove_reward = function(player, amount, message)
|
||||||
@ -92,7 +107,7 @@ end
|
|||||||
Command.add(
|
Command.add(
|
||||||
'reward',
|
'reward',
|
||||||
{
|
{
|
||||||
description = 'Gives a reward to a target player',
|
description = 'Gives a reward to a target player (removes if quantity is negative)',
|
||||||
arguments = {'target', 'quantity', 'reason'},
|
arguments = {'target', 'quantity', 'reason'},
|
||||||
default_values = {reason = false},
|
default_values = {reason = false},
|
||||||
admin_only = true,
|
admin_only = true,
|
||||||
@ -106,21 +121,32 @@ Command.add(
|
|||||||
player_name = player.name
|
player_name = player.name
|
||||||
end
|
end
|
||||||
|
|
||||||
local target = game.players[args.target]
|
local target_name = args.target
|
||||||
local target_name
|
local target = game.players[target_name]
|
||||||
if target then
|
if not target then
|
||||||
target_name = args.target
|
|
||||||
else
|
|
||||||
player.print('Target not found.')
|
player.print('Target not found.')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
Public.give_reward(target, args.quantity)
|
local quantity = tonumber(args.quantity)
|
||||||
local string = format('%s has rewarded %s with %s %s', player_name, target_name, args.quantity, reward_token[1])
|
if quantity > 0 then
|
||||||
if args.reason then
|
Public.give_reward(target, quantity)
|
||||||
string = format('%s for %s', string, args.reason)
|
local string = format('%s has rewarded %s with %s %s', player_name, target_name, get_token_plural(quantity), reward_token[1])
|
||||||
|
if args.reason then
|
||||||
|
string = format('%s for %s', string, args.reason)
|
||||||
|
end
|
||||||
|
game.print(string)
|
||||||
|
elseif quantity < 0 then
|
||||||
|
quantity = abs(quantity)
|
||||||
|
Public.remove_reward(target, quantity)
|
||||||
|
local string = format('%s has punished %s by taking %s %s', player_name, target_name, quantity, get_token_plural(quantity))
|
||||||
|
if args.reason then
|
||||||
|
string = format('%s for %s', string, args.reason)
|
||||||
|
end
|
||||||
|
game.print(string)
|
||||||
|
else
|
||||||
|
Game.player_print(" A reward of 0 is neither a reward nor a punishment, it's just dumb. Try harder.")
|
||||||
end
|
end
|
||||||
game.print(string)
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user