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@71
|
40 state = "neutral"
|
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@71
|
46
|
John@71
|
47 local rollListeners = {}
|
John@71
|
48 function RegisterListenerRolls(listener)
|
John@71
|
49 table.insert(rollListeners,listener)
|
John@71
|
50 end
|
John@71
|
51 function AlertRollListeners()
|
John@71
|
52 for i,v in pairs(rollListeners) do
|
John@71
|
53 print("roll out")
|
John@71
|
54 v["RollEvent"](v)
|
John@71
|
55 end
|
John@71
|
56 end
|
John@71
|
57
|
John@71
|
58 function BeginLoot(listValue)
|
John@71
|
59 if state == "neutral" then
|
John@71
|
60 state = "looting"
|
John@71
|
61 looting = true
|
John@71
|
62 active = listValue
|
John@71
|
63 else
|
John@71
|
64 _G.error("Bad state transition", state, "looting")
|
John@71
|
65 end
|
John@71
|
66 end
|
John@71
|
67
|
John@71
|
68 function ActivateList(value) -- doesn't cause a transition, but we only enforce a list selection during certain times
|
John@71
|
69 if state == "looting" then
|
John@71
|
70 active = value
|
John@71
|
71 end
|
John@71
|
72 end
|
John@71
|
73
|
John@71
|
74 function OpenBid(packet)
|
John@71
|
75 local item = unpack(packet)
|
John@71
|
76 --if state == "looting" then
|
John@71
|
77 state = "bidding"
|
John@71
|
78 item = value
|
John@71
|
79 --end
|
John@71
|
80 end
|
John@71
|
81
|
John@71
|
82 function InitiateOpenBid(item)
|
John@71
|
83 --if state == "looting" then
|
John@71
|
84 Comm:SendStateChange("OB",item)
|
John@71
|
85 --end
|
John@71
|
86 end
|
John@71
|
87
|
John@71
|
88 function DispatchState(packet)
|
John@71
|
89 local state = table.remove(packet,1)
|
John@71
|
90 print("Dispatching", state)
|
John@71
|
91 if state == "RB" then
|
John@71
|
92 ReceivedBid(packet)
|
John@71
|
93 elseif state == "RR" then
|
John@71
|
94 ReceivedRetraction(packet)
|
John@71
|
95 elseif state == "OB" then
|
John@71
|
96 OpenBid(packet)
|
John@71
|
97 else -- todo ...
|
John@71
|
98
|
John@71
|
99 end
|
John@71
|
100 end
|
John@71
|
101
|
John@71
|
102 function ReceivedBid(packet) -- no state transition, but only matters during one state
|
John@71
|
103 local person, roll = unpack(packet)
|
John@71
|
104
|
John@71
|
105 if state == "bidding" then
|
John@71
|
106 if roll then
|
John@71
|
107 table.insert(statebids,person) -- todo:
|
John@71
|
108 else
|
John@71
|
109 table.insert(statebids,person) -- todo: keep sorted
|
John@71
|
110 end
|
John@71
|
111 AlertRollListeners()
|
John@71
|
112 end
|
John@71
|
113
|
John@71
|
114 -- else ignore ...
|
John@71
|
115 end
|
John@71
|
116
|
John@71
|
117 function InitiateBid(person,roll)
|
John@71
|
118 if state == "bidding" then
|
John@71
|
119 for i,v in pairs(statebids) do
|
John@71
|
120 if person.value == v.value then
|
John@71
|
121 print(person.value .. " is already on the list")
|
John@71
|
122 return -- no double adds please
|
John@71
|
123 end
|
John@71
|
124 end
|
John@71
|
125 Comm:SendStateChange("RB",person,roll)
|
John@71
|
126 end
|
John@71
|
127 end
|
John@71
|
128
|
John@71
|
129
|
John@71
|
130 function ReceivedRetraction(packet)
|
John@71
|
131 local person = unpack(packet)
|
John@71
|
132 if state == "bidding" then
|
John@71
|
133 for i,v in pairs(statebids) do
|
John@71
|
134 if v.value == person.value then
|
John@71
|
135 table.remove(statebids,i)
|
John@71
|
136 AlertRollListeners()
|
John@71
|
137 return
|
John@71
|
138 end
|
John@71
|
139 end
|
John@71
|
140 end
|
John@71
|
141 end
|
John@71
|
142
|
John@71
|
143 function InitiateRetract(person)
|
John@71
|
144 if state == "bidding" then
|
John@71
|
145 Comm:SendStateChange("RR",person,roll)
|
John@71
|
146 end
|
John@71
|
147 end
|
John@71
|
148
|
John@71
|
149 function CloseBidding(awardedTo)
|
John@71
|
150 state = "looting"
|
John@71
|
151 -- remove the item, record history
|
John@71
|
152 end
|
John@71
|
153
|
John@71
|
154 function CloseLooting()
|
John@71
|
155 state = "neutral"
|
John@71
|
156 active = nil
|
John@71
|
157 item = nil
|
John@71
|
158 bids = {}
|
John@71
|
159 rolls = {}
|
John@71
|
160 end
|