func AddUserPositionValidation(validator func(*user.User, int64, int64, int64) bool)
AddUserPositionValidation registers a validation function to be called on Sync
func AfterDisappearCmd(callback func(*user.User, func(error)))
AfterDisappearCmd registers a command to be executed after field disappear: Must be called before ExposeCommands()
func AfterSyncCmd(callback func(*user.User, func(error)))
AfterSyncCmd registers a command to be executed after field sync: Must be called before ExposeCommands()
func AfterSyncInitCmd(callback func(*user.User, func(error)))
AfterSyncInitCmd registers a command to be executed after field sync: Must be called before ExposeCommands()
func BeforeDisappearCmd(callback func(*user.User, func(error)))
BeforeDisappearCmd registers a command to be executed before field disappear: Must be called before ExposeCommands()
func BeforeSyncCmd(callback func(*user.User, func(error)))
BeforeSyncCmd registers a command to be executed before field sync: Must be called before ExposeCommands()
func BeforeSyncInitCmd(callback func(*user.User, func(error)))
BeforeSyncInitCmd registers a command to be executed before field sync: Must be called before ExposeCommands()
func Broadcast(userData *user.User, uList []*UserEntity, ver uint8, cmd uint16, msg []byte, reliable bool)
Broadcast broadcast a message to the list of users found by Sync or FindUsersByPosition function
func Disappear(userData *user.User, syncLimit int)
Disappear removes the user client from field map
func DiscardNPCByID(id string) bool
DiscardNPCByID deletes an NPC with matching ID
func ExposeCommands()
ExposeCommands exposes commands to the client to work with Field package
func GetGridRangeByGridKey(gridKey string) (int64, int64, int64, int64)
GetGridRangeByGridKey returns grid range of x and y. This function works only with grids of the node.
return x1, x2, y1, y2
Grid key is obtained by GetGrids()
func GetGridSize() int64
GetGridSize returns the size of the grid
func GetGrids() []string
GetGrids returns a list of grids the node is assigned to
func GetServerSyncLimit() int
GetServerSyncLimit returns serverSyncLimit
func GetUserPosition(userData *user.User) (int64, int64, int64)
GetUserPosition returns the current user position: x, y, z
func IsUserInSight(mySID, otherSID string) bool
IsUserInSight evalutes if the remote user is in sight or not
func IsWS()
IsWS called internally by commands.go
func Reconnect(meshEndPoint string, userData *user.User, callback func(error, string))
Reconnect returns the end point of the node for the client to reconnect to
func ServerSync(x, y, z int64, message []byte, syncLimit int, reliable bool)
ServerSync sends messages from the server to the clients within the view
This is meant to be used to synchronize NPC entities from the server to the client.
func SetCustomFilter(customFilterID uint8, filter func(*user.User, []*UserEntity) []*UserEntity)
SetCustomFilter defines a custom filter and create an association to the ID
func SetOnGridUpdated(callback func([]string))
SetOnGridUpdated registers a callback function on grid map updated.
The callback receives grid x1, y1 and grid x2, y2.
Each node has two sets of grid x and y
func SetOnNPCTransported(callback func(*Npc))
SetOnNPCTransported registers a callback on NPC transport
func Setup(confpath string)
Setup setup Field module
func Sync(userData *user.User, x, y, z int64, syncLimit int, msg []byte) (string, []*UserEntity, []*UserEntity, [][]byte)
Sync updates its own position and find others within the sight (fieldOfVisionSize) - z is used as a literal value instead of range like x and y. Use z value to create "different" area.
// returned values: meshNodeEndPoint, usersInSight, usersNotInSight, userMessageListInsight := field.Sync(userData, x, y, z, syncLimit, message)
func SyncBroadcast( userData *user.User, x, y, z int64, syncLimit int, ver uint8, cmd uint16, msg []byte, reliable bool, )
SyncBroadcast sends a message to clients that are in sight with ver and cmd given
func TestCalcDistance(myX, myY, yourX, yourY int64) int64
TestCalcDistance [USED ONLY FOR INTERNAL UNIT TEST]
func Update(userData *user.User, x int64, y int64, z int64) string
Update updates the given userData(client)'s position data and returns a mesh address if the client must reconnect to a new mesh node
GridData grid data with size and coordinate
type GridData struct {
// contains filtered or unexported fields
}
func GetMyGrids() []*GridData
GetMyGrids returns an array of grids of the node
func (gd *GridData) GetKey() string
GetKey returns the unique key of the grid
func (gd *GridData) GetPosition() (int64, int64)
GetPosition returns X and Y coordinate of the grid
x, y := gd.GetPosition()
Npc is a server-controlled non-playable-character data struct.
Npc should be synchronized with clients via ServerSync and should NEVER be controlled by clients
type Npc struct { sync.RWMutex // contains filtered or unexported fields }
func GetNPCByID(id string) *Npc
GetNPCByID returns an NPC with matching ID
func NewNPC(x int64, y int64, z int64) (*Npc, error)
NewNPC creates a new non-player-character entity to be controlled on the server and synchronized by ServerSync.
func (n *Npc) Get(key string) interface{}
Get returns custom property value
func (n *Npc) GetID() string
GetID returns the unique ID of NPC
func (n *Npc) GetPosition() (int64, int64, int64)
GetPosition returns X, Y position and Z space
func (n *Npc) Incr(key string, delta int) (int, error)
Incr increments custom property (it must be an int) by delta
func (n *Npc) Set(key string, value interface{})
Set sets custom property
func (n *Npc) SetOnDiscard(cb func(*Npc))
SetOnDiscard registers a callback function to be invoked on DiscardNPCByID
func (n *Npc) SetPosition(x int64, y int64, z int64)
SetPosition sets X, Y position and Z space
UserEntity a user data structure
type UserEntity struct {
// contains filtered or unexported fields
}
func ApplyCustomFilter(customFilterID uint8, userData *user.User, uList []*UserEntity) []*UserEntity
ApplyCustomFilter executes pre-defined custom filter on the list of UserEntity to controll who to broadcast to
func FindUsersByPosition(x, y, z int64, howMany int) ([]*UserEntity, []string)
FindUsersByPosition returns an array of user entities and an array of user IDs within the given area (x, y, z)
func (ue *UserEntity) GetCoordinates() (int64, int64)
GetCoordinates returns the X,Y position of UserEntity
func (ue *UserEntity) GetDistance() int64
GetDistance returns the distance between the user and you
func (ue *UserEntity) GetID() string
GetID returnns the user ID of the user
func (ue *UserEntity) GetSID() string
GetSID returns SID of the user