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