Skip to content

Commit

Permalink
Merge pull request #127 from KazeNoYumeX/feature/add-start-game
Browse files Browse the repository at this point in the history
Feat: Added the Start Game API with the GAAS
  • Loading branch information
KazeNoYumeX authored Mar 22, 2024
2 parents 4c053c0 + ed1009b commit d7accdd
Show file tree
Hide file tree
Showing 46 changed files with 792 additions and 491 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-go-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: Env handle
working-directory: ./Backend
run: cp env .env
run: cp .env.example .env

- name: Install dependencies
working-directory: ./Backend
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:

- name: Env handle
working-directory: ./Backend
run: cp env .env
run: cp .env.example .env

- name: Wait for MySQL
run: |
Expand Down
17 changes: 17 additions & 0 deletions Backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# |--------------------------------------------------------------------------
# | 基本設定 Basic
# |--------------------------------------------------------------------------
APP_NAME="風聲-黑名單"
APP_VERSION="v1"
APP_FRONTEND_URL="https://messagefrontenddev.zeabur.app/"
APP_BACKEND_URL="https://messagebackenddev.zeabur.app/api/"

# |--------------------------------------------------------------------------
# | 資料庫設定 Database
# |--------------------------------------------------------------------------
DB_HOST=127.0.0.1
DB_DATABASE=test
DB_USER=user
DB_PASSWORD=P@ssw0rd!
DB_PORT=3306
DB_ROOT_PASSWORD=P@ssw0rd!
32 changes: 14 additions & 18 deletions Backend/cmd/app/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const docTemplate = `{
}
}
},
"/api/v1/heartbeat": {
"/api/v1/health": {
"get": {
"description": "Check if the server is alive",
"consumes": [
Expand Down Expand Up @@ -159,15 +159,6 @@ const docTemplate = `{
"schema": {
"$ref": "#/definitions/request.PlayCardRequest"
}
},
{
"description": "Intelligence Type",
"name": "intelligence_type",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/request.PlayCardRequest"
}
}
],
"responses": {
Expand Down Expand Up @@ -288,22 +279,26 @@ const docTemplate = `{
},
"request.CreateGameRequest": {
"type": "object",
"required": [
"players",
"roomId"
],
"properties": {
"players": {
"type": "array",
"items": {
"$ref": "#/definitions/request.PlayerInfo"
}
},
"roomId": {
"type": "string"
}
}
},
"request.CreateGameResponse": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"token": {
"url": {
"type": "string"
}
}
Expand All @@ -313,9 +308,6 @@ const docTemplate = `{
"properties": {
"card_id": {
"type": "integer"
},
"intelligence_type": {
"type": "integer"
}
}
},
Expand All @@ -341,11 +333,15 @@ const docTemplate = `{
},
"request.PlayerInfo": {
"type": "object",
"required": [
"id",
"nickname"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"nickname": {
"type": "string"
}
}
Expand Down
2 changes: 1 addition & 1 deletion Backend/cmd/migrate/game_card_seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import (

func main() {
db := config.NewDatabase()
seeders.Run(db)
seeders.OnlyCardsRun(db)
}
14 changes: 14 additions & 0 deletions Backend/cmd/migrate/game_seeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build migrate

package main

import (
"github.com/Game-as-a-Service/The-Message/config"
"github.com/Game-as-a-Service/The-Message/database/seeders"
_ "github.com/joho/godotenv/autoload"
)

func main() {
db := config.NewDatabase()
seeders.Run(db)
}
11 changes: 0 additions & 11 deletions Backend/config/test_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,8 @@ import (
"os"

"github.com/joho/godotenv"
"gorm.io/gorm"
)

func InitTestDB() *gorm.DB {
_, err := LoadEnvPath()
if err != nil {
return nil
}

db := NewDatabase()
return db
}

func LoadEnvPath() (string, error) {
envPath, err := getEnvPath()

Expand Down
8 changes: 4 additions & 4 deletions Backend/database/migrations/000001_create_games_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ BEGIN;
CREATE TABLE games
(
id INT AUTO_INCREMENT PRIMARY KEY,
token LONGTEXT NOT NULL,
status VARCHAR(10) NOT NULL,
room_id VARCHAR(100) NOT NULL,
status VARCHAR(10) NOT NULL,
current_player_id INT,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ CREATE TABLE players
(
id INT AUTO_INCREMENT PRIMARY KEY,
game_id INT NOT NULL,
user_id VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
order_number INT NOT NULL,
priority TINYINT NOT NULL,
identity_card VARCHAR(255) NOT NULL,
status VARCHAR(10) NOT NULL,
created_at DATETIME NOT NULL,
Expand Down
13 changes: 7 additions & 6 deletions Backend/database/migrations/000003_create_cards_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ BEGIN;

CREATE TABLE cards
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
color VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
color VARCHAR(255) NOT NULL,
intelligence_type TINYINT COMMENT '1 密電, 2 直達, 3 文件',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

COMMIT;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BEGIN;

DROP TABLE IF EXISTS deck_cards;
DROP TABLE IF EXISTS decks;

COMMIT;
14 changes: 13 additions & 1 deletion Backend/database/migrations/000004_create_decks_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ CREATE TABLE decks
(
id INT AUTO_INCREMENT PRIMARY KEY,
game_id INT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME,
FOREIGN KEY (game_id) REFERENCES games (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE deck_cards
(
id INT AUTO_INCREMENT PRIMARY KEY,
deck_id INT NOT NULL,
card_id INT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME
deleted_at DATETIME,
FOREIGN KEY (deck_id) REFERENCES decks (id),
FOREIGN KEY (card_id) REFERENCES cards (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

COMMIT;
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ CREATE TABLE player_cards
(
id INT AUTO_INCREMENT PRIMARY KEY,
player_id INT NOT NULL,
game_id INT NOT NULL,
card_id INT NOT NULL,
type VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
deleted_at DATETIME,
FOREIGN KEY (player_id) REFERENCES players (id),
FOREIGN KEY (game_id) REFERENCES games (id),
FOREIGN KEY (card_id) REFERENCES cards (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Expand Down

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions Backend/database/seeders/database_seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ import (

func Run(db *gorm.DB) {
SeederCards(db)
SeederGameWithPlayers(db)
}

func OnlyCardsRun(db *gorm.DB) {
SeederCards(db)
}
46 changes: 46 additions & 0 deletions Backend/database/seeders/game_seeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seeders

import (
"math/rand"

"github.com/Game-as-a-Service/The-Message/enums"
"github.com/Game-as-a-Service/The-Message/service/repository"
"github.com/go-faker/faker/v4"
"gorm.io/gorm"
)

func SeederGameWithPlayers(db *gorm.DB) {
// Get all cards
var cards []*repository.Card
_ = db.Find(&cards)

// Fake game data
game := &repository.Game{
RoomID: faker.UUIDDigit(),
Status: enums.ActionCardStage,
Players: []repository.Player{},
}

// Fake player count random 1~3
playerCount := rand.Intn(3) + 1

// Fake players data
for i := 0; i < playerCount; i++ {
player := repository.Player{
UserID: faker.UUIDDigit(),
Name: faker.FirstName(),
PlayerCards: []repository.PlayerCard{},
}

// Each player gets 3 cards
for j := 0; j < 3; j++ {
player.PlayerCards = append(player.PlayerCards, repository.PlayerCard{
CardID: cards[i*3+j].ID,
Type: "hand",
})
}
game.Players = append(game.Players, player)
}

_ = db.Create(&game)
}
6 changes: 0 additions & 6 deletions Backend/env

This file was deleted.

19 changes: 10 additions & 9 deletions Backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ go 1.22
require (
github.com/gin-gonic/gin v1.9.1
github.com/go-faker/faker/v4 v4.3.0
github.com/go-sql-driver/mysql v1.7.1
github.com/go-sql-driver/mysql v1.8.0
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
gorm.io/driver/mysql v1.5.4
gorm.io/gorm v1.25.7
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.8
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.11.2 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.20.3 // indirect
github.com/go-openapi/jsonreference v0.20.5 // indirect
github.com/go-openapi/spec v0.20.15 // indirect
github.com/go-openapi/swag v0.22.10 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
Expand All @@ -45,7 +46,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
Expand Down
Loading

0 comments on commit d7accdd

Please sign in to comment.