annotate State.lua @ 77:0a3f590f69e6

Update todo list Helper function to print list status
author John@Yosemite-PC
date Thu, 12 Apr 2012 22:18:05 -0400
parents 39be9328acd0
children 5b507f4125d4
rev   line source
John@71 1 local bsk=bsk
John@71 2 local _G=_G
John@71 3 local table=table
John@71 4 local pairs=pairs
John@71 5 local setmetatable=setmetatable
John@71 6 local ipairs=ipairs
John@71 7 local string=string
John@71 8 local sformat=string.format
John@71 9 local tostring=tostring
John@71 10 local type=type
John@71 11 local unpack=unpack
John@71 12 local getn=getn
John@71 13 setfenv(1,bsk)
John@71 14
John@71 15 -- simple state machine
John@71 16
John@71 17 --Begin loot
John@71 18 --Activate list ... only during looting?
John@71 19 --open bidding/rolling
John@71 20 --bid/roll occurred
John@71 21 --remove bid/roll
John@71 22 --close bidding
John@71 23 --remove item
John@71 24
John@71 25
John@71 26 -- we'll track state, but that may or may not result in a GUI change until down
John@71 27 -- the road
John@71 28
John@71 29 -- todo: transmit this all to only the raid, not the guild?
John@71 30
John@71 31 -- sample procedure
John@71 32 -- person B opens GUI.
John@71 33 -- person A begins looting, sets a list
John@71 34 -- person A begins bidding, transmists the state
John@71 35 -- person B goes into bidding state, their button activates
John@71 36 -- person B clicks the button. button changes state.
John@71 37 -- person B broadcasts their bid. if a bid, everyone just accepts it.
John@71 38 -- - if a roll, then the master does the roll and rebroadcasts
John@71 39
John@72 40 state = "neutral" -- states that are possible: neutral, looting, bidding
John@71 41 local looting = false
John@71 42 stateactive = nil
John@71 43 stateitem = nil
John@73 44 stateitemlist = {}
John@71 45 statebids = {}
John@71 46 staterolls = {}
John@73 47 stateactivelist = nil
John@71 48
John@76 49 -- todo: protect initiators to admin only
John@76 50
John@71 51 local rollListeners = {}
John@71 52 function RegisterListenerRolls(listener)
John@71 53 table.insert(rollListeners,listener)
John@71 54 end
John@71 55 function AlertRollListeners()
John@71 56 for i,v in pairs(rollListeners) do
John@71 57 print("roll out")
John@71 58 v["RollEvent"](v)
John@71 59 end
John@71 60 end
John@71 61
John@72 62 local listChangeListeners = {}
John@72 63 function RegisterListenerActiveListChanged(listener)
John@72 64 table.insert(listChangeListeners,listener)
John@72 65 end
John@72 66 function AlertActiveListChangedListeners()
John@72 67 for i,v in pairs(listChangeListeners) do
John@72 68 print("list out")
John@72 69 v["ActiveListEvent"](v)
John@72 70 end
John@72 71 end
John@72 72
John@72 73 local stateChangeListeners = {}
John@72 74 function RegisterListenerStateChange(listener)
John@72 75 table.insert(stateChangeListeners,listener)
John@72 76 end
John@72 77 function AlertStateChangeListeners()
John@72 78 for i,v in pairs(stateChangeListeners) do
John@72 79 print("state out")
John@72 80 v["StateEvent"](v)
John@72 81 end
John@72 82 end
John@72 83
John@73 84 local itemListListeners = {}
John@73 85 function RegisterItemListListener(listener)
John@73 86 table.insert(itemListListeners,listener)
John@73 87 end
John@73 88 function AlertItemListListeners()
John@73 89 for i,v in pairs(itemListListeners) do
John@73 90 print("item change")
John@73 91 v["ItemListEvent"](v)
John@73 92 end
John@73 93 end
John@73 94
John@73 95 function BeginLoot(packet)
John@73 96 local items, listValue = unpack(packet)
John@71 97 if state == "neutral" then
John@71 98 state = "looting"
John@73 99 stateactivelist = listValue
John@73 100 stateitemlist = items
John@73 101 AlertItemListListeners()
John@73 102 CreateGUI() -- todo: bad spot, but that's ok for now
John@71 103 else
John@71 104 _G.error("Bad state transition", state, "looting")
John@71 105 end
John@71 106 end
John@71 107
John@73 108 function InitiateBeginLoot(items,listValue)
John@73 109 if state == "neutral" then
John@73 110 Comm:SendStateChange("BL",items,listValue)
John@73 111 end
John@73 112 end
John@72 113
John@72 114 -- Activate List {{{
John@72 115 function ActivateList(packet) -- doesn't cause a transition, but we only enforce a list selection during certain times
John@72 116 local list = unpack(packet)
John@71 117 if state == "looting" then
John@72 118 stateactivelist = list
John@72 119 AlertActiveListChangedListeners()
John@71 120 end
John@71 121 end
John@71 122
John@72 123 function InitiateActivateList(list)
John@72 124 if state == "looting" then
John@72 125 Comm:SendStateChange("AL",list)
John@72 126 end
John@72 127 end
John@72 128 --}}}
John@72 129 -- Open Bidding {{{
John@71 130 function OpenBid(packet)
John@71 131 local item = unpack(packet)
John@73 132 if state == "looting" then
John@71 133 state = "bidding"
John@75 134 stateitem = item
John@73 135 AlertStateChangeListeners()
John@73 136 end
John@71 137 end
John@71 138
John@71 139 function InitiateOpenBid(item)
John@73 140 if state == "looting" then
John@73 141 Comm:SendStateChange("OB",item)
John@73 142 end
John@71 143 end
John@72 144 --}}}
John@72 145 -- Bid {{{
John@71 146 function ReceivedBid(packet) -- no state transition, but only matters during one state
John@71 147 local person, roll = unpack(packet)
John@71 148
John@71 149 if state == "bidding" then
John@71 150 if roll then
John@71 151 table.insert(statebids,person) -- todo:
John@71 152 else
John@71 153 table.insert(statebids,person) -- todo: keep sorted
John@71 154 end
John@71 155 AlertRollListeners()
John@71 156 end
John@71 157
John@71 158 -- else ignore ...
John@71 159 end
John@71 160
John@71 161 function InitiateBid(person,roll)
John@71 162 if state == "bidding" then
John@71 163 for i,v in pairs(statebids) do
John@71 164 if person.value == v.value then
John@71 165 print(person.value .. " is already on the list")
John@71 166 return -- no double adds please
John@71 167 end
John@71 168 end
John@71 169 Comm:SendStateChange("RB",person,roll)
John@71 170 end
John@71 171 end
John@72 172 --}}}
John@72 173 -- Retration {{{
John@71 174 function ReceivedRetraction(packet)
John@71 175 local person = unpack(packet)
John@71 176 if state == "bidding" then
John@71 177 for i,v in pairs(statebids) do
John@71 178 if v.value == person.value then
John@71 179 table.remove(statebids,i)
John@71 180 AlertRollListeners()
John@71 181 return
John@71 182 end
John@71 183 end
John@71 184 end
John@71 185 end
John@71 186
John@71 187 function InitiateRetract(person)
John@71 188 if state == "bidding" then
John@71 189 Comm:SendStateChange("RR",person,roll)
John@71 190 end
John@71 191 end
John@72 192 --}}}
John@72 193 -- Close Bidding {{{
John@72 194 function CloseBidding(packet)
John@72 195 local awardedTo = unpack(packet)
John@75 196 if state == "bidding" then
John@75 197 state = "looting"
John@75 198 AlertStateChangeListeners()
John@75 199 -- todo: remove the item from the window, record history
John@75 200 end
John@71 201 end
John@71 202
John@72 203 function InitiateCloseBidding(awardedTo)
John@75 204 if state == "bidding" then
John@75 205 Comm:SendStateChange("CB",awardedTo)
John@75 206 end
John@72 207 end
John@72 208 --}}}
John@73 209 -- Close Looting {{{
John@72 210 function CloseLooting(packet)
John@71 211 state = "neutral"
John@73 212 stateactive = nil
John@73 213 stateitem = nil
John@73 214 stateitemlist = {}
John@73 215 statebids = {}
John@73 216 staterolls = {}
John@73 217 AlertStateChangeListeners()
John@73 218 AlertItemListListeners()
John@71 219 end
John@72 220
John@72 221 function InitiateCloseLooting()
John@72 222 Comm:SendStateChange("CL")
John@72 223 end
John@73 224 --}}}
John@72 225 function DispatchState(packet)
John@72 226 local state = table.remove(packet,1)
John@72 227 print("Dispatching", state)
John@72 228 if state == "RB" then
John@72 229 ReceivedBid(packet)
John@73 230 elseif state == "BL" then
John@73 231 BeginLoot(packet)
John@72 232 elseif state == "RR" then
John@72 233 ReceivedRetraction(packet)
John@72 234 elseif state == "OB" then
John@72 235 OpenBid(packet)
John@72 236 elseif state == "CB" then
John@72 237 CloseBidding(packet)
John@73 238 elseif state == "CL" then
John@72 239 CloseLooting(packet)
John@72 240 elseif state == "AL" then
John@72 241 ActivateList(packet)
John@72 242 else -- todo ...
John@72 243
John@72 244 end
John@72 245 end
John@72 246
John@73 247 function InitializeState()
John@73 248 local ltemp = 0
John@73 249 local lids = LootLists:GetAllIds()
John@73 250 for _,v in pairs(lids) do
John@73 251 local l = LootLists:Select(v)
John@73 252 if l:GetLength() > 0 then
John@73 253 if ltemp == 0 then
John@73 254 ltemp = l:GetId()
John@73 255 end
John@73 256 end
John@73 257 end
John@73 258 stateactivelist = ltemp
John@73 259 end
John@73 260