2018-12-19 15:56:02 +02:00
|
|
|
local Global = require 'utils.global'
|
|
|
|
local Game = require 'utils.game'
|
|
|
|
local PlayerStats = require 'features.player_stats'
|
|
|
|
local Command = require 'utils.command'
|
2019-01-30 05:52:43 +02:00
|
|
|
local Ranks = require 'resources.ranks'
|
2019-01-23 22:28:27 +02:00
|
|
|
|
2018-12-19 15:56:02 +02:00
|
|
|
local format = string.format
|
2019-01-23 22:28:27 +02:00
|
|
|
local abs = math.abs
|
|
|
|
local concat = table.concat
|
2018-12-19 15:56:02 +02:00
|
|
|
|
|
|
|
local Public = {}
|
2019-01-10 16:18:55 +02:00
|
|
|
local reward_token = {global.config.player_rewards.token} or {global.config.market.currency} or {'coin'}
|
2018-12-19 15:56:02 +02:00
|
|
|
|
2019-01-23 22:28:27 +02:00
|
|
|
Global.register(
|
|
|
|
{
|
|
|
|
reward_token = reward_token
|
|
|
|
},
|
|
|
|
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
|
2018-12-19 15:56:02 +02:00
|
|
|
|
|
|
|
--- Set the item to use for rewards
|
|
|
|
-- @param reward string - item name to use as reward
|
|
|
|
-- @return boolean true - indicating success
|
|
|
|
Public.set_reward = function(reward)
|
|
|
|
if global.config.player_rewards.enabled == false then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
reward_token[1] = reward
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the name of the reward item
|
|
|
|
Public.get_reward = function()
|
|
|
|
return reward_token[1]
|
|
|
|
end
|
|
|
|
|
2019-01-27 22:23:52 +02:00
|
|
|
--- Gives reward tokens to the player
|
|
|
|
-- @param player <number|LuaPlayer>
|
|
|
|
-- @param amount <number> of reward tokens
|
2019-01-11 22:52:25 +02:00
|
|
|
-- @param message <string> an optional message to send to the affected player
|
|
|
|
-- @return <number> indicating how many were inserted or if operation failed
|
2018-12-19 15:56:02 +02:00
|
|
|
Public.give_reward = function(player, amount, message)
|
|
|
|
if global.config.player_rewards.enabled == false then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_index
|
|
|
|
if type(player) == 'number' then
|
|
|
|
player_index = player
|
2019-05-16 12:10:56 +02:00
|
|
|
player = game.get_player(player)
|
2018-12-19 15:56:02 +02:00
|
|
|
else
|
|
|
|
player_index = player.index
|
|
|
|
end
|
|
|
|
local reward = {name = reward_token[1], count = amount}
|
|
|
|
if not player.can_insert(reward) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if message then
|
|
|
|
player.print(message)
|
|
|
|
end
|
|
|
|
local coin_difference = player.insert(reward)
|
|
|
|
if reward_token[1] == 'coin' then
|
|
|
|
PlayerStats.change_coin_earned(player_index, coin_difference)
|
|
|
|
end
|
|
|
|
return coin_difference
|
|
|
|
end
|
|
|
|
|
2019-01-27 22:23:52 +02:00
|
|
|
--- Removes reward tokens from the player
|
|
|
|
-- @param player <number|LuaPlayer>
|
|
|
|
-- @param amount <number> of reward tokens
|
2019-01-11 22:52:25 +02:00
|
|
|
-- @param message <string> an optional message to send to the affected player
|
|
|
|
-- @return <number> indicating how many were removed or if operation failed
|
2018-12-19 15:56:02 +02:00
|
|
|
Public.remove_reward = function(player, amount, message)
|
|
|
|
if global.config.player_rewards.enabled == false then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_index
|
|
|
|
if type(player) == 'number' then
|
|
|
|
player_index = player
|
2019-05-16 12:10:56 +02:00
|
|
|
player = game.get_player(player)
|
2018-12-19 15:56:02 +02:00
|
|
|
else
|
|
|
|
player_index = player.index
|
|
|
|
end
|
|
|
|
local unreward = {name = reward_token[1], count = amount}
|
|
|
|
if message then
|
|
|
|
player.print(message)
|
|
|
|
end
|
|
|
|
local coin_difference = player.remove_item(unreward)
|
|
|
|
if reward_token[1] == 'coin' then
|
|
|
|
PlayerStats.change_coin_earned(player_index, -coin_difference)
|
|
|
|
end
|
|
|
|
return coin_difference
|
|
|
|
end
|
|
|
|
|
|
|
|
Command.add(
|
|
|
|
'reward',
|
|
|
|
{
|
2019-03-04 00:13:56 +02:00
|
|
|
description = {'command_description.reward'},
|
2018-12-19 15:56:02 +02:00
|
|
|
arguments = {'target', 'quantity', 'reason'},
|
|
|
|
default_values = {reason = false},
|
2019-01-30 05:52:43 +02:00
|
|
|
required_rank = Ranks.admin,
|
2018-12-19 15:56:02 +02:00
|
|
|
capture_excess_arguments = true,
|
|
|
|
allowed_by_server = true,
|
|
|
|
allowed_by_player = true
|
|
|
|
},
|
|
|
|
function(args, player)
|
|
|
|
local player_name = 'server'
|
|
|
|
if player then
|
|
|
|
player_name = player.name
|
|
|
|
end
|
|
|
|
|
2019-01-23 22:28:27 +02:00
|
|
|
local target_name = args.target
|
|
|
|
local target = game.players[target_name]
|
|
|
|
if not target then
|
2018-12-19 15:56:02 +02:00
|
|
|
player.print('Target not found.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-01-23 22:28:27 +02:00
|
|
|
local quantity = tonumber(args.quantity)
|
|
|
|
if quantity > 0 then
|
|
|
|
Public.give_reward(target, quantity)
|
2019-02-05 17:50:13 +02:00
|
|
|
local string = format('%s has rewarded %s with %s %s', player_name, target_name, quantity, get_token_plural(quantity))
|
2019-01-23 22:28:27 +02:00
|
|
|
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.")
|
2018-12-19 15:56:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
return Public
|