nex

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 23 Imported by: 7

README

nex

package interfaces

import "github.com/cruisechang/nex"

type service

Service is a public api interface, Plugin using service to call functions in gameServer Including functions:

module
  • Command
  • Game
  • Zone
  • Room
  • User
socket functions
  • SendCommand
  • SendCommandByData
db functions
  • selectUser
  • updateUser
func (s *Service) GetUser(userID int) (*module.User, error) 
func (s *Service) ContainsUser(userID int) bool 
func (s *Service) GetRoom(gameID, zoneID string, roomNum int) (*module.Room, error)

// DB SelectUser select  user by userID.
func (s *Service) SelectUser(userID int) ([]map[string]interface{}, error) 

// DB UpdateUser updates user data in DB
func (s *Service) UpdateUser(userID int, args map[string]interface{}) ([]map[string]interface{}, error) 

func (s *Service) SendCommand(gameID, zoneID string, senderID int, receiverIDs []int, cmd *module.Command) error
func (s *Service) SendCommand(gameID, zoneID string, senderID int, receiverIDs []int, cmd *module.Command) error

package module

import "kumay/gameServer2/interfaces/module"

type CommandInfo

commandInfo contains command name,*Command and *User

  type CommandInfo struct {
	   Command string
	   Data    *Command //command struct
	   User    *User    //nil for nouser
}
type CommandContainer

CommandContainer contains conn and command and userID socket receive command from client wrap command int CommandContainer pass to gameServer

type CommandContainer struct {
	Conn    *websocket.Conn
	Command *CommandCom
	UserID  int
}

type NotifyCommand

NotifyCommand sent by plugin to gameServer notify server what happend

type NotifyCommand struct {
	Code   int  //code is defined in interfaces.config
	UserID []int
}
type Game

Game is the top module in the structure of game. The structure of game is like following:

Game->Zone->Room->User -- Game contains zone -- Zone contains room -- Romm contains user(id)

func (m *Game) GetZone(zoneID string) (*Zone, error)
func (m *Game) ContainsZone(zoneID string) bool
func (m *Game) GetZoneAll() map[string]*Zone
func (m *Game) CountZone() int

type Zone
func (z *Zone) GetRoom(roomNum int) (*Room, error)
func (z *Zone) GetRoomAll() map[int]*Room
func (z *Zone) CountRoom() int
type Room

Room is basic module in game which contains userIDs which are in room players ids.

Plugin與server溝通管道都在Room裡

commandInfoChan

server pass commandInfo to plugin GetCommandInfoChan()

notifyCommandChan

plugin pass notify to server WriteNotifyCommandChan()

HTTPParamChan

server pass http param to plugin GetHTTParamChan()

var RoomNum int                  
fun (r *Room) IsActive()bol                
func ContainsUser(userID int)bool  
func GetUserIDs()([]int, error)

func GetCommandInfoChan() <-chan *CommandInfo  
//Get commandInfo chan to re

func WriteNotifyCommandChan(cmd *NotifyCommand)
//Plugin writes notify cmd into chan

func GetHTTPParamChan() <-chan map[string]string
//HTTP param chan is for plugin to read http param like grabRedRev....
type User

Contains user data.

type User struct {
	GameID     string `json:"WZGameID"` //自己的gameID (serverID)
	ZoneID     string `json:"WZZoneID"`
	RoomNumber int    `json:"WZRoomNumber"`
	SessionID  string `json:"WZSessionID"`

	Step         int     `json:"Step"`
	UserID       int     `json:"UserID"`
	Name         string  `json:"Name"`
	OrderID      int     `json:"OrderID"`
	Credit       float64 `json:"Credit"`
	Coin         float64 `json:"Coin"`
	VIPLevel     int     `json:"VIPLevel"`
	Exp          int     `json:"Exp"`
	UserLevel    int     `json:"UserLevel"`
	Email        string  `json:"Email"`
	Gender       string  `json:"Gender"`
	Birthday     string  `json:"Birthday"`
	QQAccount    string  `json:"QQAccount"`
	IdentityCard string  `json:"IdentityCard"`
	CellPhone    string  `json:"CellPhone"`
	Portrait     string  `json:"Portrait"`
	//不再db query中
	Account  string `json:"Account"`
	GameIDEx int    `json:"GameID"` //外部 gameID from loginEx url
	IP       string `json:"IP"`     //login時client自行傳入的
	IsRobot  bool   `json:"IsRobot"`

	AESKey []byte `json:"AESKey"`
	//Player     *interfaces.Player
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	//Code is success code, you can defined yoursef
	Code int
	//Command is string command name,
	Command string
	//Step is a serial number you can use to check command sequence
	Step int
	//Data is real data which is string format, you can define the content
	Data string
}

type CommandLowerCase

type CommandLowerCase struct {
	//Code is success code, you can defined yoursef
	Code int `json:"code"`
	//Command is string command name,
	Command string `json:"command"`
	//Step is a serial number you can use to check command sequence
	Step int `json:"step"`
	//Data is real data which is string format, you can define the content
	Data string `json:"data"`
}

type CommandManager

type CommandManager interface {
	RunProcessor(object *CommandObject) error
	RegisterProcessor(cmdName string, p CommandProcessor) error
	UnRegisterProcessor(cmdName string)
	CreateCommand(code int, step int, cmdName string, data string) (*Command, error)
}

func NewCommandManager

func NewCommandManager() (CommandManager, error)

type CommandObject

type CommandObject struct {
	Cmd  *Command
	User entity.User
}

ProcessorObject is a container contains command and user (send this command)

type CommandProcessor

type CommandProcessor interface {
	Run(obj *CommandObject) error
}

CommandProcessor is a interface user implements this interface to create command processor and register the processor to nex when command received , processor will be executed

type Configurer

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

Config config main struct

func NewConfigurer

func NewConfigurer(filePath string) (*Configurer, error)

NewConfig make a new config struct

func (*Configurer) GetRPCClientByIndex

func (c *Configurer) GetRPCClientByIndex(index int) (rpcClientConf, error)

func (*Configurer) GetRPCClientByName

func (c *Configurer) GetRPCClientByName(name string) (rpcClientConf, error)

func (*Configurer) GetRPCClients

func (c *Configurer) GetRPCClients() []rpcClientConf

rpc client

func (*Configurer) HttpClientAddress

func (c *Configurer) HttpClientAddress() (addr, port string)

func (*Configurer) HttpClientHandshakeTimeoutSecond

func (c *Configurer) HttpClientHandshakeTimeoutSecond() int

func (*Configurer) HttpClientRequestTimeoutSecond

func (c *Configurer) HttpClientRequestTimeoutSecond() int

func (*Configurer) HttpClientTCPConnectTimeoutSecond

func (c *Configurer) HttpClientTCPConnectTimeoutSecond() int

func (*Configurer) HttpServerAddress

func (c *Configurer) HttpServerAddress() (addr, port string)

http

func (*Configurer) HttpServerIdleTimeout

func (c *Configurer) HttpServerIdleTimeout() time.Duration

func (*Configurer) HttpServerMaxHeaderBytes

func (c *Configurer) HttpServerMaxHeaderBytes() int

func (*Configurer) HttpServerReadTimeout

func (c *Configurer) HttpServerReadTimeout() time.Duration

func (*Configurer) HttpServerWriteTimeout

func (c *Configurer) HttpServerWriteTimeout() time.Duration

func (*Configurer) RPCServerAddress

func (c *Configurer) RPCServerAddress() (addr, port string)

rpc server

func (*Configurer) SetWebSocketServerDefaultAESKey

func (c *Configurer) SetWebSocketServerDefaultAESKey(key []byte)

func (*Configurer) WebSocketServerAcceptTimeoutSecond

func (c *Configurer) WebSocketServerAcceptTimeoutSecond() int

func (*Configurer) WebSocketServerAddress

func (c *Configurer) WebSocketServerAddress() (addr, port string)

websocket

func (*Configurer) WebSocketServerAliveTimeoutSecond

func (c *Configurer) WebSocketServerAliveTimeoutSecond() int

func (*Configurer) WebSocketServerConnectionCapacity

func (c *Configurer) WebSocketServerConnectionCapacity() int

func (*Configurer) WebSocketServerConnectionLostChanCapacity

func (c *Configurer) WebSocketServerConnectionLostChanCapacity() int

func (*Configurer) WebSocketServerDefaultAESKey

func (c *Configurer) WebSocketServerDefaultAESKey() []byte

func (*Configurer) WebSocketServerDefaultAESKeyString

func (c *Configurer) WebSocketServerDefaultAESKeyString() string

func (*Configurer) WebSocketServerPacketChanCapacity

func (c *Configurer) WebSocketServerPacketChanCapacity() int

type HallManager

type HallManager interface {
	CreateHall(hallID int, name string) (entity.Hall, error)
	RemoveHall(hallID int)
	GetHall(hallID int) (entity.Hall, bool)
	GetHalls() []entity.Hall
	ContainHall(hallID int) bool
}

func NewHallManager

func NewHallManager() (HallManager, error)

type MiddleHandler

type MiddleHandler interface {
	PacketToCommand(packet []byte) (*Command, error)
	CommandToPacket(cmd *Command) ([]byte, error)
	CommandToPacketLowercase(cmd *CommandLowerCase) ([]byte, error)
}

func NewMiddleHandler

func NewMiddleHandler() (MiddleHandler, error)

type Nex

type Nex interface {
	Start()
	Stop()
	Version() string
	GetConfig() *Configurer

	//RegisterCommandProcessor registers a process to handle command
	RegisterCommandProcessor(name string, p CommandProcessor) error
	CreateCommand(code int, step int, cmdName string, data string) (*Command, error)
	SendCommand(cmd *Command, sender entity.User, targetConnIDs []string, lowercase bool)

	//RegisterBuiltinEventProcessor registers a procese to handle event
	RegisterBuiltinEventProcessor(code builtinEvent.EventNO, p builtinEvent.EventProcessor) error
	UnRegisterBuiltinEventProcessor(code builtinEvent.EventNO)

	//RegisterBuiltinEventProcessor registers a procese to handle event
	RegisterEventProcessor(event string, p event.EventProcessor) error
	UnRegisterEventProcessor(event string)
	DispatchEvent(event string)

	//RegisterUpdateProcessor
	RegisterUpdateProcessor(name string, p UpdateProcessor) error
	UnRegisterUpdateProcessor(name string) error
	StartUpdateProcessor(name string) (resErr error)
	StopUpdateProcessor(name string) (resErr error)
	IsUpdateProcessorRunning(name string) (bool, error)

	//logger
	GetLogger() nxlog.Logger

	//requester
	GetRequesterURL() string
	SetRequesterPostURI(path string, queryPair map[string]string) error
	GetRequesterPostURI() string
	GetRequesterPostQuery() string
	RequesterPost() (string, error)

	//user management
	GetUser(userID int) (entity.User, bool)
	DisconnectUser(userID int)
	RemoveUser(userID int)
	GetUsers() []entity.User
	GetUserConnIDs() []string

	//manager
	GetHallManager() HallManager
	GetRoomManager() RoomManager

	//for grpc server
	//fn is grpc protobuf serivce register function.
	//example pb.RegisterGreeterServer(s, &server{}),  s==*grpc.Server, &server{}=serverStruct{} contain rpc methods.
	//StartGRPCServer(addr, port string, registerServerFn interface{}, serverStruct interface{})
	StartGRPCServer(registerServerFn interface{}, serverStruct interface{}) error
	GetGRPCClient(addr, port string, newClientFunc interface{}) (interface{}, error)
	GetGRPClientConn(address, port string) (*grpc.ClientConn, error)

	//http server
	StartHTTPServer(parameters *nxhttp.ServerParameters) error
	StopHTTPServer()
	GetHTTPServerRequestRealAddr(r *http.Request) (string, error)
	RegisterHTTPServerHandler(pattern string, handler http.Handler)
}

Nex is used for main process

func NewNex

func NewNex(configFilePath string) (Nex, error)

NewNex used to new Nex instance. Call this to new not Nex{}.

type PacketHandler

type PacketHandler interface {
	Handle(packetData *websocket.ReceivePacketData) ([]byte, string, error)
}

func NewPacketHandler

func NewPacketHandler() (PacketHandler, error)

type RoomManager

type RoomManager interface {
	CreateRoom(roomID int, typ int, name string) (entity.Room, error)
	RemoveRoom(roomID int)
	GetRoom(roomID int) (entity.Room, bool)
	GetRooms() []entity.Room
}

func NewRoomManager

func NewRoomManager() (RoomManager, error)

type UpdateManager

type UpdateManager interface {
	RegisterProcessor(name string, p UpdateProcessor) error
	UnRegisterProcessor(name string) error

	StartProcessor(name string) (resErr error)
	StopProcessor(name string) (resErr error)
	IsRunning(name string) (bool, error)
}

func NewUpdateManager

func NewUpdateManager() (UpdateManager, error)

type UpdateObject

type UpdateObject struct {
	Name string
	User entity.User
}

type UpdateProcessor

type UpdateProcessor interface {
	Run() error
	StopChan() chan bool     //get a chan used for stop routine
	Duration() time.Duration //get update duration
	Running() bool           //return if processor is running
	SetRunning(bool)         //return if processor is running
}

type UserManager

type UserManager interface {
	CreateUser(connID string) (entity.User, error)
	RemoveUser(userID int)
	GetUser(userID int) (entity.User, bool)
	GetUserByConnID(connID string) (entity.User, bool)
	GetUsers() []entity.User
}

func NewUserManager

func NewUserManager() (UserManager, error)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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