annotate State.lua @ 78:5b507f4125d4

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