Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

サーバー側作ってますPR #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module milli-uni-odekakebiyori

go 1.13
3 changes: 3 additions & 0 deletions server/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package handler

func main() {}
21 changes: 21 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"milli-uni-odekakebiyori/model"
)

func main() {
fmt.Println("Hello,world")

game := model.NewGame()

fmt.Println(game.Draw(10))
fmt.Println(game.PlayerJoin(10))
fmt.Println(game.Draw(10))
fmt.Println(game.SetCurrentPlayer(10))
fmt.Println(game.Draw(10))
fmt.Println(game.Stage(10, 1, false))
fmt.Println(game.Stage(10, 0, false))

}
35 changes: 35 additions & 0 deletions server/model/card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package model

// Card だよ
type Card struct {
cardID int
idolID int
}

// CardList はデッキに入る全カードのリストを返します
func CardList() []*Card {
// TODO: 多分いつかいい感じなのになる
// 0-51のidol_idのcardを2枚ずつ作る
list := make([]*Card, 0, 104)
for i := 0; i < 104; i++ {
card := &Card{
cardID: i,
idolID: i % 52,
}
list = append(list, card)
}
return list
}

// Cards は複数カードの操作を扱います
type Cards []*Card

// FindByIdolID はidolIDが一致するカードを1枚返します
func (c Cards) FindByIdolID(idolID int) *Card {
for _, card := range c {
if card.idolID == idolID {
return card
}
}
return nil
}
36 changes: 36 additions & 0 deletions server/model/deck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package model

import "errors"

var (
// ErrDeckEmpty はデッキが空なことを示すエラーです
ErrDeckEmpty = errors.New("error: deck empty")
)

// Deck だよ
type Deck struct {
deck []*Card
}

// NewDeck は新しいDeckを作成して返します
func NewDeck() *Deck {
// TODO: シャッフルしてデッキセット
return &Deck{
deck: CardList(),
}
}

// List だよ
func (d *Deck) List() []*Card {
return d.deck
}

// Draw だよ
func (d *Deck) Draw() (*Card, int, error) {
if len(d.deck) <= 0 {
return nil, 0, ErrDeckEmpty
}
drawCard := d.deck[0]
d.deck = d.deck[1:]
return drawCard, len(d.deck), nil
}
113 changes: 113 additions & 0 deletions server/model/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package model

import (
"errors"
"fmt"
)

// PlayerAction はプレイヤーの1行動を表します
type PlayerAction struct {
PlayerID int
CardID int
ActionType int
}

// StageCard はプレイヤーが場に出したカードを表します
type StageCard struct {
card *Card
isTap bool
}

// Game は1ゲームが持つ情報を管理します
type Game struct {
PlayerHand map[int]Cards
PlayerStage map[int][]*StageCard
Deck *Deck
Log []*PlayerAction
CurrentPlayer int // 現在のターンのPlayerID
}

// NewGame 月曜日が町にやってくる 桜舞う電車に飛び乗る
func NewGame() *Game {
playerHand := make(map[int]Cards, 4)
playerStage := make(map[int][]*StageCard, 4)
return &Game{
Deck: NewDeck(),
PlayerHand: playerHand,
PlayerStage: playerStage,
}
}

// PlayerJoin はゲームにプレイヤーを登録します
func (g *Game) PlayerJoin(playerID int) error {
if _, ok := g.PlayerHand[playerID]; ok {
return fmt.Errorf("error: playerID:%d was already joined", playerID)
}
if _, ok := g.PlayerStage[playerID]; ok {
return fmt.Errorf("error: playerID:%d was already joined", playerID)
}

g.PlayerHand[playerID] = Cards{}
g.PlayerStage[playerID] = []*StageCard{}

return nil
}

// Draw は現在のターンのプレイヤーがドローします
func (g *Game) Draw(playerID int) ([]*Card, error) {
if playerID != g.CurrentPlayer {
// ターンじゃない
return []*Card{}, fmt.Errorf("error: playerID:%d is not current player", playerID)
}
card, _, err := g.Deck.Draw()
if err != nil {
// 流局
return []*Card{}, errors.New("error: deck was empty")
}
g.PlayerHand[g.CurrentPlayer] = append(g.PlayerHand[g.CurrentPlayer], card)
return g.PlayerHand[g.CurrentPlayer], nil
}

func (g *Game) SetCurrentPlayer(playerID int) error {
if _, ok := g.PlayerHand[playerID]; !ok {
return fmt.Errorf("error: playerID:%d not found in this game player stage", playerID)
}
if _, ok := g.PlayerStage[playerID]; !ok {
return fmt.Errorf("error: playerID:%d not found in this game player stage", playerID)
}

g.CurrentPlayer = playerID
return nil
}

// Stage は手札のアイドルを場に出します
func (g *Game) Stage(playerID, idolID int, isTap bool) ([]*StageCard, error) {
hands, ok := g.PlayerHand[playerID]
if !ok {
// このゲームのプレイヤーちゃうで
return []*StageCard{}, fmt.Errorf("error: playerID:%d not found in this game player hands", playerID)
}

stage, ok := g.PlayerStage[playerID]
if !ok {
// このゲームのプレイヤーちゃうで
return []*StageCard{}, fmt.Errorf("error: playerID:%d not found in this game player stage", playerID)
}

if playerID != g.CurrentPlayer {
// ターンじゃない
return []*StageCard{}, fmt.Errorf("error: playerID:%d is not current player", playerID)
}

card := hands.FindByIdolID(idolID)
if card == nil {
// そのカード持ってないで
return []*StageCard{}, fmt.Errorf("error: playerID:%d idolID:%d not found int hand", playerID, idolID)
}

g.PlayerStage[playerID] = append(stage, &StageCard{
card: card,
isTap: isTap,
})
return g.PlayerStage[playerID], nil
}
3 changes: 3 additions & 0 deletions server/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package model

func main() {}