From 467d54b7650000d296d6531805764964e76704fd Mon Sep 17 00:00:00 2001 From: mkwcat Date: Tue, 13 Feb 2024 10:55:58 -0500 Subject: [PATCH] QR2: Add create time to /api/groups --- api/groups.go | 2 +- api/stats.go | 2 +- qr2/group.go | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/api/groups.go b/api/groups.go index 5fc034e..95c0f48 100644 --- a/api/groups.go +++ b/api/groups.go @@ -21,7 +21,7 @@ func HandleGroups(w http.ResponseWriter, r *http.Request) { return } - groups := qr2.GetGroups(query["game"], query["id"]) + groups := qr2.GetGroups(query["game"], query["id"], true) var jsonData []byte if len(groups) == 0 { diff --git a/api/stats.go b/api/stats.go index 522e0a9..702cdc6 100644 --- a/api/stats.go +++ b/api/stats.go @@ -33,7 +33,7 @@ func HandleStats(w http.ResponseWriter, r *http.Request) { stats := map[string]Stats{} servers := qr2.GetSessionServers() - groups := qr2.GetGroups([]string{}, []string{}) + groups := qr2.GetGroups([]string{}, []string{}, false) globalStats := Stats{ OnlinePlayerCount: len(servers), diff --git a/qr2/group.go b/qr2/group.go index 92e396a..b81d009 100644 --- a/qr2/group.go +++ b/qr2/group.go @@ -4,8 +4,10 @@ import ( "encoding/base64" "encoding/binary" "fmt" + "sort" "strconv" "strings" + "time" "wwfc/common" "wwfc/logging" @@ -15,6 +17,7 @@ import ( type Group struct { GroupID uint32 GroupName string + CreateTime time.Time GameName string MatchType string MKWRegion string @@ -36,6 +39,7 @@ func processResvOK(moduleName string, matchVersion int, reservation common.Match group = &Group{ GroupID: resvOK.GroupID, GroupName: "", + CreateTime: time.Now(), GameName: sender.Data["gamename"], MatchType: sender.Data["dwc_mtype"], MKWRegion: "", @@ -382,12 +386,13 @@ type PlayerInfo struct { type GroupInfo struct { GroupName string `json:"id"` GameName string `json:"game"` + CreateTime time.Time `json:"created"` MatchType string `json:"type"` Suspend bool `json:"suspend"` ServerIndex string `json:"host,omitempty"` MKWRegion string `json:"rk,omitempty"` PlayersRaw map[string]map[string]string `json:"-"` - Players []PlayerInfo `json:"players"` + Players map[string]PlayerInfo `json:"players"` } func getGroupsRaw(gameNames []string, groupNames []string) []GroupInfo { @@ -408,11 +413,13 @@ func getGroupsRaw(gameNames []string, groupNames []string) []GroupInfo { groupInfo := GroupInfo{ GroupName: group.GroupName, GameName: group.GameName, + CreateTime: group.CreateTime, MatchType: "", Suspend: true, ServerIndex: "", MKWRegion: "", PlayersRaw: map[string]map[string]string{}, + Players: map[string]PlayerInfo{}, } if group.MatchType == "0" || group.MatchType == "1" { @@ -456,12 +463,12 @@ func getGroupsRaw(gameNames []string, groupNames []string) []GroupInfo { return groupsCopy } -// GetGroups returns an unsorted copy of all online rooms -func GetGroups(gameNames []string, groupNames []string) []GroupInfo { +// GetGroups returns a copy of all online rooms +func GetGroups(gameNames []string, groupNames []string, sorted bool) []GroupInfo { groupsCopy := getGroupsRaw(gameNames, groupNames) for i, group := range groupsCopy { - for _, rawPlayer := range group.PlayersRaw { + for joinIndex, rawPlayer := range group.PlayersRaw { playerInfo := PlayerInfo{ Count: rawPlayer["+localplayers"], ProfileID: rawPlayer["dwc_pid"], @@ -490,9 +497,19 @@ func GetGroups(gameNames []string, groupNames []string) []GroupInfo { }) } - groupsCopy[i].Players = append(groupsCopy[i].Players, playerInfo) + groupsCopy[i].Players[joinIndex] = playerInfo } } + if sorted { + sort.Slice(groupsCopy, func(i, j int) bool { + if groupsCopy[i].CreateTime.Equal(groupsCopy[j].CreateTime) { + return groupsCopy[i].GroupName < groupsCopy[j].GroupName + } + + return groupsCopy[i].CreateTime.Before(groupsCopy[j].CreateTime) + }) + } + return groupsCopy }