John@71: local bsk=bsk John@71: local _G=_G John@71: local table=table John@71: local pairs=pairs John@71: local setmetatable=setmetatable John@71: local ipairs=ipairs John@71: local string=string John@71: local sformat=string.format John@71: local tostring=tostring John@71: local type=type John@71: local unpack=unpack John@71: local getn=getn John@71: setfenv(1,bsk) John@71: John@71: -- simple state machine John@71: John@71: --Begin loot John@71: --Activate list ... only during looting? John@71: --open bidding/rolling John@71: --bid/roll occurred John@71: --remove bid/roll John@71: --close bidding John@71: --remove item John@71: John@71: John@71: -- we'll track state, but that may or may not result in a GUI change until down John@71: -- the road John@71: John@71: -- todo: transmit this all to only the raid, not the guild? John@71: John@71: -- sample procedure John@71: -- person B opens GUI. John@71: -- person A begins looting, sets a list John@71: -- person A begins bidding, transmists the state John@71: -- person B goes into bidding state, their button activates John@71: -- person B clicks the button. button changes state. John@71: -- person B broadcasts their bid. if a bid, everyone just accepts it. John@71: -- - if a roll, then the master does the roll and rebroadcasts John@71: John@72: state = "neutral" -- states that are possible: neutral, looting, bidding John@71: local looting = false John@71: stateactive = nil John@71: stateitem = nil John@71: statebids = {} John@71: staterolls = {} John@72: stateactivelist = nil John@71: John@71: local rollListeners = {} John@71: function RegisterListenerRolls(listener) John@71: table.insert(rollListeners,listener) John@71: end John@71: function AlertRollListeners() John@71: for i,v in pairs(rollListeners) do John@71: print("roll out") John@71: v["RollEvent"](v) John@71: end John@71: end John@71: John@72: local listChangeListeners = {} John@72: function RegisterListenerActiveListChanged(listener) John@72: table.insert(listChangeListeners,listener) John@72: end John@72: function AlertActiveListChangedListeners() John@72: for i,v in pairs(listChangeListeners) do John@72: print("list out") John@72: v["ActiveListEvent"](v) John@72: end John@72: end John@72: John@72: local stateChangeListeners = {} John@72: function RegisterListenerStateChange(listener) John@72: table.insert(stateChangeListeners,listener) John@72: end John@72: function AlertStateChangeListeners() John@72: for i,v in pairs(stateChangeListeners) do John@72: print("state out") John@72: v["StateEvent"](v) John@72: end John@72: end John@72: John@71: function BeginLoot(listValue) John@71: if state == "neutral" then John@71: state = "looting" John@71: looting = true John@71: active = listValue John@71: else John@71: _G.error("Bad state transition", state, "looting") John@71: end John@71: end John@71: John@72: John@72: -- Activate List {{{ John@72: function ActivateList(packet) -- doesn't cause a transition, but we only enforce a list selection during certain times John@72: local list = unpack(packet) John@71: if state == "looting" then John@72: stateactivelist = list John@72: AlertActiveListChangedListeners() John@71: end John@71: end John@71: John@72: function InitiateActivateList(list) John@72: if state == "looting" then John@72: Comm:SendStateChange("AL",list) John@72: end John@72: end John@72: --}}} John@72: -- Open Bidding {{{ John@71: function OpenBid(packet) John@71: local item = unpack(packet) John@71: --if state == "looting" then John@71: state = "bidding" John@71: item = value John@71: --end John@71: end John@71: John@71: function InitiateOpenBid(item) John@71: --if state == "looting" then John@71: Comm:SendStateChange("OB",item) John@71: --end John@71: end John@72: --}}} John@72: -- Bid {{{ John@71: function ReceivedBid(packet) -- no state transition, but only matters during one state John@71: local person, roll = unpack(packet) John@71: John@71: if state == "bidding" then John@71: if roll then John@71: table.insert(statebids,person) -- todo: John@71: else John@71: table.insert(statebids,person) -- todo: keep sorted John@71: end John@71: AlertRollListeners() John@71: end John@71: John@71: -- else ignore ... John@71: end John@71: John@71: function InitiateBid(person,roll) John@71: if state == "bidding" then John@71: for i,v in pairs(statebids) do John@71: if person.value == v.value then John@71: print(person.value .. " is already on the list") John@71: return -- no double adds please John@71: end John@71: end John@71: Comm:SendStateChange("RB",person,roll) John@71: end John@71: end John@72: --}}} John@72: -- Retration {{{ John@71: function ReceivedRetraction(packet) John@71: local person = unpack(packet) John@71: if state == "bidding" then John@71: for i,v in pairs(statebids) do John@71: if v.value == person.value then John@71: table.remove(statebids,i) John@71: AlertRollListeners() John@71: return John@71: end John@71: end John@71: end John@71: end John@71: John@71: function InitiateRetract(person) John@71: if state == "bidding" then John@71: Comm:SendStateChange("RR",person,roll) John@71: end John@71: end John@72: --}}} John@72: -- Close Bidding {{{ John@72: function CloseBidding(packet) John@72: local awardedTo = unpack(packet) John@71: state = "looting" John@72: -- remove the item from the window, record history John@71: end John@71: John@72: function InitiateCloseBidding(awardedTo) John@72: Comm:SendStateChange("CB",awardedTo) John@72: end John@72: --}}} John@72: John@72: function CloseLooting(packet) John@71: state = "neutral" John@71: active = nil John@71: item = nil John@71: bids = {} John@71: rolls = {} John@71: end John@72: John@72: function InitiateCloseLooting() John@72: Comm:SendStateChange("CL") John@72: end John@72: John@72: function DispatchState(packet) John@72: local state = table.remove(packet,1) John@72: print("Dispatching", state) John@72: if state == "RB" then John@72: ReceivedBid(packet) John@72: elseif state == "RR" then John@72: ReceivedRetraction(packet) John@72: elseif state == "OB" then John@72: OpenBid(packet) John@72: elseif state == "CB" then John@72: CloseBidding(packet) John@72: elseif state == "CB" then John@72: CloseLooting(packet) John@72: elseif state == "AL" then John@72: ActivateList(packet) John@72: else -- todo ... John@72: John@72: end John@72: end John@72: