z_mirror/TaskList

git clone https://git.raiguard.me/z_mirror/TaskList

raiguardMostly hook up task details GUI (#7)fa77a7d

task-details-gui
4.2 KiB179 linesraw
1local table = require("__flib__.table")
2
3local util = require("scripts.util")
4
5--- @class Task
6local Task = {}
7
8--- Update the task info and refresh all GUIs
9--- @param title string
10--- @param description string
11--- @param assignee LuaPlayer|nil
12--- @param status string
13function Task:update(title, description, assignee, status)
14  self.title = title
15  self.description = description
16  self.assignee = assignee
17  self.status = status
18
19  self:update_guis(function(Gui)
20    Gui:update_task(self)
21  end)
22end
23
24--- Delete the task and remove it from all GUIs.
25function Task:delete()
26  for _, subtasks in pairs({ self.completed_tasks, self.tasks }) do
27    for _, task_id in pairs(subtasks) do
28      local subtask = global.tasks[task_id]
29      if subtask then
30        subtask:delete()
31      end
32    end
33  end
34
35  global.tasks[self.id] = nil
36
37  local owner_table = self.owner_table
38  local tasks_table = owner_table[self.completed and "completed_tasks" or "tasks"]
39  table.remove(tasks_table, table.find(tasks_table, self.id))
40
41  self:update_guis(function(Gui)
42    Gui:delete_task(self)
43  end)
44end
45
46function Task:toggle_completed()
47  local owner_table = self.owner_table
48
49  self.completed = not self.completed
50
51  if self.completed then
52    util.remove_task(owner_table.tasks, self.id)
53    table.insert(owner_table.completed_tasks, 1, self.id)
54  else
55    util.remove_task(owner_table.completed_tasks, self.id)
56    table.insert(owner_table.tasks, self.id)
57  end
58
59  self:update_guis(function(Gui)
60    Gui:delete_task(self, not self.completed)
61    Gui:add_task(self, self.completed and 1 or nil)
62  end)
63end
64
65--- @param delta number
66function Task:move(delta)
67  local tasks_table = self.owner_table[self.completed and "completed_tasks" or "tasks"]
68  local index = table.find(tasks_table, self.id)
69  if index then
70    if index + delta > 0 and index + delta <= #tasks_table then
71      tasks_table[index] = tasks_table[index + delta]
72      tasks_table[index + delta] = self.id
73
74      self:update_guis(function(Gui)
75        Gui:move_task(self, delta)
76      end)
77    end
78  end
79end
80
81--- @param callback fun(Gui: TasksGui)
82function Task:update_guis(callback)
83  local players = {}
84
85  -- Get ultimate owner
86  local path = {}
87  local owner = self.owner
88  while owner.object_name == "Task" do
89    table.insert(path, 1, owner.id)
90    owner = owner.owner
91  end
92
93  if owner.object_name == "LuaForce" then
94    for player_index, player in pairs(game.players) do
95      if player.force.index == owner.index then
96        table.insert(players, player_index)
97      end
98    end
99  else
100    players = { owner.index }
101  end
102
103  for _, player_index in pairs(players) do
104    local Gui = util.get_gui(player_index, "tasks")
105    if Gui then
106      callback(Gui)
107    end
108
109    -- Call on all task detail GUIs
110    for _, task_id in pairs(path) do
111      local Gui = util.get_gui(player_index, { name = "task", id = task_id })
112      if Gui then
113        callback(Gui)
114      end
115    end
116  end
117end
118
119local task = {}
120
121--- @param title string
122--- @param description string
123--- @param owner LuaForce|LuaPlayer|Task
124--- @param assignee LuaPlayer|nil
125--- @param status string
126--- @param add_to_top boolean
127function task.new(title, description, owner, assignee, status, add_to_top)
128  local id = global.next_task_id
129  global.next_task_id = id + 1
130
131  local owner_table
132
133  if owner.object_name == "Task" then
134    owner_table = owner
135  elseif owner.object_name == "LuaForce" then
136    owner_table = global.forces[owner.index]
137  else
138    owner_table = global.players[owner.index]
139  end
140
141  --- If `owner` is a `LuaPlayer`, then `assignee` will always be the same `LuaPlayer`.
142  --- @type Task
143  local self = {
144    assignee = assignee,
145    completed = false,
146    completed_tasks = {},
147    description = description,
148    id = id,
149    object_name = "Task",
150    owner = owner,
151    owner_table = owner_table,
152    status = status,
153    tasks = {}, --- @type number[]
154    title = title,
155  }
156
157  if add_to_top then
158    table.insert(owner_table.tasks, 1, self.id)
159  else
160    table.insert(owner_table.tasks, self.id)
161  end
162
163  task.load(self)
164
165  global.tasks[id] = self
166
167  self:update_guis(function(Gui)
168    Gui:add_task(self, add_to_top and 1 or nil)
169  end)
170
171  return self
172end
173
174--- @param self Task
175function task.load(self)
176  setmetatable(self, { __index = Task })
177end
178
179return task