Skip to content

Commit

Permalink
chore: Inline SQL queries with NEX types
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniElectra committed Jan 24, 2025
1 parent 7f067a8 commit bcc53de
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 372 deletions.
64 changes: 22 additions & 42 deletions match-making/database/disconnect_participant.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,21 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
}

for rows.Next() {
var gatheringID uint32
var ownerPID uint64
var hostPID uint64
var minParticipants uint16
var maxParticipants uint16
var participationPolicy uint32
var policyArgument uint32
var flags uint32
var state uint32
var description string
gathering := match_making_types.NewGathering()
var gatheringType string
var participants []uint64

err = rows.Scan(
&gatheringID,
&ownerPID,
&hostPID,
&minParticipants,
&maxParticipants,
&participationPolicy,
&policyArgument,
&flags,
&state,
&description,
&gathering.ID,
&gathering.OwnerPID,
&gathering.HostPID,
&gathering.MinimumParticipants,
&gathering.MaximumParticipants,
&gathering.ParticipationPolicy,
&gathering.PolicyArgument,
&gathering.Flags,
&gathering.State,
&gathering.Description,
&gatheringType,
pqextended.Array(&participants),
)
Expand All @@ -56,30 +47,18 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
}

// * If the gathering is a PersistentGathering and the gathering isn't set to leave when disconnecting, ignore and continue
if gatheringType == "PersistentGathering" && flags & match_making.GatheringFlags.PersistentGatheringLeaveParticipation == 0 {
if gatheringType == "PersistentGathering" && uint32(gathering.Flags) & match_making.GatheringFlags.PersistentGatheringLeaveParticipation == 0 {
continue
}

gathering := match_making_types.NewGathering()
gathering.ID = types.NewUInt32(gatheringID)
gathering.OwnerPID = types.NewPID(ownerPID)
gathering.HostPID = types.NewPID(hostPID)
gathering.MinimumParticipants = types.NewUInt16(minParticipants)
gathering.MaximumParticipants = types.NewUInt16(maxParticipants)
gathering.ParticipationPolicy = types.NewUInt32(participationPolicy)
gathering.PolicyArgument = types.NewUInt32(policyArgument)
gathering.Flags = types.NewUInt32(flags)
gathering.State = types.NewUInt32(state)
gathering.Description = types.NewString(description)

// * Since the participant is leaving, override the participant list to avoid sending notifications to them
participants, nexError = RemoveParticipantFromGathering(manager, gatheringID, uint64(connection.PID()))
participants, nexError = RemoveParticipantFromGathering(manager, uint32(gathering.ID), uint64(connection.PID()))
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
continue
}

nexError = tracking.LogDisconnectGathering(manager.Database, connection.PID(), gatheringID, participants)
nexError = tracking.LogDisconnectGathering(manager.Database, connection.PID(), uint32(gathering.ID), participants)
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
continue
Expand All @@ -88,20 +67,21 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
if len(participants) == 0 {
// * There are no more participants, so we only have to unregister the gathering
// * Since the participant is disconnecting, we don't send notification events
nexError = UnregisterGathering(manager, connection.PID(), gatheringID)
nexError = UnregisterGathering(manager, connection.PID(), uint32(gathering.ID))
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
}

continue
}

ownerPID := uint64(gathering.OwnerPID)
if connection.PID().Equals(gathering.OwnerPID) {
// * This flag tells the server to change the matchmake session owner if they disconnect
// * If the flag is not set, delete the session
// * More info: https://nintendo-wiki.pretendo.network/docs/nex/protocols/match-making/types#flags
if uint32(gathering.Flags) & match_making.GatheringFlags.DisconnectChangeOwner == 0 {
nexError = UnregisterGathering(manager, connection.PID(), gatheringID)
nexError = UnregisterGathering(manager, connection.PID(), uint32(gathering.ID))
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
continue
Expand All @@ -113,7 +93,7 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
oEvent := notifications_types.NewNotificationEvent()
oEvent.PIDSource = connection.PID().Copy().(types.PID)
oEvent.Type = types.NewUInt32(notifications.BuildNotificationType(category, subtype))
oEvent.Param1 = types.NewUInt32(gatheringID)
oEvent.Param1 = gathering.ID

common_globals.SendNotificationEvent(connection.Endpoint().(*nex.PRUDPEndPoint), oEvent, common_globals.RemoveDuplicates(participants))

Expand All @@ -129,7 +109,7 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
// * If the host has disconnected, set the owner as the new host. We can guarantee that the ownerPID is not zero,
// * since otherwise the gathering would have been unregistered by MigrateGatheringOwnership
if connection.PID().Equals(gathering.HostPID) && ownerPID != 0 {
nexError = UpdateSessionHost(manager, gatheringID, types.NewPID(ownerPID), types.NewPID(ownerPID))
nexError = UpdateSessionHost(manager, uint32(gathering.ID), types.NewPID(ownerPID), types.NewPID(ownerPID))
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
} else {
Expand All @@ -139,12 +119,12 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
oEvent := notifications_types.NewNotificationEvent()
oEvent.PIDSource = connection.PID().Copy().(types.PID)
oEvent.Type = types.NewUInt32(notifications.BuildNotificationType(category, subtype))
oEvent.Param1 = types.NewUInt32(gatheringID)
oEvent.Param1 = gathering.ID

// TODO - Should the notification actually be sent to all participants?
common_globals.SendNotificationEvent(connection.Endpoint().(*nex.PRUDPEndPoint), oEvent, common_globals.RemoveDuplicates(participants))

nexError = tracking.LogChangeHost(manager.Database, connection.PID(), gatheringID, gathering.HostPID, types.NewPID(ownerPID))
nexError = tracking.LogChangeHost(manager.Database, connection.PID(), uint32(gathering.ID), gathering.HostPID, types.NewPID(ownerPID))
if nexError != nil {
common_globals.Logger.Error(nexError.Error())
continue
Expand All @@ -158,7 +138,7 @@ func DisconnectParticipant(manager *common_globals.MatchmakingManager, connectio
oEvent := notifications_types.NewNotificationEvent()
oEvent.PIDSource = connection.PID().Copy().(types.PID)
oEvent.Type = types.NewUInt32(notifications.BuildNotificationType(category, subtype))
oEvent.Param1 = types.NewUInt32(gatheringID)
oEvent.Param1 = gathering.ID
oEvent.Param2 = types.NewUInt32(uint32(connection.PID())) // TODO - This assumes a legacy client. Will not work on the Switch

var participantEndedTargets []uint64
Expand Down
39 changes: 10 additions & 29 deletions match-making/database/find_gathering_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,21 @@ import (
func FindGatheringByID(manager *common_globals.MatchmakingManager, id uint32) (match_making_types.Gathering, string, []uint64, types.DateTime, *nex.Error) {
row := manager.Database.QueryRow(`SELECT owner_pid, host_pid, min_participants, max_participants, participation_policy, policy_argument, flags, state, description, type, participants, started_time FROM matchmaking.gatherings WHERE id=$1 AND registered=true`, id)

var ownerPID uint64
var hostPID uint64
var minParticipants uint16
var maxParticipants uint16
var participationPolicy uint32
var policyArgument uint32
var flags uint32
var state uint32
var description string
gathering := match_making_types.NewGathering()
var gatheringType string
var participants []uint64
var startedTime time.Time

gathering := match_making_types.NewGathering()

err := row.Scan(
&ownerPID,
&hostPID,
&minParticipants,
&maxParticipants,
&participationPolicy,
&policyArgument,
&flags,
&state,
&description,
&gathering.OwnerPID,
&gathering.HostPID,
&gathering.MinimumParticipants,
&gathering.MaximumParticipants,
&gathering.ParticipationPolicy,
&gathering.PolicyArgument,
&gathering.Flags,
&gathering.State,
&gathering.Description,
&gatheringType,
pqextended.Array(&participants),
&startedTime,
Expand All @@ -53,15 +43,6 @@ func FindGatheringByID(manager *common_globals.MatchmakingManager, id uint32) (m
}

gathering.ID = types.NewUInt32(id)
gathering.OwnerPID = types.NewPID(ownerPID)
gathering.HostPID = types.NewPID(hostPID)
gathering.MinimumParticipants = types.NewUInt16(minParticipants)
gathering.MaximumParticipants = types.NewUInt16(maxParticipants)
gathering.ParticipationPolicy = types.NewUInt32(participationPolicy)
gathering.PolicyArgument = types.NewUInt32(policyArgument)
gathering.Flags = types.NewUInt32(flags)
gathering.State = types.NewUInt32(state)
gathering.Description = types.NewString(description)

return gathering, gatheringType, participants, types.NewDateTime(0).FromTimestamp(startedTime), nil
}
14 changes: 7 additions & 7 deletions match-making/database/register_gathering.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func RegisterGathering(manager *common_globals.MatchmakingManager, pid types.PID
) RETURNING id`,
pid,
pid,
uint16(gathering.MinimumParticipants),
uint16(gathering.MaximumParticipants),
uint32(gathering.ParticipationPolicy),
uint32(gathering.PolicyArgument),
uint32(gathering.Flags),
uint32(gathering.State),
string(gathering.Description),
gathering.MinimumParticipants,
gathering.MaximumParticipants,
gathering.ParticipationPolicy,
gathering.PolicyArgument,
gathering.Flags,
gathering.State,
gathering.Description,
gatheringType,
startedTime.Standard(),
).Scan(&gatheringID)
Expand Down
26 changes: 13 additions & 13 deletions matchmake-extension/database/create_matchmake_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ func CreateMatchmakeSession(manager *common_globals.MatchmakingManager, connecti
$14,
$15
)`,
uint32(matchmakeSession.Gathering.ID),
uint32(matchmakeSession.GameMode),
matchmakeSession.Gathering.ID,
matchmakeSession.GameMode,
pqextended.Array(attribs),
bool(matchmakeSession.OpenParticipation),
uint32(matchmakeSession.MatchmakeSystemType),
[]byte(matchmakeSession.ApplicationBuffer),
uint32(matchmakeSession.ProgressScore),
[]byte(matchmakeSession.SessionKey),
uint32(matchmakeSession.Option),
matchmakeSession.OpenParticipation,
matchmakeSession.MatchmakeSystemType,
matchmakeSession.ApplicationBuffer,
matchmakeSession.ProgressScore,
matchmakeSession.SessionKey,
matchmakeSession.Option,
matchmakeParam.Bytes(),
string(matchmakeSession.UserPassword),
uint32(matchmakeSession.ReferGID),
bool(matchmakeSession.UserPasswordEnabled),
bool(matchmakeSession.SystemPasswordEnabled),
string(matchmakeSession.CodeWord),
matchmakeSession.UserPassword,
matchmakeSession.ReferGID,
matchmakeSession.UserPasswordEnabled,
matchmakeSession.SystemPasswordEnabled,
matchmakeSession.CodeWord,
)
if err != nil {
return nex.NewError(nex.ResultCodes.Core.Unknown, err.Error())
Expand Down
Loading

0 comments on commit bcc53de

Please sign in to comment.