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