model

package
v0.0.0-...-292fe0d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionReady = "ready"
	ActionMove  = "move"
)
View Source
const (
	FitResultAllDie = "bothdie"
	FitResultP1Win  = "p1win"
	FitResultP2Win  = "p2win"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionMsgRaw

type ActionMsgRaw struct {
	Timeout int    `json:"timeout"`
	Action  string `json:"action"` // move(移动棋子), (read)准备
}

type EndRaw

type EndRaw struct {
	WinPlayerId int64 `json:"win_player_id"` // 该赢了
}

游戏结束

type ErrorMsgRaw

type ErrorMsgRaw struct {
	Msg string `json:"msg"`
}

type GameStatusMsgRaw

type GameStatusMsgRaw struct {
	Status RoomStatus `json:"status"`
	RoomId int64      `json:"room_id"`
}

type GetRoomRaw

type GetRoomRaw struct {
	Status       RoomStatus   `json:"status"`
	PlayerStatus PlayerStatus `json:"player_status"`
	TablePieces  TablePieces  `json:"table_pieces"`
}

type JoinRoomMsgRaw

type JoinRoomMsgRaw struct {
	RoomId   int64      `json:"room_id"`
	PlayerId int64      `json:"player_id,omitempty"`
	Camp     string     `json:"camp"`
	Status   RoomStatus `json:"status"`
}

type LeaveRoomMsgRaw

type LeaveRoomMsgRaw struct {
	PlayerId int64 `json:"player_id"`
}

type LoginMsgRaw

type LoginMsgRaw struct {
	PlayerId int64 `json:"player_id"`
}

type Message

type Message struct {
	PlayerId int64           `json:"player_id,omitempty"`
	Type     MessageType     `json:"type"`
	Raw      json.RawMessage `json:"raw"`
}

func NewErrorMsg

func NewErrorMsg(err error) *Message

func (Message) Marshal

func (m Message) Marshal() (bs []byte)

func (*Message) MarshalRaw

func (m *Message) MarshalRaw(i interface{})

func (*Message) Unmarshal

func (m *Message) Unmarshal(bs []byte) (err error)

func (*Message) UnmarshalRaw

func (m *Message) UnmarshalRaw(i interface{}) (err error)

type MessageType

type MessageType string
const (
	Err MessageType = "err" // 错误

	// 游戏状态
	GameStatus MessageType = "game_status" // 当用户登录上来之后需要下发游戏状态

	// 玩家动作
	CreateRoom MessageType = "create_room" // 创建房间
	JoinRoom   MessageType = "join_room"   // 加入房间
	LeaveRoom  MessageType = "leave_room"  // 离开房间
	GetRoom    MessageType = "get_room"    // 获取房间所有信息(重连恢复)
	SetPiece   MessageType = "set_piece"   // 摆放棋子 并准备
	Move       MessageType = "move"        // 移动棋子, 如果两个棋子打架则还会包括打架结果

	// 系统发送游戏的消息
	Start  MessageType = "start"   // 开始游戏
	TimeTo MessageType = "time_to" // 改xx走棋了
	//Fit    MessageType = "fit"     // 两个棋子打架了, 消息体中包含结果
	End MessageType = "end" // 结束, 消息体包含结果(谁胜利了)
	// 命令动作, 将取代timeTo
	Action MessageType = "action"
)

type MoveMsgRaw

type MoveMsgRaw struct {
	From Point `json:"from"`
	To   Point `json:"to"`

	PlayerId  int64  `json:"player_id,omitempty"`
	FitResult string `json:"fit_result,omitempty"` // 打架结果
}

type Piece

type Piece int

数值表示是什么动物 1-8分别表示老鼠到大象

func (Piece) Fit

func (p1 Piece) Fit(p2 Piece) (win bool, allDie bool)

type Pieces

type Pieces map[Point]Piece

func (Pieces) Move

func (p Pieces) Move(from Point, to Point) error

func (Pieces) Remove

func (p Pieces) Remove(from Point) (pi Piece, err error)

func (Pieces) ValidateSet

func (p Pieces) ValidateSet(camp string) (err error)

检查是否摆放完整 camp: p1/p2

type Player

type Player struct {
	Id       int64  `json:"id" xorm:"int(11) pk autoincr index"`
	InRoomId int64  `json:"in_room_id" xorm:"int(11) index"`
	Uid      string `json:"uid" xorm:"varchar(32) unique"`
}

玩家保存则正在进行的游戏房间

type PlayerStatus

type PlayerStatus []*PlayerStatusOne

第一个人就是蓝色方, 第二个是红色方

func (*PlayerStatus) Get

func (ps *PlayerStatus) Get(playerId int64) (*PlayerStatusOne, bool)

func (*PlayerStatus) GetByCamp

func (ps *PlayerStatus) GetByCamp(camp string) (*PlayerStatusOne, bool)

func (*PlayerStatus) GetP1

func (ps *PlayerStatus) GetP1() (*PlayerStatusOne, bool)

func (*PlayerStatus) GetP2

func (ps *PlayerStatus) GetP2() (*PlayerStatusOne, bool)

func (PlayerStatus) IsAllReady

func (ps PlayerStatus) IsAllReady() bool

func (PlayerStatus) IsFull

func (p PlayerStatus) IsFull() bool

func (*PlayerStatus) Join

func (ps *PlayerStatus) Join(playerId int64) (s *PlayerStatusOne, err error)

加入房间, 不是准备状态

func (*PlayerStatus) Leave

func (ps *PlayerStatus) Leave(playerId int64) (err error)

离开房间

func (*PlayerStatus) Next

func (ps *PlayerStatus) Next(playerId int64) (po *PlayerStatusOne, err error)

下一个人

func (*PlayerStatus) Ready

func (ps *PlayerStatus) Ready(playerId int64) (err error)

安放棋子并准备开始

type PlayerStatusOne

type PlayerStatusOne struct {
	PlayerId int64  `json:"player_id"`
	Ready    bool   `json:"ready"`
	Camp     string `json:"camp"` // p1, p2 第一个进入房间的是p1
}

func (PlayerStatusOne) IsP1

func (po PlayerStatusOne) IsP1() bool

type Point

type Point string

方便存储到数据库, 使用string表示

const (
	P1Lair Point = "4-0"
	P2Lair Point = "4-12"

	P1CaveLeft  Point = "2-4"
	P1CaveRight Point = "6-4"

	P2CaveLeft  Point = "2-8"
	P2CaveRight Point = "6-8"
)

func (Point) Int

func (p Point) Int() (int, int)

type Room

type Room struct {
	Id             int64        `json:"id" xorm:"pk autoincr int(11)"`
	PlayerId       int64        `json:"player_id" xorm:"int(11)"` // 房主
	PlayerStatus   PlayerStatus `json:"player_status" xorm:"json"`
	TimeToPlayerId int64        `json:"timeto_player_id" xorm:"int(11)"` // 该谁走棋
	WinPlayerId    int64        `json:"win_player_id" xorm:"int(11)"`    // 胜利方
	Status         RoomStatus   `json:"status" xorm:"tinyint(1)"`        // 游戏状态
	TablePieces    TablePieces  `json:"table_pieces" xorm:"json"`        // 当前桌面上的棋子, 包括两方
}

房间管理着一局棋局和玩家

type RoomStatus

type RoomStatus int64
const (
	WaitPeopleStatus RoomStatus = 1 // 等待玩家加入
	WaitReadStatus   RoomStatus = 2 // 等待准备
	PlayingStatus    RoomStatus = 3 // 正在游戏中
	EndStatus        RoomStatus = 4 // 游戏结束
)

type SetPieceMsgRaw

type SetPieceMsgRaw struct {
	Pieces   Pieces `json:"pieces"`
	PlayerId int64  `json:"player_id,omitempty"`
}

type TablePieces

type TablePieces struct {
	P1 *TablePiecesOne `json:"p1"` // 蓝方的棋子
	P2 *TablePiecesOne `json:"p2"` // 红方的棋子
}

func (TablePieces) IsWin

func (tp TablePieces) IsWin() string

返回赢方的camp: p1/p2

func (TablePieces) Move

func (tp TablePieces) Move(camp string, from Point, to Point) (fitResult string, err error)

camp: p1/p2

type TablePiecesOne

type TablePiecesOne struct {
	Pieces Pieces  `json:"pieces"`
	Die    []Piece `json:"die"` // 死掉的棋子
}

type TimeToRaw

type TimeToRaw struct {
	PlayerId int64 `json:"player_id"` // 该谁走棋
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL