Guest User

Untitled

a guest
Jul 27th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. import urllib2, smtplib
  2.  
  3. def send_email(text):
  4. server = smtplib.SMTP_SSL('smtp.gmail.com')
  5. server.login("bot.nomic@gmail.com", "tankshot")
  6. msg = "\n" + text
  7. server.sendmail("bot.nomic@gmail.com", "benderjacob44@gmail.com", msg)
  8.  
  9. def sendMessageToPlayer(player, message): #Should send message to player
  10. print "*"*50
  11. print "Message for player %s:" %(player)
  12. print message
  13. print "*"*50
  14. def askPlayer(player, question): #Ask player question and return answer
  15. print "-"*50
  16. response=raw_input("%s, %s" %(player,question))
  17. print "-"*50
  18. return response
  19. def askPlayerMultiline(player,question):
  20. print "/"*50
  21. print "%s, %s" %(player,question)
  22. output=""
  23. line=None
  24. while True:
  25. try: line = raw_input("")
  26. except EOFError: break
  27. output += line + "\n"
  28. print "/"*50
  29. return output
  30.  
  31. def initPlayerList(): #Get the initial player list somehow
  32. try:
  33. for i in range(int(raw_input("How many players? "))):
  34. players.append(raw_input("What is the name of player %d? " % (i)))
  35. emails.append(raw_input("What is their email? "))
  36. except(ValueError):
  37. print "Error processing that number of players!"
  38. initPlayerList()
  39.  
  40. #Initial Rule Set goes here
  41.  
  42. def broadCast(message):
  43. for player in players:
  44. sendMessageToPlayer(player, message.format(player=player))
  45.  
  46. Debug = False
  47. try: ruleSet
  48. except NameError: ruleSet = 0
  49. tiebreaker=False #Ties are not broken, but they result in rule proposal failure.
  50. if ruleSet == 0:
  51. players = []
  52. emails = []
  53. initPlayerList()
  54. broadCast("Welcome to a game which is called Nomic. The current rules are simple. Get this program to output '{player} wins!'. However, all rules can be changed, including this one! Good luck!")
  55. #broadCast("The current ruleset is:\n" + open(__file__).read())
  56. ruleUrl = None
  57. oldRuleUrl = None
  58. gameOver=False
  59. while not gameOver:
  60. for player in players:
  61. if ruleUrl:
  62. sendMessageToPlayer(player, "The current ruleset is found at " + ruleUrl)
  63. if askPlayer(player, "Would you like to propose a change to the rules?(y/n) ")[0].lower() == 'y':
  64. oldRuleUrl = ruleUrl
  65. ruleUrl = askPlayer(player, "Please send the url for the modified ruleset: ")
  66. try: rule = urllib2.urlopen(ruleUrl).read()
  67. except Exception, e: sendMessageToPlayer(player, "Error: " + str(e)); ruleUrl = oldRuleUrl; continue
  68. else:
  69. continue
  70.  
  71. votes=[]
  72. for p in players:
  73. send_email(("Player %s has proposed this rule change: " % player) + ruleUrl)
  74. vote=askPlayer(p, "Do you approve %s's rule change? " % (player)) #Vote on rules
  75. votes.append(vote)
  76. total=0.0
  77. for v in votes:
  78. res=v.lower()
  79. if res == "y" or res == "yes" or res== "1":
  80. total+=1.0
  81. elif res == "n" or res == "no" or res == "-1":
  82. total -= 1.0
  83. elif res == "a" or res == "abstain" or res == "0":
  84. total += 0.0
  85. else:
  86. print "Invalid vote detected. Assuming it is a vote to abstain."
  87. rulePassed=False
  88. if not tiebreaker:
  89. if total > 0:
  90. rulePassed=True
  91. else:
  92. if total >= 0:
  93. rulePassed=True
  94.  
  95. if rulePassed:
  96. try:
  97. ruleSet += 1
  98. exec rule
  99. gameOver = True
  100. break
  101. except Exception, e:
  102. broadCast("Ruleset %d has crashed! Reverting to ruleset %d." % (ruleSet, ruleSet-1))
  103. ruleSet -= 1
  104. ruleUrl = oldRuleUrl
  105. else:
  106. ruleUrl = oldRuleUrl
  107. #aaaaa
Add Comment
Please, Sign In to add comment