Skip to content

Commit

Permalink
Merge all fixed and updates of Spunky Bot
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkress committed Nov 15, 2015
1 parent 700ff0c commit 27cd989
Showing 1 changed file with 31 additions and 44 deletions.
75 changes: 31 additions & 44 deletions pcwbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Run the bot: python pcwbot.py
"""

__version__ = '0.9.9'
__version__ = '0.9.10'


### IMPORTS
Expand Down Expand Up @@ -113,7 +113,7 @@ def recv(self, timeout=1):
except socket.error, err:
raise Exception('Error receiving the packet: %s' % err[1])

def command(self, cmd, timeout=1, retries=3):
def command(self, cmd, timeout=1, retries=5):
"""
send command and receive response
"""
Expand Down Expand Up @@ -296,7 +296,7 @@ def __init__(self, config_file):
@type config_file: String
"""
# RCON commands for the different admin roles
self.admin_cmds = ['cyclemap', 'force', 'kick', 'list', 'map', 'password', 'reload', 'setnextmap', 'veto']
self.admin_cmds = ['cyclemap', 'exec', 'force', 'kick', 'list', 'map', 'password', 'reload', 'setnextmap', 'swapteams', 'veto']
self.headadmin_cmds = self.admin_cmds + ['leveltest', 'putgroup', 'ungroup']
# alphabetic sort of the commands
self.admin_cmds.sort()
Expand Down Expand Up @@ -333,7 +333,7 @@ def read_log(self):

while self.log_file:
line = self.log_file.readline()
if len(line) != 0:
if line:
self.parse_line(line)
else:
if not self.game.live:
Expand All @@ -348,14 +348,13 @@ def parse_line(self, string):
tmp = line.split(":", 1)
line = tmp[1].strip() if len(tmp) > 1 else tmp[0].strip()
try:
if tmp:
action = tmp[0].strip()
if action == 'ClientUserinfo':
self.handle_userinfo(line)
elif action == 'ClientDisconnect':
self.handle_disconnect(line)
elif action == 'say':
self.handle_say(line)
action = tmp[0].strip()
if action == 'ClientUserinfo':
self.handle_userinfo(line)
elif action == 'ClientDisconnect':
self.handle_disconnect(line)
elif action == 'say':
self.handle_say(line)
except (IndexError, KeyError):
pass
except Exception, err:
Expand Down Expand Up @@ -387,25 +386,9 @@ def handle_userinfo(self, line):
player_num = int(line[:2].strip())
line = line[2:].lstrip("\\").lstrip()
values = self.explode_line(line)
challenge = True if 'challenge' in values else False
try:
guid = values['cl_guid'].rstrip('\n')
name = re.sub(r"\s+", "", values['name'])
ip_port = values['ip']
except KeyError:
if 'cl_guid' in values:
guid = values['cl_guid']
else:
guid = "None"
if 'name' in values:
name = re.sub(r"\s+", "", values['name'])
else:
name = "UnnamedPlayer"
if 'ip' in values:
ip_port = values['ip']
else:
ip_port = "0.0.0.0:0"

name = re.sub(r"\s+", "", values['name']) if 'name' in values else "UnnamedPlayer"
ip_port = values['ip'] if 'ip' in values else "0.0.0.0:0"
guid = values['cl_guid'] if 'cl_guid' in values else "None"
ip_address = ip_port.split(":")[0].strip()

if player_num not in self.game.players:
Expand All @@ -417,10 +400,6 @@ def handle_userinfo(self, line):
if self.game.players[player_num].get_name() != name:
self.game.players[player_num].set_name(name)

if not challenge:
if 'name' in values and values['name'] != self.game.players[player_num].get_name():
self.game.players[player_num].set_name(values['name'])

def handle_disconnect(self, line):
"""
handle player disconnect
Expand Down Expand Up @@ -465,7 +444,7 @@ def map_found(self, map_name):
break
elif map_name.lower() in maps:
append(maps)
if len(map_list) == 0:
if not map_list:
return False, None, "^3Map not found"
elif len(map_list) > 1:
return False, None, "^7Maps matching %s: ^3%s" % (map_name, ', '.join(map_list))
Expand Down Expand Up @@ -593,6 +572,10 @@ def handle_say(self, line):
elif sar['command'] == '!cyclemap' and self.game.players[sar['player_num']].get_admin_role() >= 40:
self.game.send_rcon('cyclemap')

# swapteams - swap current teams
elif sar['command'] == '!swapteams' and self.game.players[sar['player_num']].get_admin_role() >= 80:
self.game.send_rcon('swapteams')

## head admin level 100
# leveltest
elif (sar['command'] == '!leveltest' or sar['command'] == '!lt') and self.game.players[sar['player_num']].get_admin_role() == 100:
Expand Down Expand Up @@ -667,7 +650,7 @@ def handle_say(self, line):
self.game.rcon_tell(sar['player_num'], "^7You are registered as ^6Head Admin")

## unknown command
elif sar['command'].startswith('!') and self.game.players[sar['player_num']].get_admin_role() >= 40:
elif sar['command'].startswith('!') and len(sar['command']) > 1 and self.game.players[sar['player_num']].get_admin_role() >= 40:
if sar['command'].lstrip('!') in self.headadmin_cmds:
self.game.rcon_tell(sar['player_num'], "^7Insufficient privileges to use command ^3%s" % sar['command'])

Expand All @@ -686,7 +669,7 @@ def __init__(self, player_num, ip_address, guid, name):
"""
self.player_num = player_num
self.guid = guid
self.name = "".join(name.split())
self.name = name.replace(' ', '')
self.registered_user = False
self.admin_role = 0
self.address = ip_address
Expand Down Expand Up @@ -723,7 +706,7 @@ def update_db_admin_role(self, role):
self.set_admin_role(role)

def set_name(self, name):
self.name = "".join(name.split())
self.name = name.replace(' ', '')

def get_name(self):
return self.name
Expand Down Expand Up @@ -771,7 +754,7 @@ def __init__(self, config_file):
self.add_player(world)
print "- Added pcwbot successful to the game.\n"
print "pcwbot is running until you are closing this session or pressing CTRL + C to abort this process."
print "Note: Use the provided initscript to run pcwbot as daemon.\n"
print "*** Note: Use the provided initscript to run pcwbot as daemon ***\n"

def send_rcon(self, command):
"""
Expand All @@ -792,7 +775,7 @@ def rcon_tell(self, player_num, msg):
@param msg: The message to display in private chat
@type msg: String
"""
lines = textwrap.wrap(msg, 135)
lines = textwrap.wrap(msg, 128)
for line in lines:
self.send_rcon('tell %d %s' % (player_num, line))

Expand Down Expand Up @@ -829,10 +812,14 @@ def set_all_maps(self):
set a list of all available maps
"""
all_maps = self.rcon_handle.get_rcon_output("dir map bsp")[1].split()
all_maps.sort()
all_maps_list = [maps.replace("/", "").replace(".bsp", "") for maps in all_maps if maps.startswith("/")]
if all_maps_list:
self.all_maps_list = all_maps_list
pk3_list = self.rcon_handle.get_rcon_output("fdir *.pk3")[1].split()
all_pk3_list = [maps.replace("/", "").replace(".pk3", "").replace(".bsp", "") for maps in pk3_list if maps.startswith("/ut4_")]

all_together = list(set(all_maps_list + all_pk3_list))
all_together.sort()
if all_together:
self.all_maps_list = all_together

def get_all_maps(self):
"""
Expand Down

0 comments on commit 27cd989

Please sign in to comment.