DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

9b88fcfc675eca89a4b645a7a24650233c87f03a — Jonni Liljamo 1 year, 10 months ago 13e933c
feat(sdbapi): GameData and Action tables
4 files changed, 58 insertions(+), 17 deletions(-)

M sdbapi/db/db.go
M sdbapi/models/action.go
M sdbapi/models/game.go
A sdbapi/models/gamedata.go
M sdbapi/db/db.go => sdbapi/db/db.go +2 -0
@@ 32,4 32,6 @@ func Migrate() {
	log.Println("running migrations")
	DbConn.AutoMigrate(&models.User{})
	DbConn.AutoMigrate(&models.Game{})
	DbConn.AutoMigrate(&models.Action{})
	DbConn.AutoMigrate(&models.GameData{})
}

M sdbapi/models/action.go => sdbapi/models/action.go +9 -7
@@ 11,15 11,17 @@ package models
import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

type Action struct {
	ID        string `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
	Game      Game           `json:"game" gorm:"foreignkey:ID"`
	Invoker   User           `json:"invoker" gorm:"foreignkey:ID"`
	Timestamp time.Time      `json:"timestamp" gorm:"type:timestamptz"`
	ID         string         `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
	CreatedAt  time.Time      `json:"created_at"`
	UpdatedAt  time.Time      `json:"updated_at"`
	DeletedAt  gorm.DeletedAt `json:"-" gorm:"index"`
	GameDataID string         `json:"game_data_id"`
	Invoker    string         `json:"invoker"`
	Data       datatypes.JSON `json:"data"`
	Timestamp  time.Time      `json:"timestamp" gorm:"type:timestamptz"`
}

M sdbapi/models/game.go => sdbapi/models/game.go +12 -10
@@ 22,14 22,16 @@ const (
)

type Game struct {
	ID        string         `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
	HostID    string         `json:"host_id"`
	Host      User           `json:"host" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	GuestID   string         `json:"guest_id" gorm:"default:NULL;"`
	Guest     User           `json:"guest" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	State     uint8          `json:"state" gorm:"type:smallint"`
	EndedAt   time.Time      `json:"ended_at" gorm:"type:timestamptz"`
	ID         string         `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
	CreatedAt  time.Time      `json:"created_at"`
	UpdatedAt  time.Time      `json:"updated_at"`
	DeletedAt  gorm.DeletedAt `json:"-" gorm:"index"`
	HostID     string         `json:"host_id"`
	Host       User           `json:"host" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	GuestID    string         `json:"guest_id" gorm:"default:NULL;"`
	Guest      User           `json:"guest" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	State      uint8          `json:"state" gorm:"type:smallint"`
	EndedAt    time.Time      `json:"ended_at" gorm:"type:timestamptz"`
	GameDataID string         `json:"game_data_id" gorm:"default:NULL;"`
	GameData   User           `json:"game_data" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

A sdbapi/models/gamedata.go => sdbapi/models/gamedata.go +35 -0
@@ 0,0 1,35 @@
/*
 * This file is part of sdbapi
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

package models

import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

const (
	GAMETURN_HOST  uint8 = 0
	GAMETURN_GUEST uint8 = 1
)

type GameData struct {
	ID        string         `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
	Actions   []Action       `json:"actions" gorm:"foreignKey:GameDataID"`
	Turn      uint8          `json:"turn" gorm:"type:smallint"`
	// NOTE: These are not final
	HostHand  datatypes.JSON `json:"host_hand"`
	HostDeck  datatypes.JSON `json:"host_deck"`
	GuestHand datatypes.JSON `json:"guest_hand"`
	GuestDeck datatypes.JSON `json:"guest_deck"`
}