diff State.lua @ 79:7b8fcea357d2

Rolls are active
author John@Yosemite-PC
date Thu, 12 Apr 2012 23:50:56 -0400
parents 5b507f4125d4
children 40c882db34f8
line wrap: on
line diff
--- a/State.lua	Thu Apr 12 22:19:15 2012 -0400
+++ b/State.lua	Thu Apr 12 23:50:56 2012 -0400
@@ -164,16 +164,39 @@
     end
     return t
 end
+local function SortByRoll(values,unordered)
+    local t = {}
+    for r = 100,1,-1 do -- 100:1
+        if values[r] then
+            for i,v in pairs(unordered) do
+                if v.value == values[r].value then
+                    print("Sort time: insert", v.value, r)
+                    table.insert(t,v)
+                    table.remove(unordered,i)
+                    break
+                end
+            end
+        end
+    end
+    if getn(t) > 0 then
+        for i,v in pairs(unordered) do
+            printf("Disregarding bid/roll from %s because they are not on the list", v.textPlain)
+        end
+    end
+    return t
+end
 function ReceivedBid(packet) -- no state transition, but only matters during one state
     local person, roll = unpack(packet)
 
     if state == "bidding" then
-        local lref = LootLists:Select(stateactivelist)
-
         if roll then
-            table.insert(statebids,person) -- todo: keep sorted
+            print("RB: ",person.value,roll)
+            table.insert(staterolls,person)
+            staterollvalues[roll] = person
+            staterolls = SortByRoll(staterollvalues,staterolls)
         else
-            table.insert(statebids,person) -- todo:
+            local lref = LootLists:Select(stateactivelist)
+            table.insert(statebids,person)
             statebids = SortByList(lref,statebids)
         end
         AlertRollListeners()
@@ -194,6 +217,12 @@
                 return -- no double adds please
             end
         end
+        for i,v in pairs(staterolls) do
+            if person and person.value == v.value then
+                print(person.value .. " is already on the list")
+                return -- no double adds please
+            end
+        end
         Comm:SendStateChange("RB",person,roll)
     end
 end
@@ -209,6 +238,13 @@
                 return
             end
         end
+        for i,v in pairs(staterolls) do
+            if v.value == person.value then
+                table.remove(staterolls,i)
+                AlertRollListeners()
+                return
+            end
+        end
     end
 end
 
@@ -251,6 +287,48 @@
     Comm:SendStateChange("CL")
 end
 --}}}
+function RollRequest(packet)
+    local person = unpack(packet)
+    
+    if state == "bidding" and admin then -- todo: admin should be ML
+        local roll
+        for i,v in pairs(staterollvalues) do
+            if v and v.value == person.value then
+                roll = i
+                break
+            end
+        end
+        if not roll then -- roll isn't on cache
+            repeat -- random until you find a good value
+                roll = _G.random(100)
+            until staterollvalues[roll] == nil
+            print("rolling! ", roll)
+            staterollvalues[roll] = person
+        end
+        InitiateBid(person,roll)
+    end
+end
+function InitiateRollRequest(person)
+    if state == "bidding" then
+        if not person then
+            print("You cannot bid becuase you are not on the list")
+            return
+        end
+        for i,v in pairs(staterolls) do
+            if person and person.value == v.value then
+                print(person.value .. " is already on the list")
+                return -- no double adds please
+            end
+        end
+        for i,v in pairs(statebids) do
+            if person and person.value == v.value then
+                print(person.value .. " is already on the list")
+                return -- no double adds please
+            end
+        end
+        Comm:SendStateChange("IR",person,roll)
+    end
+end
 function DispatchState(packet)
     local state = table.remove(packet,1)
     print("Dispatching", state)
@@ -268,12 +346,18 @@
         CloseLooting(packet)
     elseif state == "AL" then
         ActivateList(packet)
+    elseif state == "IR" then
+        RollRequest(packet)
     else -- todo ...
 
     end
 end
 
 function InitializeState()
+    -- basically, find a value for stateactivelist. it really doesn't matter
+    -- which one, but I decided on trying to choose one that has entries on it
+    -- so the whole thing isn't all empty. stateactivelist being anything
+    -- besides a valid ID could trigger errors
     local ltemp = 0
     local lids = LootLists:GetAllIds()
     for _,v in pairs(lids) do