import random
from Credentials import botowner, botname, botpassword
from SubspaceBot import *
from threading import Timer
CHALLENGE_EXPIRE = 30
CHALLENGE_AVAIL = 0
CHALLENGE_PENDING_FROM = 90
CHALLENGE_PENDING_TARGET = 91
CHALLENGE_ACCEPTED = 92
CHALLENGER_PLAYER = 93
CHALLENGER_OPPONENT = 94
class StoredStuff():
def __init__(self):
self.timer_on = 0
self.timer2_on = 0
def handle_slap(bot, event):
player = event.player
if len(event.arguments_after):
target = bot.findPlayerByName(event.arguments_after[0])
else:
bot.sendPrivateMessage(player.name, 'Please specify a player.')
return
if player.name == botowner:
bot.sendPublicMessage('%s has slapped the shit out of %s.'%(player.name, target.name))
else:
bot.sendPrivateMessage(player.name, 'LOL, !r hahahahahahahah! Newbie.')
def is_spec(player): #define that if the player is a spectator, then return this message
if player.ship == SHIP_SPECTATOR:
return '%s is a spectator'%player.name
return True
def not_self(player, target): #define if the player was to try and chellenge themselves.
if player.name.lower() == target.name.lower():
return 'Player and target are the same.'
return True
def can_challenge(player): #define what to display if the player is in a challenge, or has one pending
if player.info.challenge_status in (CHALLENGE_PENDING_FROM, CHALLENGE_PENDING_TARGET):
return '%s already has a pending challenge.'%player.name
if player.info.challenge_status == CHALLENGE_ACCEPTED:
return '%s already is already participating in a challenge.'%player.name
return True
def require(reqlist):
for req in reqlist:
rfunc = req[0]
args = req[1:]
test = rfunc(*args)
if test is not True:
return test
return None
def handle_challenge(bot, event):
player = event.player #name of the person using the !ch
if len(event.arguments_after):
target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
else:
bot.sendPublicMessage('Please specify a player')
return
def expire_challenge_target(target):
if MyStore.timer_on == 1:
bot.sendPublicMessage('%s\'s challenge has expired'%target.name)
if player.info.challenge_status == CHALLENGE_PENDING_FROM:
player.info.challenge_status = CHALLENGE_AVAIL
player.info.challenge_opponent = None
return
if target.info.challenge_status == CHALLENGE_PENDING_TARGET :
target.info.challenge_status = CHALLENGE_AVAIL
target.info.challenge_opponent = None
def expire_challenge_player(player):
if MyStore.timer2_on == 1:
bot.sendPublicMessage('%s\'s challenge has expired'%player.name)
if player.info.challenge_status == CHALLENGE_PENDING_FROM:
player.info.challenge_status = CHALLENGE_AVAIL
player.info.challenge_opponent = None
return
if target.info.challenge_status == CHALLENGE_PENDING_TARGET :
target.info.challenge_status = CHALLENGE_AVAIL
target.info.challenge_opponent = None
if not target: #check, to see if target exists, if not, then return the message that the player does not exist.
bot.sendPublicMessage('%s, that player does not exist, please check and type the exact player name.'%player.name)
return
#(funcion, arg1, arg2...)
errmsg = require([(not_self, player, target), #if the player is not_self, the player, or the target, then play the error messages
(is_spec, player), #if the player is in spec play errmsg
(is_spec, target), #if the target is in spec, play msg
(can_challenge, target), #if the target is in a challenge, or already has a challenge pending
(can_challenge, player)]) #if the player is in a challenge, or already has a target pending
if errmsg:
bot.sendPublicMessage(errmsg)
else:
Timer(CHALLENGE_EXPIRE, expire_challenge_target, args=[target]).start() #start the 30 sec timer, then after 30 seconds, send it to def expire_challenge
Timer(CHALLENGE_EXPIRE, expire_challenge_player, args=[player]).start()
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
player.info.challenge_status = CHALLENGE_PENDING_FROM
target.info.challenge_status = CHALLENGE_PENDING_TARGET #change both the targets player.info so they can neither be challenged
target.info.challenge_opponent = CHALLENGER_OPPONENT
player.info.challenge_opponent = CHALLENGER_PLAYER
MyStore.timer_on = 1
MyStore.timer2_on = 1
player.info.whoisin_game = target
target.info.whoisin_game = player
def handle_accept(bot, event):
player = event.player
if len(event.arguments_after):
target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
else:
bot.sendPublicMessage('Please specify a player')
return
if player.info.challenge_status == CHALLENGE_AVAIL:
bot.sendPublicMessage('%s, No pending challenge'%player.name)
elif player.info.challenge_opponent == CHALLENGER_PLAYER:
bot.sendPublicMessage('You cannot accept your own challenge')
elif player.info.challenge_status == CHALLENGER_PLAYER:
bot.sendPublicMessage('You cannot accept your own challenge')
elif target.info.challenge_status == CHALLENGE_ACCEPTED:
bot.sendPublicMessage('A Challenge is already in progress with %s.'%target.name)
else:
if player.info.challenge_opponent == CHALLENGER_OPPONENT and target.info.challenge_opponent == CHALLENGER_PLAYER:
MyStore.timer_on = 0
MyStore.timer2_on = 0
player.info.challenge_status = CHALLENGE_ACCEPTED
target.info.challenge_status = CHALLENGE_ACCEPTED
player.info.consecutive_kills = 0
target.info.consecutive_kills = 0
bot.sendPublicMessage('%s, you have accepted the challenge, good luck!'%player.name)
def handle_reject(bot, event):
player = event.player
if len(event.arguments_after):
target = bot.findPlayerByName(event.arguments_after[0]) #find that player, and name him target
else:
bot.sendPublicMessage('Please specify a player')
return
if event.player.info.challenge_status != CHALLENGE_PENDING_TARGET:
bot.sendPublicMessage('You have no challenge pending.')
elif event.player.info.challenge_status == CHALLENGE_PENDING_TARGET:
event.player.info.challenge_status = CHALLENGE_AVAIL
target.info.challenge_status = CHALLENGE_AVAIL
bot.sendPublicMessage('%s has rejected %s\'s challenge.'%(player.name, target.name)) #let the other player know that his challenge has been rejected
else:
bot.sendPublicMessage('You have no pending challenges')
def init_dispatch(bot, commands):
dispatch = {}
for cmd, desc, func in commands:
id = bot.registerCommand(cmd, desc)
dispatch[id] = func
return dispatch
class PlayerInfo():
def __init__(self):
self.consecutive_kills = 0
self.challenge_status = CHALLENGE_AVAIL
self.challenge_opponent = None
self.whoisin_game = None
if __name__ == '__main__':
bot = SubspaceBot(botowner, 'This bot allows a player to challenge another player to a spree race')
bot.connectToServer('66.235.184.102', 7900, botname, botpassword, '#python')
print "Bot connected to server"
commands = [('!ch', 'Challenge a player to race to a spree', handle_challenge),
('!a', 'Accept a pending challenge', handle_accept),
('!r', 'Reject a pending challenge', handle_reject),
('!slap', 'Bot owner command.', handle_slap)]
dispatch = init_dispatch(bot, commands)
while bot.isConnected():
event = bot.waitForEvent()
MyStore = StoredStuff()
if event.type == EVENT_ENTER:
event.player.info = PlayerInfo()
if event.type == EVENT_KILL:
event.killer.info.consecutive_kills =+1
if event.killed.name == event.killer.name:
event.killed.info.consecutive_kills = 0
elif event.killer.info.challenge_status == CHALLENGE_ACCEPTED and event.killer.info.consecutive_kills == 5:
bot.sendAreanMessage('%s, has won the challenge against %s.'%event.killer.name, event.killer.info.whoisin_game)
event.killer.info.challenge_status = CHALLENGE_AVAIL
event.killer.info.challenge_opponent = None
event.killer.info.whoisin_game = None
event.killer.info.whoisin_game.info.challenge_status = CHALLENGE_AVAIL
event.killer.info.whoisin_game.info.challenge_opponent = None
event.killer.info.whoisin_game.info = None
elif event.type == EVENT_COMMAND:
# When a player uses the command !ch, find <name> and challenge them to a spree race.
func = dispatch.get(event.command.id, None)
if func:
func(bot, event)
print "Bot Disconnected"