mahjong

package
v0.0.0-...-fc2bb65 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BeforeStart = iota
	DealTile
	ChangeTile
	ChooseLack
	IdxTurn
)

Game State

View Source
const (
	WAITING = 0
	MATCHED = 1
	READY   = 2
	PLAYING = 4
	LEAVE   = 8
)

Player's state

Variables

View Source
var COMMAND = map[string]int{
	"NONE":   0,
	"PON":    1,
	"GON":    2,
	"ONGON":  4,
	"PONGON": 8,
	"HU":     16,
	"ZIMO":   32,
}

COMMAND is a map of command type

Functions

func AddPlayer

func AddPlayer(name string) (string, bool)

AddPlayer adds a new player into PlayerManager

func Auth

func Auth(room string, uuid string) bool

Auth authenticates a player

func CalTai

func CalTai(hand uint64, door uint64) int

CalTai cals tai

func CreateRoom

func CreateRoom()

CreateRoom creates a new room and add player to that room

func Exec

func Exec()

Exec executes the whole game

func FindPlayerByName

func FindPlayerByName(name string) int

FindPlayerByName gets player's index by player's name

func FindPlayerBySocket

func FindPlayerBySocket(socket socketio.Socket) int

FindPlayerBySocket gets player's index by player's socket

func FindPlayerByUUID

func FindPlayerByUUID(uuid string) int

FindPlayerByUUID gets player's index by player's uuid

func GetNameList

func GetNameList(list []*IPlayer) []string

GetNameList returns the list of player's name

func GetServer

func GetServer() *socketio.Server

GetServer returns socket io server

func GetUUIDList

func GetUUIDList(list []IPlayer) []string

GetUUIDList returns the list of player's uuid

func IF

func IF(condition bool, trueVal, falseVal interface{}) interface{}

IF implements ternary conditional operator

func InitHuTable

func InitHuTable() bool

InitHuTable intis the hu table

func IsValidTile

func IsValidTile(tile string) bool

IsValidTile checks if tile string is vaild

func Login

func Login(name string, socket *socketio.Socket) (string, bool)

Login handles player's login

func Logout

func Logout(socket socketio.Socket)

Logout handles player's logout

func Match

func Match() []string

Match matchs 4 player into a room

func NewGameManager

func NewGameManager() bool

NewGameManager creates a new gameManager

func RemovePlayer

func RemovePlayer(id int)

RemovePlayer remove a player from PlayerList

func RemoveRoom

func RemoveRoom(name string)

RemoveRoom removes a room by room name

func SocketConnect

func SocketConnect(so socketio.Socket)

SocketConnect is callback of socket connect event

func SocketError

func SocketError(so socketio.Socket, err error)

SocketError is callback of socket error event

func WaitingNum

func WaitingNum() int

WaitingNum returns the number of player which state are waiting

Types

type Action

type Action struct {
	Command int
	Tile    Tile
	Score   int
}

Action represent a command made by player

func JSONToAction

func JSONToAction(actionStr string) Action

JSONToAction converts json string to action

func NewAction

func NewAction(command int, tile Tile, score int) Action

NewAction creates a new action

func (Action) ToJSON

func (act Action) ToJSON() string

ToJSON converts action to json string

type ActionSet

type ActionSet map[int][]Tile

ActionSet represents a set of action

func NewActionSet

func NewActionSet() ActionSet

NewActionSet creates a new action set

func (ActionSet) ToJSON

func (set ActionSet) ToJSON() string

ToJSON converts action set to json string

type GameManager

type GameManager struct {
	Rooms  map[string]*Room
	Server *socketio.Server
}

GameManager represents a gameManager

type GameResult

type GameResult struct {
	Hand     []string
	Door     []string
	Score    int
	ScoreLog []ScoreRecord
}

GameResult represents the result of mahjong

type IPlayer

type IPlayer struct {
	Name   string
	UUID   string
	Room   string
	Socket *socketio.Socket
	State  int
	Index  int
}

IPlayer represents the player's info

func FindPlayerListInRoom

func FindPlayerListInRoom(room string) []*IPlayer

FindPlayerListInRoom gets list of player which in the same room

func FindPlayerListIsSameState

func FindPlayerListIsSameState(state int) []*IPlayer

FindPlayerListIsSameState gets list of player which are same state

type Player

type Player struct {
	Hand         SuitSet
	Door         SuitSet
	VisiableDoor SuitSet
	DiscardTiles SuitSet
	HuTiles      SuitSet
	GonRecord    [4]int
	ScoreLog     []ScoreRecord
	Lack         int
	Credit       int
	MaxTai       int
	IsHu         bool
	IsTing       bool
	JustGon      bool
	IsPenalize   bool
	ID           int
	UUID         string
	// contains filtered or unexported fields
}

Player represents a player in mahjong

func NewPlayer

func NewPlayer(room *Room, id int, uuid string) *Player

NewPlayer creates a new player

func (*Player) ChangeTiles

func (player *Player) ChangeTiles() []Tile

ChangeTiles emits to client to get the change tiles

func (*Player) CheckGon

func (player *Player) CheckGon(tile Tile) bool

CheckGon checks if the player can gon

func (*Player) CheckHu

func (player *Player) CheckHu(tile Tile, tai *int) bool

CheckHu checks if the player can hu

func (*Player) CheckPon

func (player *Player) CheckPon(tile Tile) bool

CheckPon checks if the player can pon

func (*Player) CheckTing

func (player *Player) CheckTing(max *int) bool

CheckTing checks if the player is ting

func (*Player) ChooseLack

func (player *Player) ChooseLack() int

ChooseLack emits to client to get the choose lack

func (*Player) Command

func (player *Player) Command(actionSet ActionSet, command int) Action

Command emits to client to get command

func (*Player) Draw

func (player *Player) Draw(drawTile Tile) Action

Draw draws a Tile

func (*Player) Fail

func (player *Player) Fail(command int)

Fail emits to client to notice the command is failed

func (*Player) Gon

func (player *Player) Gon(tile Tile, Type int, fromID int) int

Gon gons the tile

func (*Player) Hu

func (player *Player) Hu(tile Tile, tai int, Type int, addOneTai, addToRoom bool, fromID int) int

Hu hus tile tile

func (*Player) Init

func (player *Player) Init()

Init inits the player's state

func (Player) Name

func (player Player) Name() string

Name returns the player's name

func (*Player) Pon

func (player *Player) Pon(tile Tile)

Pon pons the tile

func (Player) Room

func (player Player) Room() string

Room returns the player's room

func (Player) Socket

func (player Player) Socket() socketio.Socket

Socket returns the player's socket

func (*Player) Success

func (player *Player) Success(from int, command int, tile Tile, score int)

Success emits to client to notice the command is successed

func (*Player) Tai

func (player *Player) Tai(tile Tile) int

Tai cals the tai

func (*Player) Throw

func (player *Player) Throw(drawTile Tile) Tile

Throw emits to client to get the throw Tile

type PlayerManager

type PlayerManager []*IPlayer

PlayerManager represents the array of pointer of IPlayer

var PlayerList PlayerManager

PlayerList is a PlayerManager

type Room

type Room struct {
	Players      []*Player
	ChangedTiles [4][]Tile
	ChoosedLack  [4]int
	Deck         SuitSet
	HuTiles      SuitSet
	Waiting      bool
	IO           *socketio.Server
	Name         string
	State        int
}

Room represents a round of mahjong

func NewRoom

func NewRoom(name string) *Room

NewRoom creates a new room

func (*Room) Accept

func (room *Room) Accept(uuid string, callback func(int))

Accept checks player's info and constructs the player

func (*Room) AddPlayer

func (room *Room) AddPlayer(playerList []string)

AddPlayer adds 4 player into this room

func (Room) BroadcastChange

func (room Room) BroadcastChange(id int)

BroadcastChange broadcasts the player's id who already change tiles

func (Room) BroadcastCommand

func (room Room) BroadcastCommand(from int, to int, command int, tile Tile, score int)

BroadcastCommand broadcasts the player's id and the command he made

func (Room) BroadcastDraw

func (room Room) BroadcastDraw(id int, num uint)

BroadcastDraw broadcasts the player's id who draw a tile

func (Room) BroadcastEnd

func (room Room) BroadcastEnd(data []GameResult)

BroadcastEnd broadcasts the game result

func (Room) BroadcastGameStart

func (room Room) BroadcastGameStart()

BroadcastGameStart broadcasts player list

func (Room) BroadcastLack

func (room Room) BroadcastLack()

BroadcastLack broadcasts the player's id who already choose lack

func (Room) BroadcastReady

func (room Room) BroadcastReady(name string)

BroadcastReady broadcasts the player's name who is ready

func (Room) BroadcastRemainTile

func (room Room) BroadcastRemainTile(num uint)

BroadcastRemainTile broadcasts remain tile

func (Room) BroadcastRobGon

func (room Room) BroadcastRobGon(id int, tile Tile)

BroadcastRobGon broadcasts rob gon

func (Room) BroadcastStopWaiting

func (room Room) BroadcastStopWaiting()

BroadcastStopWaiting broadcasts stop waiting signal

func (Room) BroadcastThrow

func (room Room) BroadcastThrow(id int, tile Tile)

BroadcastThrow broadcasts the player's id and the tile he threw

func (Room) GetCurrentIdx

func (room Room) GetCurrentIdx() int

GetCurrentIdx returns current index

func (Room) GetDoor

func (room Room) GetDoor(id int) ([][]string, []int, bool)

GetDoor returns each player's door

func (Room) GetHandCount

func (room Room) GetHandCount() []int

GetHandCount returns each player's amount of hand

func (Room) GetHu

func (room Room) GetHu() ([][]string, bool)

GetHu returns each player's hu tile

func (Room) GetLack

func (room Room) GetLack() []int

GetLack returns each player's lack

func (Room) GetPlayerList

func (room Room) GetPlayerList() []string

GetPlayerList returns the list of player's name

func (Room) GetReadyPlayers

func (room Room) GetReadyPlayers() []string

GetReadyPlayers returns the name list of ready player

func (Room) GetRemainCount

func (room Room) GetRemainCount() int

GetRemainCount returns amount of deck

func (Room) GetScore

func (room Room) GetScore() []int

GetScore returns each player's score

func (Room) GetSea

func (room Room) GetSea() ([][]string, bool)

GetSea returns each player's discard tile

func (Room) NumPlayer

func (room Room) NumPlayer() int

NumPlayer returns the number of player in the room

func (*Room) RemovePlayer

func (room *Room) RemovePlayer(id int)

RemovePlayer reomves id th player from this room

func (*Room) Run

func (room *Room) Run()

Run runs mahjong logic

func (*Room) StopWaiting

func (room *Room) StopWaiting()

StopWaiting stops waiting

func (*Room) WaitToStart

func (room *Room) WaitToStart()

WaitToStart checks if all player in this room are ready and run the mahjong logic

type ScoreRecord

type ScoreRecord struct {
	Message string
	Tile    string
	Score   int
}

ScoreRecord represents the record of score

func NewScoreRecord

func NewScoreRecord(message string, direct string, player string, tile string, score int) ScoreRecord

NewScoreRecord creates a new scoreRecord

type Suit

type Suit uint32

Suit represents a mahjong suit

func (*Suit) Add

func (suit *Suit) Add(idx uint)

Add add a tile to suit

func (Suit) Count

func (suit Suit) Count() uint

Count returns amount of suit

func (Suit) GetIndex

func (suit Suit) GetIndex(idx uint) uint

GetIndex returns tile amount at idx

func (Suit) Have

func (suit Suit) Have(idx int) bool

Have returns if suit have tile

func (*Suit) Sub

func (suit *Suit) Sub(idx uint)

Sub subs a tile from suit

type SuitSet

type SuitSet [3]Suit

SuitSet represents a set of suit

func ArrayToSuitSet

func ArrayToSuitSet(tile []Tile) SuitSet

ArrayToSuitSet converts tile array to suit set

func NewSuitSet

func NewSuitSet(full bool) SuitSet

NewSuitSet creates a new suit suitSet

func (*SuitSet) Add

func (suitSet *SuitSet) Add(input interface{})

Add adds a tile or a suit set to a suit set

func (SuitSet) At

func (suitSet SuitSet) At(idx int) Tile

At returns idx th tile in suit set

func (SuitSet) Count

func (suitSet SuitSet) Count() uint

Count returns amount of suit set

func (*SuitSet) Draw

func (suitSet *SuitSet) Draw() Tile

Draw draws a tile from suit set

func (SuitSet) Have

func (suitSet SuitSet) Have(tile Tile) bool

Have returns if suit set has tlie

func (SuitSet) IsContainColor

func (suitSet SuitSet) IsContainColor(color int) bool

IsContainColor return if suit set contain color

func (SuitSet) IsEmpty

func (suitSet SuitSet) IsEmpty() bool

IsEmpty returns if suit set is empty

func (*SuitSet) Sub

func (suitSet *SuitSet) Sub(input interface{})

Sub subs a tile or a suit set from a suit set

func (SuitSet) ToStringArray

func (suitSet SuitSet) ToStringArray() []string

ToStringArray converts suit set to string array

func (SuitSet) Translate

func (suitSet SuitSet) Translate(lack int) uint64

Translate translates suit set to uint64

type Tile

type Tile struct {
	Suit  int
	Value uint
}

Tile represents a mahjong tile

func NewTile

func NewTile(suit int, value uint) Tile

NewTile creates a new tile

func StringArrayToTileArray

func StringArrayToTileArray(tiles []string) []Tile

StringArrayToTileArray converts string array to tile array

func StringToTile

func StringToTile(tile string) Tile

StringToTile converts string to tile

func (Tile) ToString

func (tile Tile) ToString() string

ToString converts tile to string

Jump to

Keyboard shortcuts

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