-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
53 lines (45 loc) · 1.03 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package domain
import (
"database/sql"
"github.com/google/uuid"
"time"
)
type Game struct {
ID uuid.UUID `gorm:"primary_key;size:36;<-:create"`
ChatID int64
Chat Chat
OwnerID int64
Owner User
Gunslingers []*Gunslinger
CreatedAt time.Time
PlayedAt sql.NullTime
}
type GameRepository interface {
GetLatestForChat(int64) (Game, error)
GetActiveForChat(int64) (Game, error)
Store(*Game) error
Update(*Game) error
HasActiveOrCreatedTodayInChat(id int64) bool
}
func NewGame(chatID int64, ownerID int64) *Game {
ID := uuid.New()
gunslinger := NewGunslinger(ID, ownerID)
game := &Game{
ID: ID,
ChatID: chatID,
OwnerID: ownerID,
Gunslingers: []*Gunslinger{gunslinger},
CreatedAt: time.Now(),
PlayedAt: sql.NullTime{Time: time.Now()},
}
gunslinger.Game = game
return game
}
func (g *Game) IsPlayed() bool {
return g.PlayedAt.Valid == true
}
func (g *Game) MarkAsPlayed() {
if !g.IsPlayed() {
g.PlayedAt = sql.NullTime{Time: time.Now(), Valid: true}
}
}