Update the lobbybot such that it can work both with 2.0,2.1 and 2.2 lobbies, defaulting to 2.1 (as that's the current release)

git-svn-id: https://warzone2100.svn.sourceforge.net/svnroot/warzone2100/trunk@7101 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2009-04-20 21:30:10 +00:00 committed by Git SVN Gateway
parent d001fdbafc
commit 7261e3392a
2 changed files with 23 additions and 5 deletions

View File

@ -23,6 +23,7 @@
from __future__ import with_statement from __future__ import with_statement
from contextlib import contextmanager from contextlib import contextmanager
from pkg_resources import parse_version
import socket import socket
import struct import struct
@ -40,12 +41,29 @@ def _swap_endianness(i):
return struct.unpack(">I", struct.pack("<I", i)) return struct.unpack(">I", struct.pack("<I", i))
class masterserver_connection: class masterserver_connection:
# >= 2.0 data
name_length = 64
host_length = 16
# >= 2.2 data
misc_length = 64
extra_length = 255
versionstring_length = 64
modlist_length = 255
host = None host = None
port = None port = None
def __init__(self, host, port): def __init__(self, host, port, version = '2.2'):
# Binary struct format to use (GAMESTRUCT)
if parse_version(version) >= parse_version('2.0'):
gameFormat = "!%dsII%ds6I" % (self.name_length, self.host_length)
if parse_version(version) >= parse_version('2.2'):
gameFormat += '%ds%ds%ds%ds10I' % (self.misc_length, self.extra_length, self.versionstring_length, self.modlist_length)
self.host = host self.host = host
self.port = port self.port = port
self.version = version
def list(self, allowException = False): def list(self, allowException = False):
with _socket(socket.AF_INET, socket.SOCK_STREAM) as s: with _socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@ -58,7 +76,7 @@ class masterserver_connection:
(count,) = self._recv(s, "!I") (count,) = self._recv(s, "!I")
for i in xrange(0,count): for i in xrange(0,count):
(description, size, flags, host, maxPlayers, currentPlayers, (description, size, flags, host, maxPlayers, currentPlayers,
user1, user2, user3, user4 ) = self._recv(s, "!64sII16sIIIIII") user1, user2, user3, user4) = self._recv(s, self.gameFormat)
description = description.strip("\0") description = description.strip("\0")
host = host.strip("\0") host = host.strip("\0")
# Workaround for the fact that some of the # Workaround for the fact that some of the

View File

@ -46,7 +46,7 @@ class Bot:
self.commands = BotCommands() self.commands = BotCommands()
self.commands.bot = self self.commands.bot = self
self.irc = bot_connection(self.commands, ircServer, ircPort, ircChannel, ircNick) self.irc = bot_connection(self.commands, ircServer, ircPort, ircChannel, ircNick)
self.lobby = masterserver_connection(lobbyServer, lobbyPort) self.lobby = masterserver_connection(lobbyServer, lobbyPort, version='2.1')
self.check = change_notifier(self.irc, self.lobby) self.check = change_notifier(self.irc, self.lobby)
def start(self): def start(self):