diff 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
line wrap: on
line diff
--- a/State.lua	Tue Apr 03 23:23:22 2012 -0400
+++ b/State.lua	Sat Apr 07 13:35:57 2012 -0400
@@ -37,12 +37,13 @@
 -- person B broadcasts their bid. if a bid, everyone just accepts it.
 --              - if a roll, then the master does the roll and rebroadcasts
 
-state = "neutral"
+state = "neutral" -- states that are possible: neutral, looting, bidding
 local looting = false
 stateactive = nil
 stateitem = nil
 statebids = {}
 staterolls = {}
+stateactivelist = nil
 
 local rollListeners = {}
 function RegisterListenerRolls(listener)
@@ -55,6 +56,28 @@
     end
 end
 
+local listChangeListeners = {}
+function RegisterListenerActiveListChanged(listener)
+    table.insert(listChangeListeners,listener)
+end
+function AlertActiveListChangedListeners()
+    for i,v in pairs(listChangeListeners) do
+        print("list out")
+        v["ActiveListEvent"](v)
+    end
+end
+
+local stateChangeListeners = {}
+function RegisterListenerStateChange(listener)
+    table.insert(stateChangeListeners,listener)
+end
+function AlertStateChangeListeners()
+    for i,v in pairs(stateChangeListeners) do
+        print("state out")
+        v["StateEvent"](v)
+    end
+end
+
 function BeginLoot(listValue)
     if state == "neutral" then
         state = "looting"
@@ -65,12 +88,23 @@
     end
 end
 
-function ActivateList(value) -- doesn't cause a transition, but we only enforce a list selection during certain times
+
+-- Activate List {{{
+function ActivateList(packet) -- doesn't cause a transition, but we only enforce a list selection during certain times
+    local list = unpack(packet)
     if state == "looting" then
-        active = value
+        stateactivelist = list
+        AlertActiveListChangedListeners()
     end
 end
 
+function InitiateActivateList(list)
+    if state == "looting" then
+        Comm:SendStateChange("AL",list)
+    end
+end
+--}}}
+-- Open Bidding {{{
 function OpenBid(packet)
     local item = unpack(packet)
     --if state == "looting" then
@@ -84,21 +118,8 @@
     Comm:SendStateChange("OB",item)
     --end
 end
-
-function DispatchState(packet)
-    local state = table.remove(packet,1)
-    print("Dispatching", state)
-    if state == "RB" then
-        ReceivedBid(packet)
-    elseif state == "RR" then
-        ReceivedRetraction(packet)
-    elseif state == "OB" then
-        OpenBid(packet)
-    else -- todo ...
-
-    end
-end
-
+--}}}
+-- Bid {{{
 function ReceivedBid(packet) -- no state transition, but only matters during one state
     local person, roll = unpack(packet)
 
@@ -125,8 +146,8 @@
         Comm:SendStateChange("RB",person,roll)
     end
 end
-
-
+--}}}
+-- Retration {{{
 function ReceivedRetraction(packet)
     local person = unpack(packet)
     if state == "bidding" then
@@ -145,16 +166,48 @@
         Comm:SendStateChange("RR",person,roll)
     end
 end
-
-function CloseBidding(awardedTo)
+--}}}
+-- Close Bidding {{{
+function CloseBidding(packet)
+    local awardedTo = unpack(packet) 
     state = "looting"
-    -- remove the item, record history
+    -- remove the item from the window, record history
 end
 
-function CloseLooting()
+function InitiateCloseBidding(awardedTo)
+    Comm:SendStateChange("CB",awardedTo)
+end
+--}}}
+
+function CloseLooting(packet)
     state = "neutral"
     active = nil
     item = nil
     bids = {}
     rolls = {}
 end
+
+function InitiateCloseLooting()
+    Comm:SendStateChange("CL")
+end
+
+function DispatchState(packet)
+    local state = table.remove(packet,1)
+    print("Dispatching", state)
+    if state == "RB" then
+        ReceivedBid(packet)
+    elseif state == "RR" then
+        ReceivedRetraction(packet)
+    elseif state == "OB" then
+        OpenBid(packet)
+    elseif state == "CB" then
+        CloseBidding(packet)
+    elseif state == "CB" then
+        CloseLooting(packet)
+    elseif state == "AL" then
+        ActivateList(packet)
+    else -- todo ...
+
+    end
+end
+