z_mirror/TaskList

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

austinrobichaux18Fully finish areaa87f091

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