Feat: session table and interface
This commit is contained in:
3
Makefile
3
Makefile
@ -39,3 +39,6 @@ migrate-up:
|
|||||||
|
|
||||||
migrate-down:
|
migrate-down:
|
||||||
migrate -database 'sqlite3://gralias.db' -path migrations down
|
migrate -database 'sqlite3://gralias.db' -path migrations down
|
||||||
|
|
||||||
|
install-migrate:
|
||||||
|
go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
|
||||||
|
BIN
gralias.db
BIN
gralias.db
Binary file not shown.
@ -21,15 +21,12 @@ var (
|
|||||||
func StartTurnTimer(roomID string, duration time.Duration) {
|
func StartTurnTimer(roomID string, duration time.Duration) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
if _, exists := timers[roomID]; exists {
|
if _, exists := timers[roomID]; exists {
|
||||||
return // Timer already running
|
return // Timer already running
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
timers[roomID] = &roomTimer{ticker: ticker, done: done}
|
timers[roomID] = &roomTimer{ticker: ticker, done: done}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -20,7 +20,7 @@ CREATE TABLE rooms (
|
|||||||
CREATE TABLE players (
|
CREATE TABLE players (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
room_id TEXT, -- nullable
|
room_id TEXT, -- nullable
|
||||||
username TEXT NOT NULL,
|
username TEXT NOT NULL UNIQUE,
|
||||||
team TEXT NOT NULL DEFAULT '', -- 'red' or 'blue'
|
team TEXT NOT NULL DEFAULT '', -- 'red' or 'blue'
|
||||||
role TEXT NOT NULL DEFAULT '', -- 'guesser' or 'mime'
|
role TEXT NOT NULL DEFAULT '', -- 'guesser' or 'mime'
|
||||||
is_bot BOOLEAN NOT NULL DEFAULT FALSE,
|
is_bot BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
@ -67,3 +67,12 @@ CREATE TABLE settings (
|
|||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (room_id) REFERENCES rooms(id)
|
FOREIGN KEY (room_id) REFERENCES rooms(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE sessions(
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
lifetime INTEGER NOT NULL DEFAULT 3600,
|
||||||
|
cookie_token TEXT NOT NULL DEFAULT '', -- encoded value
|
||||||
|
username TEXT NOT NULL,
|
||||||
|
FOREIGN KEY (username) REFERENCES players(username)
|
||||||
|
);
|
||||||
|
@ -6,20 +6,25 @@ import (
|
|||||||
|
|
||||||
// each session contains the username of the user and the time at which it expires
|
// each session contains the username of the user and the time at which it expires
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Username string
|
ID uint32
|
||||||
CurrentRoom string
|
// CurrentRoom string
|
||||||
Expiry time.Time
|
// Expiry time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
Lifetime uint32 // minutes
|
||||||
|
CookieToken string
|
||||||
|
Username string // username is playerid
|
||||||
}
|
}
|
||||||
|
|
||||||
// we'll use this method later to determine if the session has expired
|
// we'll use this method later to determine if the session has expired
|
||||||
func (s Session) IsExpired() bool {
|
func (s Session) IsExpired() bool {
|
||||||
return s.Expiry.Before(time.Now())
|
return time.Now().After(s.UpdatedAt.Add(time.Minute * time.Duration(s.Lifetime)))
|
||||||
|
// return s.Expiry.Before(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListUsernames(ss map[string]*Session) []string {
|
// func ListUsernames(ss map[string]*Session) []string {
|
||||||
resp := make([]string, 0, len(ss))
|
// resp := make([]string, 0, len(ss))
|
||||||
for _, s := range ss {
|
// for _, s := range ss {
|
||||||
resp = append(resp, s.Username)
|
// resp = append(resp, s.Username)
|
||||||
}
|
// }
|
||||||
return resp
|
// return resp
|
||||||
}
|
// }
|
||||||
|
9
repos/session.go
Normal file
9
repos/session.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package repos
|
||||||
|
|
||||||
|
import "gralias/models"
|
||||||
|
|
||||||
|
type SessionsRepo interface {
|
||||||
|
SessionByToken(token string) (*models.Session, error)
|
||||||
|
SessionCreate(req *models.Session) error
|
||||||
|
SessionUpdateLifetime(minutes uint32) error
|
||||||
|
}
|
Reference in New Issue
Block a user