DLS1: Implement the 'count' action

This commit is contained in:
MikeIsAStar
2023-12-05 22:05:00 -05:00
parent 698cdb8bd8
commit 333c79701d
3 changed files with 51 additions and 1 deletions

View File

@@ -34,3 +34,22 @@ func GetString(buf []byte) string {
nullTerminator := bytes.IndexByte(buf, 0)
return string(buf[:nullTerminator])
}
// Checks if the given string is composed exclusively of uppercase alphanumeric characters.
func IsUppercaseAlphanumeric(str string) bool {
strLength := len(str)
if strLength == 0 {
return false
}
for i := 0; i < strLength; i++ {
c := str[i]
if (c < '0' || c > '9') && (c < 'A' || c > 'Z') {
return false
}
}
return true
}

3
dlc/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Downloadable Content
Create a folder in this directory using the game's game code as the folder name. Afterwards, place downloadable content into the aforementioned folder.

View File

@@ -5,6 +5,8 @@ import (
"github.com/logrusorgru/aurora/v3"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@@ -13,6 +15,10 @@ import (
"wwfc/logging"
)
var (
dlcDir = "./dlc"
)
func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@@ -87,6 +93,13 @@ func handleAuthRequest(moduleName string, w http.ResponseWriter, r *http.Request
return
}
rhgamecd, ok := fields["rhgamecd"]
if !ok || !isValidRhgamecd(rhgamecd) {
logging.Error(moduleName, "Missing or invalid rhgamecd")
replyHTTPError(w, 400, "400 Bad Request")
return
}
switch action {
case "count":
response = []byte(dlsCount(moduleName, fields))
@@ -210,7 +223,22 @@ func handleProfanity(moduleName string, fields map[string]string) map[string]str
}
func dlsCount(moduleName string, fields map[string]string) string {
return "0"
dlcFolder := filepath.Join(dlcDir, fields["rhgamecd"])
dir, ok := os.ReadDir(dlcFolder)
if ok != nil {
return "0"
}
return strconv.Itoa(len(dir))
}
func isValidRhgamecd(rhgamecd string) bool {
if len(rhgamecd) != 4 {
return false
}
return common.IsUppercaseAlphanumeric(rhgamecd)
}
func getDateTime() string {