Keep track of minutes played per session

This commit is contained in:
Ben 2019-09-09 22:23:21 +01:00
parent 78d042b1a3
commit 2ecd9f1850
4 changed files with 23 additions and 2 deletions

View File

@ -1017,6 +1017,7 @@ CREATE TABLE login (
penguin_id INT NOT NULL,
date TIMESTAMP NOT NULL,
ip_address CHAR(255) NOT NULL,
minutes_played INT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
CONSTRAINT login_ibfk_1 FOREIGN KEY (penguin_id) REFERENCES penguin (id) ON DELETE CASCADE ON UPDATE CASCADE
);
@ -1029,6 +1030,7 @@ COMMENT ON COLUMN login.id IS 'Unique login ID';
COMMENT ON COLUMN login.penguin_id IS 'Login penguin ID';
COMMENT ON COLUMN login.date IS 'Login date';
COMMENT ON COLUMN login.ip_address IS 'Connection IP address';
COMMENT ON COLUMN login.minutes_played IS 'Minutes played for session';
DROP TABLE IF EXISTS penguin_postcard;
CREATE TABLE penguin_postcard (

View File

@ -123,6 +123,15 @@ class Penguin(db.Model):
def safe_nickname(self, language_bitmask):
return self.nickname if self.approval & language_bitmask else "P" + str(self.id)
@property
def minutes_played_today(self):
async def get_minutes():
yesterday = datetime.combine(date.today(), datetime.min.time())
minutes_played_today = await db.select([db.func.sum(Login.minutes_played)]) \
.where((Login.penguin_id == self.id) & (Login.date > yesterday)).gino.scalar()
return minutes_played_today or 0
return get_minutes()
@property
def age(self):
return (datetime.now() - self.registration_date).days
@ -162,3 +171,4 @@ class Login(db.Model):
penguin_id = db.Column(db.ForeignKey('penguin.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
date = db.Column(db.DateTime, nullable=False, server_default=db.text("now()"))
ip_address = db.Column(db.CHAR(255), nullable=False)
minutes_played = db.Column(db.Integer, nullable=False, server_default=db.text("0"))

View File

@ -42,6 +42,7 @@ async def handle_join_server(p, penguin_id: int, login_key: str):
if p.data.character is not None:
p.server.penguins_by_character_id[p.data.character] = p
p.login_timestamp = datetime.now()
p.joined_world = True
server_key = '{}.players'.format(p.server.server_config['Id'])
@ -66,6 +67,14 @@ async def handle_refresh_room(p):
async def handle_disconnect_room(p):
await p.room.remove_penguin(p)
minutes_played = (datetime.now() - p.login_timestamp).total_seconds() / 60.0
await Login.create(penguin_id=p.data.id,
date=p.login_timestamp,
ip_address=p.peer_name[0],
minutes_played=minutes_played)
await p.data.update(minutes_played=p.data.minutes_played + minutes_played).apply()
del p.server.penguins_by_id[p.data.id]
del p.server.penguins_by_username[p.data.username]

View File

@ -8,7 +8,7 @@ class Penguin(Spheniscidae):
__slots__ = ['x', 'y', 'frame', 'toy', 'room', 'waddle', 'table',
'data', 'muted', 'login_key', 'member', 'membership_days',
'avatar', 'walking_puffle', 'permissions', 'active_quests',
'buddy_requests']
'buddy_requests', 'heartbeat', 'login_timestamp',
def __init__(self, *args):
super().__init__(*args)
@ -33,7 +33,7 @@ class Penguin(Spheniscidae):
self.active_quests = None
self.buddy_requests = set()
self.login_timestamp = None
@property
def party_state(self):