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