summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-04-18 21:25:32 +0200
committerSanto Cariotti <santo@dcariotti.me>2025-04-18 21:25:32 +0200
commit42d68aa99d59339dbdf928a54c28242635728daa (patch)
tree98dadfd64a0fc05d1fb6f6ddbc9a3e8963fbf1dd /internal
parent7c5a6176b27b6b0c0c3ef8a4aedbdec871391a80 (diff)
Restore a game
Diffstat (limited to 'internal')
-rw-r--r--internal/api/database/models.go23
-rw-r--r--internal/api/handlers/handlers.go31
2 files changed, 34 insertions, 20 deletions
diff --git a/internal/api/database/models.go b/internal/api/database/models.go
index cd0d12d..4acbca5 100644
--- a/internal/api/database/models.go
+++ b/internal/api/database/models.go
@@ -11,15 +11,16 @@ type User struct {
}
type Game struct {
- ID int `json:"id"`
- Player1ID int `json:"-"`
- Player1 User `gorm:"foreignKey:Player1ID" json:"player1"`
- Player2ID *int `json:"-"`
- Player2 *User `gorm:"foreignKey:Player2ID;null" json:"player2"`
- Name string `json:"name"`
- IP1 string `json:"ip1"`
- IP2 string `json:"ip2"`
- Outcome string `json:"outcome"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
+ ID int `json:"id"`
+ Player1ID int `json:"-"`
+ Player1 User `gorm:"foreignKey:Player1ID" json:"player1"`
+ Player2ID *int `json:"-"`
+ Player2 *User `gorm:"foreignKey:Player2ID;null" json:"player2"`
+ Name string `json:"name"`
+ IP1 string `json:"ip1"`
+ IP2 string `json:"ip2"`
+ Outcome string `json:"outcome"`
+ LastPlayer int `json:"last_player"` // Last player entered in game
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
}
diff --git a/internal/api/handlers/handlers.go b/internal/api/handlers/handlers.go
index c8b7425..db42f77 100644
--- a/internal/api/handlers/handlers.go
+++ b/internal/api/handlers/handlers.go
@@ -118,12 +118,13 @@ func NewPlay(w http.ResponseWriter, r *http.Request) {
name := p2p.NewSession()
play := database.Game{
- Player1ID: claims.UserID,
- Player2ID: nil,
- Name: name,
- IP1: payload.IP,
- IP2: "",
- Outcome: "*",
+ Player1ID: claims.UserID,
+ Player2ID: nil,
+ Name: name,
+ IP1: payload.IP,
+ IP2: "",
+ Outcome: "*",
+ LastPlayer: 1,
}
if result := db.Create(&play); result.Error != nil {
@@ -158,13 +159,25 @@ func EnterGame(w http.ResponseWriter, r *http.Request) {
var game database.Game
- if result := db.Where("name = ? AND player2_id IS NULL", payload.Name).First(&game); result.Error != nil {
+ if result := db.Where("name = ?", payload.Name).First(&game); result.Error != nil {
JsonError(&w, result.Error.Error())
return
}
- game.Player2ID = &claims.UserID
- game.IP2 = payload.IP
+ if game.Player2ID == nil {
+ game.Player2ID = &claims.UserID
+ game.IP2 = payload.IP
+ game.LastPlayer = 2
+ } else {
+ if game.Player1ID == claims.UserID {
+ game.IP1 = payload.IP
+ game.LastPlayer = 1
+ } else {
+ game.IP2 = payload.IP
+ game.LastPlayer = 2
+ }
+ }
+
game.UpdatedAt = time.Now()
if err := db.Save(&game).Error; err != nil {