Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: Python | Size: 9.67 KB | Hits: 22 | Expires: Never
Copy text to clipboard
  1. import random
  2. from Credentials import botowner, botname, botpassword
  3. from SubspaceBot import *
  4. from threading import Timer
  5.  
  6. CHALLENGE_EXPIRE = 30
  7.  
  8. CHALLENGE_AVAIL = 0
  9. CHALLENGE_PENDING_FROM = 90
  10. CHALLENGE_PENDING_TARGET = 91
  11. CHALLENGE_ACCEPTED = 92
  12. CHALLENGER_PLAYER = 93
  13. CHALLENGER_OPPONENT = 94
  14.  
  15. class StoredStuff():
  16.         def __init__(self):
  17.             self.timer_on = 0
  18.             self.timer2_on = 0
  19.        
  20.  
  21. def handle_slap(bot, event):
  22.         player = event.player
  23.         if len(event.arguments_after):
  24.                 target = bot.findPlayerByName(event.arguments_after[0])
  25.         else:
  26.                 bot.sendPrivateMessage(player.name, 'Please specify a player.')
  27.                 return
  28.         if player.name == botowner:
  29.                 bot.sendPublicMessage('%s has slapped the shit out of %s.'%(player.name, target.name))
  30.         else:
  31.                 bot.sendPrivateMessage(player.name, 'LOL, !r hahahahahahahah! Newbie.')
  32. def is_spec(player): #define that if the player is a spectator, then return this message
  33.    
  34.     if player.ship == SHIP_SPECTATOR:
  35.         return '%s is a spectator'%player.name
  36.     return True
  37.    
  38. def not_self(player, target): #define if the player was to try and chellenge themselves.
  39.    
  40.     if player.name.lower() == target.name.lower():
  41.         return 'Player and target are the same.'
  42.     return True
  43.  
  44. def can_challenge(player): #define what to display if the player is in a challenge, or has one pending
  45.    
  46.     if player.info.challenge_status in (CHALLENGE_PENDING_FROM, CHALLENGE_PENDING_TARGET):
  47.         return '%s already has a pending challenge.'%player.name
  48.        
  49.     if player.info.challenge_status == CHALLENGE_ACCEPTED:
  50.         return '%s already is already participating in a challenge.'%player.name
  51.        
  52.     return True
  53.  
  54.  
  55. def require(reqlist):
  56.     for req in reqlist:
  57.         rfunc = req[0]
  58.         args = req[1:]
  59.         test = rfunc(*args)
  60.         if test is not True:
  61.             return test
  62.     return None
  63.    
  64.    
  65. def handle_challenge(bot, event):
  66.     player = event.player #name of the person using the !ch
  67.     if len(event.arguments_after):
  68.         target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
  69.     else:
  70.         bot.sendPublicMessage('Please specify a player')
  71.         return
  72.  
  73.     def expire_challenge_target(target):
  74.         if MyStore.timer_on == 1:
  75.             bot.sendPublicMessage('%s\'s challenge has expired'%target.name)
  76.             if player.info.challenge_status == CHALLENGE_PENDING_FROM:
  77.                 player.info.challenge_status = CHALLENGE_AVAIL
  78.                 player.info.challenge_opponent = None
  79.                 return
  80.             if target.info.challenge_status == CHALLENGE_PENDING_TARGET :    
  81.                 target.info.challenge_status = CHALLENGE_AVAIL
  82.                 target.info.challenge_opponent = None
  83.  
  84.     def expire_challenge_player(player):
  85.         if MyStore.timer2_on == 1:
  86.             bot.sendPublicMessage('%s\'s challenge has expired'%player.name)
  87.             if player.info.challenge_status == CHALLENGE_PENDING_FROM:
  88.                 player.info.challenge_status = CHALLENGE_AVAIL
  89.                 player.info.challenge_opponent = None
  90.                 return
  91.             if target.info.challenge_status == CHALLENGE_PENDING_TARGET :    
  92.                 target.info.challenge_status = CHALLENGE_AVAIL
  93.                 target.info.challenge_opponent = None
  94.  
  95.  
  96.     if not target: #check, to see if target exists, if not, then return the message that the player does not exist.
  97.         bot.sendPublicMessage('%s, that player does not exist, please check and type the exact player name.'%player.name)
  98.         return
  99.        
  100.                      #(funcion, arg1, arg2...)
  101.     errmsg = require([(not_self, player, target), #if the player is not_self, the player, or the target, then play the error messages
  102.                       (is_spec, player), #if the player is in spec play errmsg
  103.                       (is_spec, target), #if the target is in spec, play msg
  104.                       (can_challenge, target), #if the target is in a challenge, or already has a challenge pending
  105.                       (can_challenge, player)]) #if the player is in a challenge, or already has a target pending
  106.  
  107.     if errmsg:
  108.         bot.sendPublicMessage(errmsg)
  109.        
  110.     else:
  111.         Timer(CHALLENGE_EXPIRE, expire_challenge_target, args=[target]).start() #start the 30 sec timer, then after 30 seconds, send it to def expire_challenge
  112.         Timer(CHALLENGE_EXPIRE, expire_challenge_player, args=[player]).start()
  113.         bot.sendPublicMessage('%s, %s has challenged you to a spree race. Please type !a <playername> to accept or !r <playername> to reject.'%(target.name, player.name)) #inform the targeted player that he has been challenged
  114.         player.info.challenge_status = CHALLENGE_PENDING_FROM
  115.         target.info.challenge_status = CHALLENGE_PENDING_TARGET #change both the targets player.info so they can neither be challenged
  116.         target.info.challenge_opponent = CHALLENGER_OPPONENT
  117.         player.info.challenge_opponent = CHALLENGER_PLAYER
  118.         MyStore.timer_on = 1
  119.         MyStore.timer2_on = 1
  120.         player.info.whoisin_game = target
  121.         target.info.whoisin_game = player
  122.  
  123.  
  124. def handle_accept(bot, event):
  125.     player = event.player
  126.     if len(event.arguments_after):
  127.         target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
  128.     else:
  129.         bot.sendPublicMessage('Please specify a player')
  130.         return
  131.     if player.info.challenge_status == CHALLENGE_AVAIL:
  132.         bot.sendPublicMessage('%s, No pending challenge'%player.name)
  133.     elif player.info.challenge_opponent == CHALLENGER_PLAYER:
  134.         bot.sendPublicMessage('You cannot accept your own challenge')
  135.     elif player.info.challenge_status == CHALLENGER_PLAYER:
  136.         bot.sendPublicMessage('You cannot accept your own challenge')
  137.     elif target.info.challenge_status == CHALLENGE_ACCEPTED:
  138.         bot.sendPublicMessage('A Challenge is already in progress with %s.'%target.name)
  139.  
  140.     else:
  141.         if player.info.challenge_opponent == CHALLENGER_OPPONENT and target.info.challenge_opponent == CHALLENGER_PLAYER:
  142.             MyStore.timer_on = 0
  143.             MyStore.timer2_on = 0
  144.             player.info.challenge_status = CHALLENGE_ACCEPTED
  145.             target.info.challenge_status = CHALLENGE_ACCEPTED
  146.             player.info.consecutive_kills = 0
  147.             target.info.consecutive_kills = 0
  148.             bot.sendPublicMessage('%s, you have accepted the challenge, good luck!'%player.name)
  149.  
  150.  
  151.  
  152. def handle_reject(bot, event):
  153.     player = event.player
  154.     if len(event.arguments_after):
  155.         target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
  156.     else:
  157.         bot.sendPublicMessage('Please specify a player')
  158.         return
  159.     if event.player.info.challenge_status != CHALLENGE_PENDING_TARGET:
  160.         bot.sendPublicMessage('You have no challenge pending.')
  161.     elif event.player.info.challenge_status == CHALLENGE_PENDING_TARGET:        
  162.         event.player.info.challenge_status = CHALLENGE_AVAIL
  163.         target.info.challenge_status = CHALLENGE_AVAIL
  164.         bot.sendPublicMessage('%s has rejected %s\'s challenge.'%(player.name, target.name)) #let the other player know that his challenge has been rejected
  165.     else:
  166.         bot.sendPublicMessage('You have no pending challenges')
  167.  
  168.  
  169. def init_dispatch(bot, commands):
  170.     dispatch = {}
  171.     for cmd, desc, func in commands:
  172.         id = bot.registerCommand(cmd, desc)
  173.         dispatch[id] = func
  174.     return dispatch
  175.  
  176. class PlayerInfo():
  177.     def __init__(self):
  178.         self.consecutive_kills = 0
  179.         self.challenge_status = CHALLENGE_AVAIL
  180.         self.challenge_opponent = None
  181.         self.whoisin_game = None
  182.  
  183. if __name__ == '__main__':
  184.     bot = SubspaceBot(botowner, 'This bot allows a player to challenge another player to a spree race')
  185.     bot.connectToServer('66.235.184.102', 7900, botname, botpassword, '#python')
  186.    
  187.     print "Bot connected to server"
  188.     commands = [('!ch', 'Challenge a player to race to a spree', handle_challenge),
  189.         ('!a', 'Accept a pending challenge', handle_accept),
  190.         ('!r', 'Reject a pending challenge', handle_reject),
  191.         ('!slap', 'Bot owner command.', handle_slap)]
  192.  
  193.     dispatch = init_dispatch(bot, commands)
  194.    
  195.     while bot.isConnected():
  196.         event = bot.waitForEvent()
  197.         MyStore = StoredStuff()
  198.        
  199.         if event.type == EVENT_ENTER:
  200.             event.player.info = PlayerInfo()
  201.  
  202.         if event.type == EVENT_KILL:
  203.             event.killer.info.consecutive_kills =+1
  204.             if event.killed.name == event.killer.name:
  205.                 event.killed.info.consecutive_kills = 0
  206.                
  207.             elif event.killer.info.challenge_status == CHALLENGE_ACCEPTED and event.killer.info.consecutive_kills == 5:
  208.                 bot.sendAreanMessage('%s, has won the challenge against %s.'%event.killer.name, event.killer.info.whoisin_game)
  209.                 event.killer.info.challenge_status = CHALLENGE_AVAIL
  210.                 event.killer.info.challenge_opponent = None
  211.                 event.killer.info.whoisin_game = None
  212.                 event.killer.info.whoisin_game.info.challenge_status = CHALLENGE_AVAIL
  213.                 event.killer.info.whoisin_game.info.challenge_opponent = None
  214.                 event.killer.info.whoisin_game.info = None
  215.            
  216.            
  217.         elif event.type == EVENT_COMMAND:
  218.             # When a player uses the command !ch, find <name> and challenge them to a spree race.
  219.          
  220.             func = dispatch.get(event.command.id, None)
  221.             if func:
  222.                 func(bot, event)
  223.                    
  224.                
  225.                
  226.     print "Bot Disconnected"