Skip to content

Commit

Permalink
Merge pull request #11 from shutterbug2000/matchmaking-common-2
Browse files Browse the repository at this point in the history
Matchmaking protocol additions and changes
  • Loading branch information
jonbarrow authored May 23, 2023
2 parents 175bbe4 + 3e7de77 commit 618b185
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 35 deletions.
59 changes: 58 additions & 1 deletion globals/matchmaking_globals.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common_globals
import (
match_making "github.com/PretendoNetwork/nex-protocols-go/match-making"
"math"
"reflect"
)

type CommonMatchmakeSession struct {
Expand All @@ -9,4 +11,59 @@ type CommonMatchmakeSession struct {
ConnectionIDs []uint32 //players in the room, referenced by their connection IDs. This is used instead of the PID in order to ensure we're talking to the correct client (in case of e.g. multiple logins)
}

var Sessions = []CommonMatchmakeSession{}
var Sessions map[uint32]*CommonMatchmakeSession
var CurrentGatheringID uint32

func DeleteIndex(s []uint32, index int) []uint32 {
return append(s[:index], s[index+1:]...)
}

func RemoveConnectionIDFromRoom(clientConnectionID uint32, gathering uint32) {
for index, connectionID := range Sessions[gathering].ConnectionIDs {
if connectionID == clientConnectionID {
Sessions[gathering].ConnectionIDs = DeleteIndex(Sessions[gathering].ConnectionIDs, index)
}
}
if len(Sessions[gathering].ConnectionIDs) == 0 {
delete(Sessions, gathering)
}
}

func FindClientSession(clientConnectionID uint32) uint32 {
for gatheringID := range Sessions {
for _, connectionID := range Sessions[gatheringID].ConnectionIDs {
if connectionID == clientConnectionID {
return gatheringID
}
}
}
return math.MaxUint32
}

func RemoveConnectionIDFromAllSessions(clientConnectionID uint32) {
foundSession := FindClientSession(clientConnectionID)
if(foundSession != math.MaxUint32){
RemoveConnectionIDFromRoom(clientConnectionID, foundSession)
}
}

func FindSearchMatchmakeSession(searchMatchmakeSession match_making.MatchmakeSession) int {
returnSessionIndex := math.MaxUint32
//this portion finds any sessions that match the search session. It does not care about anything beyond that, such as if the match is already full. This is handled below.
candidateSessionIndexes := make([]uint32, 0, len(Sessions))
for index, session := range Sessions {
if reflect.DeepEqual(session.SearchMatchmakeSession, searchMatchmakeSession) { // TODO - for Jon: Equals in StructureInterface
candidateSessionIndexes = append(candidateSessionIndexes, index)
}
}
for _, sessionIndex := range candidateSessionIndexes {
sessionToCheck := Sessions[sessionIndex]
if len(sessionToCheck.ConnectionIDs) >= int(sessionToCheck.GameMatchmakeSession.MaximumParticipants) {
continue
} else {
returnSessionIndex = int(sessionIndex) //found a match
break
}
}
return returnSessionIndex
}
7 changes: 4 additions & 3 deletions matchmake-extension/auto_matchmake_postpone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ func AutoMatchmake_Postpone(err error, client *nex.Client, callID uint32, matchm
matchmakeSessionCopy := match_making.NewMatchmakeSession()
json.Unmarshal(tmp, &matchmakeSessionCopy)
searchMatchmakeSession := commonMatchmakeExtensionProtocol.CleanupSearchMatchmakeSessionHandler(*matchmakeSessionCopy)
sessionIndex := FindSearchMatchmakeSession(searchMatchmakeSession)
sessionIndex := uint32(common_globals.FindSearchMatchmakeSession(searchMatchmakeSession))
if sessionIndex == math.MaxUint32 {
session := common_globals.CommonMatchmakeSession{
SearchMatchmakeSession: searchMatchmakeSession,
GameMatchmakeSession: *matchmakeSession,
}
sessionIndex = len(common_globals.Sessions)
common_globals.Sessions = append(common_globals.Sessions, session)
sessionIndex = common_globals.CurrentGatheringID
common_globals.Sessions[sessionIndex] = &session
common_globals.Sessions[sessionIndex].GameMatchmakeSession.Gathering.ID = uint32(sessionIndex)
common_globals.Sessions[sessionIndex].GameMatchmakeSession.Gathering.OwnerPID = client.PID()
common_globals.Sessions[sessionIndex].GameMatchmakeSession.Gathering.HostPID = client.PID()
common_globals.CurrentGatheringID++
}

common_globals.Sessions[sessionIndex].ConnectionIDs = append(common_globals.Sessions[sessionIndex].ConnectionIDs, client.ConnectionID())
Expand Down
31 changes: 0 additions & 31 deletions matchmake-extension/utility.go

This file was deleted.

43 changes: 43 additions & 0 deletions matchmaking-ext/end_participation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package match_making_ext

import (
nex "github.com/PretendoNetwork/nex-go"
match_making_ext "github.com/PretendoNetwork/nex-protocols-go/match-making-ext"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/globals"
)

func EndParticipation(err error, client *nex.Client, callID uint32, idGathering uint32, strMessage string) {
server := commonMatchMakingExtProtocol.server
common_globals.RemoveConnectionIDFromRoom(client.ConnectionID(), idGathering)

rmcResponseStream := nex.NewStreamOut(server)

rmcResponseStream.WriteBool(true) // %retval%

rmcResponseBody := rmcResponseStream.Bytes()

rmcResponse := nex.NewRMCResponse(match_making_ext.ProtocolID, callID)
rmcResponse.SetSuccess(match_making_ext.MethodEndParticipation, rmcResponseBody)

rmcResponseBytes := rmcResponse.Bytes()

var responsePacket nex.PacketInterface

if server.PRUDPVersion() == 0 {
responsePacket, _ = nex.NewPacketV0(client, nil)
responsePacket.SetVersion(0)
} else {
responsePacket, _ = nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
}

responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)

responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)

server.Send(responsePacket)
}
25 changes: 25 additions & 0 deletions matchmaking-ext/protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package match_making_ext

import (
nex "github.com/PretendoNetwork/nex-go"
match_making_ext "github.com/PretendoNetwork/nex-protocols-go/match-making-ext"
"github.com/PretendoNetwork/plogger-go"
)

var commonMatchMakingExtProtocol *CommonMatchMakingExtProtocol
var logger = plogger.NewLogger()

type CommonMatchMakingExtProtocol struct {
*match_making_ext.MatchMakingExtProtocol
server *nex.Server
}

// NewCommonMatchmakeExtensionProtocol returns a new CommonMatchmakeExtensionProtocol
func NewCommonMatchMakingExtProtocol(server *nex.Server) *CommonMatchMakingExtProtocol {
MatchMakingExtProtocol := match_making_ext.NewMatchMakingExtProtocol(server)
commonMatchMakingExtProtocol = &CommonMatchMakingExtProtocol{MatchMakingExtProtocol: MatchMakingExtProtocol, server: server}

MatchMakingExtProtocol.EndParticipation(EndParticipation)

return commonMatchMakingExtProtocol
}
9 changes: 9 additions & 0 deletions matchmaking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
nex "github.com/PretendoNetwork/nex-go"
match_making "github.com/PretendoNetwork/nex-protocols-go/match-making"
"github.com/PretendoNetwork/plogger-go"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/globals"
"fmt"
)

var commonMatchMakingProtocol *CommonMatchMakingProtocol
Expand Down Expand Up @@ -50,10 +52,17 @@ func NewCommonMatchMakingProtocol(server *nex.Server) *CommonMatchMakingProtocol
matchMakingProtocol := match_making.NewMatchMakingProtocol(server)
commonMatchMakingProtocol = &CommonMatchMakingProtocol{MatchMakingProtocol: matchMakingProtocol, server: server}

common_globals.Sessions = make(map[uint32]*common_globals.CommonMatchmakeSession)

commonMatchMakingProtocol.GetSessionURLs(getSessionURLs)
commonMatchMakingProtocol.UnregisterGathering(unregisterGathering)
commonMatchMakingProtocol.UpdateSessionHostV1(updateSessionHostV1)
commonMatchMakingProtocol.UpdateSessionHost(updateSessionHost)

server.On("Kick", func(packet nex.PacketInterface) {
fmt.Println("Leaving")
common_globals.RemoveConnectionIDFromAllSessions(packet.Sender().ConnectionID())
})

return commonMatchMakingProtocol
}

0 comments on commit 618b185

Please sign in to comment.