Skip to content

Commit

Permalink
feat!: Implement Matchmaking Rewrite (#10)
Browse files Browse the repository at this point in the history
* feat(secure): Mark Splatfest sessions as OpenParticipation

Bit of a hack but good enough to make things work for now

* feat(secure): Add stubbed Splatoon Ranking protocol

Fixes freezes related to Splatfest results upload

* feat: matchmaking rewrite implementation

* feat(secure): attempt to fix index bugs

* fix: merge issue

* fix(secure): forgot to change the import name

* fix(secure): remove old index ignore

* fix(secure): misspelling

* fix(secure): remove unused var

* fix(secure): revert previous fix

* fix(secure): using another function

* fix(secure): using another function

* working on it

* fix(secure): i hate these variable names

* fix(secure): misspelled again

* debug(secure): adding debugging

* fix(secure): hopefully fix the error (thanks dani)

* std(secure): standardize variable names

* fix(secure): fix string issue

* fix(secure): oops

* fix(secure): remove attribute 1

* fix(secure): final fixups

* fix(secure): final fixups

* feat(secure): try fixing fest rooms

* feat(secure): implement postgresql fix

* merge

* fix(secure): try once again to fix fests

* fix(secure): ignore first param

* fix: bump library versions and remove override

* chore: go fmt all files

* chore: Remove unused code

* chore: Explicitly ignore error in criteria cleanup

It's fine we just won't mm into this room

---------

Co-authored-by: Ash Logan <[email protected]>
  • Loading branch information
AToska21 and ashquarky authored Nov 7, 2024
1 parent e6811ca commit 238c85d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 75 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ log
*.pem
*.key
*.pcap
*.pcapng
*.pcapng

# misc
.DS_Store
5 changes: 5 additions & 0 deletions globals/globals.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package globals

import (
"database/sql"
pb "github.com/PretendoNetwork/grpc-go/account"
"github.com/PretendoNetwork/nex-go/v2"
"github.com/PretendoNetwork/nex-protocols-common-go/v2/globals"
"github.com/PretendoNetwork/plogger-go"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand All @@ -20,3 +22,6 @@ var SecureEndpoint *nex.PRUDPEndPoint
var GRPCAccountClientConnection *grpc.ClientConn
var GRPCAccountClient pb.AccountClient
var GRPCAccountCommonMetadata metadata.MD

var Postgres *sql.DB
var MatchmakingManager *common_globals.MatchmakingManager
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module github.com/PretendoNetwork/splatoon

go 1.21

toolchain go1.21.9
go 1.22.1

require (
github.com/PretendoNetwork/grpc-go v1.0.2
github.com/PretendoNetwork/nex-go/v2 v2.0.2
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.5
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.3
github.com/PretendoNetwork/nex-go/v2 v2.0.5
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.6
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.4
github.com/PretendoNetwork/plogger-go v1.0.4
github.com/joho/godotenv v1.5.1
google.golang.org/grpc v1.63.2
)

require (
github.com/PretendoNetwork/pq-extended v1.0.0 // indirect
github.com/dolthub/maphash v0.1.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/jwalton/go-supportscolor v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/lxzan/gws v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
github.com/PretendoNetwork/grpc-go v1.0.2 h1:9TvKmX7dCOANyoHEra1MMYqS1N/RGav66TRG4SHInvo=
github.com/PretendoNetwork/grpc-go v1.0.2/go.mod h1:XZjEsij9lL7HJBNkH6JPbBIkUSq/1rjflvjGdv+DAj0=
github.com/PretendoNetwork/nex-go/v2 v2.0.2 h1:mPp27Ero3Rr28nekuKg4f0tdp28NzmCJXgupt2paT18=
github.com/PretendoNetwork/nex-go/v2 v2.0.2/go.mod h1:O5v200Ro8bdrmqSAmzV0cqQsG7cdmwLHAet5X7Jxz20=
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.5 h1:17b7vA8wjWLW9JdJTHeOoX2g2e4S/EwA32m9AuPEA80=
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.5/go.mod h1:0wDEYxDYbUOjsKjsTUjU0/f3pL1MT54h+tGn4UyeB+A=
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.3 h1:80CG7f9w/34MN1vAZ1XP3ec4LJ4mqKlkests+HQzrvc=
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.3/go.mod h1:2UN8khoMDNaeJ4GcIG7ez7MqqmbLfLzbVS6hSa+kGnk=
github.com/PretendoNetwork/nex-go/v2 v2.0.5 h1:S/bYPb2SNUb9MSzai4wlqj/9J1JIiLuMtZcAAbBughM=
github.com/PretendoNetwork/nex-go/v2 v2.0.5/go.mod h1:iW1xjbg/vl2c3uheitUFxGcrt0sxaDxeXR5QqDcyLpI=
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.6 h1:8nNJvJi5aTmGBRtEGVYAI6cjNK1WyIiF+AnGD1QyD+E=
github.com/PretendoNetwork/nex-protocols-common-go/v2 v2.0.6/go.mod h1:Mngar0XMLs7oG9KfeyVJBBhYNCNRXxDcucJTthpP9GY=
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.4 h1:7Vb/jV/cO6c8XcGzGCoowSNXjZDLb/D1lYkLk8rxm7c=
github.com/PretendoNetwork/nex-protocols-go/v2 v2.0.4/go.mod h1:2UN8khoMDNaeJ4GcIG7ez7MqqmbLfLzbVS6hSa+kGnk=
github.com/PretendoNetwork/plogger-go v1.0.4 h1:PF7xHw9eDRHH+RsAP9tmAE7fG0N0p6H4iPwHKnsoXwc=
github.com/PretendoNetwork/plogger-go v1.0.4/go.mod h1:7kD6M4vPq1JL4LTuPg6kuB1OvUBOwQOtAvTaUwMbwvU=
github.com/PretendoNetwork/pq-extended v1.0.0 h1:GHZ0hLvCvmYKQPTV9I9XtTx8J1iB5Z9CEnfW2tUpsYg=
github.com/PretendoNetwork/pq-extended v1.0.0/go.mod h1:bq6Ai+3lG4/M0iamUBt2Uzi5vL/nYy1a1Ar2ow9NDF0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
Expand All @@ -24,6 +26,8 @@ github.com/jwalton/go-supportscolor v1.2.0 h1:g6Ha4u7Vm3LIsQ5wmeBpS4gazu0UP1DRDE
github.com/jwalton/go-supportscolor v1.2.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lxzan/gws v1.8.5 h1:6x+wW3EHtoGFNeCtZP1OVZ1IHrpZZzDaEjQGg1lUJqU=
github.com/lxzan/gws v1.8.5/go.mod h1:FcGeRMB7HwGuTvMLR24ku0Zx0p6RXqeKASeMc4VYgi4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
6 changes: 6 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"database/sql"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -104,4 +105,9 @@ func init() {
globals.GRPCAccountCommonMetadata = metadata.Pairs(
"X-API-Key", accountGRPCAPIKey,
)
globals.Postgres, err = sql.Open("postgres", os.Getenv("PN_SPLATOON_POSTGRES_URI"))
if err != nil {
globals.Logger.Critical(err.Error())
}
globals.Logger.Success("Connected to Postgres!")
}
91 changes: 29 additions & 62 deletions nex/register_common_secure_server_protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package nex
import (
"github.com/PretendoNetwork/nex-go/v2"
"github.com/PretendoNetwork/nex-go/v2/types"
commonnattraversal "github.com/PretendoNetwork/nex-protocols-common-go/v2/nat-traversal"
commonsecure "github.com/PretendoNetwork/nex-protocols-common-go/v2/secure-connection"
nattraversal "github.com/PretendoNetwork/nex-protocols-go/v2/nat-traversal"
secure "github.com/PretendoNetwork/nex-protocols-go/v2/secure-connection"
"github.com/PretendoNetwork/splatoon/globals"
"strconv"
"strings"

commonmatchmaking "github.com/PretendoNetwork/nex-protocols-common-go/v2/match-making"
commonmatchmakingext "github.com/PretendoNetwork/nex-protocols-common-go/v2/match-making-ext"
commonmatchmakeextension "github.com/PretendoNetwork/nex-protocols-common-go/v2/matchmake-extension"
commonnattraversal "github.com/PretendoNetwork/nex-protocols-common-go/v2/nat-traversal"
commonranking "github.com/PretendoNetwork/nex-protocols-common-go/v2/ranking"
commonsecure "github.com/PretendoNetwork/nex-protocols-common-go/v2/secure-connection"
matchmaking "github.com/PretendoNetwork/nex-protocols-go/v2/match-making"
matchmakingext "github.com/PretendoNetwork/nex-protocols-go/v2/match-making-ext"
matchmakingtypes "github.com/PretendoNetwork/nex-protocols-go/v2/match-making/types"
match_making_types "github.com/PretendoNetwork/nex-protocols-go/v2/match-making/types"
matchmakeextension "github.com/PretendoNetwork/nex-protocols-go/v2/matchmake-extension"
nattraversal "github.com/PretendoNetwork/nex-protocols-go/v2/nat-traversal"
ranking "github.com/PretendoNetwork/nex-protocols-go/v2/ranking/splatoon"
secure "github.com/PretendoNetwork/nex-protocols-go/v2/secure-connection"
"github.com/PretendoNetwork/splatoon/globals"
)

func CreateReportDBRecord(_ *types.PID, _ *types.PrimitiveU32, _ *types.QBuffer) error {
Expand All @@ -33,12 +32,7 @@ func stubGetPlayingSession(err error, packet nex.PacketInterface, callID uint32,
connection := packet.Sender().(*nex.PRUDPConnection)
endpoint := connection.Endpoint().(*nex.PRUDPEndPoint)

lstPlayingSession := types.NewList[*matchmakingtypes.PlayingSession]()

// * There are no sessions, I tell you!
//for _, playingSession := range playingSessions {
// lstPlayingSession.Append(playingSession)
//}
lstPlayingSession := types.NewList[*match_making_types.PlayingSession]()

rmcResponseStream := nex.NewByteStreamOut(endpoint.LibraryVersions(), endpoint.ByteStreamSettings())

Expand All @@ -54,56 +48,21 @@ func stubGetPlayingSession(err error, packet nex.PacketInterface, callID uint32,
return rmcResponse, nil
}

// from nex-protocols-common-go/matchmaking_utils.go
func compareSearchCriteria[T ~uint16 | ~uint32](original T, search string) bool {
if search == "" { // * Accept any value
return true
}

before, after, found := strings.Cut(search, ",")
if found {
min, err := strconv.ParseUint(before, 10, 64)
if err != nil {
return false
}

max, err := strconv.ParseUint(after, 10, 64)
if err != nil {
return false
}

return min <= uint64(original) && max >= uint64(original)
} else {
searchNum, err := strconv.ParseUint(before, 10, 64)
if err != nil {
return false
}

return searchNum == uint64(original)
func cleanupMatchmakeSessionSearchCriteriasHandler(searchCriterias *types.List[*match_making_types.MatchmakeSessionSearchCriteria]) {
for _, searchCriteria := range searchCriterias.Slice() {
_ = searchCriteria.Attribs.SetIndex(4, types.NewString(""))
}
}

func gameSpecificMatchmakeSessionSearchCriteriaChecksHandler(searchCriteria *matchmakingtypes.MatchmakeSessionSearchCriteria, matchmakeSession *matchmakingtypes.MatchmakeSession) bool {
original := matchmakeSession.Attributes.Slice()
search := searchCriteria.Attribs.Slice()
if len(original) != len(search) {
return false
}
func onAfterAutoMatchmakeWithParamPostpone(_ nex.PacketInterface, _ *match_making_types.AutoMatchmakeParam) {
globals.MatchmakingManager.Mutex.Lock()

for index, originalAttribute := range original {
// ignore dummy criterias for matchmaking
// everyone ends up in different rooms if you don't skip these
if index == 1 || index == 4 {
continue
}
searchAttribute := search[index]

if !compareSearchCriteria(originalAttribute.Value, searchAttribute.Value) {
return false
}
_, err := globals.MatchmakingManager.Database.Exec(`UPDATE matchmaking.matchmake_sessions SET open_participation=true WHERE game_mode=12`)
if err != nil {
globals.Logger.Error(err.Error())
}

return true
globals.MatchmakingManager.Mutex.Unlock()
}

func registerCommonSecureServerProtocols() {
Expand All @@ -119,15 +78,23 @@ func registerCommonSecureServerProtocols() {

matchMakingProtocol := matchmaking.NewProtocol()
globals.SecureEndpoint.RegisterServiceProtocol(matchMakingProtocol)
commonmatchmaking.NewCommonProtocol(matchMakingProtocol)
commonMatchMakingProtocol := commonmatchmaking.NewCommonProtocol(matchMakingProtocol)
commonMatchMakingProtocol.SetManager(globals.MatchmakingManager)

matchMakingExtProtocol := matchmakingext.NewProtocol()
globals.SecureEndpoint.RegisterServiceProtocol(matchMakingExtProtocol)
commonmatchmakingext.NewCommonProtocol(matchMakingExtProtocol)
commonMatchMakingExtProtocol := commonmatchmakingext.NewCommonProtocol(matchMakingExtProtocol)
commonMatchMakingExtProtocol.SetManager(globals.MatchmakingManager)

matchmakeExtensionProtocol := matchmakeextension.NewProtocol()
globals.SecureEndpoint.RegisterServiceProtocol(matchmakeExtensionProtocol)
commonMatchmakeExtensionProtocol := commonmatchmakeextension.NewCommonProtocol(matchmakeExtensionProtocol)
matchmakeExtensionProtocol.SetHandlerGetPlayingSession(stubGetPlayingSession)
commonMatchmakeExtensionProtocol.GameSpecificMatchmakeSessionSearchCriteriaChecks = gameSpecificMatchmakeSessionSearchCriteriaChecksHandler
commonMatchmakeExtensionProtocol.CleanupMatchmakeSessionSearchCriterias = cleanupMatchmakeSessionSearchCriteriasHandler
commonMatchmakeExtensionProtocol.OnAfterAutoMatchmakeWithParamPostpone = onAfterAutoMatchmakeWithParamPostpone
commonMatchmakeExtensionProtocol.SetManager(globals.MatchmakingManager)

rankingProtocol := ranking.NewProtocol(globals.SecureEndpoint)
globals.SecureEndpoint.RegisterServiceProtocol(rankingProtocol)
commonranking.NewCommonProtocol(rankingProtocol)
}
3 changes: 3 additions & 0 deletions nex/secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nex

import (
"fmt"
common_globals "github.com/PretendoNetwork/nex-protocols-common-go/v2/globals"
"os"
"strconv"

Expand Down Expand Up @@ -36,6 +37,8 @@ func StartSecureServer() {
globals.Logger.Errorf("Secure: %v", err)
})

globals.MatchmakingManager = common_globals.NewMatchmakingManager(globals.SecureEndpoint, globals.Postgres)

registerCommonSecureServerProtocols()

port, _ := strconv.Atoi(os.Getenv("PN_SPLATOON_SECURE_SERVER_PORT"))
Expand Down
Binary file added splatoon
Binary file not shown.

0 comments on commit 238c85d

Please sign in to comment.