From f3b56bb7b737dd124d6432474ee7e47411e8a565 Mon Sep 17 00:00:00 2001 From: "Timofey.Kovalev" Date: Tue, 22 Jun 2021 19:55:53 +0300 Subject: [PATCH] add logout time --- dataBase.go | 6 +++++- handlers.go | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dataBase.go b/dataBase.go index d5ae585..4fb05cb 100644 --- a/dataBase.go +++ b/dataBase.go @@ -200,10 +200,14 @@ func (db *dbLayer) increasePlayerOnlineDuration(ctx context.Context, playerID st return db.execInTransaction(ctx, "UPDATE players SET online_duration = online_duration + $2 WHERE id = $1", playerID, onlineDurationSecond) } -func (db *dbLayer) updatePlayerLastOnline(ctx context.Context, playerID string, lastOnline time.Time) error { +func (db *dbLayer) updatePlayerLastLogin(ctx context.Context, playerID string, lastOnline time.Time) error { return db.execInTransaction(ctx, "UPDATE players SET last_login = $2 WHERE id = $1", playerID, lastOnline.Format(time.RFC3339)) } +func (db *dbLayer) updatePlayerLastLogout(ctx context.Context, playerID string, lastOnline time.Time) error { + return db.execInTransaction(ctx, "UPDATE players SET last_logout = $2 WHERE id = $1", playerID, lastOnline.Format(time.RFC3339)) +} + func (db *dbLayer) increasePlayerDeath(ctx context.Context, playerID string, deaths int) error { return db.execInTransaction(ctx, "UPDATE players SET deaths = deaths + $2 WHERE id = $1", playerID, deaths) } diff --git a/handlers.go b/handlers.go index 336e901..2257daa 100644 --- a/handlers.go +++ b/handlers.go @@ -70,7 +70,7 @@ func (c *joinCommand) run(ctx context.Context) { return } } else { - err = db.updatePlayerLastOnline(ctx, p.id, time.Now()) + err = db.updatePlayerLastLogin(ctx, p.id, time.Now()) if err != nil { log.Error(err) return @@ -111,6 +111,12 @@ func (c *quitCommand) run(ctx context.Context) { return } + err = db.updatePlayerLastLogout(ctx, p.id, time.Now()) + if err != nil { + log.Error(err) + return + } + err = db.increasePlayerOnlineDuration(ctx, p.id, time.Now().Sub(p.lastOnline)) if err != nil { log.Error(err)