class Server(object):
- def __init__(self, name=None):
+ def __init__(self, name=None, hashkey=None, ip_addr=None):
self.name = name
+ self.hashkey = hashkey
+ self.ip_addr = ip_addr
def __repr__(self):
return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
-class ServerHashkey(object):
- def __init__(self, server_id=None, hashkey=None):
- self.server_id = server_id
- self.hashkey = hashkey
-
- def __repr__(self):
- return "<ServerHashkey(%s, %s)>" % (self.server_id, self.hashkey)
-
-
def initialize_db(engine=None):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
player_weapon_stats_table = MetaData.tables['player_weapon_stats']
servers_table = MetaData.tables['servers']
player_nicks_table = MetaData.tables['player_nicks']
- server_hashkeys_table = MetaData.tables['server_hashkeys']
# now map the tables and the objects together
mapper(PlayerAchievement, achievements_table)
mapper(PlayerWeaponStat, player_weapon_stats_table)
mapper(Server, servers_table)
mapper(PlayerNick, player_nicks_table)
- mapper(ServerHashkey, server_hashkeys_table)
name - server name of the server to be found or created\r
hashkey - server hashkey\r
"""\r
- # see if the server is already in the database\r
- # if not, create one and the hashkey along with it\r
try:\r
- hashkey = session.query(ServerHashkey).filter_by(\r
- hashkey=hashkey).one()\r
- server = session.query(Server).filter_by(\r
- server_id=hashkey.server_id).one()\r
- log.debug("Found existing server {0} with hashkey {1}".format(\r
- server.server_id, hashkey.hashkey))\r
- except:\r
- server = Server()\r
- server.name = name\r
+ # find one by that name, if it exists\r
+ server = session.query(Server).filter_by(name=name).one()\r
+ log.debug("Found existing server {0}".format(server.server_id))\r
+\r
+ except MultipleResultsFound, e:\r
+ # multiple found, so also filter by hashkey\r
+ server = session.query(Server).filter_by(name=name).\\r
+ filter_by(hashkey=hashkey).one()\r
+ log.debug("Found existing server {0}".format(server.server_id))\r
+\r
+ except NoResultFound, e:\r
+ # not found, create one\r
+ server = Server(name=name, hashkey=hashkey)\r
session.add(server)\r
session.flush()\r
-\r
- hashkey = ServerHashkey(server_id=server.server_id, \r
- hashkey=hashkey)\r
- session.add(hashkey)\r
log.debug("Created server {0} with hashkey {1}".format(\r
- server.server_id, hashkey.hashkey))\r
+ server.server_id, server.hashkey))\r
\r
return server\r
\r