mahjong

package
v0.0.0-...-8fa3014 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BeforeStart = iota
	DealTile
	BuHua
	IdxTurn
)

Game State

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

Player's state

View Source
const (
	CanHu           = uint(1)
	HasEye          = uint(2)
	OnlyHasEye      = uint(4)
	TingOnlyOne     = uint(8)
	Clean           = uint(16)
	PingHu          = uint(32)
	MixSuit         = uint(64)
	SameSuit        = uint(128)
	PonPair         = uint(256)
	ConcealedPonCnt = uint(3584)
	Zimo            = uint(4096)

	East               = uint(16)
	South              = uint(32)
	West               = uint(64)
	North              = uint(128)
	Bonus              = uint(256)
	Red                = uint(512)
	Green              = uint(1024)
	White              = uint(2048)
	LittleFourWinds    = uint(4096)
	BigFourWinds       = uint(8192)
	LittleThreeDragons = uint(16384)
	BigThreeDragons    = uint(32768)

	EyeNumber = uint(61440)
)

Feature

View Source
const BASE = 3

BASE represents base score

View Source
const (
	MONGODB_URL = "127.0.0.1:27017"
)
View Source
const SIZE = 76695844

SIZE represents table size

View Source
const TAI = 1

TAI represents base tai

Variables

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

COMMAND is a map of command type

View Source
var Condition = map[string]int{
	"Eat":    0,
	"Pon":    1,
	"Gon":    2,
	"OnGon":  3,
	"PonGon": 4,
	"Hu":     5,
	"Zimo":   6,
	"Tin":    7,
}
View Source
var GroupMask = [40]int32{
	02, 020, 0200, 02000, 020000, 0200000, 02000000, 020000000, 0200000000,
	03, 030, 0300, 03000, 030000, 0300000, 03000000, 030000000, 0300000000,
	0111, 01110, 011100, 0111000, 01110000, 011100000, 0111000000,
	011, 0110, 01100, 011000, 0110000, 01100000, 011000000, 0110000000,
	0101, 01010, 010100, 0101000, 01010000, 010100000, 0101000000,
}
View Source
var SuitTileCount = [5]uint{9, 9, 9, 7, 8}

SuitTileCount represents each suit's tile count

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 CreateRoom

func CreateRoom()

CreateRoom creates a new room and add player to that room

func Disconnect

func Disconnect(socket socketio.Socket) int

Disconnect handles the player's socket disconnect

func Exec

func Exec()

Exec executes the whole game

func FindGroups

func FindGroups(suitSet SuitSet, Components *[]int32, result *[]Groups, tmp []uint32, start int)

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 HaveComponent

func HaveComponent(suit int32, component int32) bool

func IF

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

IF implements ternary conditional operator

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(index int)

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(isBot int) int

WaitingNum returns the number of player which state are waiting

Types

type Action

type Action struct {
	Command int
	Tile    Tile
	Score   int
}

Action represents 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 ActionInfo

type ActionInfo struct {
	Turn   int
	Action string
	From   int
	Card   string
}

type ActionSet

type ActionSet map[int][]string

ActionSet represents a set of action

func NewActionSet

func NewActionSet() ActionSet

NewActionSet creates a new action set

type EatAction

type EatAction struct {
	Idx    int
	First  Tile
	Center Tile
}

EatAction represents

type GameManager

type GameManager struct {
	Rooms  map[string]*Room
	Server *socketio.Server
	Locker sync.Mutex
	//GossipDB     *mgo.Database
	GossipDealer socketio.Socket
}

GameManager represents a gameManager

type GameResult

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

GameResult represents the result of mahjong

type GossipInfo

type GossipInfo struct {
	CurAction   string
	Id          int
	Hand        SuitSet
	ThrowTimes  int
	StepsToHu   int
	IsTing      bool
	OtherTing   bool
	SafeBefore  []string
	Actions     []ActionInfo
	DeadCards   SuitSet
	PlayerNames []string
}

type GossipLog

type GossipLog struct {
	Name     string       `bson:"Name,omitempty"`
	Actions  []ActionInfo `bson:"Actions,omitempty"`
	Sentence string       `bson:"Sentence,omitempty"`
}

type Groups

type Groups struct {
	Code     []uint32
	Hu_Tiles []uint32
	Steps    int
}

func CountSteps

func CountSteps(G *[]Groups) []Groups

type HonorsTable

type HonorsTable struct {
	// contains filtered or unexported fields
}

HonorsTable represenpts honors huTable.table

func NewHonorsTable

func NewHonorsTable() *HonorsTable

NewHonorsTable creates a new honors huTable.table

type HonorsTaiTable

type HonorsTaiTable struct {
	TaiTable map[uint]TaiData
}

HonorsTaiTable represents honors tai table

func NewHonorsTaiTable

func NewHonorsTaiTable() *HonorsTaiTable

NewHonorsTaiTable creates a new simples tai table

func (*HonorsTaiTable) Get

func (honorsTaiTable *HonorsTaiTable) Get(idx uint) TaiData

Get gets honorsTaiTable data

type HuInfo

type HuInfo struct {
	Hand    SuitSet
	Door    SuitSet
	Winds   Wind
	IsZimo  uint
	IsClean uint
	AllPon  uint
	AllChow uint
	NoBonus uint

	CertainTile Tile
}

HuInfo represents information of hu

type HuTable

type HuTable struct {
	Melds     []uint
	AllEyes   []uint
	AllSingle []uint
	PreTable  *[]uint
	Table     map[uint]uint
}

HuTable represents hu table

func NewHuTable

func NewHuTable() *HuTable

NewHuTable creates a new hu table

func (HuTable) CanHu

func (huTable HuTable) CanHu(t uint) bool

CanHu check if hand can hu

func (HuTable) Get

func (huTable HuTable) Get(idx uint) uint

Get gets table value

func (HuTable) Have

func (huTable HuTable) Have(now uint, check uint) bool

Have checks if hand has feature

type IPlayer

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

IPlayer represents the player's info

func FindPlayerListInRoom

func FindPlayerListInRoom(room string, checkIsBot int) []*IPlayer

FindPlayerListInRoom gets list of player which in the same room

func FindPlayerListIsSameState

func FindPlayerListIsSameState(state int, checkIsBot int) []*IPlayer

FindPlayerListIsSameState gets list of player which are same state

func NewIPlayer

func NewIPlayer(name string, uuid string) *IPlayer

NewIPlayer creates a new player info

type Player

type Player struct {
	Hand         SuitSet
	EatTiles     []EatAction
	PonTiles     SuitSet
	GonTiles     SuitSet
	OngonTiles   SuitSet
	Flowers      SuitSet
	DiscardTiles SuitSet
	ScoreLog     ScoreRecord
	Credit       int
	JustGon      bool
	FirstDraw    bool
	ID           int
	UUID         string
	ThrowTimes   int //哈拉加的
	StepsToHu    int
	IsTing       bool
	OtherTing    bool
	SafeBefore   []string
	Actions      []ActionInfo
	// 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) CheckEat

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

CheckEat checks if the player can eat

func (*Player) CheckHu

func (player *Player) CheckHu(tile Tile, isZimo uint, tai *TaiData) 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() bool

CheckTing checks if the player can ting

func (*Player) Command

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

Command emits to client to get command

func (*Player) Draw

func (player *Player) Draw(currentIdx int) (Action, bool, bool)

Draw draws a Tile

func (*Player) Eat

func (player *Player) Eat(eatAction EatAction)

Eat eats the 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)

Gon gons the tile

func (*Player) Hu

func (player *Player) Hu(tile Tile, tai TaiData, Type int, robGon bool, 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 interface{}, score int)

Success emits to client to notice the command is successed

func (*Player) Throw

func (player *Player) Throw(currentIdx int, 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
	Deck        SuitSet
	Turn        int
	Waiting     bool
	IO          *socketio.Server
	Name        string
	State       int
	Info        HuInfo
	KeepWin     bool
	NumKeepWin  int
	Banker      int
	SevenFlower bool
	SevenID     int
	Wind        int
	Round       int
	OpenIdx     int
	EastIdx     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) BroadcastBanker

func (room Room) BroadcastBanker(id int, keepWin int)

BroadcastBanker broadcasts banker ID

func (Room) BroadcastBuHua

func (room Room) BroadcastBuHua(flowers [][]string)

BroadcastBuHua broadcasts the player's flower

func (Room) BroadcastCommand

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

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

func (Room) BroadcastCoversation

func (room Room) BroadcastCoversation(id int, sentence string)

BroadcastConversation broadcasts when player have something to say

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) BroadcastGameEnd

func (room Room) BroadcastGameEnd()

BroadcastGameEnd broadcasts when the game end

func (Room) BroadcastGameStart

func (room Room) BroadcastGameStart()

BroadcastGameStart broadcasts player list

func (Room) BroadcastHua

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

BroadcastHua broadcasts the player's draw flower

func (Room) BroadcastOpenDoor

func (room Room) BroadcastOpenDoor(idx int, dice int)

BroadcastOpenDoor broadcasts poen door index and dice number

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) BroadcastSetSeat

func (room Room) BroadcastSetSeat(idx int)

BroadcastSetSeat broadcasts east seat idx

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) BroadcastTing

func (room Room) BroadcastTing(id int)

BroadcastTing broadcasts the player's id which is ting

func (Room) BroadcastWindAndRound

func (room Room) BroadcastWindAndRound(wind int, round int)

BroadcastWindAndRound broadcasts wind and round index

func (Room) CountDeadCard

func (room Room) CountDeadCard() SuitSet

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) GetFlower

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

GetFlower returns each player's flower tile

func (Room) GetHandCount

func (room Room) GetHandCount() []int

GetHandCount returns each player's amount of hand

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) GetTing

func (room Room) GetTing() []bool

GetTing returns each player's isTing

func (Room) GetWindAndRound

func (room Room) GetWindAndRound() (int, int)

GetWindAndRound returns the wind and round

func (Room) GossipTalk

func (room Room) GossipTalk(action string, card string, id int)

func (Room) NumPlayer

func (room Room) NumPlayer() int

NumPlayer returns the number of player in the room

func (Room) Record

func (room Room) Record(action string, card Tile, currentIdx int, actionIdx int)

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 ScoreCalc

type ScoreCalc struct {
	TaiTable     *TaiTable
	SimplesTable *SimplesTable
	HonorsTable  *HonorsTable
}

ScoreCalc represents score calculator

func NewScoreCalc

func NewScoreCalc() ScoreCalc

NewScoreCalc creates a new ccore calculator

func (ScoreCalc) CountTai

func (scoreCalc ScoreCalc) CountTai(info HuInfo) TaiData

CountTai cal tai

type ScoreRecord

type ScoreRecord struct {
	Message string
	Score   int
	Tile    string
}

ScoreRecord represents the record of score

func NewScoreRecord

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

NewScoreRecord creates a new scoreRecord

type Sentence

type Sentence struct {
	Sentence string `bson:"Sentence,omitempty"`
}

type SimplesTable

type SimplesTable struct {
	// contains filtered or unexported fields
}

SimplesTable represents simples table

func NewSimplesTable

func NewSimplesTable() *SimplesTable

NewSimplesTable creates a new simples table

type SimplesTaiTable

type SimplesTaiTable struct {
	TaiTable map[uint]TaiData
}

SimplesTaiTable represents simples tai table

func NewSimpleTaiTable

func NewSimpleTaiTable() *SimplesTaiTable

NewSimpleTaiTable creates a new simples tai table

func (*SimplesTaiTable) Get

func (simplesTaiTable *SimplesTaiTable) Get(idx uint) TaiData

Get gets simplesTaiTable data

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 [5]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) CountStepsToHu

func (suitSet SuitSet) CountStepsToHu() int

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

type TaiData

type TaiData struct {
	Tai     int
	Message string
}

TaiData represents tai data

type TaiTable

type TaiTable struct {
	// contains filtered or unexported fields
}

TaiTable represents tai table

func NewTaiTable

func NewTaiTable() *TaiTable

NewTaiTable creates a new tai table

func (*TaiTable) Get

func (taiTable *TaiTable) Get(idx uint) TaiData

Get gets taiTable data

type ThrowCardInfo

type ThrowCardInfo struct {
	Id          int
	Card        string
	PlayerNames []string
}

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

type Wind

type Wind byte

Wind represents wind

Jump to

Keyboard shortcuts

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