Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAKE: Support file download requests for rival ghosts from Mario Kart Wii #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions database/mario_kart_wii.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const (
"AND courseid = $2 " +
"ORDER BY score ASC " +
"LIMIT 10"
getGhostDataQuery = "" +
"SELECT id " +
"FROM mario_kart_wii_sake " +
"WHERE courseid = $1 " +
"AND score < $2 " +
"ORDER BY score DESC " +
"LIMIT 1"
getStoredGhostDataQuery = "" +
"SELECT pid, id " +
"FROM mario_kart_wii_sake " +
Expand Down Expand Up @@ -73,6 +80,17 @@ func GetMarioKartWiiTopTenRankings(pool *pgxpool.Pool, ctx context.Context, regi
return topTenRankings, nil
}

func GetMarioKartWiiGhostData(pool *pgxpool.Pool, ctx context.Context, courseId common.MarioKartWiiCourseId, time int) (int, error) {
row := pool.QueryRow(ctx, getGhostDataQuery, courseId, time)

var fileId int
if err := row.Scan(&fileId); err != nil {
return 0, err
}

return fileId, nil
}

func GetMarioKartWiiStoredGhostData(pool *pgxpool.Pool, ctx context.Context, regionId common.MarioKartWiiLeaderboardRegionId,
courseId common.MarioKartWiiCourseId) (int, int, error) {
row := pool.QueryRow(ctx, getStoredGhostDataQuery, regionId, courseId)
Expand Down
6 changes: 3 additions & 3 deletions sake/mario_kart_wii.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func handleMarioKartWiiGhostDownloadRequest(moduleName string, responseWriter ht
}

time, err := strconv.Atoi(timeString)
if err != nil || time <= 0 {
if err != nil || time <= 0 || time >= 360000 /* 6 minutes */ {
logging.Error(moduleName, "Invalid time:", aurora.Cyan(timeString))
responseWriter.Header().Set(SakeFileResultHeader, strconv.Itoa(SakeFileResultMissingParameter))
return
Expand Down Expand Up @@ -167,14 +167,14 @@ func handleMarioKartWiiGhostUploadRequest(moduleName string, responseWriter http
return
}
courseId := common.MarioKartWiiCourseId(courseIdInt)
if courseId < common.MarioCircuit || courseId > 32767 {
if courseId < common.MarioCircuit || isContest == courseId.IsValid() || courseId > 32767 {
logging.Error(moduleName, "Invalid course ID:", aurora.Cyan(courseIdString))
responseWriter.Header().Set(SakeFileResultHeader, strconv.Itoa(SakeFileResultMissingParameter))
return
}

score, err := strconv.Atoi(scoreString)
if err != nil || score <= 0 {
if err != nil || score <= 0 || score >= 360000 /* 6 minutes */ {
logging.Error(moduleName, "Invalid score:", aurora.Cyan(scoreString))
responseWriter.Header().Set(SakeFileResultHeader, strconv.Itoa(SakeFileResultMissingParameter))
return
Expand Down
69 changes: 68 additions & 1 deletion sake/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,73 @@ func searchForRecords(moduleName string, gameInfo common.GameInfo, request Stora
},
}

case "mariokartwii/GhostData":
if request.TableID != "GhostData" {
logging.Error(moduleName, "Invalid table name:", aurora.Cyan(request.TableID))
return &errorResponse
}

if request.Sort != "time desc" {
logging.Error(moduleName, "Invalid sort string:", aurora.Cyan(request.Sort))
return &errorResponse
}

if request.Offset != 0 {
logging.Error(moduleName, "Invalid offset value:", aurora.Cyan(request.Offset))
return &errorResponse
}

if request.Max != 1 {
logging.Error(moduleName, "Invalid number of records to return:", aurora.Cyan(request.Max))
return &errorResponse
}

if request.Surrounding != 0 {
logging.Error(moduleName, "Invalid number of surrounding records to return:", aurora.Cyan(request.Surrounding))
return &errorResponse
}

if request.OwnerIDs != "" {
logging.Error(moduleName, "Invalid owner id array:", aurora.Cyan(request.OwnerIDs))
return &errorResponse
}

if request.CacheFlag != 0 {
logging.Error(moduleName, "Invalid cache value:", aurora.Cyan(request.CacheFlag))
return &errorResponse
}

match := regexp.MustCompile(`^course = ([1-9]\d?|0) and gameid = 1687 and time < ([1-9][0-9]{0,5})$`).FindStringSubmatch(request.Filter)
if match == nil {
logging.Error(moduleName, "Invalid filter string:", aurora.Cyan(request.Filter))
return &errorResponse
}

courseIdInt, _ := strconv.Atoi(match[1])
courseId := common.MarioKartWiiCourseId(courseIdInt)
if !courseId.IsValid() {
logging.Error(moduleName, "Invalid course ID:", aurora.Cyan(match[1]))
return &errorResponse
}

time, _ := strconv.Atoi(match[2])
if time >= 360000 /* 6 minutes */ {
logging.Error(moduleName, "Invalid time:", aurora.Cyan(match[2]))
return &errorResponse
}

fileId, err := database.GetMarioKartWiiGhostData(pool, ctx, courseId, time)
if err != nil {
logging.Error(moduleName, "Failed to get the ghost data from the database:", err)
return &errorResponse
}

values = []map[string]StorageValue{
{
"fileid": intValue(int32(fileId)),
},
}

case "mariokartwii/StoredGhostData":
if request.Sort != "time" {
logging.Error(moduleName, "Invalid sort string:", aurora.Cyan(request.Sort))
Expand Down Expand Up @@ -423,7 +490,7 @@ func searchForRecords(moduleName string, gameInfo common.GameInfo, request Stora
courseIdInt, _ := strconv.Atoi(match[1])
courseId := common.MarioKartWiiCourseId(courseIdInt)
if !courseId.IsValid() {
logging.Error(moduleName, "Invalid course ID:", aurora.Cyan(courseIdInt))
logging.Error(moduleName, "Invalid course ID:", aurora.Cyan(match[1]))
return &errorResponse
}

Expand Down
2 changes: 1 addition & 1 deletion schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ALTER TABLE public.users OWNER TO wiilink;
CREATE TABLE IF NOT EXISTS public.mario_kart_wii_sake (
regionid smallint NOT NULL CHECK (regionid >= 1 AND regionid <= 7),
courseid smallint NOT NULL CHECK (courseid >= 0 AND courseid <= 32767),
score integer NOT NULL CHECK (score > 0),
score integer NOT NULL CHECK (score > 0 AND score < 360000),
pid integer NOT NULL CHECK (pid > 0),
playerinfo varchar(108) NOT NULL CHECK (LENGTH(playerinfo) = 108),
ghost bytea CHECK (ghost IS NULL OR (OCTET_LENGTH(ghost) BETWEEN 148 AND 10240)),
Expand Down