2018-08-16 18:44:05 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Gui = require 'utils.gui'
|
|
|
|
local Global = require 'utils.global'
|
|
|
|
local UserGroups = require 'user_groups'
|
|
|
|
local Utils = require 'utils.utils'
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local normal_color = {r = 1, g = 1, b = 1}
|
|
|
|
local focus_color = {r = 1, g = 0.55, b = 0.1}
|
|
|
|
|
|
|
|
local server_player = {
|
|
|
|
valid = true,
|
|
|
|
index = 0,
|
|
|
|
name = '<server>',
|
|
|
|
admin = true,
|
|
|
|
print = print
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local init_announcements = 'There are no announcements.'
|
|
|
|
local announcements = {
|
|
|
|
text = init_announcements,
|
|
|
|
edit_text = init_announcements,
|
|
|
|
editing_players = {},
|
|
|
|
last_edit_player = nil,
|
|
|
|
last_update_player = nil,
|
|
|
|
last_update_time = nil
|
|
|
|
}
|
|
|
|
local tasks = {}
|
2018-08-17 18:22:53 +02:00
|
|
|
local player_tasks = {}
|
2018-08-17 11:46:04 +02:00
|
|
|
local tasks_counter = {0}
|
2018-08-17 21:35:54 +02:00
|
|
|
local last_task_update_data = {
|
|
|
|
player = nil,
|
|
|
|
time = nil
|
|
|
|
}
|
2018-08-16 18:44:05 +02:00
|
|
|
local no_notify_players = {}
|
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
|
|
|
announcements = announcements,
|
|
|
|
tasks = tasks,
|
2018-08-17 18:22:53 +02:00
|
|
|
player_tasks = player_tasks,
|
2018-08-17 11:46:04 +02:00
|
|
|
tasks_counter = tasks_counter,
|
2018-08-17 21:35:54 +02:00
|
|
|
last_task_update_data = last_task_update_data,
|
2018-08-16 18:44:05 +02:00
|
|
|
no_notify_announcements_players = no_notify_players
|
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
announcements = tbl.announcements
|
|
|
|
tasks = tbl.tasks
|
2018-08-17 18:22:53 +02:00
|
|
|
player_tasks = tbl.player_tasks
|
2018-08-17 11:46:04 +02:00
|
|
|
tasks_counter = tbl.tasks_counter
|
2018-08-17 21:35:54 +02:00
|
|
|
last_task_update_data = tbl.last_task_update_data
|
2018-08-16 18:44:05 +02:00
|
|
|
no_notify_players = tbl.no_notify_announcements_players
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
local main_frame_name = Gui.uid_name()
|
|
|
|
local main_button_name = Gui.uid_name()
|
|
|
|
local announcements_edit_button_name = Gui.uid_name()
|
2018-08-17 11:46:04 +02:00
|
|
|
local add_task_button_name = Gui.uid_name()
|
|
|
|
local edit_task_button_name = Gui.uid_name()
|
|
|
|
local delete_task_button_name = Gui.uid_name()
|
|
|
|
local move_task_up_button_name = Gui.uid_name()
|
|
|
|
local move_task_down_button_name = Gui.uid_name()
|
|
|
|
local volunteer_task_button_name = Gui.uid_name()
|
2018-08-16 18:44:05 +02:00
|
|
|
local notify_checkbox_name = Gui.uid_name()
|
|
|
|
|
|
|
|
local edit_announcements_frame_name = Gui.uid_name()
|
|
|
|
local edit_announcements_textbox_name = Gui.uid_name()
|
|
|
|
local edit_close_button_name = Gui.uid_name()
|
|
|
|
local edit_clear_button_name = Gui.uid_name()
|
|
|
|
local edit_reset_button_name = Gui.uid_name()
|
|
|
|
local edit_confirm_button_name = Gui.uid_name()
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local create_task_frame_name = Gui.uid_name()
|
|
|
|
local create_task_close_button_name = Gui.uid_name()
|
|
|
|
local create_task_clear_button_name = Gui.uid_name()
|
|
|
|
local create_task_confirm_button_name = Gui.uid_name()
|
|
|
|
local create_task_edit_button_name = Gui.uid_name()
|
|
|
|
|
|
|
|
local function get_task_id()
|
|
|
|
local count = tasks_counter[1] + 1
|
|
|
|
tasks_counter[1] = count
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function get_announcements_updated_by_message()
|
|
|
|
local player = announcements.last_update_player
|
|
|
|
|
|
|
|
if not player then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat {
|
|
|
|
'Updated by ',
|
|
|
|
player.name,
|
|
|
|
' (',
|
|
|
|
Utils.format_time(game.tick - announcements.last_update_time),
|
|
|
|
' ago).'
|
|
|
|
}
|
2017-09-24 14:17:10 +02:00
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function get_edit_announcements_last_player_message()
|
|
|
|
local player = announcements.last_edit_player
|
|
|
|
if not player then
|
|
|
|
return ''
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
return 'Last edit by ' .. player.name
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function get_editing_players_message(players)
|
|
|
|
local message = {'Editing players: '}
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
for pi, _ in pairs(players) do
|
|
|
|
local name = game.players[pi].name
|
|
|
|
table.insert(message, name)
|
|
|
|
table.insert(message, ', ')
|
|
|
|
end
|
|
|
|
table.remove(message)
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
return table.concat(message)
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
local function get_task_updated_by_message()
|
|
|
|
local player = last_task_update_data.player
|
|
|
|
|
|
|
|
if not player then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat {
|
|
|
|
'Updated by ',
|
|
|
|
player.name,
|
|
|
|
' (',
|
|
|
|
Utils.format_time(game.tick - last_task_update_data.time),
|
|
|
|
' ago).'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local function apply_direction_button_style(button)
|
|
|
|
local button_style = button.style
|
|
|
|
button_style.width = 26
|
|
|
|
button_style.height = 26
|
|
|
|
button_style.top_padding = 0
|
|
|
|
button_style.bottom_padding = 0
|
|
|
|
button_style.left_padding = 0
|
|
|
|
button_style.right_padding = 0
|
|
|
|
button_style.font = 'default-listbox'
|
|
|
|
end
|
|
|
|
|
|
|
|
local function apply_button_style(button)
|
|
|
|
local style = button.style
|
|
|
|
style.width = 26
|
|
|
|
style.height = 26
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_volunteer_button(button, task)
|
|
|
|
local volunteers = task.volunteers
|
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
local tooltip = {'Volunteers: '}
|
2018-08-17 11:46:04 +02:00
|
|
|
local count = 0
|
|
|
|
|
|
|
|
for _, p in pairs(volunteers) do
|
2018-08-17 18:22:53 +02:00
|
|
|
if p.connected then
|
|
|
|
table.insert(tooltip, p.name)
|
|
|
|
table.insert(tooltip, ', ')
|
|
|
|
count = count + 1
|
|
|
|
end
|
2018-08-17 11:46:04 +02:00
|
|
|
end
|
2018-08-17 18:22:53 +02:00
|
|
|
table.remove(tooltip)
|
2018-08-17 11:46:04 +02:00
|
|
|
|
|
|
|
button.caption = count
|
2018-08-17 18:22:53 +02:00
|
|
|
if count == 0 then
|
|
|
|
button.tooltip = 'No volunteers'
|
|
|
|
else
|
|
|
|
button.tooltip = table.concat(tooltip)
|
|
|
|
end
|
2018-08-17 11:46:04 +02:00
|
|
|
|
|
|
|
if volunteers[button.player_index] then
|
|
|
|
button.style.font_color = focus_color
|
|
|
|
else
|
|
|
|
button.style.font_color = normal_color
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
local function redraw_tasks(data)
|
|
|
|
local parent = data.tasks_content
|
2018-08-17 11:46:04 +02:00
|
|
|
Gui.clear(parent)
|
2018-08-17 18:22:53 +02:00
|
|
|
local volunteer_buttons = {}
|
|
|
|
data.volunteer_buttons = volunteer_buttons
|
2018-08-17 11:46:04 +02:00
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
local task_count = #tasks
|
|
|
|
if task_count == 0 then
|
2018-08-17 11:46:04 +02:00
|
|
|
parent.add {type = 'label', caption = 'There are no Tasks.'}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for task_index, task in ipairs(tasks) do
|
|
|
|
local delete_button =
|
|
|
|
parent.add({type = 'flow'}).add {
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = delete_task_button_name,
|
|
|
|
sprite = 'utility/remove',
|
|
|
|
tooltip = 'Delete task.'
|
|
|
|
}
|
|
|
|
apply_button_style(delete_button)
|
|
|
|
Gui.set_data(delete_button, task_index)
|
|
|
|
|
|
|
|
local edit_button =
|
|
|
|
parent.add({type = 'flow'}).add {
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = edit_task_button_name,
|
|
|
|
sprite = 'utility/rename_icon_normal',
|
|
|
|
tooltip = 'Edit task.'
|
|
|
|
}
|
|
|
|
apply_button_style(edit_button)
|
|
|
|
Gui.set_data(edit_button, task)
|
|
|
|
|
|
|
|
local up_button =
|
2018-08-17 18:22:53 +02:00
|
|
|
parent.add({type = 'flow'}).add {
|
|
|
|
type = 'button',
|
|
|
|
name = move_task_up_button_name,
|
|
|
|
caption = '▲',
|
|
|
|
tooltip = 'Move the task up, right click moves 5 spaces, shift click moves the task to the top.'
|
|
|
|
}
|
|
|
|
up_button.enabled = task_index ~= 1
|
2018-08-17 11:46:04 +02:00
|
|
|
apply_direction_button_style(up_button)
|
|
|
|
Gui.set_data(up_button, task_index)
|
|
|
|
local down_button =
|
2018-08-17 18:22:53 +02:00
|
|
|
parent.add({type = 'flow'}).add {
|
|
|
|
type = 'button',
|
|
|
|
name = move_task_down_button_name,
|
|
|
|
caption = '▼',
|
|
|
|
tooltip = 'Move the task down, right click moves 5 spaces, shift click moves the task to the bottom.'
|
|
|
|
}
|
|
|
|
down_button.enabled = task_index ~= task_count
|
2018-08-17 11:46:04 +02:00
|
|
|
apply_direction_button_style(down_button)
|
|
|
|
Gui.set_data(down_button, task_index)
|
|
|
|
|
|
|
|
local volunteer_button_flow = parent.add {type = 'flow'}
|
|
|
|
local volunteer_button = volunteer_button_flow.add {type = 'button', name = volunteer_task_button_name}
|
|
|
|
local volunteer_button_style = volunteer_button.style
|
|
|
|
volunteer_button_style.font = 'default-small'
|
2018-08-17 18:22:53 +02:00
|
|
|
volunteer_button_style.height = 26
|
2018-08-17 11:46:04 +02:00
|
|
|
volunteer_button_style.width = 26
|
|
|
|
volunteer_button_style.top_padding = 0
|
|
|
|
volunteer_button_style.bottom_padding = 0
|
|
|
|
volunteer_button_style.left_padding = 0
|
|
|
|
volunteer_button_style.right_padding = 0
|
|
|
|
Gui.set_data(volunteer_button, task)
|
|
|
|
update_volunteer_button(volunteer_button, task)
|
2018-08-17 18:22:53 +02:00
|
|
|
|
|
|
|
volunteer_buttons[task] = volunteer_button
|
|
|
|
|
|
|
|
local label = parent.add {type = 'label', caption = table.concat {'#', task.task_id, ' ', task.name}}
|
|
|
|
label.style.left_padding = 4
|
2018-08-17 11:46:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function draw_main_frame(left)
|
|
|
|
local data = {}
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local frame = left.add {type = 'frame', name = main_frame_name, direction = 'vertical'}
|
2018-08-16 18:44:05 +02:00
|
|
|
Gui.set_data(frame, data)
|
|
|
|
|
|
|
|
local announcements_header_flow = frame.add {type = 'flow'}
|
|
|
|
|
|
|
|
local edit_announcments_button =
|
|
|
|
announcements_header_flow.add {
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = announcements_edit_button_name,
|
|
|
|
sprite = 'utility/rename_icon_normal',
|
|
|
|
tooltip = 'Edit announcments.'
|
|
|
|
}
|
2018-08-17 21:35:54 +02:00
|
|
|
apply_button_style(edit_announcments_button)
|
2018-08-16 18:44:05 +02:00
|
|
|
|
|
|
|
local announcements_header = announcements_header_flow.add {type = 'label', caption = 'Announcements'}
|
|
|
|
announcements_header.style.font = 'default-listbox'
|
|
|
|
|
|
|
|
local last_edit_message = get_announcements_updated_by_message()
|
|
|
|
local announcements_updated_label =
|
|
|
|
announcements_header_flow.add {
|
|
|
|
type = 'label',
|
|
|
|
caption = last_edit_message,
|
|
|
|
tooltip = last_edit_message
|
|
|
|
}
|
|
|
|
|
|
|
|
local announcements_textbox = frame.add {type = 'text-box', text = announcements.text}
|
|
|
|
announcements_textbox.read_only = true
|
|
|
|
announcements_textbox.word_wrap = true
|
|
|
|
local announcements_textbox_style = announcements_textbox.style
|
2018-08-17 11:46:04 +02:00
|
|
|
announcements_textbox_style.width = 400
|
2018-08-16 18:44:05 +02:00
|
|
|
announcements_textbox_style.height = 100
|
|
|
|
|
|
|
|
data.announcements_textbox = announcements_textbox
|
|
|
|
data.announcements_updated_label = announcements_updated_label
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local tasks_header_flow = frame.add {type = 'flow'}
|
2018-08-17 21:35:54 +02:00
|
|
|
|
|
|
|
local add_task_button =
|
|
|
|
tasks_header_flow.add {
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = add_task_button_name,
|
|
|
|
sprite = 'utility/add',
|
|
|
|
tooltip = 'Create new task.'
|
|
|
|
}
|
|
|
|
apply_button_style(add_task_button)
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local tasks_header = tasks_header_flow.add {type = 'label', caption = 'Tasks'}
|
|
|
|
tasks_header.style.font = 'default-listbox'
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
local last_task_updated_message = get_task_updated_by_message()
|
|
|
|
local tasks_updated_label =
|
|
|
|
tasks_header_flow.add {
|
|
|
|
type = 'label',
|
|
|
|
caption = last_task_updated_message,
|
|
|
|
tooltip = last_task_updated_message
|
|
|
|
}
|
|
|
|
data.tasks_updated_label = tasks_updated_label
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local tasks_scroll_pane = frame.add {type = 'scroll-pane', direction = 'vertical'}
|
|
|
|
local tasks_scroll_pane_style = tasks_scroll_pane.style
|
2018-08-17 18:22:53 +02:00
|
|
|
tasks_scroll_pane_style.width = 400
|
|
|
|
tasks_scroll_pane_style.maximal_height = 250
|
2018-08-17 11:46:04 +02:00
|
|
|
|
|
|
|
local tasks_content = tasks_scroll_pane.add {type = 'table', column_count = 6}
|
|
|
|
local tasks_content_style = tasks_content.style
|
|
|
|
tasks_content_style.horizontal_spacing = 0
|
|
|
|
tasks_content_style.vertical_spacing = 0
|
|
|
|
data.tasks_content = tasks_content
|
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
redraw_tasks(data)
|
2018-08-17 11:46:04 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
frame.add {
|
|
|
|
type = 'checkbox',
|
|
|
|
name = notify_checkbox_name,
|
|
|
|
caption = 'Notify me about new annoucements or tasks',
|
|
|
|
state = not no_notify_players[left.player_index]
|
|
|
|
}
|
|
|
|
|
|
|
|
frame.add {type = 'button', name = main_button_name, caption = 'Close'}
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function toggle(event)
|
|
|
|
local left = event.player.gui.left
|
|
|
|
local frame = left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
Gui.destroy(frame)
|
2018-08-17 11:46:04 +02:00
|
|
|
frame = left[edit_announcements_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
Gui.destroy(frame)
|
|
|
|
end
|
|
|
|
frame = left[create_task_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
Gui.destroy(frame)
|
|
|
|
end
|
2018-08-16 18:44:05 +02:00
|
|
|
else
|
|
|
|
draw_main_frame(left)
|
|
|
|
end
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function update_edit_announcements_textbox(text, player)
|
2018-08-17 11:46:04 +02:00
|
|
|
local player_index = player.index
|
2018-08-16 18:44:05 +02:00
|
|
|
announcements.edit_text = text
|
|
|
|
announcements.last_edit_player = player
|
|
|
|
local editing_players = announcements.editing_players
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local last_edit_message = get_edit_announcements_last_player_message()
|
|
|
|
local editing_players_message = get_editing_players_message(editing_players)
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
for pi, data in pairs(editing_players) do
|
|
|
|
if pi ~= player_index then
|
|
|
|
data.textbox.text = text
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local last_edit_label = data.last_edit_player_label
|
|
|
|
last_edit_label.caption = last_edit_message
|
|
|
|
last_edit_label.tooltip = last_edit_message
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local editing_players_label = data.editing_players_label
|
|
|
|
editing_players_label.caption = editing_players_message
|
|
|
|
editing_players_label.tooltip = editing_players_message
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function close_edit_announcments_frame(frame)
|
|
|
|
local editing_players = announcements.editing_players
|
|
|
|
editing_players[frame.player_index] = nil
|
|
|
|
Gui.destroy(frame)
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
if not next(editing_players) then
|
|
|
|
return
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local editing_players_message = get_editing_players_message(editing_players)
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
for _, data in pairs(editing_players) do
|
|
|
|
local editing_players_label = data.editing_players_label
|
|
|
|
editing_players_label.caption = editing_players_message
|
|
|
|
editing_players_label.tooltip = editing_players_message
|
|
|
|
end
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function update_announcements(player)
|
|
|
|
local text = announcements.edit_text
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
announcements.text = text
|
|
|
|
announcements.last_update_player = player
|
|
|
|
announcements.last_update_time = game.tick
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local last_edit_message = get_announcements_updated_by_message()
|
|
|
|
local update_message = 'The announcements have been updated by ' .. player.name
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
for pi, p in ipairs(game.connected_players) do
|
|
|
|
local notify = not no_notify_players[pi]
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
if notify then
|
|
|
|
p.print(update_message)
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local left = p.gui.left
|
|
|
|
local frame = left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local data = Gui.get_data(frame)
|
|
|
|
data.announcements_textbox.text = text
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local label = data.announcements_updated_label
|
|
|
|
label.caption = last_edit_message
|
|
|
|
label.tooltip = last_edit_message
|
|
|
|
elseif notify then
|
|
|
|
draw_main_frame(left)
|
|
|
|
end
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
end
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local function create_new_tasks(task_name, player)
|
|
|
|
local task_id = get_task_id()
|
2018-08-17 21:35:54 +02:00
|
|
|
local tick = game.tick
|
2018-08-17 11:46:04 +02:00
|
|
|
local task = {
|
|
|
|
task_id = task_id,
|
|
|
|
created_by = player,
|
|
|
|
edited_by = nil,
|
2018-08-17 21:35:54 +02:00
|
|
|
tick = tick,
|
2018-08-17 11:46:04 +02:00
|
|
|
name = task_name,
|
|
|
|
volunteers = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(tasks, task)
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
last_task_update_data.player = player
|
|
|
|
last_task_update_data.time = tick
|
|
|
|
|
|
|
|
local update_message = get_task_updated_by_message()
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
local message =
|
|
|
|
table.concat {
|
|
|
|
player.name,
|
|
|
|
' has create a new task #',
|
|
|
|
task_id,
|
|
|
|
' - ',
|
|
|
|
task_name
|
|
|
|
}
|
|
|
|
|
|
|
|
for pi, p in ipairs(game.connected_players) do
|
|
|
|
local notify = not no_notify_players[pi]
|
|
|
|
local left = p.gui.left
|
|
|
|
local frame = left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local frame_data = Gui.get_data(frame)
|
2018-08-17 21:35:54 +02:00
|
|
|
frame_data.tasks_updated_label.caption = update_message
|
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
redraw_tasks(frame_data)
|
2018-08-17 11:46:04 +02:00
|
|
|
elseif notify then
|
|
|
|
draw_main_frame(left)
|
|
|
|
end
|
|
|
|
|
|
|
|
if notify then
|
|
|
|
p.print(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
local function draw_create_task_frame(left, previous_task)
|
|
|
|
local frame_caption
|
|
|
|
local confirm_button_name
|
|
|
|
local confirm_button_caption
|
|
|
|
local text
|
|
|
|
|
|
|
|
if previous_task then
|
|
|
|
frame_caption = 'Edit Task #' .. previous_task.task_id
|
|
|
|
confirm_button_name = create_task_edit_button_name
|
|
|
|
confirm_button_caption = 'Edit Task'
|
|
|
|
text = previous_task.name
|
|
|
|
else
|
|
|
|
frame_caption = 'Create New Task'
|
|
|
|
confirm_button_name = create_task_confirm_button_name
|
|
|
|
confirm_button_caption = 'Create Task'
|
|
|
|
end
|
|
|
|
|
|
|
|
local frame =
|
|
|
|
left.add {type = 'frame', name = create_task_frame_name, caption = frame_caption, direction = 'vertical'}
|
|
|
|
|
|
|
|
local textbox = frame.add {type = 'text-box', text = text}
|
|
|
|
local textbox_style = textbox.style
|
|
|
|
textbox_style.width = 400
|
|
|
|
|
|
|
|
local bottom_flow = frame.add {type = 'flow'}
|
|
|
|
|
|
|
|
local close_button = bottom_flow.add {type = 'button', name = create_task_close_button_name, caption = 'Close'}
|
|
|
|
Gui.set_data(close_button, frame)
|
|
|
|
local clear_button = bottom_flow.add {type = 'button', name = create_task_clear_button_name, caption = 'Clear'}
|
|
|
|
Gui.set_data(clear_button, textbox)
|
|
|
|
bottom_flow.add({type = 'flow'}).style.horizontally_stretchable = true
|
|
|
|
local confirm_button =
|
|
|
|
bottom_flow.add {type = 'button', name = confirm_button_name, caption = confirm_button_caption}
|
|
|
|
Gui.set_data(confirm_button, {frame = frame, textbox = textbox, previous_task = previous_task})
|
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function player_created(event)
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local gui = player.gui
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local frame = gui.left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local text = announcements.edit_text
|
|
|
|
local last_edit_message = get_announcements_updated_by_message()
|
2017-09-24 14:17:10 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local data = Gui.get_data(frame)
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
data.announcements_textbox.text = text
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local label = data.announcements_updated_label
|
|
|
|
label.caption = last_edit_message
|
|
|
|
label.tooltip = last_edit_message
|
2018-08-17 18:22:53 +02:00
|
|
|
|
|
|
|
redraw_tasks(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
local tasks_for_player = player_tasks[player.index]
|
|
|
|
if tasks_for_player and next(tasks_for_player) then
|
|
|
|
for _, p in game.connected_players do
|
|
|
|
local frame = p.gui.left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local data = Gui.get_data(frame)
|
|
|
|
local volunteer_buttons = data.volunteer_buttons
|
|
|
|
|
|
|
|
for t, _ in pairs(tasks_for_player) do
|
|
|
|
update_volunteer_button(volunteer_buttons[t], t)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-16 18:44:05 +02:00
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local top = gui.top
|
|
|
|
if not top[main_button_name] then
|
|
|
|
top.add {type = 'sprite-button', name = main_button_name, sprite = 'item/repair-pack'}
|
|
|
|
end
|
|
|
|
end
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local function player_left(event)
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
local left = player.gui.left
|
2017-09-25 00:36:54 +02:00
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
local frame = left[edit_announcements_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
close_edit_announcments_frame(frame)
|
|
|
|
end
|
2017-09-24 14:17:10 +02:00
|
|
|
end
|
|
|
|
|
2018-08-16 18:44:05 +02:00
|
|
|
Event.add(defines.events.on_player_joined_game, player_created)
|
|
|
|
Event.add(defines.events.on_player_left_game, player_left)
|
|
|
|
|
|
|
|
Gui.on_click(main_button_name, toggle)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
announcements_edit_button_name,
|
|
|
|
function(event)
|
|
|
|
local player = event.player
|
|
|
|
local left = player.gui.left
|
|
|
|
|
|
|
|
local frame = left[edit_announcements_frame_name]
|
|
|
|
if frame then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = {}
|
|
|
|
|
|
|
|
frame =
|
|
|
|
left.add {
|
|
|
|
type = 'frame',
|
|
|
|
name = edit_announcements_frame_name,
|
|
|
|
caption = 'Edit Announcements',
|
|
|
|
direction = 'vertical'
|
|
|
|
}
|
|
|
|
|
|
|
|
Gui.set_data(frame, data)
|
|
|
|
|
|
|
|
local top_flow = frame.add {type = 'flow'}
|
|
|
|
local last_edit_player_label = top_flow.add {type = 'label'}
|
|
|
|
local editing_players_label = top_flow.add {type = 'label'}
|
|
|
|
|
|
|
|
local textbox =
|
|
|
|
frame.add {type = 'text-box', name = edit_announcements_textbox_name, text = announcements.edit_text}
|
|
|
|
textbox.word_wrap = true
|
|
|
|
local textbox_style = textbox.style
|
2018-08-17 11:46:04 +02:00
|
|
|
textbox_style.width = 400
|
2018-08-16 18:44:05 +02:00
|
|
|
textbox_style.height = 100
|
|
|
|
|
|
|
|
data.textbox = textbox
|
|
|
|
|
|
|
|
local bottom_flow = frame.add {type = 'flow'}
|
|
|
|
|
|
|
|
local close_button = bottom_flow.add {type = 'button', name = edit_close_button_name, caption = 'Close'}
|
2018-08-17 11:46:04 +02:00
|
|
|
local clear_button = bottom_flow.add {type = 'button', name = edit_clear_button_name, caption = 'Clear'}
|
|
|
|
local reset_button = bottom_flow.add {type = 'button', name = edit_reset_button_name, caption = 'Reset'}
|
2018-08-16 18:44:05 +02:00
|
|
|
bottom_flow.add({type = 'flow'}).style.horizontally_stretchable = true
|
|
|
|
local confirm_button = bottom_flow.add {type = 'button', name = edit_confirm_button_name, caption = 'Confirm'}
|
|
|
|
|
|
|
|
Gui.set_data(close_button, frame)
|
2018-08-17 11:46:04 +02:00
|
|
|
Gui.set_data(clear_button, textbox)
|
|
|
|
Gui.set_data(reset_button, textbox)
|
2018-08-16 18:44:05 +02:00
|
|
|
Gui.set_data(confirm_button, frame)
|
|
|
|
|
|
|
|
announcements.editing_players[player.index] = {
|
|
|
|
textbox = textbox,
|
|
|
|
last_edit_player_label = last_edit_player_label,
|
|
|
|
editing_players_label = editing_players_label
|
|
|
|
}
|
|
|
|
|
|
|
|
local last_edit_message = get_edit_announcements_last_player_message()
|
|
|
|
local editing_players_message = get_editing_players_message(announcements.editing_players)
|
|
|
|
|
|
|
|
last_edit_player_label.caption = last_edit_message
|
|
|
|
last_edit_player_label.tooltip = last_edit_message
|
|
|
|
editing_players_label.caption = editing_players_message
|
|
|
|
editing_players_label.tooltip = editing_players_message
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
notify_checkbox_name,
|
|
|
|
function(event)
|
|
|
|
local checkbox = event.element
|
|
|
|
local player_index = event.player_index
|
|
|
|
if checkbox.state then
|
|
|
|
no_notify_players[player_index] = nil
|
|
|
|
else
|
|
|
|
no_notify_players[player_index] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
edit_close_button_name,
|
|
|
|
function(event)
|
|
|
|
local frame = Gui.get_data(event.element)
|
|
|
|
close_edit_announcments_frame(frame)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
edit_clear_button_name,
|
|
|
|
function(event)
|
2018-08-17 11:46:04 +02:00
|
|
|
local text = ''
|
|
|
|
local textbox = Gui.get_data(event.element)
|
|
|
|
textbox.text = text
|
|
|
|
update_edit_announcements_textbox(text, event.player)
|
2018-08-16 18:44:05 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
edit_reset_button_name,
|
|
|
|
function(event)
|
2018-08-17 11:46:04 +02:00
|
|
|
local text = announcements.text
|
|
|
|
local textbox = Gui.get_data(event.element)
|
|
|
|
textbox.text = text
|
|
|
|
update_edit_announcements_textbox(text, event.player)
|
2018-08-16 18:44:05 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
edit_confirm_button_name,
|
|
|
|
function(event)
|
|
|
|
local frame = Gui.get_data(event.element)
|
|
|
|
close_edit_announcments_frame(frame)
|
|
|
|
|
|
|
|
local player = event.player
|
|
|
|
update_announcements(player)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_text_changed(
|
|
|
|
edit_announcements_textbox_name,
|
|
|
|
function(event)
|
|
|
|
local textbox = event.element
|
|
|
|
local text = textbox.text
|
|
|
|
|
|
|
|
update_edit_announcements_textbox(text, event.player)
|
|
|
|
end
|
|
|
|
)
|
2018-08-17 11:46:04 +02:00
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
Gui.on_click(
|
|
|
|
delete_task_button_name,
|
|
|
|
function(event)
|
|
|
|
local task_index = Gui.get_data(event.element)
|
|
|
|
|
|
|
|
local task = table.remove(tasks, task_index)
|
|
|
|
|
|
|
|
local message =
|
|
|
|
table.concat {
|
|
|
|
event.player.name,
|
|
|
|
' has deleted task #',
|
|
|
|
task.task_id,
|
|
|
|
' - ',
|
|
|
|
task.name
|
|
|
|
}
|
|
|
|
|
|
|
|
for pi, _ in pairs(task.volunteers) do
|
|
|
|
local tasks_for_player = player_tasks[pi]
|
|
|
|
if tasks_for_player then
|
|
|
|
tasks_for_player[task] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for pi, p in ipairs(game.connected_players) do
|
|
|
|
local notify = not no_notify_players[pi]
|
|
|
|
local left = p.gui.left
|
|
|
|
local frame = left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local data = Gui.get_data(frame)
|
|
|
|
redraw_tasks(data)
|
|
|
|
elseif notify then
|
|
|
|
draw_main_frame(left)
|
|
|
|
end
|
|
|
|
|
|
|
|
if notify then
|
|
|
|
p.print(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
Gui.on_click(
|
|
|
|
edit_task_button_name,
|
|
|
|
function(event)
|
|
|
|
local previous_task = Gui.get_data(event.element)
|
|
|
|
local left = event.player.gui.left
|
|
|
|
local frame = left[create_task_frame_name]
|
|
|
|
|
|
|
|
if frame then
|
|
|
|
Gui.destroy(frame)
|
|
|
|
end
|
|
|
|
|
|
|
|
draw_create_task_frame(left, previous_task)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-08-17 18:22:53 +02:00
|
|
|
local function do_direction(event, sign)
|
|
|
|
local count
|
|
|
|
if event.shift then
|
|
|
|
count = #tasks
|
|
|
|
else
|
|
|
|
local button = event.button
|
|
|
|
if button == defines.mouse_button_type.right then
|
|
|
|
count = 5
|
|
|
|
else
|
|
|
|
count = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
count = count * sign
|
|
|
|
|
|
|
|
local old_index = Gui.get_data(event.element)
|
|
|
|
|
|
|
|
local new_index = old_index + count
|
|
|
|
new_index = math.clamp(new_index, 1, #tasks)
|
|
|
|
|
|
|
|
local task = table.remove(tasks, old_index)
|
|
|
|
table.insert(tasks, new_index, task)
|
|
|
|
|
|
|
|
for _, p in ipairs(game.connected_players) do
|
|
|
|
local frame = p.gui.left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local data = Gui.get_data(frame)
|
|
|
|
redraw_tasks(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
move_task_up_button_name,
|
|
|
|
function(event)
|
|
|
|
do_direction(event, -1)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
move_task_down_button_name,
|
|
|
|
function(event)
|
|
|
|
do_direction(event, 1)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
volunteer_task_button_name,
|
|
|
|
function(event)
|
|
|
|
local button = event.element
|
|
|
|
local task = Gui.get_data(button)
|
|
|
|
|
|
|
|
local player_index = event.player_index
|
|
|
|
local volunteers = task.volunteers
|
|
|
|
|
|
|
|
if volunteers[player_index] then
|
|
|
|
volunteers[player_index] = nil
|
|
|
|
|
|
|
|
local tasks_for_player = player_tasks[player_index]
|
|
|
|
tasks_for_player[task] = nil
|
|
|
|
else
|
|
|
|
volunteers[player_index] = event.player
|
|
|
|
|
|
|
|
local tasks_for_player = player_tasks[player_index]
|
|
|
|
if not tasks_for_player then
|
|
|
|
tasks_for_player = {}
|
|
|
|
player_tasks[player_index] = tasks_for_player
|
|
|
|
end
|
|
|
|
|
|
|
|
tasks_for_player[task] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, p in ipairs(game.connected_players) do
|
|
|
|
local frame = p.gui.left[main_frame_name]
|
|
|
|
if frame and frame.valid then
|
|
|
|
local data = Gui.get_data(frame)
|
|
|
|
local volunteer_buttons = data.volunteer_buttons
|
|
|
|
|
|
|
|
update_volunteer_button(volunteer_buttons[task], task)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
Gui.on_click(
|
|
|
|
add_task_button_name,
|
|
|
|
function(event)
|
2018-08-17 21:35:54 +02:00
|
|
|
local left = event.player.gui.left
|
2018-08-17 11:46:04 +02:00
|
|
|
local frame = left[create_task_frame_name]
|
|
|
|
|
|
|
|
if frame then
|
2018-08-17 21:35:54 +02:00
|
|
|
Gui.destroy(frame)
|
2018-08-17 11:46:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
draw_create_task_frame(left)
|
2018-08-17 11:46:04 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
create_task_close_button_name,
|
|
|
|
function(event)
|
|
|
|
local frame = Gui.get_data(event.element)
|
|
|
|
Gui.destroy(frame)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
create_task_clear_button_name,
|
|
|
|
function(event)
|
|
|
|
local textbox = Gui.get_data(event.element)
|
|
|
|
textbox.text = ''
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
create_task_confirm_button_name,
|
|
|
|
function(event)
|
|
|
|
local data = Gui.get_data(event.element)
|
|
|
|
|
|
|
|
local frame = data.frame
|
|
|
|
local textbox = data.textbox
|
|
|
|
local task_name = textbox.text
|
|
|
|
|
|
|
|
Gui.destroy(frame)
|
|
|
|
|
|
|
|
create_new_tasks(task_name, event.player)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-08-17 21:35:54 +02:00
|
|
|
Gui.on_click(
|
|
|
|
create_task_edit_button_name,
|
|
|
|
function(event)
|
|
|
|
local data = Gui.get_data(event.element)
|
|
|
|
|
|
|
|
local frame = data.frame
|
|
|
|
local textbox = data.textbox
|
|
|
|
local name = textbox.text
|
|
|
|
local task = data.previous_task
|
|
|
|
|
|
|
|
Gui.destroy(frame)
|
|
|
|
|
|
|
|
task.name = name
|
|
|
|
|
|
|
|
local task_index
|
|
|
|
for i, t in ipairs(tasks) do
|
|
|
|
if task == t then
|
|
|
|
task_index = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not task_index then
|
|
|
|
table.insert(tasks, task)
|
|
|
|
end
|
|
|
|
|
|
|
|
local message =
|
|
|
|
table.concat {
|
|
|
|
event.player.name,
|
|
|
|
' has edited task #',
|
|
|
|
task.task_id,
|
|
|
|
' - ',
|
|
|
|
name
|
|
|
|
}
|
|
|
|
|
|
|
|
for pi, p in ipairs(game.connected_players) do
|
|
|
|
local notify = not no_notify_players[pi]
|
|
|
|
local left = p.gui.left
|
|
|
|
local main_frame = left[main_frame_name]
|
|
|
|
|
|
|
|
if main_frame then
|
|
|
|
local main_frame_data = Gui.get_data(main_frame)
|
|
|
|
redraw_tasks(main_frame_data)
|
|
|
|
elseif notify then
|
|
|
|
draw_main_frame(left)
|
|
|
|
end
|
|
|
|
|
|
|
|
if notify then
|
|
|
|
p.print(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-08-17 11:46:04 +02:00
|
|
|
commands.add_command(
|
|
|
|
'task',
|
|
|
|
'<task> - Creates a new task (Admins and regulars only).',
|
|
|
|
function(cmd)
|
|
|
|
local player = game.player
|
|
|
|
|
|
|
|
if player then
|
2018-08-17 18:22:53 +02:00
|
|
|
if not player.admin and not UserGroups.is_regular(player.name) then
|
2018-08-17 11:46:04 +02:00
|
|
|
cant_run(cmd.name)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
player = server_player
|
|
|
|
end
|
|
|
|
|
|
|
|
local task_name = cmd.parameter
|
|
|
|
|
|
|
|
if not task_name or task_name == '' then
|
|
|
|
player.print('Usage: /task <task>')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
create_new_tasks(task_name, player)
|
|
|
|
end
|
|
|
|
)
|