annotate State.lua @ 72:9e5b0a2368ad

Big progress towards GUI usefulness and communication between clients
author John@Yosemite-PC
date Sat, 07 Apr 2012 13:35:57 -0400
parents d5e2dfe0c269
children 7eb2963eea7d
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@71 44 statebids = {}
John@71 45 staterolls = {}
John@72 46 stateactivelist = nil
John@71 47
John@71 48 local rollListeners = {}
John@71 49 function RegisterListenerRolls(listener)
John@71 50 table.insert(rollListeners,listener)
John@71 51 end
John@71 52 function AlertRollListeners()
John@71 53 for i,v in pairs(rollListeners) do
John@71 54 print("roll out")
John@71 55 v["RollEvent"](v)
John@71 56 end
John@71 57 end
John@71 58
John@72 59 local listChangeListeners = {}
John@72 60 function RegisterListenerActiveListChanged(listener)
John@72 61 table.insert(listChangeListeners,listener)
John@72 62 end
John@72 63 function AlertActiveListChangedListeners()
John@72 64 for i,v in pairs(listChangeListeners) do
John@72 65 print("list out")
John@72 66 v["ActiveListEvent"](v)
John@72 67 end
John@72 68 end
John@72 69
John@72 70 local stateChangeListeners = {}
John@72 71 function RegisterListenerStateChange(listener)
John@72 72 table.insert(stateChangeListeners,listener)
John@72 73 end
John@72 74 function AlertStateChangeListeners()
John@72 75 for i,v in pairs(stateChangeListeners) do
John@72 76 print("state out")
John@72 77 v["StateEvent"](v)
John@72 78 end
John@72 79 end
John@72 80
John@71 81 function BeginLoot(listValue)
John@71 82 if state == "neutral" then
John@71 83 state = "looting"
John@71 84 looting = true
John@71 85 active = listValue
John@71 86 else
John@71 87 _G.error("Bad state transition", state, "looting")
John@71 88 end
John@71 89 end
John@71 90
John@72 91
John@72 92 -- Activate List {{{
John@72 93 function ActivateList(packet) -- doesn't cause a transition, but we only enforce a list selection during certain times
John@72 94 local list = unpack(packet)
John@71 95 if state == "looting" then
John@72 96 stateactivelist = list
John@72 97 AlertActiveListChangedListeners()
John@71 98 end
John@71 99 end
John@71 100
John@72 101 function InitiateActivateList(list)
John@72 102 if state == "looting" then
John@72 103 Comm:SendStateChange("AL",list)
John@72 104 end
John@72 105 end
John@72 106 --}}}
John@72 107 -- Open Bidding {{{
John@71 108 function OpenBid(packet)
John@71 109 local item = unpack(packet)
John@71 110 --if state == "looting" then
John@71 111 state = "bidding"
John@71 112 item = value
John@71 113 --end
John@71 114 end
John@71 115
John@71 116 function InitiateOpenBid(item)
John@71 117 --if state == "looting" then
John@71 118 Comm:SendStateChange("OB",item)
John@71 119 --end
John@71 120 end
John@72 121 --}}}
John@72 122 -- Bid {{{
John@71 123 function ReceivedBid(packet) -- no state transition, but only matters during one state
John@71 124 local person, roll = unpack(packet)
John@71 125
John@71 126 if state == "bidding" then
John@71 127 if roll then
John@71 128 table.insert(statebids,person) -- todo:
John@71 129 else
John@71 130 table.insert(statebids,person) -- todo: keep sorted
John@71 131 end
John@71 132 AlertRollListeners()
John@71 133 end
John@71 134
John@71 135 -- else ignore ...
John@71 136 end
John@71 137
John@71 138 function InitiateBid(person,roll)
John@71 139 if state == "bidding" then
John@71 140 for i,v in pairs(statebids) do
John@71 141 if person.value == v.value then
John@71 142 print(person.value .. " is already on the list")
John@71 143 return -- no double adds please
John@71 144 end
John@71 145 end
John@71 146 Comm:SendStateChange("RB",person,roll)
John@71 147 end
John@71 148 end
John@72 149 --}}}
John@72 150 -- Retration {{{
John@71 151 function ReceivedRetraction(packet)
John@71 152 local person = unpack(packet)
John@71 153 if state == "bidding" then
John@71 154 for i,v in pairs(statebids) do
John@71 155 if v.value == person.value then
John@71 156 table.remove(statebids,i)
John@71 157 AlertRollListeners()
John@71 158 return
John@71 159 end
John@71 160 end
John@71 161 end
John@71 162 end
John@71 163
John@71 164 function InitiateRetract(person)
John@71 165 if state == "bidding" then
John@71 166 Comm:SendStateChange("RR",person,roll)
John@71 167 end
John@71 168 end
John@72 169 --}}}
John@72 170 -- Close Bidding {{{
John@72 171 function CloseBidding(packet)
John@72 172 local awardedTo = unpack(packet)
John@71 173 state = "looting"
John@72 174 -- remove the item from the window, record history
John@71 175 end
John@71 176
John@72 177 function InitiateCloseBidding(awardedTo)
John@72 178 Comm:SendStateChange("CB",awardedTo)
John@72 179 end
John@72 180 --}}}
John@72 181
John@72 182 function CloseLooting(packet)
John@71 183 state = "neutral"
John@71 184 active = nil
John@71 185 item = nil
John@71 186 bids = {}
John@71 187 rolls = {}
John@71 188 end
John@72 189
John@72 190 function InitiateCloseLooting()
John@72 191 Comm:SendStateChange("CL")
John@72 192 end
John@72 193
John@72 194 function DispatchState(packet)
John@72 195 local state = table.remove(packet,1)
John@72 196 print("Dispatching", state)
John@72 197 if state == "RB" then
John@72 198 ReceivedBid(packet)
John@72 199 elseif state == "RR" then
John@72 200 ReceivedRetraction(packet)
John@72 201 elseif state == "OB" then
John@72 202 OpenBid(packet)
John@72 203 elseif state == "CB" then
John@72 204 CloseBidding(packet)
John@72 205 elseif state == "CB" then
John@72 206 CloseLooting(packet)
John@72 207 elseif state == "AL" then
John@72 208 ActivateList(packet)
John@72 209 else -- todo ...
John@72 210
John@72 211 end
John@72 212 end
John@72 213