Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # rock paper scissors game plugin for Skybot
- import re
- from random import choice
- from util import hook
- def win(db, nick):
- db.execute("""UPDATE rockpaperscissors SET
- wins = wins+1,
- total = total+1 WHERE nick=nick""")
- db.commit()
- def loss(db, nick):
- db.execute("""UPDATE rockpaperscissors SET
- losses = losses+1,
- total = total+1 WHERE nick=nick""")
- db.commit()
- def tie(db, nick):
- db.execute("""UPDATE rockpaperscissors SET
- ties = ties+1,
- total = total+1 WHERE nick=nick""")
- db.commit()
- def get_stats(db, nick):
- return db.execute("""SELECT * FROM rockpaperscissors WHERE nick=nick""").fetchall()
- @hook.command('rps')
- @hook.command()
- def rockpaperscissors(inp, nick='', db=None):
- """.rps/.rockpaperscissors <hand>/<stats> -- plays rock-paper-scissors with you or returns stats for all plays"""
- print nick
- stats = re.match('stats', inp)
- if stats:
- out = get_stats(db, nick)
- if not out:
- return "no plays"
- else:
- return "you've won %s times, lost %s times, tied %s times and" \
- " played a total of %s times" % (out[0][1:])
- db.execute("""CREATE TABLE if not exists rockpaperscissors(
- nick TEXT PRIMARY KEY,
- wins INTEGER,
- losses INTEGER,
- ties INTEGER,
- total INTEGER)""")
- hands = ['rock', 'paper', 'scissors']
- hand_beats = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
- if inp and inp.lower() in hands:
- db.execute("""INSERT or IGNORE INTO rockpaperscissors(
- nick,
- wins,
- losses,
- ties,
- total) values(?,?,?,?,?)""", (nick.lower(),0,0,0,0))
- player_hand = inp.lower()
- bot_hand = choice(hands)
- if player_hand == bot_hand:
- tie(db, nick)
- return "%s - tie" % bot_hand
- if hand_beats[player_hand] == bot_hand:
- win(db, nick)
- return "%s - you win" % bot_hand
- else:
- loss(db, nick)
- return "%s - you lose" % bot_hand
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement