GameStats: Apply lint suggestions

This commit is contained in:
Palapeli
2026-04-06 07:07:23 -04:00
parent 5dee992b47
commit 0ea3d33158
3 changed files with 43 additions and 33 deletions

View File

@@ -6,10 +6,15 @@ import (
"wwfc/logging"
)
func (g *GameStatsSession) replyError(err gpcm.GPError) {
logging.Error(g.ModuleName, "Reply error:", err.ErrorString)
common.SendPacket(ServerName, g.ConnIndex, []byte(err.GetMessage()))
if err.Fatal {
common.CloseConnection(ServerName, g.ConnIndex)
func (g *GameStatsSession) replyError(gpErr gpcm.GPError) {
logging.Error(g.ModuleName, "Reply error:", gpErr.ErrorString)
err := common.SendPacket(ServerName, g.ConnIndex, []byte(gpErr.GetMessage()))
if gpErr.Fatal || err != nil {
if err != nil {
logging.Error(g.ModuleName, "Failed to send error message:", err)
}
if err := common.CloseConnection(ServerName, g.ConnIndex); err != nil {
logging.Error(g.ModuleName, "Failed to close connection:", err)
}
}
}

View File

@@ -61,22 +61,19 @@ func StartServer(reload bool) {
if err != nil {
panic(err)
}
defer func() {
common.ShouldNotError(file.Close())
}()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&sessionsByConnIndex)
file.Close()
if err != nil {
panic(err)
}
common.ShouldNotError(decoder.Decode(&sessionsByConnIndex))
for _, session := range sessionsByConnIndex {
session.gameInfo = common.GetGameInfoByName(session.GameName)
if session.gameInfo == nil {
logging.Error(session.ModuleName, "Unknown game from reload:", aurora.Cyan(session.GameName))
// Force close the session now to prevent a panic later
common.CloseConnection(ServerName, session.ConnIndex)
_ = common.CloseConnection(ServerName, session.ConnIndex)
delete(sessionsByConnIndex, session.ConnIndex)
}
}
@@ -88,20 +85,14 @@ func StartServer(reload bool) {
func Shutdown() {
// Save state
file, err := os.OpenFile("state/gstats_sessions.gob", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
common.ShouldNotError(err)
defer func() {
common.ShouldNotError(file.Close())
}()
defer db.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(sessionsByConnIndex)
file.Close()
if err != nil {
panic(err)
}
db.Close()
common.ShouldNotError(encoder.Encode(sessionsByConnIndex))
logging.Notice("GSTATS", "Saved", aurora.Cyan(len(sessionsByConnIndex)), "sessions")
}
@@ -132,7 +123,10 @@ func NewConnection(index uint64, address string) {
"id": "1",
},
})
common.SendPacket(ServerName, index, []byte(session.WriteBuffer))
err := common.SendPacket(ServerName, index, []byte(session.WriteBuffer))
if err != nil {
logging.Error(session.ModuleName, "Failed to send initial packet:", err)
}
session.WriteBuffer = []byte{}
logging.Notice(session.ModuleName, "Connection established from", address)
@@ -241,8 +235,12 @@ func HandlePacket(index uint64, data []byte) {
}
if len(session.WriteBuffer) > 0 {
common.SendPacket(ServerName, session.ConnIndex, session.WriteBuffer)
session.WriteBuffer = []byte{}
err := common.SendPacket(ServerName, session.ConnIndex, session.WriteBuffer)
if err != nil {
logging.Error(session.ModuleName, "Failed to send packet:", err)
} else {
session.WriteBuffer = []byte{}
}
}
}

View File

@@ -33,9 +33,7 @@ func HandleWebRequest(w http.ResponseWriter, r *http.Request) {
}
path := u.Path
if strings.HasPrefix(path, "/") {
path = path[1:]
}
path = strings.TrimPrefix(path, "/")
gameName := path
subPath := ""
@@ -97,7 +95,10 @@ func HandleWebRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "ASP.NET")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.WriteHeader(200)
w.Write(response)
_, err = w.Write(response)
if err != nil {
logging.Error("GSTATS", "Error writing response:", err)
}
}
func calculateToken(u *url.URL, host string) string {
@@ -113,6 +114,9 @@ func calculateToken(u *url.URL, host string) string {
}
func handleGet2(game *common.GameInfo, query url.Values) []byte {
// TODO
common.MaybeUnused(game, query)
data := binary.LittleEndian.AppendUint32([]byte{}, 1) // RNK_GET
data = binary.LittleEndian.AppendUint32(data, 0) // count
return data
@@ -131,5 +135,8 @@ func replyHTTPError(w http.ResponseWriter, errorCode int, errorString string) {
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Header().Set("Connection", "close")
w.WriteHeader(errorCode)
w.Write([]byte(response))
_, err := w.Write([]byte(response))
if err != nil {
logging.Error("GSTATS", "Error writing response:", err)
}
}