mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 12:16:37 +09:00
Updated Tasks
This commit is contained in:
@@ -11,14 +11,19 @@ local Store = require 'expcore.store' --- @dep expcore.store
|
||||
local config = require 'config.bonuses' --- @dep config.bonuses
|
||||
require 'config.expcore-commands.parse_general'
|
||||
|
||||
local bonus_store =
|
||||
Store.register(function(value,category)
|
||||
local player = Game.get_player_from_any(category)
|
||||
-- Store bonus percentages keyed by player name
|
||||
local bonus_store = Store.register(function(player)
|
||||
return player.name
|
||||
end)
|
||||
|
||||
-- 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
|
||||
local increase = min_max[2]*value
|
||||
local increase = min_max[2]*amount
|
||||
player[bonus] = min_max[1]+increase
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
--- Changes the amount of bonus you receive
|
||||
-- @command bonus
|
||||
@@ -27,22 +32,25 @@ 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)
|
||||
Store.set(bonus_store,player,percent)
|
||||
Commands.print{'expcom-bonus.set',amount}
|
||||
Commands.print({'expcom-bonus.wip'},'orange')
|
||||
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
|
||||
-- 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
|
||||
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)
|
||||
end)
|
||||
|
||||
-- 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)
|
||||
if Roles.player_has_flag(player,'instance-respawn') then
|
||||
@@ -50,12 +58,15 @@ Event.add(defines.events.on_player_died,function(event)
|
||||
end
|
||||
end)
|
||||
|
||||
-- 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)
|
||||
if not Roles.player_allowed(player,'command/bonus') then
|
||||
Store.clear(bonus_store,player.name)
|
||||
Store.clear(bonus_store,player)
|
||||
end
|
||||
end
|
||||
|
||||
Event.add(Roles.events.on_role_assigned,role_update)
|
||||
Event.add(Roles.events.on_role_unassigned,role_update)
|
||||
Event.add(Roles.events.on_role_unassigned,role_update)
|
||||
|
||||
return bonus_store
|
||||
@@ -1,27 +1,12 @@
|
||||
--[[-- Control Module - Tasks
|
||||
- Stores tasks for each force.
|
||||
@control Tasks
|
||||
@alias Tasks
|
||||
- Stores tasks for each force.
|
||||
@control Tasks
|
||||
@alias Tasks
|
||||
|
||||
@usage
|
||||
-- import the module from the control modules
|
||||
local Tasks = require 'modules.control.tasks' --- @dep modules.control.tasks
|
||||
@usage-- Making and then editing a new task
|
||||
local task_id = Tasks.add_task(game.player.force.name,nil,game.player.name)
|
||||
|
||||
-- To create a new task all you need is the name of the force you want to add the task to
|
||||
-- you can give a place to add it but this is optional
|
||||
Tasks.new_task('player')
|
||||
|
||||
-- You can then update this task to what ever value that you want
|
||||
-- the task id is returned by new_task, or within an update handler
|
||||
-- if a player name is not given that it will assume '<server>'
|
||||
Tasks.update_task(task_id,'My number one task!','Cooldude2606')
|
||||
|
||||
-- You can then remove the task and all data linked with it
|
||||
Tasks.remove_task(task_id)
|
||||
|
||||
-- set_editing and is_editing may be used to block other or provide warnings
|
||||
-- none of this is enforced by this module and so you must do so in your own module
|
||||
Tasks.set_editing(task_id,'Cooldude2606',true)
|
||||
Tasks.update_task(task_id,'We need more iron!',game.player.name)
|
||||
|
||||
]]
|
||||
|
||||
@@ -29,148 +14,169 @@ local Store = require 'expcore.store' --- @dep expcore.store
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local Token = require 'utils.token' --- @dep utils.token
|
||||
|
||||
local Tasks = {
|
||||
store = 'gui.left.task-list.tasks',
|
||||
handlers = {},
|
||||
details = {},
|
||||
forces = {}
|
||||
}
|
||||
local Tasks = {}
|
||||
|
||||
local task_details = Tasks.details
|
||||
local force_tasks = Tasks.forces
|
||||
Global.register({
|
||||
task_details=task_details,
|
||||
force_tasks=force_tasks
|
||||
},function(tbl)
|
||||
Tasks.details = tbl.task_details
|
||||
Tasks.forces = tbl.force_tasks
|
||||
task_details = Tasks.details
|
||||
force_tasks = Tasks.forces
|
||||
-- Global lookup table for force name to task ids
|
||||
local force_tasks = {}
|
||||
Global.register(force_tasks,function(tbl)
|
||||
force_tasks = tbl
|
||||
end)
|
||||
|
||||
local task_store = Tasks.store
|
||||
Store.register(task_store,function(value,task_id)
|
||||
local details = task_details[task_id]
|
||||
local force = game.forces[details.force]
|
||||
for _,handler in pairs(Tasks.handlers) do
|
||||
handler(force,task_id)
|
||||
end
|
||||
end)
|
||||
-- Task store is keyed by task id, value is a table
|
||||
local task_store = Store.register()
|
||||
Tasks.store = task_store
|
||||
|
||||
--- Setters.
|
||||
-- functions used to created and alter tasks
|
||||
-- @section setters
|
||||
|
||||
--- Adds a new handler for when a task is updated
|
||||
-- @tparam function callback the callback which is ran when a task is updated
|
||||
-- @treturn boolean true if the callback was added
|
||||
function Tasks.add_handler(callback)
|
||||
if type(callback) == 'function' then
|
||||
table.insert(Tasks.handlers,callback)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
--[[-- Add a new task for a force, the task can be placed into a certain position for that force
|
||||
@tparam string force_name the name of the force to add the task for
|
||||
@tparam[opt] number task_number the order place to add the task to, appends to end if omited
|
||||
@tparam[opt] string player_name the player who added this task, will cause them to be listed under editing
|
||||
@treturn string the uid of the task which was created
|
||||
|
||||
--- Adds a new task for a force, with option to place it in a certain order
|
||||
-- @tparam string force_name the name of the force to add the task for
|
||||
-- @tparam[opt] number task_number the order place to add the task to, adds to end if omited
|
||||
-- @tparam[opt] string player_name when given this player will be added to the editing list
|
||||
-- @treturn string the uid of the task which was created
|
||||
function Tasks.new_task(force_name,task_number,player_name)
|
||||
@usage-- Adding a new task for your force
|
||||
local task_id = Tasks.add_task(game.player.force.name,nil,game.player.name)
|
||||
|
||||
]]
|
||||
function Tasks.add_task(force_name,task_number,player_name)
|
||||
-- Get a new task id
|
||||
local task_id = tostring(Token.uid())
|
||||
|
||||
-- Get the existing tasks for this force
|
||||
local tasks = force_tasks[force_name]
|
||||
if not tasks then
|
||||
force_tasks[force_name] = {}
|
||||
tasks = force_tasks[force_name]
|
||||
end
|
||||
|
||||
-- Insert the task id into the forces tasks
|
||||
if task_number then
|
||||
table.insert(tasks,task_number,task_id)
|
||||
else
|
||||
table.insert(tasks,task_id)
|
||||
end
|
||||
|
||||
task_details[task_id] = {
|
||||
task_id=task_id,
|
||||
force=force_name,
|
||||
last_edit_player=player_name or '<server>',
|
||||
last_edit_time=game.tick,
|
||||
editing={}
|
||||
}
|
||||
|
||||
-- Create the editing table
|
||||
local editing = {}
|
||||
if player_name then
|
||||
task_details[task_id].editing[player_name] = true
|
||||
editing[player_name] = true
|
||||
end
|
||||
|
||||
Store.set(task_store,task_id,'New Task')
|
||||
-- Add the new task to the store
|
||||
Store.set(task_store,task_id,{
|
||||
task_id = task_id,
|
||||
force_name = force_name,
|
||||
message = 'New Task',
|
||||
last_edit_name = player_name or '<server>',
|
||||
last_edit_time = game.tick,
|
||||
curently_editing = editing
|
||||
})
|
||||
|
||||
return task_id
|
||||
end
|
||||
|
||||
--- Removes a task and all data linked with it
|
||||
-- @tparam string task_id the uid of the task which you want to remove
|
||||
--[[-- Removes a task and any data that is linked with it
|
||||
@tparam string task_id the uid of the task which you want to remove
|
||||
|
||||
@usage-- Removing a task
|
||||
Tasks.remove_task(task_id)
|
||||
|
||||
]]
|
||||
function Tasks.remove_task(task_id)
|
||||
local details = task_details[task_id]
|
||||
local force = details.force
|
||||
local task = Store.get(task_store,task_id)
|
||||
local force_name = task.force_name
|
||||
Store.clear(task_store,task_id)
|
||||
task_details[task_id] = nil
|
||||
table.remove_element(force_tasks[force],task_id)
|
||||
table.remove_element(force_tasks[force_name],task_id)
|
||||
end
|
||||
|
||||
--- Updates a task message
|
||||
-- @tparam string task_id the uid of the task that you want to update
|
||||
-- @tparam string task the message that you want to change the task to
|
||||
-- @tparam[opt='server'] string player_name the name of the player who made the edit
|
||||
function Tasks.update_task(task_id,task,player_name)
|
||||
local details = task_details[task_id]
|
||||
details.last_edit_player = player_name or '<server>'
|
||||
details.last_edit_time = game.tick
|
||||
Store.set(task_store,task_id,task)
|
||||
--[[-- Update the message and last edited information for a task
|
||||
@tparam string task_id the uid of the task that you want to update
|
||||
@tparam string new_message the message that you want to have for the task
|
||||
@tparam[opt='server'] string player_name the name of the player who made the edit
|
||||
|
||||
@usage-- Updating the message for on a task
|
||||
Task.update_task(task_id,'We need more iron!',game.player.name)
|
||||
|
||||
]]
|
||||
function Tasks.update_task(task_id,new_message,player_name)
|
||||
Store.update(task_store,task_id,function(task)
|
||||
task.last_edit_name = player_name or '<server>'
|
||||
task.last_edit_time = game.tick
|
||||
task.message = new_message
|
||||
end)
|
||||
end
|
||||
|
||||
--- Sets a player to be editing this task, used with is_editing
|
||||
-- @tparam string task_id the uid of the task that you want to editing for
|
||||
-- @tparam string player_name the name of the player you want to set editing for
|
||||
-- @tparam[opt] boolean state the new state to set editing to
|
||||
--[[-- Set the editing state for a player, can be used as a warning or to display a text field
|
||||
@tparam string task_id the uid of the task that you want to effect
|
||||
@tparam string player_name the name of the player you want to set the state for
|
||||
@tparam boolean state the new state to set editing to
|
||||
|
||||
@usage-- Setting your editing state to true
|
||||
Tasks.set_editing(task_id,game.player.name,true)
|
||||
|
||||
]]
|
||||
function Tasks.set_editing(task_id,player_name,state)
|
||||
local details = task_details[task_id]
|
||||
details.editing[player_name] = state
|
||||
Store.update(task_store,task_id,function(task)
|
||||
task.curently_editing[player_name] = state
|
||||
end)
|
||||
end
|
||||
|
||||
--[[-- Adds an update handler for when a task is added, removed, or updated
|
||||
@tparam function handler the handler which is called when a task is updated
|
||||
|
||||
@usage-- Add a game print when a task is updated
|
||||
Tasks.on_update(function(task)
|
||||
game.print(task.force_name..' now has the task: '..task.message)
|
||||
end)
|
||||
|
||||
]]
|
||||
function Tasks.on_update(handler)
|
||||
Store.watch(task_store,handler)
|
||||
end
|
||||
|
||||
--- Getters.
|
||||
-- function used to get information about tasks
|
||||
-- @section getters
|
||||
|
||||
--- Gets the task stored at this id
|
||||
-- @tparam string task_id the uid of the task you want to get
|
||||
-- @treturn string the task message that was stored here
|
||||
--[[-- Gets the task information that is linked with this id
|
||||
@tparam string task_id the uid of the task you want to get
|
||||
@treturn table the task information
|
||||
|
||||
@usage-- Getting task information outside of on_update
|
||||
local task = Tasks.get_task(task_id)
|
||||
|
||||
]]
|
||||
function Tasks.get_task(task_id)
|
||||
return Store.get(task_store,task_id)
|
||||
end
|
||||
|
||||
--- Gets the task details stored at this id
|
||||
-- @tparam string task_id the uid of the task you want to get
|
||||
-- @treturn table the task details that was stored here
|
||||
function Tasks.get_details(task_id)
|
||||
return task_details[task_id]
|
||||
end
|
||||
--[[-- Gets all the task ids that a force has
|
||||
@tparam string force_name the name of the force that you want the task ids for
|
||||
@treturn table an array of all the task ids
|
||||
|
||||
--- Gets the task ids for a force
|
||||
-- @tparam string force_name the name of the force that you want the ids for
|
||||
-- @treturn table an array of all the task ids
|
||||
function Tasks.get_force_tasks(force_name)
|
||||
@usage-- Getting the task ids for a force
|
||||
local task_ids = Tasks.get_force_task_ids(game.player.force.name)
|
||||
|
||||
]]
|
||||
function Tasks.get_force_task_ids(force_name)
|
||||
return force_tasks[force_name] or {}
|
||||
end
|
||||
|
||||
--- Gets if a player is currently editing this task
|
||||
-- @tparam string task_id the uid of the task you want to check
|
||||
-- @tparam string player_name the name of the player that you want to check
|
||||
-- @treturn boolean weather the player is currently editing this task
|
||||
function Tasks.is_editing(task_id,player_name)
|
||||
local details = task_details[task_id]
|
||||
return details.editing[player_name]
|
||||
--[[-- Gets the editing state for a player
|
||||
@tparam string task_id the uid of the task you want to check
|
||||
@tparam string player_name the name of the player that you want to check
|
||||
@treturn boolean weather the player is currently editing this task
|
||||
|
||||
@usage-- Check if a player is editing a task or not
|
||||
local editing = Tasks.get_editing(task_id,game.player.name)
|
||||
|
||||
]]
|
||||
function Tasks.get_editing(task_id,player_name)
|
||||
local task = Store.get(task_store,task_id)
|
||||
return task.curently_editing[player_name]
|
||||
end
|
||||
|
||||
-- Module Return
|
||||
return Tasks
|
||||
@@ -12,10 +12,9 @@ local format_time,table_keys = ext_require('expcore.common','format_time','table
|
||||
local Tasks = require 'modules.control.tasks' --- @dep modules.control.tasks
|
||||
|
||||
--- If a player is allowed to use the edit buttons
|
||||
local function player_allowed_edit(player,task_id)
|
||||
if task_id then
|
||||
local details = Tasks.get_details(task_id)
|
||||
if config.user_can_edit_own_tasks and details.last_edit_player == player.name then
|
||||
local function player_allowed_edit(player,task)
|
||||
if task then
|
||||
if config.user_can_edit_own_tasks and task.last_edit_player == player.name then
|
||||
return true
|
||||
end
|
||||
else
|
||||
@@ -37,7 +36,6 @@ end
|
||||
|
||||
--- Button in the header to add a new task
|
||||
-- @element add_new_task
|
||||
local update_all
|
||||
local add_new_task =
|
||||
Gui.new_button()
|
||||
:set_sprites('utility/add')
|
||||
@@ -48,7 +46,7 @@ Gui.new_button()
|
||||
style.width = 20
|
||||
end)
|
||||
:on_click(function(player,element)
|
||||
Tasks.new_task(player.force.name,nil,player.name)
|
||||
Tasks.add_task(player.force.name,nil,player.name)
|
||||
end)
|
||||
|
||||
--- Used to save changes to a task
|
||||
@@ -64,14 +62,13 @@ Gui.new_button()
|
||||
end)
|
||||
:on_click(function(player,element)
|
||||
local task_id = element.parent.name
|
||||
local task = element.parent.task.text
|
||||
local new_message = element.parent.task.text
|
||||
Tasks.set_editing(task_id,player.name)
|
||||
Tasks.update_task(task_id,task,player.name)
|
||||
Tasks.update_task(task_id,new_message,player.name)
|
||||
end)
|
||||
|
||||
--- Used to cancel any changes you made to a task
|
||||
-- @element cancel_edit
|
||||
local generate_task
|
||||
local cancel_edit =
|
||||
Gui.new_button()
|
||||
:set_sprites('utility/close_black')
|
||||
@@ -84,7 +81,6 @@ end)
|
||||
:on_click(function(player,element)
|
||||
local task_id = element.parent.name
|
||||
Tasks.set_editing(task_id,player.name)
|
||||
generate_task(player,element.parent.parent,task_id)
|
||||
end)
|
||||
|
||||
--- Removes the task from the list
|
||||
@@ -101,7 +97,6 @@ end)
|
||||
:on_click(function(player,element)
|
||||
local task_id = element.parent.name
|
||||
Tasks.remove_task(task_id)
|
||||
update_all()
|
||||
end)
|
||||
|
||||
--- Opens edit mode for the task
|
||||
@@ -118,7 +113,6 @@ end)
|
||||
:on_click(function(player,element)
|
||||
local task_id = element.parent.name
|
||||
Tasks.set_editing(task_id,player.name,true)
|
||||
generate_task(player,element.parent.parent.parent,task_id)
|
||||
end)
|
||||
|
||||
--[[ Generates each task, handles both view and edit mode
|
||||
@@ -133,23 +127,24 @@ end)
|
||||
>> edit_task
|
||||
>> discard_task
|
||||
]]
|
||||
function generate_task(player,element,task_id)
|
||||
local function generate_task(player,element,task_id)
|
||||
local task = Tasks.get_task(task_id)
|
||||
local editing = Tasks.is_editing(task_id,player.name)
|
||||
local details = Tasks.get_details(task_id)
|
||||
local last_edit_player = details.last_edit_player
|
||||
local last_edit_time = details.last_edit_time
|
||||
local tasks = Tasks.get_force_tasks(player.force.name)
|
||||
local task_number = table.index_of(tasks, task_id)
|
||||
local task_ids = Tasks.get_force_task_ids(player.force.name)
|
||||
local task_number = table.index_of(task_ids, task_id)
|
||||
|
||||
if not task then
|
||||
-- task is nil so remove it from the list
|
||||
element.parent.no_tasks.visible = #tasks == 01
|
||||
element.parent.no_tasks.visible = #task_ids == 1
|
||||
Gui.destroy_if_valid(element['count-'..task_id])
|
||||
Gui.destroy_if_valid(element['edit-'..task_id])
|
||||
Gui.destroy_if_valid(element[task_id])
|
||||
|
||||
else
|
||||
local message = task.message
|
||||
local editing = task.curently_editing[player.name]
|
||||
local last_edit_name = task.last_edit_name
|
||||
local last_edit_time = task.last_edit_time
|
||||
|
||||
element.parent.no_tasks.visible = false
|
||||
-- if it is not already present then add it now
|
||||
local task_area = element[task_id]
|
||||
@@ -182,8 +177,8 @@ function generate_task(player,element,task_id)
|
||||
element['count-'..task_id].caption = task_number..')'
|
||||
|
||||
local edit_area = element['edit-'..task_id][task_id]
|
||||
local players = table_keys(details.editing)
|
||||
local allowed = player_allowed_edit(player,task_id)
|
||||
local players = table_keys(task.editing)
|
||||
local allowed = player_allowed_edit(player,task)
|
||||
|
||||
edit_area.visible = allowed
|
||||
|
||||
@@ -197,8 +192,8 @@ function generate_task(player,element,task_id)
|
||||
local element_type = task_area.task and task_area.task.type or nil
|
||||
if not editing and element_type == 'label' then
|
||||
-- update the label already present
|
||||
task_area.task.caption = task
|
||||
task_area.task.tooltip = {'task-list.last-edit',last_edit_player,format_time(last_edit_time)}
|
||||
task_area.task.caption = message
|
||||
task_area.task.tooltip = {'task-list.last-edit',last_edit_name,format_time(last_edit_time)}
|
||||
|
||||
elseif not editing then
|
||||
-- create the label, view mode
|
||||
@@ -212,8 +207,8 @@ function generate_task(player,element,task_id)
|
||||
task_area.add{
|
||||
name='task',
|
||||
type='label',
|
||||
caption=task,
|
||||
tooltip={'task-list.last-edit',last_edit_player,format_time(last_edit_time)}
|
||||
caption=message,
|
||||
tooltip={'task-list.last-edit',last_edit_name,format_time(last_edit_time)}
|
||||
}
|
||||
label.style.single_line = false
|
||||
label.style.maximal_width = 150
|
||||
@@ -230,7 +225,7 @@ function generate_task(player,element,task_id)
|
||||
task_area.add{
|
||||
name='task',
|
||||
type='textfield',
|
||||
text=task
|
||||
text=message
|
||||
}
|
||||
entry.style.maximal_width = 150
|
||||
entry.style.height = 20
|
||||
@@ -311,26 +306,32 @@ Gui.new_left_frame('gui/task-list')
|
||||
:set_open_by_default()
|
||||
:on_creation(function(player,element)
|
||||
local data_table = generate_container(player,element)
|
||||
local tasks = Tasks.get_force_tasks(player.force.name)
|
||||
local task_ids = Tasks.get_force_task_ids(player.force.name)
|
||||
|
||||
for _,task_id in pairs(tasks) do
|
||||
for _,task_id in pairs(task_ids) do
|
||||
generate_task(player,data_table,task_id)
|
||||
end
|
||||
end)
|
||||
:on_update(function(player,element)
|
||||
local data_table = element.container.scroll.table
|
||||
local tasks = Tasks.get_force_tasks(player.force.name)
|
||||
local task_ids = Tasks.get_force_task_ids(player.force.name)
|
||||
|
||||
for _,task_id in pairs(tasks) do
|
||||
for _,task_id in pairs(task_ids) do
|
||||
generate_task(player,data_table,task_id)
|
||||
end
|
||||
end)
|
||||
|
||||
update_all = task_list 'update_all'
|
||||
|
||||
--- When a new task is added it will udpate the task list for everyone on that force
|
||||
Tasks.add_handler(function(force,task_id)
|
||||
for _,player in pairs(force.players) do
|
||||
Tasks.on_update(function(task,task_id)
|
||||
local players
|
||||
if task then
|
||||
local force = game.forces[task.force_name]
|
||||
players = force.players
|
||||
else
|
||||
players = game.connected_players
|
||||
end
|
||||
|
||||
for _,player in pairs(players) do
|
||||
local frame = task_list:get_frame(player)
|
||||
local element = frame.container.scroll.table
|
||||
generate_task(player,element,task_id)
|
||||
|
||||
Reference in New Issue
Block a user