Advertisement
Atheuz

scaevolus rps update

Jun 16th, 2011 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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, nick):
  9. db.execute("""UPDATE rockpaperscissors SET
  10. wins = wins+1,
  11. total = total+1 WHERE nick=nick""")
  12. db.commit()
  13.  
  14. def loss(db, nick):
  15. db.execute("""UPDATE rockpaperscissors SET
  16. losses = losses+1,
  17. total = total+1 WHERE nick=nick""")
  18. db.commit()
  19.  
  20. def tie(db, nick):
  21. db.execute("""UPDATE rockpaperscissors SET
  22. ties = ties+1,
  23. total = total+1 WHERE nick=nick""")
  24. db.commit()
  25.  
  26. def get_stats(db, nick):
  27. return db.execute("""SELECT * FROM rockpaperscissors WHERE nick=nick""").fetchall()
  28.  
  29. @hook.command('rps')
  30. @hook.command()
  31. def rockpaperscissors(inp, nick='', db=None):
  32. """.rps/.rockpaperscissors <hand>/<stats> -- plays rock-paper-scissors with you or returns stats for all plays"""
  33.  
  34. print nick
  35.  
  36. stats = re.match('stats', inp)
  37. if stats:
  38. out = get_stats(db, nick)
  39.  
  40. if not out:
  41. return "no plays"
  42. else:
  43. return "you've won %s times, lost %s times, tied %s times and" \
  44. " played a total of %s times" % (out[0][1:])
  45.  
  46. db.execute("""CREATE TABLE if not exists rockpaperscissors(
  47. nick TEXT PRIMARY KEY,
  48. wins INTEGER,
  49. losses INTEGER,
  50. ties INTEGER,
  51. total INTEGER)""")
  52.  
  53. hands = ['rock', 'paper', 'scissors']
  54. hand_beats = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
  55.  
  56. if inp and inp.lower() in hands:
  57. db.execute("""INSERT or IGNORE INTO rockpaperscissors(
  58. nick,
  59. wins,
  60. losses,
  61. ties,
  62. total) values(?,?,?,?,?)""", (nick.lower(),0,0,0,0))
  63. player_hand = inp.lower()
  64. bot_hand = choice(hands)
  65.  
  66. if player_hand == bot_hand:
  67. tie(db, nick)
  68. return "%s - tie" % bot_hand
  69. if hand_beats[player_hand] == bot_hand:
  70. win(db, nick)
  71. return "%s - you win" % bot_hand
  72. else:
  73. loss(db, nick)
  74. return "%s - you lose" % bot_hand
  75.  
  76. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement