mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Basic task with title and body implementation
- Parsing of the message is a bit low effort and will need to be improved.
This commit is contained in:
@@ -32,17 +32,19 @@ 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] string player_name the player who added this task, will cause them to be listed under editing
|
||||
@tparam[opt] string task_message the message that is used for this task, if not given default is used
|
||||
@tparam[opt] string task_title the message title that is used for this task, if not given default is used
|
||||
@tparam[opt] string task_body the message body that is used for this task, if not given default is used
|
||||
@treturn string the uid of the task which was created
|
||||
|
||||
@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, player_name, task_message)
|
||||
function Tasks.add_task(force_name, player_name, message_title, message_body)
|
||||
-- Get a new task id
|
||||
local task_id = tostring(force_tasks._uid)
|
||||
task_message = task_message or 'New Task'
|
||||
message_title = message_title or 'New Task'
|
||||
message_body = message_body or 'Do x or y'
|
||||
force_tasks._uid = force_tasks._uid + 1
|
||||
|
||||
-- Get the existing tasks for this force
|
||||
@@ -65,7 +67,8 @@ function Tasks.add_task(force_name, player_name, task_message)
|
||||
TaskData:set(task_id, {
|
||||
task_id = task_id,
|
||||
force_name = force_name,
|
||||
message = task_message,
|
||||
title = message_title,
|
||||
body = message_body,
|
||||
last_edit_name = player_name or '<server>',
|
||||
last_edit_time = game.tick,
|
||||
currently_editing = editing
|
||||
@@ -90,18 +93,20 @@ end
|
||||
|
||||
--[[-- 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 string message_title the message title that you want to have for the task
|
||||
@tparam string message_body the message body 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)
|
||||
function Tasks.update_task(task_id, message_title, message_body, player_name)
|
||||
TaskData:update(task_id, function(_, task)
|
||||
task.last_edit_name = player_name or '<server>'
|
||||
task.last_edit_time = game.tick
|
||||
task.message = new_message
|
||||
task.title = message_title
|
||||
task.body = message_body
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -190,7 +190,7 @@ local task_list_item =
|
||||
name = event_trigger,
|
||||
type = "button",
|
||||
style = "list_box_item",
|
||||
caption = task.message
|
||||
caption = task.title
|
||||
}
|
||||
button.style.horizontally_stretchable = true
|
||||
button.style.horizontally_squashable = true
|
||||
@@ -289,14 +289,23 @@ local task_view_footer =
|
||||
local footer = subfooter_frame(parent, "view")
|
||||
subfooter_label(footer, {"task-list.view-footer-header"})
|
||||
|
||||
local label =
|
||||
local title_label =
|
||||
footer.add {
|
||||
type = "label",
|
||||
name = "message",
|
||||
name = "title",
|
||||
caption = "New task"
|
||||
}
|
||||
label.style.padding = 4
|
||||
label.style.single_line = false
|
||||
title_label.style.padding = 4
|
||||
title_label.style.font = "default-bold"
|
||||
title_label.style.single_line = false
|
||||
local body_label =
|
||||
footer.add {
|
||||
type = "label",
|
||||
name = "body",
|
||||
caption = "Do x or y"
|
||||
}
|
||||
body_label.style.padding = 4
|
||||
body_label.style.single_line = false
|
||||
|
||||
local action_flow = subfooter_actions(footer)
|
||||
view_task_delete_button(action_flow)
|
||||
@@ -306,6 +315,29 @@ local task_view_footer =
|
||||
end
|
||||
)
|
||||
|
||||
local message_pattern = "(.-)\n(.*)"
|
||||
|
||||
--- Parce a string into a message object with title and body
|
||||
-- @tparam string str message data
|
||||
local function parse_message(str)
|
||||
-- Trimm the spaces of the string
|
||||
local trimmed = (string.gsub(str, "^%s*(.-)%s*$", "%1"))
|
||||
local message = { title = "", body = "" }
|
||||
-- If it doesn't match the patter return the str as a title
|
||||
local match = string.match(trimmed, message_pattern)
|
||||
if not match then
|
||||
message.title = trimmed
|
||||
return message
|
||||
end
|
||||
-- If message has multiple lines
|
||||
for key, value in string.gmatch(trimmed, message_pattern) do
|
||||
message.title = key
|
||||
message.body = value
|
||||
break
|
||||
end
|
||||
return message
|
||||
end
|
||||
|
||||
-- Button variable initialisation because it is used inside the textfield element events
|
||||
local edit_task_confirm_button
|
||||
local create_task_confirm_button
|
||||
@@ -314,11 +346,12 @@ local create_task_confirm_button
|
||||
-- @element task_message_textfield
|
||||
local task_message_textfield =
|
||||
Gui.element {
|
||||
type = "textfield",
|
||||
type = "text-box",
|
||||
text = ""
|
||||
}:style(
|
||||
{
|
||||
maximal_width = 268,
|
||||
minimal_height = 100,
|
||||
horizontally_stretchable = true
|
||||
}
|
||||
):on_text_changed(
|
||||
@@ -334,29 +367,6 @@ local task_message_textfield =
|
||||
element.parent.actions[edit_task_confirm_button.name].enabled = valid
|
||||
end
|
||||
end
|
||||
):on_confirmed(
|
||||
function(player, element, _)
|
||||
local isEditing = PlayerIsEditing:get(player)
|
||||
local isCreating = PlayerIsCreating:get(player)
|
||||
|
||||
local message = element.text
|
||||
local valid = string.len(element.text) > 5
|
||||
if not valid then
|
||||
return
|
||||
end
|
||||
|
||||
if isCreating then
|
||||
PlayerIsCreating:set(player, false)
|
||||
|
||||
local task_id = Tasks.add_task(player.force.name, player.name, message)
|
||||
PlayerSelected:set(player, task_id)
|
||||
elseif isEditing then
|
||||
local selected = PlayerSelected:get(player)
|
||||
PlayerIsEditing:set(player, false)
|
||||
Tasks.update_task(selected, message, player.name)
|
||||
Tasks.set_editing(selected, player.name, nil)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
--- Button to confirm the changes
|
||||
@@ -372,7 +382,8 @@ edit_task_confirm_button =
|
||||
local selected = PlayerSelected:get(player)
|
||||
PlayerIsEditing:set(player, false)
|
||||
local new_message = element.parent.parent[task_message_textfield.name].text
|
||||
Tasks.update_task(selected, new_message, player.name)
|
||||
local parsed = parse_message(new_message)
|
||||
Tasks.update_task(selected, parsed.title, parsed.body, player.name)
|
||||
Tasks.set_editing(selected, player.name, nil)
|
||||
end
|
||||
)
|
||||
@@ -425,8 +436,8 @@ create_task_confirm_button =
|
||||
function(player, element, _)
|
||||
local message = element.parent.parent[task_message_textfield.name].text
|
||||
PlayerIsCreating:set(player, false)
|
||||
|
||||
local task_id = Tasks.add_task(player.force.name, player.name, message)
|
||||
local parsed = parse_message(message)
|
||||
local task_id = Tasks.add_task(player.force.name, player.name, parsed.title, parsed.body)
|
||||
PlayerSelected:set(player, task_id)
|
||||
end
|
||||
)
|
||||
@@ -535,7 +546,7 @@ local update_task = function(player, task_list_element, task_id)
|
||||
else
|
||||
-- If the task exists update the caption
|
||||
element = task_list_element["task-" .. task_id]
|
||||
element[task_list_item.name].caption = task.message
|
||||
element[task_list_item.name].caption = task.title
|
||||
end
|
||||
-- Set tooltip
|
||||
local last_edit_name = task.last_edit_name
|
||||
@@ -552,7 +563,7 @@ local update_task_edit_footer = function(player, task_id)
|
||||
local message_element = edit_flow[task_message_textfield.name]
|
||||
|
||||
message_element.focus()
|
||||
message_element.text = task.message
|
||||
message_element.text = task.title .. "\n" .. task.body
|
||||
end
|
||||
|
||||
-- Update the footer task view
|
||||
@@ -562,13 +573,15 @@ local update_task_view_footer = function(player, task_id)
|
||||
local view_flow = frame.container.view
|
||||
local has_permission = check_player_permissions(player, task)
|
||||
|
||||
local message_element = view_flow.message
|
||||
local title_element = view_flow.title
|
||||
local body_element = view_flow.body
|
||||
local edit_button_element = view_flow.actions[view_task_edit_button.name]
|
||||
local delete_button_element = view_flow.actions[view_task_delete_button.name]
|
||||
|
||||
edit_button_element.visible = has_permission
|
||||
delete_button_element.visible = has_permission
|
||||
message_element.caption = task.message
|
||||
title_element.caption = task.title
|
||||
body_element.caption = task.body
|
||||
|
||||
local players_editing = table.get_keys(task.currently_editing)
|
||||
if #players_editing > 0 then
|
||||
|
||||
Reference in New Issue
Block a user