game

package
Version: v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UpdateDuration = 850 * time.Millisecond
	IdleStart      = 5 * time.Second
	IdleTimeout    = 1 * time.Minute
)
View Source
const (
	LogStandard = iota
	LogDebug
	LogVerbose
)
View Source
const (
	CommandQueueSize = 10
	LogQueueSize     = 10
	PlayerHost       = -1
	PlayerUnknown    = 0
)
View Source
const (
	DefaultPort   = 1984
	DefaultServer = "netris.rocketnine.space"
)
View Source
const ConnTimeout = 30 * time.Second

Variables

View Source
var Version = "0.0.0"

Functions

func GameName

func GameName(name string) string

func NetworkAndAddress

func NetworkAndAddress(address string) (string, string)

func Nickname

func Nickname(nick string) string

Types

type Command

type Command int
const (
	CommandUnknown Command = iota
	CommandDisconnect
	CommandPing
	CommandPong
	CommandNickname
	CommandMessage
	CommandNewGame
	CommandJoinGame
	CommandQuitGame
	CommandUpdateGame
	CommandStartGame
	CommandGameOver
	CommandUpdateMatrix
	CommandSendGarbage
	CommandReceiveGarbage
	CommandStats
	CommandListGames
)

The order of these constants must be preserved

func (Command) String

func (c Command) String() string

type Conn

type Conn struct {
	LastTransfer time.Time
	Terminated   bool

	Player int
	In     chan GameCommandInterface

	*sync.WaitGroup
	sync.Mutex
	// contains filtered or unexported fields
}

func Connect

func Connect(address string) (*Conn, error)

func NewServerConn

func NewServerConn(conn net.Conn, forwardOut chan GameCommandInterface) *Conn

func (*Conn) Close

func (s *Conn) Close()

func (*Conn) JoinGame

func (s *Conn) JoinGame(name string, gameID int, newGame *ListedGame, logger chan string, draw chan event.DrawObject) (*Game, error)

When newGame is set to a ListedGame and gameID is 0, a new custom game is created

func (*Conn) Write

func (s *Conn) Write(gc GameCommandInterface)

type ConnectingPlayer

type ConnectingPlayer struct {
	Name string
}

type Game

type Game struct {
	ID   int
	Name string

	Starting    bool
	Started     bool
	TimeStarted time.Time

	Eternal    bool
	Terminated bool

	Local       bool
	LocalPlayer int

	Players    map[int]*Player
	MaxPlayers int

	Event chan interface{}

	LogLevel int

	Rank  int
	Minos []mino.Mino
	Seed  int64

	FallTime   time.Duration
	SpeedLimit int

	sync.Mutex
	// contains filtered or unexported fields
}

func NewGame

func NewGame(rank int, out func(GameCommandInterface), logger chan string, draw chan event.DrawObject) (*Game, error)

func (*Game) AddPlayer

func (g *Game) AddPlayer(p *Player)

func (*Game) AddPlayerL

func (g *Game) AddPlayerL(p *Player)

func (*Game) HandleReadCommands

func (g *Game) HandleReadCommands(in chan GameCommandInterface)

func (*Game) Log

func (g *Game) Log(level int, a ...interface{})

func (*Game) Logf

func (g *Game) Logf(level int, format string, a ...interface{})

func (*Game) ProcessAction

func (g *Game) ProcessAction(a event.GameAction)

func (*Game) ProcessActionL

func (g *Game) ProcessActionL(a event.GameAction)

func (*Game) RemovePlayer

func (g *Game) RemovePlayer(playerID int)

func (*Game) RemovePlayerL

func (g *Game) RemovePlayerL(playerID int)

func (*Game) Reset

func (g *Game) Reset()

func (*Game) ResetL

func (g *Game) ResetL()

func (*Game) Start

func (g *Game) Start(seed int64) int64

func (*Game) StartL

func (g *Game) StartL(seed int64) int64

func (*Game) StopL

func (g *Game) StopL()

func (*Game) WriteAll

func (g *Game) WriteAll(gc GameCommandInterface)

func (*Game) WriteAllAndLogL

func (g *Game) WriteAllAndLogL(gc GameCommandInterface)

func (*Game) WriteAllL

func (g *Game) WriteAllL(gc GameCommandInterface)

func (*Game) WriteMessage

func (g *Game) WriteMessage(message string)

type GameCommand

type GameCommand struct {
	SourcePlayer int `json:"sp,omitempty"`
}

func (*GameCommand) SetSource

func (gc *GameCommand) SetSource(source int)

func (*GameCommand) Source

func (gc *GameCommand) Source() int

type GameCommandDisconnect

type GameCommandDisconnect struct {
	GameCommand
	Player  int    `json:"p,omitempty"`
	Message string `json:"m,omitempty"`
}

func (GameCommandDisconnect) Command

func (gc GameCommandDisconnect) Command() Command

type GameCommandGameOver

type GameCommandGameOver struct {
	GameCommand
	Player int    `json:"p,omitempty"`
	Winner string `json:"w,omitempty"`
}

func (GameCommandGameOver) Command

func (gc GameCommandGameOver) Command() Command

type GameCommandInterface

type GameCommandInterface interface {
	Command() Command
	Source() int
	SetSource(int)
}

type GameCommandJoinGame

type GameCommandJoinGame struct {
	GameCommand
	Version  int    `json:"v,omitempty"`
	Name     string `json:"n,omitempty"`
	GameID   int    `json:"g,omitempty"`
	PlayerID int    `json:"p,omitempty"`

	Listing ListedGame `json:"l,omitempty"`
}

func (GameCommandJoinGame) Command

func (gc GameCommandJoinGame) Command() Command

type GameCommandListGames

type GameCommandListGames struct {
	GameCommand

	Games []*ListedGame `json:"g,omitempty"`
}

func (GameCommandListGames) Command

func (gc GameCommandListGames) Command() Command

type GameCommandMessage

type GameCommandMessage struct {
	GameCommand
	Player  int    `json:"p,omitempty"`
	Message string `json:"m,omitempty"`
}

func (GameCommandMessage) Command

func (gc GameCommandMessage) Command() Command

type GameCommandNickname

type GameCommandNickname struct {
	GameCommand
	Player   int    `json:"p,omitempty"`
	Nickname string `json:"n,omitempty"`
}

func (GameCommandNickname) Command

func (gc GameCommandNickname) Command() Command

type GameCommandPing

type GameCommandPing struct {
	GameCommand
	Message string `json:"m,omitempty"`
}

func (GameCommandPing) Command

func (gc GameCommandPing) Command() Command

type GameCommandPong

type GameCommandPong struct {
	GameCommand
	Message string `json:"m,omitempty"`
}

func (GameCommandPong) Command

func (gc GameCommandPong) Command() Command

type GameCommandQuitGame

type GameCommandQuitGame struct {
	GameCommand
	Player int `json:"p,omitempty"`
}

func (GameCommandQuitGame) Command

func (gc GameCommandQuitGame) Command() Command

type GameCommandReceiveGarbage

type GameCommandReceiveGarbage struct {
	GameCommand
	Lines int `json:"l,omitempty"`
}

func (GameCommandReceiveGarbage) Command

func (gc GameCommandReceiveGarbage) Command() Command

type GameCommandSendGarbage

type GameCommandSendGarbage struct {
	GameCommand
	Lines int `json:"l,omitempty"`
}

func (GameCommandSendGarbage) Command

func (gc GameCommandSendGarbage) Command() Command

type GameCommandStartGame

type GameCommandStartGame struct {
	GameCommand
	Seed    int64 `json:"s,omitempty"`
	Started bool  `json:"st,omitempty"`
}

func (GameCommandStartGame) Command

func (gc GameCommandStartGame) Command() Command

type GameCommandStats

type GameCommandStats struct {
	GameCommand
	Created time.Time `json:"c,omitempty"`
	Players int       `json:"p,omitempty"`
	Games   int       `json:"g,omitempty"`
}

func (GameCommandStats) Command

func (gc GameCommandStats) Command() Command

type GameCommandTransport

type GameCommandTransport struct {
	Command Command `json:"cmd"`
	Data    json.RawMessage
}

type GameCommandUpdateGame

type GameCommandUpdateGame struct {
	GameCommand
	Players map[int]string `json:"p,omitempty"`
}

func (GameCommandUpdateGame) Command

func (gc GameCommandUpdateGame) Command() Command

type GameCommandUpdateMatrix

type GameCommandUpdateMatrix struct {
	GameCommand
	Matrixes map[int]*mino.Matrix `json:"m,omitempty"`
}

func (GameCommandUpdateMatrix) Command

func (gc GameCommandUpdateMatrix) Command() Command

type IncomingPlayer

type IncomingPlayer struct {
	Name string
	Conn *Conn
}

type ListedGame

type ListedGame struct {
	ID         int
	Name       string `json:"n,omitempty"`
	Players    int    `json:"p,omitempty"`
	MaxPlayers int    `json:"pl,omitempty"`
	SpeedLimit int    `json:"sl,omitempty"`
}

type Player

type Player struct {
	Name string

	*Conn

	Score   int
	Preview *mino.Matrix
	Matrix  *mino.Matrix
	Moved   time.Time     // Time of last piece move
	Idle    time.Duration // Time spent idling
	// contains filtered or unexported fields
}

func NewPlayer

func NewPlayer(name string, conn *Conn) *Player

type Server

type Server struct {
	I []ServerInterface

	In  chan GameCommandInterface
	Out chan GameCommandInterface

	Games map[int]*Game

	Logger chan string

	NewPlayers chan *IncomingPlayer

	sync.RWMutex
	// contains filtered or unexported fields
}

func NewServer

func NewServer(si []ServerInterface, logLevel int) *Server

func (*Server) FindGame

func (s *Server) FindGame(p *Player, gameID int, newGame ListedGame) *Game

func (*Server) Listen

func (s *Server) Listen(address string)

func (*Server) Log

func (s *Server) Log(a ...interface{})

func (*Server) Logf

func (s *Server) Logf(format string, a ...interface{})

func (*Server) NewGame

func (s *Server) NewGame() (*Game, error)

func (*Server) StopListening

func (s *Server) StopListening()

type ServerInterface

type ServerInterface interface {
	// Load config
	Host(newPlayers chan<- *IncomingPlayer)
	Shutdown(reason string)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL