Advertisement
Guest User

Untitled

a guest
May 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.36 KB | None | 0 0
  1. # system imports
  2. import time, sys, urllib, pickle, random, re, os, shelve, math, httplib
  3. #from datetime import *
  4.  
  5. from twisted.words.protocols import irc
  6. from twisted.internet import reactor, protocol, task, defer
  7.  
  8. global debug
  9. debug = False
  10.  
  11. class Komurin(irc.IRCClient):
  12.    
  13.     def __init__(self):
  14.         self.nickname = "Assassins"
  15.         self.password = ""
  16.         self.version = "0.2.1"
  17.         self.Owner = 'Crayboff'
  18.         self.trigger = '-'
  19.         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!"
  20.         self.helpcommands = "Possible commands are: %shelp  %sjoin  %sstart  %squit" % (self.trigger, self.trigger, self.trigger, self.trigger)
  21.         self.resetcycle = 1440
  22.         self.gamedata = {}
  23.         self.isgame = False
  24.         self.playerlist = []
  25.         self.userlists = {}
  26.         self.userlist = []
  27.         self.alreadyassigned = []
  28.         self.deadlist = []
  29.         self.quelist = []
  30.    
  31.     def resetstats(self):
  32.         self.gameend.cancel()
  33.         self.hourwarning.cancel()
  34.         self.gamedata = {}
  35.         self.isgame = False
  36.         self.playerlist = []
  37.         self.userlists = {}
  38.         self.userlist = []
  39.         self.alreadyassigned = []
  40.         self.deadlist = []
  41.         self.quelist = []
  42.    
  43.     def connectionMade(self):
  44.         irc.IRCClient.connectionMade(self)
  45.        
  46.         m = open('assassins_wordlist.txt', 'r')
  47.         self.wordlist = m.read()
  48.         m.close()
  49.         self.wordlist = self.wordlist.split()
  50.         #print self.wordlist
  51.        
  52.     def connectionLost(self, reason):
  53.         print("LOST CONNECTION! REASON:  %s" % reason)
  54.         irc.IRCClient.connectionLost(self, reason)
  55.  
  56.     def signedOn(self):
  57.         self.join(self.factory.channel)
  58.         self.mode(self.nickname, True, '+B', limit=None, user=self.nickname)
  59.         print('Joined irc.foonetic.net as %s' %(self.nickname))
  60.        
  61.     def joined(self, channel):
  62.         print('Joined %s' %(channel))
  63.         self.me(channel, 'version '+self.version)      
  64.        
  65.     def noticed(self, user, channel, msg):
  66.         user = user.split('!', 1)[0]
  67.         if user == 'Nickserv':
  68.             self.msg('Nickserv', 'identify '+self.password)
  69.            
  70.     def privmsg(self, user, channel, msg):
  71.         user = user.split('!', 1)[0]
  72.         msg = msg.strip().lower()
  73.         if msg.startswith(self.trigger) or msg.startswith(self.nickname.lower()+':') or msg.startswith(self.nickname.lower()+','):
  74.             self.gameparser(user, channel, msg)
  75.         elif user in self.gamedata.keys() and self.isgame:
  76.             self.checkusermsg(user, channel, msg)
  77.            
  78.     def gameparser(self, user, channel, msg):
  79.         if msg.startswith(self.nickname.lower()):
  80.             msg = msg.split(' ',1)[1]
  81.         else:
  82.             msg = msg.strip(self.trigger)
  83.        
  84.         if msg.startswith('join'):
  85.             self.playerjoin(user)
  86.         elif msg.startswith('start'):
  87.             self.startgame(user)
  88.         elif msg.startswith('stats') or msg.startswith('status'):
  89.             self.userstats(user)
  90.         elif msg.startswith('end'):
  91.             self.endgame(user)
  92.         elif msg.startswith('help'):
  93.             self.help(user)
  94.         elif msg.startswith('quit'):
  95.             self.quitgame(user, channel, msg)
  96.         elif msg.startswith('version'):
  97.             self.notifyuser(user, self.version)
  98.         elif msg.startswith('joinchan') and user == self.Owner:
  99.             self.channeljoiner(user, msg)
  100.         elif msg.startswith('partchan') and user == self.Owner:
  101.             self.channelparter(user, msg)
  102.            
  103.     def channeljoiner(self, msg):
  104.         chan = msg.split(' ', 1)
  105.         self.notifyuser(user, "Attempting to join channel %s..." % (chan[1]))
  106.         self.join(chan[1])
  107.    
  108.     def channelparter(self, msg):
  109.         chan = msg.split(' ', 1)
  110.         self.notifyuser(user, "Attempting to part channel %s..." % (chan[1]))
  111.         self.join(chan[1])
  112.            
  113.     def playerjoin(self, user):
  114.         if self.isgame:
  115.             self.quelist.append(user)
  116.             #print 'quelist:'
  117.             #print self.quelist
  118.             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.")
  119.         elif user in self.playerlist:
  120.             self.notifyuser(user, 'You are already in this game')
  121.         else:
  122.             self.playerlist.append(user)
  123.             #print self.playerlist
  124.             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)
  125.        
  126.     def startgame(self, user):
  127.         self.alllist = self.gamedata.keys()
  128.         self.gameend = reactor.callLater(self.resetcycle, self.hours24)
  129.         self.hourwarning = reactor.callLater(self.resetcycle-60, self.hours23)
  130.         #print 'reached startgame'
  131.         try:
  132.             if self.isgame == False:
  133.                 if len(self.playerlist) <= 2:
  134.                     self.notifyuser(user, "There aren't enough players to begin! You need atleast 3 players")
  135.                     return
  136.                 else:
  137.                     for player in self.playerlist:
  138.                         #print player
  139.                         target = None
  140.                         chooselist = []
  141.                         chooselist = filter(lambda n: n not in self.alreadyassigned, self.playerlist)
  142.                         #print chooselist
  143.                         if player in chooselist:
  144.                             chooselist.remove(player)
  145.                         #print self.playerlist
  146.                         target = random.choice(chooselist)
  147.                         self.alreadyassigned.append(target)
  148.                         #print player, target
  149.                         deathword = random.choice(self.wordlist)
  150.                         #print deathword
  151.                         self.gamedata[player] = {'target':target, 'wordtodie':deathword}
  152.                     for player in self.gamedata.keys():
  153.                         targ = self.gamedata[player]['target']
  154.                         wordtokill = self.gamedata[targ]['wordtodie']
  155.                         self.notifyuser(player, "Your mission is to kill: %s. You must get this person to say: %s." % (targ, wordtokill))
  156.                        
  157.                     #print self.gamedata
  158.                     self.isgame = True
  159.             else:
  160.                 self.notifyuser(user, "There is already a game in progress.")
  161.         except IndexError:
  162.             #print "IndexError, restarting"
  163.             self.gamedata = {}
  164.             self.alreadyassigned = []
  165.             self.startgame(user)
  166.            
  167.     def quitgame(self, user, channel, msg):
  168.         if user in self.gamedata.keys() and self.isgame:
  169.             self.playerdeath(user, channel, msg)
  170.         elif user in self.playerlist and self.isgame == False:
  171.             self.notifylist("%s has quit!" % (user))
  172.             self.playerlist.remove(user)
  173.             print self.playerlist
  174.         else:
  175.             self.notifyuser(user, "You can't quit a game you aren't in!")
  176.            
  177.     def checkusermsg(self, user, channel, msg):
  178.         if self.gamedata[user]['wordtodie'] in msg:
  179.             self.playerdeath(user, channel, msg)
  180.            
  181.     def playerdeath(self, user, channel, msg):
  182.         newtarget = self.gamedata[user]['target']
  183.         newdeathword = self.gamedata[newtarget]['wordtodie']
  184.         for player in self.gamedata:
  185.             if self.gamedata[player]['target'] == user:
  186.                 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))
  187.                 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'])
  188.                 self.gamedata[player]['target'] = newtarget
  189.                 #print self.gamedata
  190.         del self.gamedata[user]
  191.         self.deadlist.append(user)
  192.         #print self.deadlist
  193.         self.notifylist("%s has just died, his/her mysterious killer roams free." % (user))
  194.         if len(self.gamedata.keys()) == 1:
  195.             #print self.playerlist
  196.             #print 'A winner has been decided'
  197.             winner = self.gamedata.keys()[0]
  198.             for person in self.quelist:
  199.                 self.notifyuser(person, "The game of Assassins has ended, you may join now.")
  200.             self.notifylist("The Assassin game is OVER. Congratulations the new winner: %s" % winner)
  201.             self.resetstats()
  202.             #self.notifyuser(winner, 'Congratulations, you are the winner of Word Assassin!')
  203.    
  204.     def userstats(self, user):
  205.         #print 'userstats'
  206.         if user in self.gamedata.keys() and self.isgame:
  207.             target = self.gamedata[user]['target']
  208.             wordtokill = self.gamedata[target]['wordtodie']
  209.             self.notifyuser(user, "Your mission is to kill: %s. You must get this person to say: %s." % (target, wordtokill))
  210.         elif self.isgame == False:
  211.             self.notifyuser(user, "There is no game currently in play to get your information from!")
  212.         elif user not in self.gamedata.keys():
  213.             self.notifyuser(user, "You are not currently playing!")
  214.            
  215.     def endgame(self, user):
  216.         if user == self.Owner:
  217.             #print "owner is ending"
  218.             #print self.playerlist
  219.             self.notifylist("Game has been ended by owner.")
  220.             for person in self.quelist:
  221.                 self.notifyuser(person, "The game of Assassins has ended, you may join now.")
  222.             self.resetstats()
  223.         else:
  224.             self.notifyuser(user, "Only my owner may end games at this point.")
  225.    
  226.     def notifylist(self, message):
  227.         for player in self.playerlist:
  228.             self.notifyuser(player, message)
  229.            
  230.     def notifyuser(self, person, message):
  231.         #time.sleep(1)
  232.         self.notice(person, message)
  233.    
  234.     def hours23(self):
  235.         self.notifylist("There is one hour remaining before the game ends by default. Hurry up and get killing!")
  236.    
  237.     def hours24(self):
  238.         self.notifylist("24 hours have elapsed without a victor declared. Please rejoin now.")
  239.         for person in self.quelist:
  240.             self.notifyuser(person, "24 hours have elapsed without a victor declared. Please rejoin now.")
  241.         self.resetstats()
  242.        
  243.     def help(self, user):
  244.         self.notifyuser(user, self.helpmessage)
  245.         self.notifyuser(user, self.helpcommands)   
  246.            
  247. class KomurinFactory(protocol.ClientFactory):       #A 'factory' for the bot.
  248.     protocol = Komurin
  249.  
  250.     def __init__(self, channel):
  251.         self.channel = channel
  252.  
  253.     def clientConnectionLost(self, connector, reason):      #We got disconnected, reconnect.
  254.         connector.connect()
  255.        
  256.  
  257.     def clientConnectionFailed(self, connector, reason):
  258.         #print "connection failed:", reason
  259.         reactor.stop()
  260.  
  261.  
  262. if __name__ == '__main__':                              #Check if we ran it, not imported it.
  263.     dev = '#kiwi'
  264.     chan = '#kiwi,#pygbot,#mafia,#mafia-testing,#mafia-discuss,#xkcd-love,#xkcd-sex,#xkcd-cs,#bots,#boats,#uno,#TheJohnHenry,###,#,#assassins'
  265.     #global debug
  266.    
  267.     if debug:
  268.         f = KomurinFactory(dev)                         #No spaces between channels
  269.     else:
  270.         f = KomurinFactory(chan)
  271.  
  272.     reactor.connectTCP("irc.foonetic.net", 6667, f)     #Connect the factory to the network.
  273.  
  274.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement