1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/tasklist.lua

1095 lines
32 KiB
Lua
Raw Normal View History

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).'
}
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
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 23:30:41 +02:00
local function get_task_label_tooltip(task, game_tick)
local tooltip = {'Created by ', task.created_by.name}
local edited_by = task.edited_by
if edited_by then
table.insert(tooltip, ' Edited by ')
for _, p in pairs(edited_by) do
table.insert(tooltip, p.name)
table.insert(tooltip, ', ')
end
table.remove(tooltip)
end
table.insert(tooltip, ' (')
table.insert(tooltip, Utils.format_time(game_tick - task.tick))
table.insert(tooltip, ' ago)')
return table.concat(tooltip)
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 23:30:41 +02:00
local function redraw_tasks(data, enabled)
2018-08-17 18:22:53 +02:00
local parent = data.tasks_content
2018-08-17 11:46:04 +02:00
Gui.clear(parent)
2018-08-17 23:30:41 +02:00
2018-08-17 18:22:53 +02:00
local volunteer_buttons = {}
2018-08-17 23:30:41 +02:00
local task_labels = {}
2018-08-17 18:22:53 +02:00
data.volunteer_buttons = volunteer_buttons
2018-08-17 23:30:41 +02:00
data.task_labels = task_labels
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
2018-08-17 23:30:41 +02:00
local delete_button_tooltip
local edit_button_tooltip
local up_button_tooltip
local down_button_tooltip
if enabled then
delete_button_tooltip = 'Delete task.'
edit_button_tooltip = 'Edit task.'
up_button_tooltip = 'Move the task up, right click moves 5 spaces, shift click moves the task to the top.'
down_button_tooltip =
'Move the task down, right click moves 5 spaces, shift click moves the task to the bottom.'
else
delete_button_tooltip = 'Sorry, you have to be a regular to delete tasks.'
edit_button_tooltip = 'Sorry, you have to be a regular to edit tasks.'
up_button_tooltip = 'Sorry, you have to be a regualr to move tasks.'
down_button_tooltip = 'Sorry, you have to be a regualr to move tasks.'
end
local game_tick = game.tick
2018-08-17 11:46:04 +02:00
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',
2018-08-17 23:30:41 +02:00
tooltip = delete_button_tooltip
2018-08-17 11:46:04 +02:00
}
2018-08-17 23:30:41 +02:00
delete_button.enabled = enabled
2018-08-17 11:46:04 +02:00
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',
2018-08-17 23:30:41 +02:00
tooltip = edit_button_tooltip
2018-08-17 11:46:04 +02:00
}
2018-08-17 23:30:41 +02:00
edit_button.enabled = enabled
2018-08-17 11:46:04 +02:00
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 = '',
2018-08-17 23:30:41 +02:00
tooltip = up_button_tooltip
2018-08-17 18:22:53 +02:00
}
2018-08-17 23:30:41 +02:00
up_button.enabled = enabled and 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 = '',
2018-08-17 23:30:41 +02:00
tooltip = down_button_tooltip
2018-08-17 18:22:53 +02:00
}
2018-08-17 23:30:41 +02:00
down_button.enabled = enabled and 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
2018-08-17 23:30:41 +02:00
local label =
parent.add {
type = 'label',
caption = table.concat {'#', task.task_id, ' ', task.name},
tooltip = get_task_label_tooltip(task, game_tick)
}
2018-08-17 18:22:53 +02:00
label.style.left_padding = 4
2018-08-17 23:30:41 +02:00
task_labels[task_index] = label
2018-08-17 11:46:04 +02:00
end
end
2018-08-17 23:30:41 +02:00
local function draw_main_frame(left, player)
local enabled = player.admin or UserGroups.is_regular(player.name)
2018-08-16 18:44:05 +02:00
local data = {}
2018-08-17 23:30:41 +02:00
local edit_announcments_button_tooltip
local add_task_button_tooltip
if enabled then
edit_announcments_button_tooltip = 'Edit announcments.'
add_task_button_tooltip = 'Create a new task.'
else
edit_announcments_button_tooltip = 'Sorry, you need to a regular to edit announcments.'
add_task_button_tooltip = 'Sorry, you need to be a regular to create a new tasks.'
end
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',
2018-08-17 23:30:41 +02:00
tooltip = edit_announcments_button_tooltip
2018-08-16 18:44:05 +02:00
}
2018-08-17 23:30:41 +02:00
edit_announcments_button.enabled = enabled
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 23:30:41 +02:00
announcements_textbox_style.width = 450
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',
2018-08-17 23:30:41 +02:00
tooltip = add_task_button_tooltip
2018-08-17 21:35:54 +02:00
}
2018-08-17 23:30:41 +02:00
add_task_button.enabled = enabled
2018-08-17 21:35:54 +02:00
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 23:30:41 +02:00
tasks_scroll_pane_style.width = 450
2018-08-17 18:22:53 +02:00
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 23:30:41 +02:00
redraw_tasks(data, enabled)
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)
2018-08-17 23:30:41 +02:00
local player = event.player
local left = player.gui.left
2018-08-16 18:44:05 +02:00
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
2018-08-17 23:30:41 +02:00
draw_main_frame(left, player)
2018-08-16 18:44:05 +02:00
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
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)
2018-08-16 18:44:05 +02:00
if not next(editing_players) then
return
end
2018-08-16 18:44:05 +02:00
local editing_players_message = get_editing_players_message(editing_players)
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
2018-08-16 18:44:05 +02:00
local function update_announcements(player)
local text = announcements.edit_text
2018-08-16 18:44:05 +02:00
announcements.text = text
announcements.last_update_player = player
announcements.last_update_time = game.tick
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
2018-08-18 12:05:35 +02:00
for _, p in ipairs(game.connected_players) do
local notify = not no_notify_players[p.index]
2018-08-16 18:44:05 +02:00
if notify then
p.print(update_message)
end
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
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
2018-08-17 23:30:41 +02:00
draw_main_frame(left, p)
2018-08-16 18:44:05 +02:00
end
end
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
}
2018-08-18 12:05:35 +02:00
for _, p in ipairs(game.connected_players) do
local notify = not no_notify_players[p.index]
2018-08-17 11:46:04 +02:00
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 23:30:41 +02:00
local enabled = p.admin or UserGroups.is_regular(p.name)
redraw_tasks(frame_data, enabled)
2018-08-17 11:46:04 +02:00
elseif notify then
2018-08-17 23:30:41 +02:00
draw_main_frame(left, p)
2018-08-17 11:46:04 +02:00
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'}
2018-08-17 23:30:41 +02:00
frame.style.width = 470
2018-08-17 21:35:54 +02:00
2018-08-18 12:05:35 +02:00
local textbox = frame.add {type = 'textfield', text = text}
2018-08-17 21:35:54 +02:00
local textbox_style = textbox.style
2018-08-17 23:30:41 +02:00
textbox_style.width = 450
2018-08-17 21:35:54 +02:00
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
2018-08-16 18:44:05 +02:00
local gui = player.gui
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()
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
2018-08-17 23:30:41 +02:00
local enabled = player.admin or UserGroups.is_regular(player.name)
redraw_tasks(data, enabled)
2018-08-17 18:22:53 +02:00
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
2018-08-17 23:30:41 +02:00
local main_frame = p.gui.left[main_frame_name]
if main_frame then
local data = Gui.get_data(main_frame)
2018-08-17 18:22:53 +02:00
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
end
2018-08-17 23:30:41 +02:00
local function on_tick()
for _, p in ipairs(game.connected_players) do
local left = p.gui.left
local frame = left[main_frame_name]
if frame then
local data = Gui.get_data(frame)
data.tasks_updated_label.caption = get_task_updated_by_message()
data.announcements_updated_label.caption = get_announcements_updated_by_message()
local game_tick = game.tick
for task_index, label in ipairs(data.task_labels) do
label.tooltip = get_task_label_tooltip(tasks[task_index], game_tick)
end
end
end
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)
2018-08-17 23:30:41 +02:00
Event.on_nth_tick(3600, on_tick)
2018-08-16 18:44:05 +02:00
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'
}
2018-08-17 23:30:41 +02:00
frame.style.width = 470
2018-08-16 18:44:05 +02:00
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 23:30:41 +02:00
textbox_style.width = 450
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
2018-08-18 12:05:35 +02:00
for _, p in ipairs(game.connected_players) do
local notify = not no_notify_players[p.index]
2018-08-17 18:22:53 +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)
2018-08-17 23:30:41 +02:00
local enabled = p.admin or UserGroups.is_regular(p.name)
redraw_tasks(data, enabled)
2018-08-17 18:22:53 +02:00
elseif notify then
2018-08-17 23:30:41 +02:00
draw_main_frame(left, p)
2018-08-17 18:22:53 +02:00
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)
2018-08-17 23:30:41 +02:00
local enabled = p.admin or UserGroups.is_regular(p.name)
redraw_tasks(data, enabled)
2018-08-17 18:22:53 +02:00
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)
2018-08-17 23:30:41 +02:00
local player = event.player
2018-08-17 11:46:04 +02:00
local textbox = data.textbox
local task_name = textbox.text
2018-08-17 23:30:41 +02:00
if task_name == '' then
player.print('Sorry, tasks cannot be empty.')
return
end
local frame = data.frame
2018-08-17 11:46:04 +02:00
Gui.destroy(frame)
2018-08-17 23:30:41 +02:00
create_new_tasks(task_name, player)
2018-08-17 11:46:04 +02:00
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)
2018-08-17 23:30:41 +02:00
local player = event.player
2018-08-17 21:35:54 +02:00
local textbox = data.textbox
local name = textbox.text
2018-08-17 23:30:41 +02:00
if name == '' then
player.print('Sorry, tasks cannot be empty.')
return
end
local frame = data.frame
2018-08-17 21:35:54 +02:00
local task = data.previous_task
Gui.destroy(frame)
2018-08-17 23:30:41 +02:00
if task.name == name then
return
end
local tick = game.tick
2018-08-17 21:35:54 +02:00
task.name = name
2018-08-17 23:30:41 +02:00
local edited_by = task.edited_by
if not edited_by then
edited_by = {}
task.edited_by = edited_by
end
edited_by[player.index] = player
task.tick = tick
last_task_update_data.player = player
last_task_update_data.time = tick
2018-08-17 21:35:54 +02:00
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
}
2018-08-17 23:30:41 +02:00
local update_message = get_task_updated_by_message()
2018-08-18 12:05:35 +02:00
for _, p in ipairs(game.connected_players) do
local notify = not no_notify_players[p.index]
2018-08-17 21:35:54 +02:00
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)
2018-08-17 23:30:41 +02:00
main_frame_data.tasks_updated_label.caption = update_message
local enabled = p.admin or UserGroups.is_regular(p.name)
redraw_tasks(main_frame_data, enabled)
2018-08-17 21:35:54 +02:00
elseif notify then
2018-08-17 23:30:41 +02:00
draw_main_frame(left, p)
2018-08-17 21:35:54 +02:00
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
)