core

package
v0.0.0-...-88cc3fa Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: Apache-2.0 Imports: 5 Imported by: 5

Documentation

Overview

Package core contains all the tools to make and work with Users and Rooms.

A User is a client who has successfully logged into the server. You can think of clients who are not attached to a User as, for instance, someone in the login screen, but are still connected to the server. A client doesn't have to be a User to be able to call your CustomClientActions, so keep that in mind when making them (Refer to the Usage for CustomClientActions).

Users have their own variables which can be accessed and changed anytime. A User variable can be anything compatible with interface{}, so pretty much anything.

A Room represents a place on the server where a User can join other Users. Rooms can either be public or private. Private Rooms must be assigned an "owner", which is the name of a User, or the ServerName from ServerSettings. The server's name that will be used for ownership of private Rooms can be set with the ServerSettings option ServerName when starting the server. Though keep in mind, setting the ServerName in ServerSettings will prevent a User who wants to go by that name from logging in. Public Rooms will accept a join request from any User, and private Rooms will only accept a join request from someone who is on it's invite list. Only the owner of the Room or the server itself can invite Users to a private Room. But remember, just because a User owns a private room doesn't mean the server cannot also invite to the room via *Room.AddInvite() function.

Rooms have their own variables which can be accessed and changed anytime. Like User variables, a Room variable can be anything compatible with interface{}.

Index

Constants

View Source
const (
	MessageTypeChat = iota
	MessageTypeServer
)

These represent the types of room messages the server sends.

View Source
const (
	ServerMessageGame = iota
	ServerMessageNotice
	ServerMessageImportant
)

These are the sub-types that a MessageTypeServer will come with. Ordered by their visible priority for your UI.

View Source
const (
	StatusAvailable = iota // User is available
	StatusInGame           // User is in a game
	StatusIdle             // User is idle
	StatusOffline          // User is offline
)

These represent the four statuses a User could be.

Variables

View Source
var (

	// LoginCallback is only for internal Gopher Game Server mechanics.
	LoginCallback func(string, int, map[string]interface{}, map[string]interface{}) bool
	// LogoutCallback is only for internal Gopher Game Server mechanics.
	LogoutCallback func(string, int)
)

Functions

func AutoLogIn

func AutoLogIn(tag string, pass string, newPass string, dbID int, conn *websocket.Conn, connUser **User, clientMux *sync.Mutex) (string, helpers.GopherError)

AutoLogIn logs a user in automatically with RememberMe and SqlFeatures enabled in ServerSettings.

WARNING: This is only meant for internal Gopher Game Server mechanics. If you want the "Remember Me" (AKA auto login) feature, enable it in ServerSettings along with the SqlFeatures and corresponding options. You can read more about the "Remember Me" login in the project's usage section.

func GetRoomTypes

func GetRoomTypes() map[string]*RoomType

GetRoomTypes gets a map of all the RoomTypes.

func GetRoomsState

func GetRoomsState() map[string]RoomRecoveryState

GetRoomsState is only for internal Gopher Game Server mechanics.

func Login

func Login(userName string, dbID int, autologPass string, isGuest bool, remMe bool, socket *websocket.Conn,
	connUser **User, clientMux *sync.Mutex) (string, helpers.GopherError)

Login logs a User in to the service.

func Pause

func Pause()

Pause is only for internal Gopher Game Server mechanics.

func Resume

func Resume()

Resume is only for internal Gopher Game Server mechanics.

func RoomCount

func RoomCount() int

RoomCount returns the number of Rooms created on the server.

func SetChatMessageCallback

func SetChatMessageCallback(cb func(string, *Room, interface{}))

SetChatMessageCallback sets the callback function for when a *User sends a chat message to a *Room. The function passed must have the same parameter types as the following example:

   func onChatMessage(userName string, room *core.Room, message interface{}) {
	     //code...
	 }

func SetPrivateMessageCallback

func SetPrivateMessageCallback(cb func(*User, *User, interface{}))

SetPrivateMessageCallback sets the callback function for when a *User sends a private message to another *User. The function passed must have the same parameter types as the following example:

   func onPrivateMessage(from *core.User, to *core.User, message interface{}) {
	     //code...
	 }

func SetServerMessageCallback

func SetServerMessageCallback(cb func(*Room, int, interface{}))

SetServerMessageCallback sets the callback function for when the server sends a message to a *Room. The function passed must have the same parameter types as the following example:

   func onServerMessage(room *core.Room, messageType int, message interface{}) {
	     //code...
	 }

The messageType value can be one of: core.ServerMessageGame, core.ServerMessageNotice, core.ServerMessageImportant, or a custom value you have set.

func SetServerStarted

func SetServerStarted(val bool)

SetServerStarted is for Gopher Game Server internal mechanics only.

func SettingsSet

func SettingsSet(kickDups bool, name string, deleteOnLeave bool, sqlFeat bool, remMe bool, multiConn bool, maxConns uint8)

SettingsSet is for Gopher Game Server internal mechanics only.

func UserCount

func UserCount() int

UserCount returns the number of Users logged into the server.

Types

type Room

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

Room represents a room on the server that Users can join and leave. Use core.NewRoom() to make a new Room.

WARNING: When you use a *Room object in your code, DO NOT dereference it. Instead, there are many methods for *Room for maniupulating and retrieving any information about them you could possibly need. Dereferencing them could cause data races in the Room fields that get locked by mutexes.

func GetRoom

func GetRoom(roomName string) (*Room, error)

GetRoom finds a Room on the server. If the room does not exit, an error will be returned.

func NewRoom

func NewRoom(name string, rType string, isPrivate bool, maxUsers int, owner string) (*Room, error)

NewRoom adds a new room to the server. This can be called before or after starting the server. Parameters:

- name (string): Name of the Room

- rType (string): Room type name (Note: must be a valid RoomType's name)

- isPrivate (bool): Indicates if the room is private or not

- maxUsers (int): Maximum User capacity (Note: 0 means no limit)

- owner (string): The owner of the room. If provided a blank string, will set the owner to the ServerName from ServerSettings

func (*Room) AddInvite

func (r *Room) AddInvite(userName string) error

AddInvite adds a User to a private Room's invite list. This is only meant for internal Gopher Game Server mechanics. If you want a User to invite someone to a private room, use the *User.Invite() function instead.

NOTE: Remember that private rooms are designed to have an "owner", and only the owner should be able to send an invite and revoke an invitation for their Rooms. Also, *User.Invite() will send an invite notification message to the invited User that the client API can easily receive. Though if you wish to make your own implementations for sending and receiving these notifications, this function is safe to use.

func (*Room) AddUser

func (r *Room) AddUser(user *User, connID string) error

AddUser adds a User to the Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when adding a User to a Room with MultiConnect enabled. Otherwise, an empty string can be used.

func (*Room) ChatMessage

func (r *Room) ChatMessage(author string, message interface{}) error

ChatMessage sends a chat message to all Users in the Room.

func (*Room) DataMessage

func (r *Room) DataMessage(message interface{}, recipients []string) error

DataMessage sends a data message to the specified recipients in the Room. The parameter recipients can be nil or an empty slice of string. In which case, the data message will be sent to all Users in the Room.

func (*Room) Delete

func (r *Room) Delete() error

Delete deletes the Room from the server. Will also send a room leave message to all the Users in the Room that you can capture with the client APIs.

func (*Room) GetUserMap

func (r *Room) GetUserMap() (map[string]*RoomUser, error)

GetUserMap retrieves all the RoomUsers as a map[string]*RoomUser.

func (*Room) GetVariable

func (r *Room) GetVariable(key string) (interface{}, error)

GetVariable gets one of the Room's variables.

func (*Room) GetVariables

func (r *Room) GetVariables(keys []string) (map[string]interface{}, error)

GetVariables gets all the specified (or all if not) Room variables as a map[string]interface{}.

func (*Room) InviteList

func (r *Room) InviteList() ([]string, error)

InviteList gets a private Room's invite list.

func (*Room) IsPrivate

func (r *Room) IsPrivate() bool

IsPrivate returns true of the Room is private.

func (*Room) MaxUsers

func (r *Room) MaxUsers() int

MaxUsers gets the maximum User capacity of the Room.

func (*Room) Name

func (r *Room) Name() string

Name gets the name of the Room.

func (*Room) NumUsers

func (r *Room) NumUsers() int

NumUsers gets the number of Users in the Room.

func (*Room) Owner

func (r *Room) Owner() string

Owner gets the name of the owner of the room

func (*Room) RemoveInvite

func (r *Room) RemoveInvite(userName string) error

RemoveInvite removes a User from a private Room's invite list. To make a User remove someone from their room themselves, use the *User.RevokeInvite() function.

NOTE: You can use this function safely, but remember that private rooms are designed to have an "owner", and only the owner should be able to send an invite and revoke an invitation for their Rooms. But if you find the need to break the rules here, by all means do so!

func (*Room) RemoveUser

func (r *Room) RemoveUser(user *User, connID string) error

RemoveUser removes a User from the room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when removing a User from a Room with MultiConnect enabled. Otherwise, an empty string can be used.

func (*Room) ServerMessage

func (r *Room) ServerMessage(message interface{}, messageType int, recipients []string) error

ServerMessage sends a server message to the specified recipients in the Room. The parameter recipients can be nil or an empty slice of string. In which case, the server message will be sent to all Users in the Room.

func (*Room) SetVariable

func (r *Room) SetVariable(key string, value interface{})

SetVariable sets a Room variable.

func (*Room) SetVariables

func (r *Room) SetVariables(values map[string]interface{})

SetVariables sets all the specified Room variables at once.

func (*Room) Type

func (r *Room) Type() string

Type gets the type of the Room.

func (*Room) VoiceStream

func (r *Room) VoiceStream(userName string, userSocket *websocket.Conn, stream interface{})

VoiceStream sends a voice stream from the client API to all the users in the room besides the user who is speaking.

type RoomRecoveryState

type RoomRecoveryState struct {
	T string                 // rType
	P bool                   // private
	O string                 // owner
	M int                    // maxUsers
	I []string               // inviteList
	V map[string]interface{} // vars
}

RoomRecoveryState is used internally for persisting room states on shutdown.

type RoomType

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

RoomType represents a type of room a client or the server can make. You can only make and set options for a RoomType before starting the server. Doing so at any other time will have no effect at all.

func NewRoomType

func NewRoomType(name string, serverOnly bool) *RoomType

NewRoomType Adds a RoomType to the server. A RoomType is used in conjunction with it's corresponding callbacks and options. You cannot make a Room on the server until you have at least one RoomType to set it to. A RoomType requires at least a name and the serverOnly option, which when set to true will prevent the client API from being able to create, destroy, invite or revoke an invitation with that RoomType. Though you can always make a CustomClientAction to create a Room, initialize it, send requests, etc. When making a new RoomType you can chain the broadcasts and callbacks you want for it like so:

rooms.NewRoomType("lobby", true).EnableBroadcastUserEnter().EnableBroadcastUserLeave().
     .SetCreateCallback(yourFunc).SetDeleteCallback(anotherFunc)

func (*RoomType) BroadcastUserEnter

func (r *RoomType) BroadcastUserEnter() bool

BroadcastUserEnter returns true if this RoomType has a user entry broadcast

func (*RoomType) BroadcastUserLeave

func (r *RoomType) BroadcastUserLeave() bool

BroadcastUserLeave returns true if this RoomType has a user leave broadcast

func (*RoomType) CreateCallback

func (r *RoomType) CreateCallback() func(*Room)

CreateCallback returns the function that this RoomType calls when a Room of this RoomType is created.

func (*RoomType) DeleteCallback

func (r *RoomType) DeleteCallback() func(*Room)

DeleteCallback returns the function that this RoomType calls when a Room of this RoomType is deleted.

func (*RoomType) EnableBroadcastUserEnter

func (r *RoomType) EnableBroadcastUserEnter() *RoomType

EnableBroadcastUserEnter sends an "entry" message to all Users in the Room when another User enters the Room. You can capture these messages on the client side easily with the client APIs.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) EnableBroadcastUserLeave

func (r *RoomType) EnableBroadcastUserLeave() *RoomType

EnableBroadcastUserLeave sends a "left" message to all Users in the Room when another User leaves the Room. You can capture these messages on the client side easily with the client APIs.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) EnableVoiceChat

func (r *RoomType) EnableVoiceChat() *RoomType

EnableVoiceChat Enables voice chat for this RoomType.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) HasCreateCallback

func (r *RoomType) HasCreateCallback() bool

HasCreateCallback returns true if this RoomType has a creation callback.

func (*RoomType) HasDeleteCallback

func (r *RoomType) HasDeleteCallback() bool

HasDeleteCallback returns true if this RoomType has a delete callback.

func (*RoomType) HasUserEnterCallback

func (r *RoomType) HasUserEnterCallback() bool

HasUserEnterCallback returns true if this RoomType has a user enter callback.

func (*RoomType) HasUserLeaveCallback

func (r *RoomType) HasUserLeaveCallback() bool

HasUserLeaveCallback returns true if this RoomType has a user leave callback.

func (*RoomType) ServerOnly

func (r *RoomType) ServerOnly() bool

ServerOnly returns true if the RoomType can only be manipulated by the server.

func (*RoomType) SetCreateCallback

func (r *RoomType) SetCreateCallback(callback func(*Room)) *RoomType

SetCreateCallback is executed when someone creates a Room of this RoomType by setting the creation callback. Your function must take in a Room object as the parameter which is a reference of the created room.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) SetDeleteCallback

func (r *RoomType) SetDeleteCallback(callback func(*Room)) *RoomType

SetDeleteCallback is executed when someone deletes a Room of this RoomType by setting the delete callback. Your function must take in a Room object as the parameter which is a reference of the deleted room.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) SetUserEnterCallback

func (r *RoomType) SetUserEnterCallback(callback func(*Room, *RoomUser)) *RoomType

SetUserEnterCallback is executed when a User enters a Room of this RoomType by setting the User enter callback. Your function must take in a Room and a string as the parameters. The Room is the Room in which the User entered, and the string is the name of the User that entered.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) SetUserLeaveCallback

func (r *RoomType) SetUserLeaveCallback(callback func(*Room, *RoomUser)) *RoomType

SetUserLeaveCallback is executed when a User leaves a Room of this RoomType by setting the User leave callback. Your function must take in a Room and a string as the parameters. The Room is the Room in which the User left, and the string is the name of the User that left.

Note: You must call this BEFORE starting the server in order for it to take effect.

func (*RoomType) UserEnterCallback

func (r *RoomType) UserEnterCallback() func(*Room, *RoomUser)

UserEnterCallback returns the function that this RoomType calls when a User enters a Room of this RoomType.

func (*RoomType) UserLeaveCallback

func (r *RoomType) UserLeaveCallback() func(*Room, *RoomUser)

UserLeaveCallback returns the function that this RoomType calls when a User leaves a Room of this RoomType.

func (*RoomType) VoiceChatEnabled

func (r *RoomType) VoiceChatEnabled() bool

VoiceChatEnabled returns true if voice chat is enabled for this RoomType

type RoomUser

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

RoomUser represents a User inside of a Room. Use the *RoomUser.User() function to get a *User from a *RoomUser

func (*RoomUser) ConnectionIDs

func (u *RoomUser) ConnectionIDs() []string

ConnectionIDs returns a []string of all the RoomUser's connection IDs. With MultiConnect in ServerSettings enabled, this will give you all the connections for this User that are currently in the Room. Otherwise, if you want all the User's connection IDs (not just the connections in the specified Room), use *User.ConnectionIDs() after getting the *User object with the *RoomUser.User() function.

func (*RoomUser) User

func (u *RoomUser) User() *User

User gets the *User object of a *RoomUser.

type User

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

User represents a client who has logged into the service. A User can be a guest, join/leave/create rooms, and call any client action, including your custom client actions. If you are not using the built-in authentication, be aware that you will need to make sure any client who has not been authenticated by the server can't simply log themselves in through the client API. A User has a lot of useful information, so it's highly recommended you look through all the *User methods to get a good understanding about everything you can do with them.

WARNING: When you use a *User object in your code, DO NOT dereference it. Instead, there are many methods for *User for retrieving any information about them you could possibly need. Dereferencing them could cause data races (which will panic and stop the server) in the User fields that get locked for synchronizing access.

func GetUser

func GetUser(userName string) (*User, error)

GetUser finds a logged in User by their name. Returns an error if the User is not online.

func (*User) AcceptFriendRequest

func (u *User) AcceptFriendRequest(friendName string) error

AcceptFriendRequest accepts a friend request from another User by their name.

func (*User) ConnectionIDs

func (u *User) ConnectionIDs() []string

ConnectionIDs returns a []string of all the User's connection IDs

func (*User) DataMessage

func (u *User) DataMessage(data interface{}, connID string)

DataMessage sends a data message directly to the User.

func (*User) DatabaseID

func (u *User) DatabaseID() int

DatabaseID gets the database table index of the User.

func (*User) DeclineFriendRequest

func (u *User) DeclineFriendRequest(friendName string) error

DeclineFriendRequest declines a friend request from another User by their name.

func (*User) FriendRequest

func (u *User) FriendRequest(friendName string) error

FriendRequest sends a friend request to another User by their name.

func (*User) Friends

func (u *User) Friends() map[string]database.Friend

Friends gets the Friend list of the User as a map[string]database.Friend where the key string is the friend's User name.

func (*User) GetVariable

func (u *User) GetVariable(key string, connID string) interface{}

GetVariable gets one of the User's variables by it's key. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when getting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) GetVariables

func (u *User) GetVariables(keys []string, connID string) map[string]interface{}

GetVariables gets the specified (or all if nil) User variables as a map[string]interface{}. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when getting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Invite

func (u *User) Invite(invUser *User, connID string) error

Invite allows Users to invite other Users to their private Rooms. The inviting User must be in the Room, and the Room must be private and owned by the inviting User. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when making a User invite another with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) IsGuest

func (u *User) IsGuest() bool

IsGuest returns true if the User is a guest.

func (*User) Join

func (u *User) Join(r *Room, connID string) error

Join makes a User join a Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when making a User join a Room with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Kick

func (u *User) Kick()

Kick will log off all connections on this User.

func (*User) Leave

func (u *User) Leave(connID string) error

Leave makes a User leave their current room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when making a User leave a Room with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Logout

func (u *User) Logout(connID string)

Logout logs a User out from the service. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when logging a User out with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Name

func (u *User) Name() string

Name gets the name of the User.

func (*User) PrivateMessage

func (u *User) PrivateMessage(userName string, message interface{})

PrivateMessage sends a private message to another User by name.

func (*User) RemoveFriend

func (u *User) RemoveFriend(friendName string) error

RemoveFriend removes a friend from this this User and this User from the friend's Friend list.

func (*User) RevokeInvite

func (u *User) RevokeInvite(revokeUser string, connID string) error

RevokeInvite revokes the invite to the specified user to their current Room, provided they are online, the Room is private, and this User is the owner of the Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when making a User revoke an invite with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) RoomIn

func (u *User) RoomIn(connID string) *Room

RoomIn gets the Room that the User is currently in. A nil Room pointer means the User is not in a Room. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when getting a User's Room with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) SetStatus

func (u *User) SetStatus(status int)

SetStatus sets the status of a User. Also sends a notification to all the User's Friends (with the request status "accepted") that they changed their status.

func (*User) SetVariable

func (u *User) SetVariable(key string, value interface{}, connID string)

SetVariable sets a User variable. The client API of the User will also receive these changes. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when setting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) SetVariables

func (u *User) SetVariables(values map[string]interface{}, connID string)

SetVariables sets all the specified User variables at once. The client API of the User will also receive these changes. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to the inviting User. This must be provided when setting a User's variables with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Socket

func (u *User) Socket(connID string) *websocket.Conn

Socket gets the WebSocket connection of a User. If you are using MultiConnect in ServerSettings, the connID parameter is the connection ID associated with one of the connections attached to that User. This must be provided when getting a User's socket connection with MultiConnect enabled. Otherwise, an empty string can be used.

func (*User) Status

func (u *User) Status() int

Status gets the status of the User.

Jump to

Keyboard shortcuts

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