Models related to games.
"""
-from calendar import timegm
+from xonstat.models.mixins import FuzzyDateMixin, EpochMixin
+from xonstat.util import strip_colors, html_colors
-from xonstat.util import pretty_date, strip_colors, html_colors
-
-class Game(object):
+class Game(FuzzyDateMixin, EpochMixin):
"""
An individual game.
"""
- def __init__(self, game_id=None, start_dt=None, game_type_cd=None,
- server_id=None, map_id=None, winner=None):
+ def __init__(self, game_id=None, start_dt=None, game_type_cd=None, server_id=None, map_id=None,
+ winner=None):
self.game_id = game_id
self.start_dt = start_dt
self.game_type_cd = game_type_cd
self.winner = winner
def __repr__(self):
- return ("<Game({0.game_id}, {0.start_dt}, {0.game_type_cd}, {0.server_id})>".format(self))
+ return "<Game({0.game_id}, {0.start_dt}, {0.game_type_cd}, {0.server_id})>".format(self)
def to_dict(self):
return {
'server_id': self.server_id
}
- def fuzzy_date(self):
- return pretty_date(self.start_dt)
-
- def epoch(self):
- return timegm(self.start_dt.timetuple())
-
class PlayerGameStat(object):
"""
from calendar import timegm
+from xonstat.models.mixins import FuzzyDateMixin, EpochMixin
from xonstat.util import pretty_date, strip_colors, html_colors
-class Map(object):
+class Map(FuzzyDateMixin, EpochMixin):
"""
A playable map. Roughly equivalent to a pk3 file, but distinguished by name instead.
"""
'version': self.version,
}
- def fuzzy_date(self):
- return pretty_date(self.create_dt)
-
- def epoch(self):
- return timegm(self.create_dt.timetuple())
-
# TODO: investigate if this model is truly a model, or really just a query (i.e. namedtuple)
class MapCapTime(object):
--- /dev/null
+"""
+Mixins for methods used by several model classes.
+"""
+from calendar import timegm
+from xonstat.util import pretty_date, html_colors
+
+
+class FuzzyDateMixin(object):
+ """Provides a class with a "create_dt" attribute the ability to return a fuzzy date."""
+
+ def fuzzy_date(self):
+ return pretty_date(self.create_dt)
+
+
+class EpochMixin(object):
+ """Provides a class with a "create_dt" attribute the ability to return the epoch time."""
+
+ def epoch(self):
+ return timegm(self.create_dt.timetuple())
+
+
+class NickColorsMixin(object):
+ """Provides a class with a "nick" attribute the ability to return the nick's HTML colors."""
+
+ def nick_html_colors(self, limit=None):
+ if self.nick is None:
+ return "Anonymous Player"
+ else:
+ return html_colors(self.nick, limit)
from calendar import timegm
-from xonstat.util import html_colors, strip_colors, pretty_date, qfont_decode
+from xonstat.models.mixins import FuzzyDateMixin, EpochMixin, NickColorsMixin
+from xonstat.util import strip_colors, pretty_date, qfont_decode
-class Player(object):
+class Player(EpochMixin, NickColorsMixin):
"""
A player, which can represent either a human or a bot.
"""
- def nick_html_colors(self, limit=None):
- if self.nick is None:
- return "Anonymous Player"
- else:
- return html_colors(self.nick, limit)
-
def nick_strip_colors(self):
if self.nick is None:
return "Anonymous Player"
else:
return strip_colors(self.nick)
+ # TODO: use FuzzyDateMixin instead, but change the method calls
def joined_pretty_date(self):
return pretty_date(self.create_dt)
'stripped_nick': qfont_decode(self.stripped_nick),
}
- def epoch(self):
- return timegm(self.create_dt.timetuple())
-
class Achievement(object):
"""
}
-class PlayerRank(object):
+class PlayerRank(NickColorsMixin):
"""
A player's rank for a given game type.
"""
- def nick_html_colors(self, limit=None):
- if self.nick is None:
- return "Anonymous Player"
- else:
- return html_colors(self.nick, limit)
-
def __repr__(self):
return ("<PlayerRank(pid={0.player_id}, gametype={0.game_type_cd}, rank={0.rank})>"
.format(self))
}
-class PlayerCaptime(object):
+class PlayerCaptime(FuzzyDateMixin, EpochMixin):
"""
A flag capture time for a player on a given map.
"""
def __repr__(self):
return "<PlayerCaptime(pid={0.player_id}, map_id={0.map_id}, mod={0.mod})>".format(self)
- def fuzzy_date(self):
- return pretty_date(self.create_dt)
-
- def epoch(self):
- return timegm(self.create_dt.timetuple())
-
class PlayerGroups(object):
"""
Models related to servers.
"""
-from calendar import timegm
from datetime import datetime as dt
-from xonstat.util import pretty_date
+from xonstat.models.mixins import FuzzyDateMixin, EpochMixin
-class Server(object):
+class Server(FuzzyDateMixin, EpochMixin):
"""
A Xonotic server, identifiable by name and (when there's a conflict) hashkey.
"""
'ip_addr': self.ip_addr,
'location': self.location,
}
-
- def fuzzy_date(self):
- return pretty_date(self.create_dt)
-
- def epoch(self):
- return timegm(self.create_dt.timetuple())
\ No newline at end of file