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