Skip to content

Commit

Permalink
Merge pull request #54 from MikeIsAStar/handle-get-top-ten-rankings-r…
Browse files Browse the repository at this point in the history
…equests

RACE: Handle 'GetTopTenRankings' requests
  • Loading branch information
mkwcat authored Jul 14, 2024
2 parents 40bf4b8 + 22213db commit a5d5bac
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
6 changes: 3 additions & 3 deletions common/mario_kart_wii.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

type MarioKartWiiRegionID uint
type MarioKartWiiCourseID uint
type MarioKartWiiRegionID int
type MarioKartWiiCourseID int

const MarioKartWiiGameSpyGameID uint = 1687
const MarioKartWiiGameSpyGameID int = 1687

const (
Worldwide = iota // 0
Expand Down
94 changes: 81 additions & 13 deletions race/nintendo_racing_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,56 @@ import (
"encoding/xml"
"io"
"net/http"
"strconv"
"wwfc/common"
"wwfc/logging"

"github.com/logrusorgru/aurora/v3"
)

type rankingsRequestEnvelope struct {
XMLName xml.Name
Body rankingsRequestBody
Body rankingsRequestBody
}

type rankingsRequestBody struct {
XMLName xml.Name
Data rankingsRequestData `xml:",any"`
Data rankingsRequestData `xml:",any"`
}

type rankingsRequestData struct {
XMLName xml.Name
GameId uint `xml:"gameid"`
GameId int `xml:"gameid"`
RegionId common.MarioKartWiiRegionID `xml:"regionid"`
CourseId common.MarioKartWiiCourseID `xml:"courseid"`
}

type rankingsResponseRankingDataResponse struct {
XMLName xml.Name `xml:"RankingDataResponse"`
XMLNSXsi string `xml:"xmlns:xsi,attr"`
XMLNSXsd string `xml:"xmlns:xsd,attr"`
XMLNS string `xml:"xmlns,attr"`
ResponseCode int `xml:"responseCode"`
DataArray rankingsResponseDataArray
}

type rankingsResponseDataArray struct {
XMLName xml.Name `xml:"dataArray"`
NumRecords int `xml:"numrecords"`
Data []rankingsResponseData
}

type rankingsResponseData struct {
XMLName xml.Name `xml:"data"`
RankingData rankingsResponseRankingData
}

type rankingsResponseRankingData struct {
XMLName xml.Name `xml:"RankingData"`
OwnerID int `xml:"ownerid"`
Rank int `xml:"rank"`
Time int `xml:"time"`
UserData string `xml:"userdata"`
}

func handleNintendoRacingServiceRequest(moduleName string, responseWriter http.ResponseWriter, request *http.Request) {
soapActionHeader := request.Header.Get("SOAPAction")
if soapActionHeader == "" {
Expand All @@ -39,25 +66,25 @@ func handleNintendoRacingServiceRequest(moduleName string, responseWriter http.R
panic(err)
}

soapMessage := rankingsRequestEnvelope{}
err = xml.Unmarshal(requestBody, &soapMessage)
requestXML := rankingsRequestEnvelope{}
err = xml.Unmarshal(requestBody, &requestXML)
if err != nil {
logging.Error(moduleName, "Malformed XML")
logging.Error(moduleName, "Got malformed XML")
return
}
soapMessageData := soapMessage.Body.Data
requestData := requestXML.Body.Data

gameId := soapMessageData.GameId
gameId := requestData.GameId
if gameId != common.MarioKartWiiGameSpyGameID {
logging.Error(moduleName, "Wrong GameSpy game id")
return
}

soapAction := soapMessageData.XMLName.Local
soapAction := requestData.XMLName.Local
switch soapAction {
case "GetTopTenRankings":
regionId := soapMessageData.RegionId
courseId := soapMessageData.CourseId
regionId := requestData.RegionId
courseId := requestData.CourseId

if !regionId.IsValid() {
logging.Error(moduleName, "Invalid region id")
Expand All @@ -69,5 +96,46 @@ func handleNintendoRacingServiceRequest(moduleName string, responseWriter http.R
}

logging.Info(moduleName, "Received a request for the Top 10 of", aurora.BrightCyan(courseId.ToString()))
handleGetTopTenRankingsRequest(moduleName, responseWriter)
}
}

func handleGetTopTenRankingsRequest(moduleName string, responseWriter http.ResponseWriter) {
rankingData := rankingsResponseRankingData{
OwnerID: 1000000404,
Rank: 1,
Time: 0,
UserData: "xC0AIABNAGkAawBlAFMAdABhAHIAIAAAhH/RTQAAAAAgB45hkAAQTEDyjqQAeLgPhq4AiiUEACAATQBpAGsAZQBTAHQAYQByACD0UwACAAE=",
}

responseData := []rankingsResponseData{
{
RankingData: rankingData,
},
}

dataArray := rankingsResponseDataArray{
NumRecords: len(responseData),
Data: responseData,
}

rankingDataResponse := rankingsResponseRankingDataResponse{
XMLNSXsi: "http://www.w3.org/2001/XMLSchema-instance",
XMLNSXsd: "http://www.w3.org/2001/XMLSchema",
XMLNS: "http://gamespy.net/RaceService/",
ResponseCode: 0,
DataArray: dataArray,
}

responseBody, err := xml.Marshal(rankingDataResponse)
if err != nil {
logging.Error(moduleName, "Failed to XML encode the data")
return
}

responseBody = append([]byte(xml.Header), responseBody...)

responseWriter.Header().Set("Content-Length", strconv.Itoa(len(responseBody)))
responseWriter.Header().Set("Content-Type", "text/xml")
responseWriter.Write(responseBody)
}

0 comments on commit a5d5bac

Please sign in to comment.