changeset 102:49d36bd742b0

A poker dealer.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Thu, 09 Jul 2009 17:15:33 +0200
parents 3e0aadd17eb7
children 7f816d773350
files examples/poker.py
diffstat 1 files changed, 144 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/poker.py	Thu Jul 09 17:15:33 2009 +0200
@@ -0,0 +1,144 @@
+
+import sys
+from optparse import OptionParser
+
+
+import viff.reactor
+viff.reactor.install()
+from twisted.internet import reactor
+from twisted.internet.defer import Deferred, DeferredList, gatherResults
+
+from viff.field import GF
+from viff.runtime import Share, create_runtime, gather_shares
+from viff.config import load_config
+from viff.util import dprint, find_prime, rand
+from viff.comparison import Toft07Runtime
+
+from secretset import SecretSet
+
+# Load the configuration from the player configuration files.
+id, players = load_config(sys.argv[1])
+
+# Initialize the field we do arithmetic over.
+Zp = GF(53)
+
+print "I am player %d" % id
+
+
+class Dealer(object):
+    
+    def __init__(self, runtime, random, one):
+        self.runtime = runtime
+        self.random = random
+        self.one = one
+        self.dealt_cards = SecretSet(random, one)
+
+    def next_card(self):
+#        print "n1"
+        def choose_random_card(x, r):
+#            print "n3"
+            if x:
+#                print "n4"
+                self.dealt_cards.add(r)
+                return r,
+            else:
+#                print "n5", self.random
+                r = self.random()
+#                print "n6"
+                contains = self.dealt_cards.contains(r)
+#                print "n7"
+                v = self.runtime.output(contains)
+#                print "n8"
+                v.addCallback(choose_random_card, r)
+#                print "n9"
+                v.addErrback(errorHandler)
+#                print "n10"
+                return v
+#        print "n2"
+        return choose_random_card(Zp(0), None)
+
+    def get_list_of_cards(self, n):
+#        print "ls", n
+        if 0 >= n:
+#            print "foobar"
+            return []
+#        print "ls2"
+        def f(card, n, acc):
+#            print "f:", card, acc, n
+            acc.append(card[0])
+            if 0 == n:
+#                print "f: done"            
+                return acc
+            else:
+#                print "f: cont."
+                card = self.next_card()
+                card.addCallback(f, n - 1, acc)
+                return card
+#        print "ls3"
+        card = self.next_card()
+#        print "ls4"
+        self.runtime.schedule_callback(card, f, n - 1, [])
+#        print "ls5"
+        return card
+                
+            
+def get_one(runtime):
+    if 1 == runtime.id:
+        one = runtime.input([1], Zp, 1)
+    else:
+        one = runtime.input([1], Zp, None)
+    return one
+
+
+def playPoker(runtime):
+    num_cards = 4
+
+    def random():
+        r = runtime.prss_share_random(Zp)
+        return r
+
+    one = get_one(runtime)
+    dealer = Dealer(runtime, random, one)
+
+    cards = dealer.get_list_of_cards(num_cards*runtime.num_players)
+
+    def deal_cards(cards):
+        my_cards = []
+        for player in runtime.players.keys():
+            for card in cards[(player-1)*num_cards:(player*num_cards)]:
+                c = runtime.output(card, [player])
+                if c is not None:
+                    my_cards.append(c)
+
+        def print_cards(cards):
+            print "You have been dealt the following cards:"
+            for card in cards:
+                print "Card nr.", card.value
+            return None
+        dls = gatherResults(my_cards)
+        dls.addCallbacks(print_cards, errorHandler)
+        return dls
+
+    runtime.schedule_callback(cards, deal_cards)
+    runtime.schedule_callback(cards, lambda _: runtime.synchronize())
+    runtime.schedule_callback(cards, lambda _: runtime.shutdown())
+
+def errorHandler(failure):
+    print "Error: %s" % failure
+
+
+# Parse command line arguments.
+parser = OptionParser()
+Toft07Runtime.add_options(parser)
+options, args = parser.parse_args()
+
+if len(args) == 0:
+    parser.error("you must specify a config file")
+else:
+    id, players = load_config(args[0])
+
+# Create a deferred Runtime and ask it to run our protocol when ready.
+pre_runtime = create_runtime(id, players, 1, options, Toft07Runtime)
+pre_runtime.addCallbacks(playPoker, errorHandler)
+# Start the Twisted event loop.
+reactor.run()