Skip to main content

Manage Game Sessions

Last updated on

Create a Game Session in the Session Browser

In this section, we will be registering the game session to the session browser. This step will also check if there are enough players in the database. Once enough players have been found, the game session will be registered to the session browser and a Session ID will be created. This ID will be used to register the session in the DSMC.

// GO-SDK session browser service
func createSession(namespaceGame string) (*sessionbrowserclientmodels.ModelsSessionResponse, error) {
allowJoinInProgress := true
currentPlayer := int32(0)
maxPlayer := int32(0)
currentInternalPlayer := int32(0)
maxInternalPlayer := int32(2)
numBot := int32(0)
username := os.Getenv("SESSION_BROWSER_USERNAME")
password := os.Getenv("SESSION_BROWSER_PASSWORD")
mapName := os.Getenv("SESSION_BROWSER_MAP_NAME")
mode := os.Getenv("SESSION_BROWSER_MODE")
sessionType := os.Getenv("SESSION_BROWSER_TYPE")
gameVersion := os.Getenv("SESSION_BROWSER_GAME_VERSION")
var settings interface{}
var sessionResponse *sessionbrowserclientmodels.ModelsSessionResponse
gameSetting := sessionbrowserclientmodels.ModelsGameSessionSetting{
AllowJoinInProgress: &allowJoinInProgress,
CurrentInternalPlayer: &currentInternalPlayer,
CurrentPlayer: &currentPlayer,
MapName: &mapName,
MaxInternalPlayer: &maxInternalPlayer,
MaxPlayer: &maxPlayer,
Mode: &mode,
NumBot: &numBot,
Password: &password,
Settings: &settings,
}
sessionBrowserService := sessionbrowser.SessionService{
Client: factory.NewSessionbrowserClient(&configImpl),
TokenRepository: &tokenRepositoryImpl,
}
body := sessionbrowserclientmodels.ModelsCreateSessionRequest{
GameSessionSetting: &gameSetting,
GameVersion: &gameVersion,
Namespace: &namespaceGame,
SessionType: &sessionType,
Username: &username,
}
input := &session.CreateSessionParams{
Body: &body,
Namespace: namespaceGame,
}
createSessionResp, err := sessionBrowserService.CreateSession(input)
if err != nil {
log.Printf("Unable to create session. namespace : %s. Error: %v", namespaceGame, err)
return createSessionResp, err
}
if createSessionResp == nil {
log.Print("create session response is nil: ", createSessionResp)
return nil, nil
} else {
createSessionResponse := &sessionbrowserclientmodels.ModelsSessionResponse{
CreatedAt: createSessionResp.CreatedAt,
GameSessionSetting: createSessionResp.GameSessionSetting,
GameVersion: createSessionResp.GameVersion,
Joinable: createSessionResp.Joinable,
Match: createSessionResp.Match,
Namespace: createSessionResp.Namespace,
Server: createSessionResp.Server,
SessionID: createSessionResp.SessionID,
SessionType: createSessionResp.SessionType,
UserID: createSessionResp.UserID,
Username: createSessionResp.Username,
}
sessionResponse = createSessionResponse
}
return sessionResponse, nil
}

Register a Game Session to the DSMC

After successfully creating a session, the handler will register a session in the DSMC service using the OAuth Client token.

// GO-SDK session DSMC service
func registerSessionDSMC(sessionId, gameMode, namespaceGame, partyId string,
allUsers []string) (*dsmcclientmodels.ModelsSessionResponse, error) {

var partyAttributes interface{}
var matchingAllies []*dsmcclientmodels.ModelsRequestMatchingAlly
var matchingParties []*dsmcclientmodels.ModelsRequestMatchParty
var partyMembers []*dsmcclientmodels.ModelsRequestMatchMember

for _, userId := range allUsers {
partyMembers = append(partyMembers, &dsmcclientmodels.ModelsRequestMatchMember{UserID: &userId})
}

matchingParty := dsmcclientmodels.ModelsRequestMatchParty{
PartyAttributes: partyAttributes,
PartyID: &partyId,
PartyMembers: partyMembers,
}
matchingParties = append(matchingParties, &matchingParty)

matchingAlly := dsmcclientmodels.ModelsRequestMatchingAlly{MatchingParties: matchingParties}
matchingAllies = append(matchingAllies, &matchingAlly)

clientVersion := ""
configuration := ""
deployment := os.Getenv("DSMC_DEPLOYMENT")
podName := ""
region := ""

dsmcService := dsmc.SessionService{
Client: factory.NewDsmcClient(&configGameImpl),
TokenRepository: oauthService.TokenRepository,
}
body := dsmcclientmodels.ModelsCreateSessionRequest{
ClientVersion: &clientVersion,
Configuration: &configuration,
Deployment: &deployment,
GameMode: &gameMode,
MatchingAllies: matchingAllies,
Namespace: &namespaceGame,
PodName: &podName,
Region: &region,
SessionID: &sessionId,
}
input := &dsmcSession.CreateSessionParams{
Body: &body,
Namespace: namespaceGame,
}
registerSession, registerSessionErr := dsmcService.CreateSession(input)
if registerSessionErr != nil {
log.Print(registerSessionErr)
}
return registerSession, nil
}

Claim a Game Server

This function is used to claim a game server from the DSMC service.

// GO-SDK DSMC service
func claimServer(namespaceGame string, sessionID *string) error {
dsmcService := dsmc.SessionService{
Client: factory.NewDsmcClient(&configGameImpl),
TokenRepository: oauthService.TokenRepository,
}
body := dsmcclientmodels.ModelsClaimSessionRequest{SessionID: sessionID}
input := &dsmcSession.ClaimServerParams{
Body: &body,
Namespace: namespaceGame,
}
claimServerErr := dsmcService.ClaimServer(input)
if claimServerErr != nil {
log.Print(claimServerErr)
}

return nil
}

Get Server Information

This function retrieves the server's information based on a specific session and displays this information in the Matchmaking log.

// GO-SDK DSMC service
func getServer(namespaceGame, sessionID string) (*dsmcclientmodels.ModelsSessionResponse, error) {
dsmcService := dsmc.SessionService{
Client: factory.NewDsmcClient(&configGameImpl),
TokenRepository: oauthService.TokenRepository,
}
input := &dsmcSession.GetSessionParams{
Namespace: namespaceGame,
SessionID: sessionID,
}
getSession, getSessionErr := dsmcService.GetSession(input)
if getSessionErr != nil {
log.Print(getSessionErr)
}
if getSession == nil {
log.Print("get session server from DSMC service is nil")
}

return getSession, nil
}

Add Players to the Game Session

This function adds players to the server in the Session browser. In this function, we use a loop in our code to add players and match allies in the Matchmaking session.

// GO-SDK session browser service
func addPlayer(namespaceGame, userId, sessionId string) (*sessionbrowserclientmodels.ModelsAddPlayerResponse, error) {
asSpectators := false
body := sessionbrowserclientmodels.ModelsAddPlayerRequest{
AsSpectator: &asSpectators,
UserID: &userId,
}
input := &session.AddPlayerToSessionParams{
Body: &body,
Namespace: namespaceGame,
SessionID: sessionId,
}
sessionBrowserService := sessionbrowser.SessionService{
Client: factory.NewSessionbrowserClient(&configImpl),
TokenRepository: &tokenRepositoryImpl,
}
addPlayerResp, addPlayerErr := sessionBrowserService.AddPlayerToSession(input)
if addPlayerErr != nil {
log.Printf("Unable to add player to session id %v. namespace : %s. Error: %v", sessionId, namespaceGame, addPlayerErr)
return addPlayerResp, addPlayerErr
}
if addPlayerResp == nil {
log.Print("add player response is nil: ", addPlayerResp)
return nil, nil
}

log.Printf("Successfully add player. userId: %v. sessionId: %v, namespace: %v", userId, sessionId, namespaceGame)
return addPlayerResp, nil
}

Get Session Update

After adding players to the Matchmaking session, create a function to obtain Matchmaking session updates by a specific Session ID. This function will find when the session was created, its joinable status, and other session parameters.

// GO-SDK session browser service
func getSessionUpdate(namespaceGame, sessionId string) (*sessionbrowserclientmodels.ModelsSessionResponse, error) {
sessionBrowserService := sessionbrowser.SessionService{
Client: factory.NewSessionbrowserClient(&configImpl),
TokenRepository: &tokenRepositoryImpl,
}
input := &session.GetSessionParams{
Namespace: namespaceGame,
SessionID: sessionId,
}
getSession, getSessionErr := sessionBrowserService.GetSession(input)
if getSessionErr != nil {
log.Print(getSessionErr)
return getSession, getSessionErr
}
if getSession == nil {
log.Print("get session response is nil: ", getSession)
} else {
getSessionResponse := &sessionbrowserclientmodels.ModelsSessionResponse{
CreatedAt: getSession.CreatedAt,
GameSessionSetting: getSession.GameSessionSetting,
GameVersion: getSession.GameVersion,
Joinable: getSession.Joinable,
Match: getSession.Match,
Namespace: getSession.Namespace,
Server: getSession.Server,
SessionID: getSession.SessionID,
SessionType: getSession.SessionType,
UserID: getSession.UserID,
Username: getSession.Username,
}
getSession = getSessionResponse
}

log.Printf("Successfully get session update : %+v", *getSession)
return getSession, nil
}