Skip to content

Instantly share code, notes, and snippets.

@thomasleveil
Created October 30, 2011 03:01
Show Gist options
  • Save thomasleveil/1325409 to your computer and use it in GitHub Desktop.
Save thomasleveil/1325409 to your computer and use it in GitHub Desktop.
suggestions for ro2 parser.
Index: b3/parsers/ro2/__init__.py
===================================================================
--- b3/parsers/ro2/__init__.py (date 1319941731000)
+++ b3/parsers/ro2/__init__.py (revision )
@@ -42,11 +42,7 @@
#
#
from b3 import functions
-from b3.clients import Client
-from b3.lib.sourcelib import SourceQuery
-from b3.parser import Parser
from ftplib import FTP
-import asyncore
import b3
import b3.cron
import ftplib
@@ -57,7 +53,6 @@
import time
import urllib
import urllib2
-import httplib
import cookielib
import hashlib
@@ -67,9 +62,9 @@
class Ro2Parser(b3.parser.Parser):
- '''
+ """
The Ref Orchestra 2 B3 parser class
- '''
+ """
gameName = "redorchestra2"
PunkBuster = None
# RO2 engine does not support color code, so we need this property
@@ -91,7 +86,6 @@
cj=None
headers = {}
opener=None
- map_rotation = {}
map_cycles = {}
map_cycle_no = 0
active_map_cycle = 0
@@ -193,12 +187,10 @@
self.working=True
while self.working:
- """
- While we are working, connect to the RO2 server
- """
+ # While we are working, connect to the RO2 server
self._paused=False
if self._paused:
- if self._pauseNotice == False:
+ if not self._pauseNotice:
self.bot('PAUSED - Not parsing any lines, B3 will be out of sync.')
self._pauseNotice = True
else:
@@ -208,7 +200,7 @@
time.sleep(.2)
counter +=1
- if len(self._write_queue) == 0:
+ if not len(self._write_queue):
self.readwriteajax()
else:
self.debug('Go to ajax')
@@ -216,7 +208,7 @@
self.debug(self._write_queue)
self.readwriteajax(message)
- while len(self._read_queue) != 0:
+ while len(self._read_queue):
chat_data = self._read_queue.pop(0)
self.handle_chat(chat_data)
@@ -224,10 +216,8 @@
time.sleep(.2)
self.bot('Stop listening.')
- if self.exiting.acquire(1):
- self._serverConnection.close()
- if self.exitcode:
+ if self.exiting.acquire(1) and self.exitcode:
- sys.exit(self.exitcode)
+ sys.exit(self.exitcode)
def readwriteweb(self, data= None, referer=None, addurl=None):
"""Handles Reading and Writing to the web interface"""
@@ -246,7 +236,7 @@
console_data = console_read.read()
return console_data
- except:
+ except Exception:
self.debug('Failed to open URL')
self.webconnect()
return
@@ -261,8 +251,8 @@
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
findpage_attempt = 0
- foundpage = False
self._paused = True
+ response = ""
while findpage_attempt < 11:
try:
#self.debug('Attempting to find Web page (webconnect)')
@@ -270,7 +260,7 @@
page = urllib2.urlopen(request)
response=page.read()
break
- except:
+ except Exception:
findpage_attempt += 1
if findpage_attempt > 10:
self.debug('Failed to find web page - wait 10 seconds')
@@ -299,7 +289,7 @@
console_read = self.opener.open(request_console)
self._paused = False
return True
- except:
+ except Exception:
self.debug('Failed to login %s' % login_attempt)
self._paused = True
login_attempt += 1
@@ -502,7 +492,7 @@
"""Write a message to Console via Ajax"""
if self.replay:
self.bot('Sent rcon message: %s' % msg)
- elif self.output == None:
+ elif self.output is None:
pass
else:
msg = self.stripColors(msg)
@@ -514,10 +504,10 @@
"""Write a sequence of messages to Console via Ajax."""
if self.replay:
self.bot('Sent rcon message: %s' % msg)
- elif self.output == None:
+ elif self.output is None:
pass
else:
- for line in lines:
+ for line in lines: # FIXME : 'lines' does not exist
self.write(line)
time.sleep(0.1)
return
@@ -583,22 +573,31 @@
self.verbose2('onServerPlayer: name: %s, team: %s' %( client.name, client.team ))
- def sync(self, c_client_list):
+ def sync(self, connected_clients=None):
"""\
- Check Clients list against all connected players returned by self.getPlayerList() and
- if required call the client.disconnect() method to remove a client from self.clients.
- """
+ if connected_clients is None :
+ get dict of connected players from self.getPlayerList()
+ else use connected_clients as the list of connected players
+ For all connected players, get the matching Client
+ object from self.clients (with self.clients.getByCID(cid) or similar methods) and
+ look for inconsistencies. If required call the client.disconnect() method to remove
+ a client from self.clients.
+ This is mainly useful for games where clients are identified by the slot number they
+ occupy. On map change, a player A on slot 1 can leave making room for player B who
+ connects on slot 1.
+ """
self.debug("synchronizing clients")
+ if connected_clients is None:
+ connected_clients = self.getPlayerList()
+
client_cid_list = []
- for c in c_client_list:
- cl = c_client_list[c]
+ for cl in connected_clients.values():
client_cid_list.append(cl['playerid'])
for client in self.clients.getList():
-
- if (client_cid_list.count(client.cid) == 0):
+ if client.cid not in client_cid_list:
- self.debug( 'Removing %s from list' % client.name)
+ self.debug('Removing %s from list' % client.name)
#self.debug(client)
client.disconnect()
@@ -687,7 +686,7 @@
ban_no = None
try:
ban_no = ban_list[banid]
- except:
+ except Exception:
if admin:
admin.message(' %s not in server banlist' %client.name)
@@ -733,7 +732,7 @@
"""\
return the available maps/levels name
"""
- self.map_rotation = []
+ map_rotation = []
self.map_cycles = {}
self.map_cycle_no = 0
self.active_map_cycle = -1
@@ -756,13 +755,13 @@
map_line = self.map_cycles[str(self.active_map_cycle)]
map_line = map_line.partition('Maps=("')[2]
map_line = map_line.partition('"),RoundLimits=')[0]
- self.map_rotation.append(map_line.partition('","')[0])
+ map_rotation.append(map_line.partition('","')[0])
while map_line.find('","') != -1:
map_line= map_line.partition('","')[2]
- self.map_rotation.append(map_line.partition('","')[0])
+ map_rotation.append(map_line.partition('","')[0])
- #self.debug(self.map_rotation)
- return self.map_rotation
+ #self.debug(map_rotation)
+ return map_rotation
def rotateMap(self):
"""\
@@ -850,7 +849,7 @@
return
client_list = self.getPlayerList()
self.findNewPlayers(client_list)
- if len(client_list) != 0:
+ if len(client_list):
self.sync(client_list)
def retrieveBanlist(self):
@@ -892,13 +891,13 @@
try:
ftp.close()
self.debug('FTP Connection Closed')
- except:
+ except Exception:
pass
ftp = None
try:
ftp.close()
- except:
+ except Exception:
pass
@@ -906,8 +905,8 @@
#self.debug('Python Version %s.%s, so setting timeout of 10 seconds' % (versionsearch.group(2), versionsearch.group(3)))
self.verbose('Connecting to %s:%s ...' % (self.ftpconfig["host"], self.ftpconfig["port"]))
ftp = FTP()
- ftp.set_debuglevel(self._ftplib_debug_level)
- ftp.connect(self.ftpconfig['host'], self.ftpconfig['port'], self._connectionTimeout)
+ ftp.set_debuglevel(self._ftplib_debug_level) # FIXME: 'self._ftplib_debug_level' does not exist
+ ftp.connect(self.ftpconfig['host'], self.ftpconfig['port'], self._connectionTimeout) # FIXME: 'self._ftplib_debug_level' does not exist
ftp.login(self.ftpconfig['user'], self.ftpconfig['password'])
ftp.voidcmd('TYPE I')
dir = os.path.dirname(self.ftpconfig['path'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment