Advertisement
Atheuz

scaevolus rps

Jun 16th, 2011 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. # rock paper scissors game plugin for Skybot
  2.  
  3. import re
  4. from random import choice
  5.  
  6. from util import hook
  7.  
  8. def win(db, chan, nick):
  9. db.execute("""INSERT or IGNORE INTO rockpaperscissors(
  10. nick,
  11. chan,
  12. wins,
  13. losses,
  14. ties,
  15. total) values(?,?,?,?,?,?)
  16. """, (nick.lower(),chan.lower(),0,0,0,0))
  17. db.execute("""UPDATE rockpaperscissors SET
  18. wins = wins+1,
  19. total = total+1
  20. WHERE nick=? AND chan=?""", (nick.lower(), chan.lower()))
  21. db.commit()
  22.  
  23. def loss(db, chan, nick):
  24. db.execute("""INSERT or IGNORE INTO rockpaperscissors(
  25. nick,
  26. chan,
  27. wins,
  28. losses,
  29. ties,
  30. total) values(?,?,?,?,?,?)
  31. """, (nick.lower(),chan.lower(),0,0,0,0))
  32. db.execute("""UPDATE rockpaperscissors SET
  33. losses = losses+1,
  34. total = total+1
  35. WHERE nick=? AND chan=?""", (nick.lower(), chan.lower()))
  36. db.commit()
  37.  
  38. def tie(db, chan, nick):
  39. db.execute("""INSERT or IGNORE INTO rockpaperscissors(
  40. nick,
  41. chan,
  42. wins,
  43. losses,
  44. ties,
  45. total) values(?,?,?,?,?,?)
  46. """, (nick.lower(),chan.lower(),0,0,0,0))
  47. db.execute("""UPDATE rockpaperscissors SET
  48. ties = ties+1,
  49. total = total+1
  50. WHERE nick=? AND chan=?""", (nick.lower(), chan.lower()))
  51. db.commit()
  52.  
  53. def get_stats(db, chan, nick):
  54. return db.execute("""SELECT * FROM rockpaperscissors WHERE nick=? AND chan=?""", ( nick.lower(), chan.lower() ) ).fetchall()
  55.  
  56. @hook.command('rps')
  57. @hook.command()
  58. def rockpaperscissors(inp, nick='', chan='', db=None):
  59. """.rps/.rockpaperscissors <hand>/<stats> -- plays rock-paper-scissors with you or returns stats for all plays"""
  60.  
  61. stats = re.match('stats', inp)
  62. if stats:
  63. out = get_stats(db, chan, nick)
  64.  
  65. if not out:
  66. return "no plays"
  67. else:
  68. return "you've won %s times, lost %s times, tied %s times and" \
  69. " played a total of %s times" % (out[0][2:])
  70.  
  71. hands = ['rock', 'paper', 'scissors']
  72.  
  73. db.execute("""CREATE TABLE if not exists rockpaperscissors(
  74. nick TEXT PRIMARY KEY,
  75. chan TEXT,
  76. wins INTEGER,
  77. losses INTEGER,
  78. ties INTEGER,
  79. total INTEGER)
  80. """)
  81.  
  82. if inp and inp.lower() in hands:
  83. player_hand = inp.lower()
  84. bot_hand = choice(hands)
  85.  
  86. if player_hand == bot_hand:
  87. tie(db, chan, nick)
  88. return "%s - tie" % bot_hand
  89. if player_hand == 'rock':
  90. if bot_hand == 'paper':
  91. loss(db, chan, nick)
  92. return "%s - you lose" % bot_hand
  93. if bot_hand == 'scissors':
  94. win(db, chan, nick)
  95. return "%s - you win" % bot_hand
  96. if player_hand == 'paper':
  97. if bot_hand == 'scissors':
  98. loss(db, chan, nick)
  99. return "%s - you lose" % bot_hand
  100. if bot_hand == 'rock':
  101. win(db, chan, nick)
  102. return "%s - you win" % bot_hand
  103. if player_hand == 'scissors':
  104. if bot_hand == 'rock':
  105. loss(db, chan, nick)
  106. return "%s - you lose" % bot_hand
  107. if bot_hand == 'paper':
  108. win(db, chan, nick)
  109. return "%s - you win" % bot_hand
  110. else:
  111. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement