From a8442675de67458416b5b2c2f9d69524de1456b7 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Thu, 31 Mar 2011 17:39:49 -0400 Subject: [PATCH] Port models from old Pyramid project. Reflect all tables. --- xonstat/models.py | 232 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) diff --git a/xonstat/models.py b/xonstat/models.py index ec3d896..567d7e1 100644 --- a/xonstat/models.py +++ b/xonstat/models.py @@ -1,4 +1,7 @@ +import datetime +import logging import sqlahelper +import sqlalchemy import transaction from sqlalchemy import Column @@ -8,6 +11,7 @@ from sqlalchemy import Unicode from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import mapper from sqlalchemy.orm import scoped_session from sqlalchemy.orm import sessionmaker @@ -15,7 +19,235 @@ Engine=sqlahelper.get_engine() DBSession = scoped_session(sessionmaker()) Base = declarative_base() +# setup logging +log = logging.getLogger(__name__) + +# reflect all the tables +MetaData = sqlalchemy.MetaData(bind=Engine, reflect=True) + +# assign all those tables to an object +achievements_table = MetaData.tables['achievements'] +cd_achievement_table = MetaData.tables['cd_achievement'] +cd_game_type_table = MetaData.tables['cd_game_type'] +cd_mutator_table = MetaData.tables['cd_mutator'] +cd_weapon_table = MetaData.tables['cd_weapon'] +db_version_table = MetaData.tables['db_version'] +game_mutators_table = MetaData.tables['game_mutators'] +games_table = MetaData.tables['games'] +hashkeys_table = MetaData.tables['hashkeys'] +map_game_types_table = MetaData.tables['map_game_types'] +maps_table = MetaData.tables['maps'] +player_game_stats_table = MetaData.tables['player_game_stats'] +players_table = MetaData.tables['players'] +player_weapon_stats_table = MetaData.tables['player_weapon_stats'] +servers_table = MetaData.tables['servers'] + +# define objects for all tables +class Player(object): + def __init__(self, player_id=None, nick=None, create_dt=datetime.datetime.now(), + location = None): + self.player_id = player_id + self.nick = nick + self.create_dt = create_dt + self.location = location + + def __repr__(self): + return "" % (self.player_id, self.nick, + self.create_dt, self.location) + +class Mutator(object): + def __init__(self, mutator_cd, name=None, descr=None, active_ind='Y'): + self.mutator_cd = mutator_cd + self.name = name + self.descr = descr + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.mutator_cd, self.name, + self.descr, self.active_ind) + +class GameType(object): + def __init__(self, game_type_cd, descr=None, active_ind='Y'): + self.game_type_cd = game_type_cd + self.descr = descr + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.game_type_cd, self.descr, + self.active_ind) + +class Weapon(object): + def __init__(self, weapon_cd=None, descr=None, active_ind='Y'): + self.weapon_cd = weapon_cd + self.descr = descr + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.weapon_cd, self.descr, + self.active_ind) + +class Server(object): + def __init__(self, server_id=None, name=None, location=None, ip_addr=None, + max_players=None, create_dt=datetime.datetime.now(), + pure_ind='Y', active_ind='Y'): + self.server_id = server_id + self.name = name + self.location = location + self.ip_addr = ip_addr + self.max_players = max_players + self.create_dt = create_dt + self.pure_ind = pure_ind + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.server_id, self.name, + self.ip_addr) + +class Map(object): + def __init__(self, map_id=None, name=None, version=None, pk3_name=None, + curl_url=None): + self.map_id = map_id + self.name = name + self.version = version + self.pk3_name = pk3_name + self.curl_url = curl_url + + def __repr__(self): + return "" % (self.map_id, self.name, self.version) + +class MapGameType(object): + def __init__(self, map_id=None, game_type_cd=None): + self.map_id = map_id + self.game_type_cd = game_type_cd + + def __repr__(self): + return "" % (self.map_id, self.game_type_cd) + +class Game(object): + def __init__(self, game_id=None, start_dt=datetime.datetime.now(), + game_type_cd=None, server_id=None, map_id=None, duration=None, + winner=None, create_dt=datetime.datetime.now()): + self.game_id = game_id + self.start_dt = start_dt + self.game_type_cd = game_type_cd + self.server_id = server_id + self.map_id = map_id + self.duration = duration + self.winner = winner + self.create_dt = create_dt + + def __repr__(self): + return "" % (self.game_id, self.start_dt, + self.game_type_cd, self.server_id) + +class PlayerGameStat(object): + def __init__(self, player_id=None, game_id=None, create_dt=None, + stat_type=None, team=None, kills=None, deaths=None, suicides=None, + score=None, time=None, held=None, captures=None, pickups=None, + carrier_frags=None, drops=None, returns=None, collects=None, + destroys=None, destroys_holding_key=None, pushes=None, + pushed=None, nick=None): + self.player_id = player_id + self.game_id = game_id + self.create_dt = create_dt + self.stat_type = stat_type + self.nick = nick + self.team = team + self.kills = kills + self.deaths = deaths + self.suicides = suicides + self.score = score + self.time = time + self.held = held + self.captures = captures + self.pickups = pickups + self.carrier_frags = carrier_frags + self.drops = drops + self.returns = returns + self.collects = collects + self.destroys = destroys + self.destroys_holding_key = destroys_holding_key + self.pushes = pushes + self.pushed = pushed + + def __repr__(self): + return "" \ + % (self.player_id, self.game_id, self.create_dt, self.stat_type) + +class GameMutator(object): + def __init__(self, game_id=None, mutator_cd=None): + self.game_id = game_id + self.mutator_cd = mutator_cd + + def __repr__(self): + return "" % (self.game_id, self.mutator_cd) + +class Achievement(object): + def __init__(self, achievement_cd=None, descr=None, limit=None, active_ind='Y'): + self.achievement_cd = achievement_cd + self.descr = descr + self.limit = limit + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.achievement_cd, self.descr, self.limit) + +class PlayerAchievement(object): + def __init__(self, achievement_id=None, achievement_cd=None, + player_id=None, create_dt=datetime.datetime.now()): + self.achievement_id = achievement_id + self.achievement_cd = achievement_cd + self.player_id = player_id + self.create_dt = create_dt + + def __repr__(self): + return "" % (self.player_id, self.achievement_cd) + +class PlayerWeaponStat(object): + def __init__(self, player_weapon_stat_id=None, + create_dt=datetime.datetime.now(), player_id=None, + game_id=None, weapon_cd=None, actual=None, max=None, frags=None): + self.player_weapon_stat_id = player_weapon_stat_id + self.create_dt = create_dt + self.player_id = player_id + self.game_id = game_id + self.weapon_cd = weapon_cd + self.actual = actual + self.max = max + self.frags = frags + + def __repr__(self): + return "" % (self.player_weapon_stat_id, + self.player_id, self.game_id) + +class Hashkey(object): + def __init__(self, player_id=None, hashkey=None, active_ind='Y', + create_dt=datetime.datetime.now()): + self.player_id = player_id + self.hashkey = hashkey + self.active_ind = active_ind + + def __repr__(self): + return "" % (self.player_id, self.hashkey) + +# now map the tables and the objects together +mapper(PlayerAchievement, achievements_table) +mapper(Achievement, cd_achievement_table) +mapper(GameType, cd_game_type_table) +mapper(Mutator, cd_mutator_table) +mapper(Weapon, cd_weapon_table) +mapper(GameMutator, game_mutators_table) +mapper(Game, games_table) +mapper(Hashkey, hashkeys_table) +mapper(MapGameType, map_game_types_table) +mapper(Map, maps_table) +mapper(PlayerGameStat, player_game_stats_table) +mapper(Player, players_table) +mapper(PlayerWeaponStat, player_weapon_stats_table) +mapper(Server, servers_table) + def initialize_sql(engine): DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all(engine) + -- 2.39.2