annotate State.lua @ 75:ed1c9fd4cc95

Progress on bidding events and GUI. Slave GUIs now respond properly.
author John@Doomsday
date Mon, 09 Apr 2012 09:40:53 -0400
parents 7eb2963eea7d
children 39be9328acd0
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@71 49 local rollListeners = {}
John@71 50 function RegisterListenerRolls(listener)
John@71 51 table.insert(rollListeners,listener)
John@71 52 end
John@71 53 function AlertRollListeners()
John@71 54 for i,v in pairs(rollListeners) do
John@71 55 print("roll out")
John@71 56 v["RollEvent"](v)
John@71 57 end
John@71 58 end
John@71 59
John@72 60 local listChangeListeners = {}
John@72 61 function RegisterListenerActiveListChanged(listener)
John@72 62 table.insert(listChangeListeners,listener)
John@72 63 end
John@72 64 function AlertActiveListChangedListeners()
John@72 65 for i,v in pairs(listChangeListeners) do
John@72 66 print("list out")
John@72 67 v["ActiveListEvent"](v)
John@72 68 end
John@72 69 end
John@72 70
John@72 71 local stateChangeListeners = {}
John@72 72 function RegisterListenerStateChange(listener)
John@72 73 table.insert(stateChangeListeners,listener)
John@72 74 end
John@72 75 function AlertStateChangeListeners()
John@72 76 for i,v in pairs(stateChangeListeners) do
John@72 77 print("state out")
John@72 78 v["StateEvent"](v)
John@72 79 end
John@72 80 end
John@72 81
John@73 82 local itemListListeners = {}
John@73 83 function RegisterItemListListener(listener)
John@73 84 table.insert(itemListListeners,listener)
John@73 85 end
John@73 86 function AlertItemListListeners()
John@73 87 for i,v in pairs(itemListListeners) do
John@73 88 print("item change")
John@73 89 v["ItemListEvent"](v)
John@73 90 end
John@73 91 end
John@73 92
John@73 93 function BeginLoot(packet)
John@73 94 local items, listValue = unpack(packet)
John@71 95 if state == "neutral" then
John@71 96 state = "looting"
John@73 97 stateactivelist = listValue
John@73 98 stateitemlist = items
John@73 99 AlertItemListListeners()
John@73 100 CreateGUI() -- todo: bad spot, but that's ok for now
John@71 101 else
John@71 102 _G.error("Bad state transition", state, "looting")
John@71 103 end
John@71 104 end
John@71 105
John@73 106 function InitiateBeginLoot(items,listValue)
John@73 107 if state == "neutral" then
John@73 108 Comm:SendStateChange("BL",items,listValue)
John@73 109 end
John@73 110 end
John@72 111
John@72 112 -- Activate List {{{
John@72 113 function ActivateList(packet) -- doesn't cause a transition, but we only enforce a list selection during certain times
John@72 114 local list = unpack(packet)
John@71 115 if state == "looting" then
John@72 116 stateactivelist = list
John@72 117 AlertActiveListChangedListeners()
John@71 118 end
John@71 119 end
John@71 120
John@72 121 function InitiateActivateList(list)
John@72 122 if state == "looting" then
John@72 123 Comm:SendStateChange("AL",list)
John@72 124 end
John@72 125 end
John@72 126 --}}}
John@72 127 -- Open Bidding {{{
John@71 128 function OpenBid(packet)
John@71 129 local item = unpack(packet)
John@73 130 if state == "looting" then
John@71 131 state = "bidding"
John@75 132 stateitem = item
John@73 133 AlertStateChangeListeners()
John@73 134 end
John@71 135 end
John@71 136
John@71 137 function InitiateOpenBid(item)
John@73 138 if state == "looting" then
John@73 139 Comm:SendStateChange("OB",item)
John@73 140 end
John@71 141 end
John@72 142 --}}}
John@72 143 -- Bid {{{
John@71 144 function ReceivedBid(packet) -- no state transition, but only matters during one state
John@71 145 local person, roll = unpack(packet)
John@71 146
John@71 147 if state == "bidding" then
John@71 148 if roll then
John@71 149 table.insert(statebids,person) -- todo:
John@71 150 else
John@71 151 table.insert(statebids,person) -- todo: keep sorted
John@71 152 end
John@71 153 AlertRollListeners()
John@71 154 end
John@71 155
John@71 156 -- else ignore ...
John@71 157 end
John@71 158
John@71 159 function InitiateBid(person,roll)
John@71 160 if state == "bidding" then
John@71 161 for i,v in pairs(statebids) do
John@71 162 if person.value == v.value then
John@71 163 print(person.value .. " is already on the list")
John@71 164 return -- no double adds please
John@71 165 end
John@71 166 end
John@71 167 Comm:SendStateChange("RB",person,roll)
John@71 168 end
John@71 169 end
John@72 170 --}}}
John@72 171 -- Retration {{{
John@71 172 function ReceivedRetraction(packet)
John@71 173 local person = unpack(packet)
John@71 174 if state == "bidding" then
John@71 175 for i,v in pairs(statebids) do
John@71 176 if v.value == person.value then
John@71 177 table.remove(statebids,i)
John@71 178 AlertRollListeners()
John@71 179 return
John@71 180 end
John@71 181 end
John@71 182 end
John@71 183 end
John@71 184
John@71 185 function InitiateRetract(person)
John@71 186 if state == "bidding" then
John@71 187 Comm:SendStateChange("RR",person,roll)
John@71 188 end
John@71 189 end
John@72 190 --}}}
John@72 191 -- Close Bidding {{{
John@72 192 function CloseBidding(packet)
John@72 193 local awardedTo = unpack(packet)
John@75 194 if state == "bidding" then
John@75 195 state = "looting"
John@75 196 AlertStateChangeListeners()
John@75 197 -- todo: remove the item from the window, record history
John@75 198 end
John@71 199 end
John@71 200
John@72 201 function InitiateCloseBidding(awardedTo)
John@75 202 if state == "bidding" then
John@75 203 Comm:SendStateChange("CB",awardedTo)
John@75 204 end
John@72 205 end
John@72 206 --}}}
John@73 207 -- Close Looting {{{
John@72 208 function CloseLooting(packet)
John@71 209 state = "neutral"
John@73 210 stateactive = nil
John@73 211 stateitem = nil
John@73 212 stateitemlist = {}
John@73 213 statebids = {}
John@73 214 staterolls = {}
John@73 215 AlertStateChangeListeners()
John@73 216 AlertItemListListeners()
John@71 217 end
John@72 218
John@72 219 function InitiateCloseLooting()
John@72 220 Comm:SendStateChange("CL")
John@72 221 end
John@73 222 --}}}
John@72 223 function DispatchState(packet)
John@72 224 local state = table.remove(packet,1)
John@72 225 print("Dispatching", state)
John@72 226 if state == "RB" then
John@72 227 ReceivedBid(packet)
John@73 228 elseif state == "BL" then
John@73 229 BeginLoot(packet)
John@72 230 elseif state == "RR" then
John@72 231 ReceivedRetraction(packet)
John@72 232 elseif state == "OB" then
John@72 233 OpenBid(packet)
John@72 234 elseif state == "CB" then
John@72 235 CloseBidding(packet)
John@73 236 elseif state == "CL" then
John@72 237 CloseLooting(packet)
John@72 238 elseif state == "AL" then
John@72 239 ActivateList(packet)
John@72 240 else -- todo ...
John@72 241
John@72 242 end
John@72 243 end
John@72 244
John@73 245 function InitializeState()
John@73 246 local ltemp = 0
John@73 247 local lids = LootLists:GetAllIds()
John@73 248 for _,v in pairs(lids) do
John@73 249 local l = LootLists:Select(v)
John@73 250 if l:GetLength() > 0 then
John@73 251 if ltemp == 0 then
John@73 252 ltemp = l:GetId()
John@73 253 end
John@73 254 end
John@73 255 end
John@73 256 stateactivelist = ltemp
John@73 257 end
John@73 258