104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (c) 2017 Tomek Wójcik <tomek@bthlabs.pl>
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
# THE SOFTWARE.
|
|
#
|
|
|
|
"""
|
|
q3stats.models.game
|
|
===================
|
|
|
|
This module contains the ``Game`` model.
|
|
"""
|
|
|
|
import uuid
|
|
|
|
import sqlalchemy as sa
|
|
import sqlalchemy.dialects.postgresql as pg
|
|
|
|
from q3stats.database import Base
|
|
from q3stats.lib import defs
|
|
|
|
|
|
class Game(Base):
|
|
"""The ``Game`` model."""
|
|
__tablename__ = 'games'
|
|
__table_args__ = (
|
|
sa.UniqueConstraint('uuid'),
|
|
)
|
|
|
|
#: Format string for UUID URL string
|
|
UUID_UID_FORMAT = 'pl.bthlabs.q3stats.{date}.{time}.{map}'
|
|
|
|
#: ``id INT PRIMARY KEY``
|
|
id = sa.Column(sa.Integer(), primary_key=True)
|
|
|
|
#: ``uuid VARCHAR(36)``
|
|
uuid = sa.Column(sa.String(255))
|
|
|
|
#: ``map VARCHAR(255)``
|
|
map = sa.Column(sa.String(255), index=True)
|
|
|
|
#: ``date DATE``
|
|
date = sa.Column(sa.Date(), index=True)
|
|
|
|
#: ``time TIME``
|
|
time = sa.Column(sa.Time())
|
|
|
|
#: ``fraglimit INT``
|
|
fraglimit = sa.Column(sa.Integer())
|
|
|
|
#: ``attrs JSON``
|
|
attrs = sa.Column(pg.JSON())
|
|
|
|
#: List of :py:class:`q3stats.models.score.Score` objects
|
|
scores = sa.orm.relationship(
|
|
'Score', backref=sa.orm.backref('game'), order_by='Score.score.desc()'
|
|
)
|
|
|
|
def update_uuid(self):
|
|
self.uuid = str(uuid.uuid3(
|
|
uuid.NAMESPACE_OID,
|
|
self.UUID_UID_FORMAT.format(
|
|
date=self.date.strftime(defs.DATE_FORMAT),
|
|
time=self.time.strftime(defs.TIME_FORMAT),
|
|
map=self.map
|
|
)
|
|
))
|
|
|
|
def to_json(self, recursive=False):
|
|
"""Returns dictionary representation suitable for serializing to JSON.
|
|
|
|
If *recursive* is ``True`` then the associated score objects will be
|
|
included."""
|
|
result = {
|
|
'id': self.id,
|
|
'uuid': self.uuid,
|
|
'map': self.map,
|
|
'date': self.date.strftime(defs.DATE_FORMAT),
|
|
'time': self.time.strftime(defs.TIME_FORMAT),
|
|
'attrs': dict(self.attrs),
|
|
'scores': None
|
|
}
|
|
|
|
if recursive:
|
|
result['scores'] = [x.to_json() for x in self.scores]
|
|
|
|
return result
|