mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-04 09:42:30 +02:00
Add player_reward, add coins as reward for looking at info (#546)
* Add player_reward, add coins as reward for looking at info * Remove table entries for completed rewards
This commit is contained in:
parent
074ea00c54
commit
eb892a372e
14
config.lua
14
config.lua
@ -1,12 +1,13 @@
|
||||
_DEBUG = false
|
||||
_CHEATS = false
|
||||
local market_item = 'coin'
|
||||
|
||||
global.config = {
|
||||
map_info = {
|
||||
-- The title of the map
|
||||
map_name_key = 'This Map has no name',
|
||||
-- The long description of the map, typically 1 paragraph
|
||||
map_description_key = "By default this section is blank as it's supposed to be filled out on a per map basis. (If you're seeing this message, ping the admin team to get a description added for this map)",
|
||||
map_description_key = "This section is supposed to be filled out on a per map basis. If you're seeing this message, ping the admin team to get a description added for this map. A 20 coin bonus is available to the first person to point this out.",
|
||||
-- The feature list of the map
|
||||
map_extra_info_key = 'This map has no extra information',
|
||||
-- New Scenario Features, appears in the "What's new" tab
|
||||
@ -48,7 +49,7 @@ global.config = {
|
||||
-- adds a fish market
|
||||
fish_market = {
|
||||
enabled = true,
|
||||
market_item = 'coin',
|
||||
market_item = market_item,
|
||||
},
|
||||
-- adds anti-nuke griefing
|
||||
nuke_control = {
|
||||
@ -76,6 +77,7 @@ global.config = {
|
||||
mentions = {
|
||||
enabled = true,
|
||||
},
|
||||
-- settings for when a player joins the server for the first time
|
||||
player_create = {
|
||||
enabled = true,
|
||||
-- items automatically inserted into the player inventory
|
||||
@ -148,6 +150,14 @@ global.config = {
|
||||
['big-worm-turret'] = {['small-biter'] = 3.8, ['medium-biter'] = 1.3, ['big-biter'] = 1.1},
|
||||
},
|
||||
},
|
||||
-- grants reward coins for certain actions
|
||||
player_rewards = {
|
||||
enabled = true,
|
||||
-- the token to use for rewards
|
||||
token = market_item,
|
||||
-- rewards players for looking through the info tabs
|
||||
info_player_reward = true,
|
||||
},
|
||||
-- automatically marks miners for deconstruction when they are depleted (currently compatible with hard mods that add miners)
|
||||
autodeconstruct = {
|
||||
enabled = true,
|
||||
|
@ -273,7 +273,8 @@ local function player_created(event)
|
||||
return
|
||||
end
|
||||
|
||||
player.insert {name = market_item, count = 10}
|
||||
local count = global.config.player_rewards.info_player_reward and 1 or 10
|
||||
player.insert {name = market_item, count = count}
|
||||
end
|
||||
|
||||
local function init()
|
||||
|
@ -3,6 +3,8 @@ local Global = require 'utils.global'
|
||||
local Event = require 'utils.event'
|
||||
local UserGroups = require 'features.user_groups'
|
||||
local Game = require 'utils.game'
|
||||
local PlayerRewards = require 'utils.player_rewards'
|
||||
local format = string.format
|
||||
|
||||
local normal_color = {r = 1, g = 1, b = 1}
|
||||
local focus_color = {r = 1, g = 0.55, b = 0.1}
|
||||
@ -13,12 +15,27 @@ local rank_colors = {
|
||||
{r = 0.093, g = 0.768, b = 0.172} -- Admin
|
||||
}
|
||||
|
||||
local reward_amount = 2
|
||||
local reward_plural_indicator = reward_amount > 1 and 's' or ''
|
||||
local reward_token = PlayerRewards.get_reward()
|
||||
local info_tab_flags = {
|
||||
0x1, -- welcome
|
||||
0x2, -- rules
|
||||
0x4, -- map_info
|
||||
0x8, -- scenario_mods
|
||||
0x10, -- whats_new
|
||||
}
|
||||
local flags_sum = 0
|
||||
for _, v in pairs(info_tab_flags) do
|
||||
flags_sum = flags_sum + v
|
||||
end
|
||||
|
||||
local map_name_key = 1
|
||||
local map_description_key = 2
|
||||
local map_extra_info_key = 3
|
||||
local new_info_key = 4
|
||||
|
||||
local welcomed_players = {}
|
||||
local rewarded_players = {}
|
||||
|
||||
local editable_info = {
|
||||
[map_name_key] = global.config.map_info.map_name_key,
|
||||
@ -29,11 +46,11 @@ local editable_info = {
|
||||
|
||||
Global.register(
|
||||
{
|
||||
welcomed_players = welcomed_players,
|
||||
editable_info = editable_info
|
||||
rewarded_players = rewarded_players,
|
||||
editable_info = editable_info,
|
||||
},
|
||||
function(tbl)
|
||||
welcomed_players = tbl.welcomed_players
|
||||
rewarded_players = tbl.rewarded_players
|
||||
editable_info = tbl.editable_info
|
||||
end
|
||||
)
|
||||
@ -121,11 +138,11 @@ end
|
||||
|
||||
local pages = {
|
||||
{
|
||||
tab_button = function(parent, player)
|
||||
tab_button = function(parent)
|
||||
local button = parent.add {type = 'button', name = tab_button_name, caption = 'Welcome'}
|
||||
return button
|
||||
end,
|
||||
content = function(parent, player)
|
||||
content = function(parent)
|
||||
local parent_style = parent.style
|
||||
parent_style.right_padding = 2
|
||||
|
||||
@ -142,14 +159,12 @@ local pages = {
|
||||
centered_label(
|
||||
parent,
|
||||
[[
|
||||
Redmew is community for players of all skill levels committed to pushing the limits of Factorio Multiplayer through custom scripts and crazy map designs.
|
||||
Redmew is a community for players of all skill levels committed to pushing the limits of Factorio Multiplayer through custom scripts and crazy map designs.
|
||||
|
||||
We are a friendly bunch, our objective is to have as much fun as possible and we hope you will too.
|
||||
]]
|
||||
)
|
||||
|
||||
|
||||
|
||||
header_label(parent, 'How To Chat')
|
||||
centered_label(
|
||||
parent,
|
||||
@ -160,7 +175,13 @@ This can be changed in options -> controls -> "toggle lua console".
|
||||
]]
|
||||
)
|
||||
|
||||
|
||||
if global.config.player_rewards.enabled and global.config.player_rewards.info_player_reward then
|
||||
local string = format('You have been given %s %s%s for looking at the welcome tab.\nChecking each tab will reward you %s more %s%s.\n', reward_amount, reward_token, reward_plural_indicator, reward_amount, reward_token, reward_plural_indicator)
|
||||
header_label(parent, 'Free Coins')
|
||||
centered_label(
|
||||
parent, string
|
||||
)
|
||||
end
|
||||
|
||||
header_label(parent, 'Useful Links')
|
||||
centered_label(parent, [[Check out our discord for new map info and to suggest new maps / ideas.]])
|
||||
@ -202,11 +223,11 @@ This can be changed in options -> controls -> "toggle lua console".
|
||||
end
|
||||
},
|
||||
{
|
||||
tab_button = function(parent, player)
|
||||
tab_button = function(parent)
|
||||
local button = parent.add {type = 'button', name = tab_button_name, caption = 'Rules'}
|
||||
return button
|
||||
end,
|
||||
content = function(parent, player)
|
||||
content = function(parent)
|
||||
header_label(parent, 'Rules')
|
||||
|
||||
centered_label(
|
||||
@ -222,7 +243,7 @@ If you suspect someone is griefing, notify the admin team by using /report or by
|
||||
end
|
||||
},
|
||||
{
|
||||
tab_button = function(parent, player)
|
||||
tab_button = function(parent)
|
||||
local button = parent.add {type = 'button', name = tab_button_name, caption = 'Map Info'}
|
||||
return button
|
||||
end,
|
||||
@ -293,7 +314,7 @@ If you suspect someone is griefing, notify the admin team by using /report or by
|
||||
end
|
||||
},
|
||||
{
|
||||
tab_button = function(parent, player)
|
||||
tab_button = function(parent)
|
||||
local button = parent.add {type = 'button', name = tab_button_name, caption = 'Scenario Mods'}
|
||||
return button
|
||||
end,
|
||||
@ -593,6 +614,29 @@ local function draw_main_frame(center, player)
|
||||
player.opened = frame
|
||||
end
|
||||
|
||||
local function reward_player(player, index, message)
|
||||
if not global.config.player_rewards.enabled or not global.config.player_rewards.info_player_reward then
|
||||
return
|
||||
end
|
||||
|
||||
local player_index = player.index
|
||||
if not rewarded_players[player_index] then
|
||||
error('Player with no entry in rewarded_players table')
|
||||
return false
|
||||
end
|
||||
local tab_flag = info_tab_flags[index]
|
||||
|
||||
if bit32.band(rewarded_players[player_index], tab_flag) == tab_flag then
|
||||
return
|
||||
else
|
||||
PlayerRewards.give_reward(player, reward_amount, message)
|
||||
rewarded_players[player_index] = rewarded_players[player_index] + tab_flag
|
||||
if rewarded_players[player_index] == flags_sum then
|
||||
rewarded_players[player_index] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function toggle(event)
|
||||
local player = event.player
|
||||
local center = player.gui.center
|
||||
@ -607,21 +651,15 @@ end
|
||||
|
||||
local function player_created(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
|
||||
if not player or not player.valid then
|
||||
return
|
||||
end
|
||||
|
||||
local gui = player.gui
|
||||
|
||||
gui.top.add {type = 'sprite-button', name = main_button_name, sprite = 'utility/questionmark'}
|
||||
|
||||
if player.admin or UserGroups.is_regular(player.name) or welcomed_players[player.index] then
|
||||
return
|
||||
end
|
||||
|
||||
welcomed_players[player.index] = true
|
||||
draw_main_frame(gui.center, player)
|
||||
rewarded_players[player.index] = 0
|
||||
reward_player(player, info_tab_flags[1])
|
||||
end
|
||||
|
||||
Event.add(defines.events.on_player_created, player_created)
|
||||
@ -655,6 +693,10 @@ Gui.on_click(
|
||||
Gui.clear(content)
|
||||
|
||||
pages[index].content(content, player)
|
||||
local string = format('%s %s%s awarded for reading a tab on the info screen.', reward_amount, reward_token, reward_plural_indicator)
|
||||
if rewarded_players[player.index] then
|
||||
reward_player(player, index, string)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
|
130
utils/player_rewards.lua
Normal file
130
utils/player_rewards.lua
Normal file
@ -0,0 +1,130 @@
|
||||
local Global = require 'utils.global'
|
||||
local Game = require 'utils.game'
|
||||
local PlayerStats = require 'features.player_stats'
|
||||
local Command = require 'utils.command'
|
||||
local format = string.format
|
||||
|
||||
local Public = {}
|
||||
local reward_token = {global.config.player_rewards.token} or {'coin'}
|
||||
|
||||
Global.register({
|
||||
reward_token = reward_token,
|
||||
}, function (tbl)
|
||||
reward_token = tbl.reward_token
|
||||
end)
|
||||
|
||||
--- 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
|
||||
|
||||
--- Gives the player the quantity of reward
|
||||
-- @param player player_index number or LuaPlayer table -- the player to reward
|
||||
-- @param amount number - the amount of reward tokens to give
|
||||
-- @param message string - an optional message to send to the affected player
|
||||
-- @return number - indicating how many were inserted or if operation failed
|
||||
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
|
||||
player = Game.get_player_by_index(player)
|
||||
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
|
||||
|
||||
--- Removes an amount of rewards from the player
|
||||
-- @param player player_index number or LuaPlayer table -- the player to reward
|
||||
-- @param amount number - the amount of reward tokens to remove
|
||||
-- @param message string - an optional message to send to the affected player
|
||||
-- @return number - indicating how many were removed or if operation failed
|
||||
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
|
||||
player = Game.get_player_by_index(player)
|
||||
else
|
||||
player_index = player.index
|
||||
end
|
||||
if type(player) == 'number' then
|
||||
player = Game.get_player_by_index(player)
|
||||
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',
|
||||
{
|
||||
description = 'Gives a reward to a target player',
|
||||
arguments = {'target', 'quantity', 'reason'},
|
||||
default_values = {reason = false},
|
||||
admin_only = true,
|
||||
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
|
||||
|
||||
local target = game.players[args.target]
|
||||
local target_name
|
||||
if target then
|
||||
target_name = args.target
|
||||
else
|
||||
player.print('Target not found.')
|
||||
return
|
||||
end
|
||||
|
||||
Public.give_reward(target, args.quantity)
|
||||
local string = format('%s has rewarded %s with %s %s', player_name, target_name, args.quantity, reward_token[1])
|
||||
if args.reason then
|
||||
string = format('%s for %s', string, args.reason)
|
||||
end
|
||||
game.print(string)
|
||||
end
|
||||
)
|
||||
|
||||
return Public
|
Loading…
Reference in New Issue
Block a user