Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # system imports
- import time, sys, urllib, pickle, random, re, os, shelve, math, httplib
- #from datetime import *
- from twisted.words.protocols import irc
- from twisted.internet import reactor, protocol, task, defer
- global debug
- debug = False
- class Komurin(irc.IRCClient):
- def __init__(self):
- self.nickname = "Assassins"
- self.password = ""
- self.version = "0.2.1"
- self.Owner = 'Crayboff'
- self.trigger = '-'
- self.helpmessage = "Welcome to Word Assassins. The goal of the game is to 'kill' your targets by having/tricking/waiting for them to say a certain common word. After you kill a person, you will obtain their target and must 'kill' the new target as well, until you are the last man standing. Out of over 800 words, only one will kill you. The game ends every 24 hours, so good luck and good killing!"
- self.helpcommands = "Possible commands are: %shelp %sjoin %sstart %squit" % (self.trigger, self.trigger, self.trigger, self.trigger)
- self.resetcycle = 1440
- self.gamedata = {}
- self.isgame = False
- self.playerlist = []
- self.userlists = {}
- self.userlist = []
- self.alreadyassigned = []
- self.deadlist = []
- self.quelist = []
- def resetstats(self):
- self.gameend.cancel()
- self.hourwarning.cancel()
- self.gamedata = {}
- self.isgame = False
- self.playerlist = []
- self.userlists = {}
- self.userlist = []
- self.alreadyassigned = []
- self.deadlist = []
- self.quelist = []
- def connectionMade(self):
- irc.IRCClient.connectionMade(self)
- m = open('assassins_wordlist.txt', 'r')
- self.wordlist = m.read()
- m.close()
- self.wordlist = self.wordlist.split()
- #print self.wordlist
- def connectionLost(self, reason):
- print("LOST CONNECTION! REASON: %s" % reason)
- irc.IRCClient.connectionLost(self, reason)
- def signedOn(self):
- self.join(self.factory.channel)
- self.mode(self.nickname, True, '+B', limit=None, user=self.nickname)
- print('Joined irc.foonetic.net as %s' %(self.nickname))
- def joined(self, channel):
- print('Joined %s' %(channel))
- self.me(channel, 'version '+self.version)
- def noticed(self, user, channel, msg):
- user = user.split('!', 1)[0]
- if user == 'Nickserv':
- self.msg('Nickserv', 'identify '+self.password)
- def privmsg(self, user, channel, msg):
- user = user.split('!', 1)[0]
- msg = msg.strip().lower()
- if msg.startswith(self.trigger) or msg.startswith(self.nickname.lower()+':') or msg.startswith(self.nickname.lower()+','):
- self.gameparser(user, channel, msg)
- elif user in self.gamedata.keys() and self.isgame:
- self.checkusermsg(user, channel, msg)
- def gameparser(self, user, channel, msg):
- if msg.startswith(self.nickname.lower()):
- msg = msg.split(' ',1)[1]
- else:
- msg = msg.strip(self.trigger)
- if msg.startswith('join'):
- self.playerjoin(user)
- elif msg.startswith('start'):
- self.startgame(user)
- elif msg.startswith('stats') or msg.startswith('status'):
- self.userstats(user)
- elif msg.startswith('end'):
- self.endgame(user)
- elif msg.startswith('help'):
- self.help(user)
- elif msg.startswith('quit'):
- self.quitgame(user, channel, msg)
- elif msg.startswith('version'):
- self.notifyuser(user, self.version)
- elif msg.startswith('joinchan') and user == self.Owner:
- self.channeljoiner(user, msg)
- elif msg.startswith('partchan') and user == self.Owner:
- self.channelparter(user, msg)
- def channeljoiner(self, msg):
- chan = msg.split(' ', 1)
- self.notifyuser(user, "Attempting to join channel %s..." % (chan[1]))
- self.join(chan[1])
- def channelparter(self, msg):
- chan = msg.split(' ', 1)
- self.notifyuser(user, "Attempting to part channel %s..." % (chan[1]))
- self.join(chan[1])
- def playerjoin(self, user):
- if self.isgame:
- self.quelist.append(user)
- #print 'quelist:'
- #print self.quelist
- self.notifyuser(user, "There is a game already in session, you will have to wait until the game is over to join again, you will be notified when the game ends.")
- elif user in self.playerlist:
- self.notifyuser(user, 'You are already in this game')
- else:
- self.playerlist.append(user)
- #print self.playerlist
- self.notifyuser(user, "Welcome to the game of Word Assassins. When the game begins, you will recieve a target and a word you must have them say. If your target says the word (and it is detected by me, yes spelling counts) then he will be killed and you will recieve his target and so on. The last player standing wins! Good luck! Use %sstats whenever you want to check your current game status. The game will restart 24 hours after it has begun." % self.trigger)
- def startgame(self, user):
- self.alllist = self.gamedata.keys()
- self.gameend = reactor.callLater(self.resetcycle, self.hours24)
- self.hourwarning = reactor.callLater(self.resetcycle-60, self.hours23)
- #print 'reached startgame'
- try:
- if self.isgame == False:
- if len(self.playerlist) <= 2:
- self.notifyuser(user, "There aren't enough players to begin! You need atleast 3 players")
- return
- else:
- for player in self.playerlist:
- #print player
- target = None
- chooselist = []
- chooselist = filter(lambda n: n not in self.alreadyassigned, self.playerlist)
- #print chooselist
- if player in chooselist:
- chooselist.remove(player)
- #print self.playerlist
- target = random.choice(chooselist)
- self.alreadyassigned.append(target)
- #print player, target
- deathword = random.choice(self.wordlist)
- #print deathword
- self.gamedata[player] = {'target':target, 'wordtodie':deathword}
- for player in self.gamedata.keys():
- targ = self.gamedata[player]['target']
- wordtokill = self.gamedata[targ]['wordtodie']
- self.notifyuser(player, "Your mission is to kill: %s. You must get this person to say: %s." % (targ, wordtokill))
- #print self.gamedata
- self.isgame = True
- else:
- self.notifyuser(user, "There is already a game in progress.")
- except IndexError:
- #print "IndexError, restarting"
- self.gamedata = {}
- self.alreadyassigned = []
- self.startgame(user)
- def quitgame(self, user, channel, msg):
- if user in self.gamedata.keys() and self.isgame:
- self.playerdeath(user, channel, msg)
- elif user in self.playerlist and self.isgame == False:
- self.notifylist("%s has quit!" % (user))
- self.playerlist.remove(user)
- print self.playerlist
- else:
- self.notifyuser(user, "You can't quit a game you aren't in!")
- def checkusermsg(self, user, channel, msg):
- if self.gamedata[user]['wordtodie'] in msg:
- self.playerdeath(user, channel, msg)
- def playerdeath(self, user, channel, msg):
- newtarget = self.gamedata[user]['target']
- newdeathword = self.gamedata[newtarget]['wordtodie']
- for player in self.gamedata:
- if self.gamedata[player]['target'] == user:
- self.notifyuser(player, "%s has said %s! You have taken on %s's target. \x02NEW TARGET:\x02 %s \x02WORD TO KILL:\x02 %s" % (user, self.gamedata[user]['wordtodie'], user, newtarget, newdeathword))
- self.notifyuser(user, "You have said %s! That was the word your killer was trying to get you to say. Your killer has now taken your target to kill. Better luck next game." % self.gamedata[user]['wordtodie'])
- self.gamedata[player]['target'] = newtarget
- #print self.gamedata
- del self.gamedata[user]
- self.deadlist.append(user)
- #print self.deadlist
- self.notifylist("%s has just died, his/her mysterious killer roams free." % (user))
- if len(self.gamedata.keys()) == 1:
- #print self.playerlist
- #print 'A winner has been decided'
- winner = self.gamedata.keys()[0]
- for person in self.quelist:
- self.notifyuser(person, "The game of Assassins has ended, you may join now.")
- self.notifylist("The Assassin game is OVER. Congratulations the new winner: %s" % winner)
- self.resetstats()
- #self.notifyuser(winner, 'Congratulations, you are the winner of Word Assassin!')
- def userstats(self, user):
- #print 'userstats'
- if user in self.gamedata.keys() and self.isgame:
- target = self.gamedata[user]['target']
- wordtokill = self.gamedata[target]['wordtodie']
- self.notifyuser(user, "Your mission is to kill: %s. You must get this person to say: %s." % (target, wordtokill))
- elif self.isgame == False:
- self.notifyuser(user, "There is no game currently in play to get your information from!")
- elif user not in self.gamedata.keys():
- self.notifyuser(user, "You are not currently playing!")
- def endgame(self, user):
- if user == self.Owner:
- #print "owner is ending"
- #print self.playerlist
- self.notifylist("Game has been ended by owner.")
- for person in self.quelist:
- self.notifyuser(person, "The game of Assassins has ended, you may join now.")
- self.resetstats()
- else:
- self.notifyuser(user, "Only my owner may end games at this point.")
- def notifylist(self, message):
- for player in self.playerlist:
- self.notifyuser(player, message)
- def notifyuser(self, person, message):
- #time.sleep(1)
- self.notice(person, message)
- def hours23(self):
- self.notifylist("There is one hour remaining before the game ends by default. Hurry up and get killing!")
- def hours24(self):
- self.notifylist("24 hours have elapsed without a victor declared. Please rejoin now.")
- for person in self.quelist:
- self.notifyuser(person, "24 hours have elapsed without a victor declared. Please rejoin now.")
- self.resetstats()
- def help(self, user):
- self.notifyuser(user, self.helpmessage)
- self.notifyuser(user, self.helpcommands)
- class KomurinFactory(protocol.ClientFactory): #A 'factory' for the bot.
- protocol = Komurin
- def __init__(self, channel):
- self.channel = channel
- def clientConnectionLost(self, connector, reason): #We got disconnected, reconnect.
- connector.connect()
- def clientConnectionFailed(self, connector, reason):
- #print "connection failed:", reason
- reactor.stop()
- if __name__ == '__main__': #Check if we ran it, not imported it.
- dev = '#kiwi'
- chan = '#kiwi,#pygbot,#mafia,#mafia-testing,#mafia-discuss,#xkcd-love,#xkcd-sex,#xkcd-cs,#bots,#boats,#uno,#TheJohnHenry,###,#,#assassins'
- #global debug
- if debug:
- f = KomurinFactory(dev) #No spaces between channels
- else:
- f = KomurinFactory(chan)
- reactor.connectTCP("irc.foonetic.net", 6667, f) #Connect the factory to the network.
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement