A fork of https://github.com/Synerty/SOAPpy-py3 This is a working tree till fixes get imported upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

113 lines
2.9 KiB

  1. #!/usr/bin/env python
  2. # Copyright (c) 2001 actzero, inc. All rights reserved.
  3. import string
  4. import sys
  5. sys.path.insert (1, '..')
  6. from SOAPpy import *
  7. ident = '$Id: cardServer.py,v 1.4 2004/02/18 21:22:13 warnes Exp $'
  8. # create the list of all cards, and keep strings for each suit
  9. __cs = "Clubs"
  10. __ds = "Diamonds"
  11. __hs = "Hearts"
  12. __ss = "Spades"
  13. __cards = []
  14. for suit in [__cs, __ds, __hs, __ss]:
  15. for num in range(9):
  16. num += 1
  17. __cards.append(str(num+1)+" of "+suit)
  18. for face in ["ace","King","Queen","Jack"]:
  19. __cards.append(face+" of "+suit)
  20. def deal(num):
  21. if num not in range(1,53):
  22. return -1
  23. else:
  24. alreadydealt = []
  25. ignore = 0
  26. handdealt = []
  27. import whrandom
  28. while num > 0:
  29. idx = int(str(whrandom.random())[2:4])
  30. if idx in range(52) and idx not in alreadydealt:
  31. handdealt.append(__cards[idx])
  32. alreadydealt.append(idx)
  33. num -= 1
  34. else:
  35. ignore += 1
  36. continue
  37. return handdealt
  38. def arrangeHand(hand):
  39. c = []
  40. d = []
  41. h = []
  42. s = []
  43. import string
  44. for card in hand:
  45. if string.find(card, __cs) != -1:
  46. c.append(card)
  47. elif string.find(card, __ds) != -1:
  48. d.append(card)
  49. elif string.find(card, __hs) != -1:
  50. h.append(card)
  51. elif string.find(card, __ss) != -1:
  52. s.append(card)
  53. for cards, str in ((c, __cs),(d, __ds),(h,__hs), (s,__ss)):
  54. cards.sort()
  55. idx = 0
  56. if "10 of "+str in cards:
  57. cards.remove("10 of "+str)
  58. if "Jack of "+str in cards: idx += 1
  59. if "Queen of "+str in cards: idx += 1
  60. if "King of "+str in cards: idx += 1
  61. if "ace of "+str in cards: idx +=1
  62. cards.insert(len(cards)-idx,"10 of "+str)
  63. if "King of "+str in cards:
  64. cards.remove("King of "+str)
  65. if "ace of "+str in cards: cards.insert(len(cards)-1,"King of "+str)
  66. else: cards.append("King of "+str)
  67. return c+d+h+s
  68. def dealHand (NumberOfCards, StringSeparator):
  69. hand = deal(NumberOfCards)
  70. return string.join(hand,StringSeparator)
  71. def dealArrangedHand (NumberOfCards, StringSeparator):
  72. if NumberOfCards < 1 or NumberOfCards > 52:
  73. raise ValueError, "NumberOfCards must be between 1 and 52"
  74. unarranged = deal(NumberOfCards)
  75. hand = arrangeHand(unarranged)
  76. return string.join(hand, StringSeparator)
  77. def dealCard ():
  78. return deal(1)[0]
  79. run = 1
  80. def quit():
  81. global run
  82. run=0;
  83. namespace = 'http://soapinterop.org/'
  84. server = SOAPServer (("localhost", 12027))
  85. server.registerKWFunction (dealHand, namespace)
  86. server.registerKWFunction (dealArrangedHand, namespace)
  87. server.registerKWFunction (dealCard, namespace)
  88. server.registerKWFunction (quit, namespace)
  89. try:
  90. while run:
  91. server.handle_request()
  92. except KeyboardInterrupt:
  93. pass