From 863e8fc860ec898b50a988336eb2b9ea7d77be0b Mon Sep 17 00:00:00 2001 From: wish Date: Sat, 23 Jul 2022 09:35:55 +1000 Subject: [PATCH] implement treasure hunt cooldowns --- Erupe/common/stringsupport/string_convert.go | 49 ++++++++++++------- Erupe/guild-additions.sql | 3 +- .../channelserver/handlers_guild_tresure.go | 6 ++- .../channelserver/handlers_mercenary.go | 43 +++++++++++----- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/Erupe/common/stringsupport/string_convert.go b/Erupe/common/stringsupport/string_convert.go index 35a131d1a..0a9f4cb10 100644 --- a/Erupe/common/stringsupport/string_convert.go +++ b/Erupe/common/stringsupport/string_convert.go @@ -1,10 +1,10 @@ package stringsupport import ( - "strings" - "strconv" "bytes" "io/ioutil" + "strconv" + "strings" "golang.org/x/text/encoding" "golang.org/x/text/encoding/japanese" @@ -83,16 +83,16 @@ func ConvertShiftJISToUTF8(text string) (string, error) { } */ -func UTF8ToSJIS(x string) ([]byte) { - e := japanese.ShiftJIS.NewEncoder() - xt, _, err := transform.String(e, x) - if err != nil { - panic(err) - } +func UTF8ToSJIS(x string) []byte { + e := japanese.ShiftJIS.NewEncoder() + xt, _, err := transform.String(e, x) + if err != nil { + panic(err) + } return []byte(xt) } -func SJISToUTF8(b []byte) (string) { +func SJISToUTF8(b []byte) string { d := japanese.ShiftJIS.NewDecoder() result, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(b), d)) if err != nil { @@ -103,13 +103,13 @@ func SJISToUTF8(b []byte) (string) { func PaddedString(x string, size uint, t bool) []byte { if t { - e := japanese.ShiftJIS.NewEncoder() - xt, _, err := transform.String(e, x) - if err != nil { - panic(err) - } - x = xt - } + e := japanese.ShiftJIS.NewEncoder() + xt, _, err := transform.String(e, x) + if err != nil { + panic(err) + } + x = xt + } out := make([]byte, size) copy(out, x) out[len(out)-1] = 0 @@ -127,8 +127,8 @@ func CSVRemove(csv string, v int) string { s := strings.Split(csv, ",") for i, e := range s { if e == strconv.Itoa(v) { - s[i] = s[len(s) - 1] - s = s[:len(s) - 1] + s[i] = s[len(s)-1] + s = s[:len(s)-1] } } return strings.Join(s, ",") @@ -153,6 +153,19 @@ func CSVLength(csv string) int { return len(s) } +func CSVElems(csv string) []int { + var r []int + if csv == "" { + return r + } + s := strings.Split(csv, ",") + for i := 0; i < len(s); i++ { + j, _ := strconv.ParseInt(s[i], 10, 64) + r = append(r, int(j)) + } + return r +} + // ConvertUTF8ToShiftJIS converts a UTF8 string to a Shift-JIS []byte. func ConvertUTF8ToShiftJIS(text string) ([]byte, error) { r := bytes.NewBuffer([]byte(text)) diff --git a/Erupe/guild-additions.sql b/Erupe/guild-additions.sql index 2c9ddde46..8d98ccf82 100644 --- a/Erupe/guild-additions.sql +++ b/Erupe/guild-additions.sql @@ -49,7 +49,8 @@ CREATE TABLE IF NOT EXISTS public.guild_hunts claimed bool NOT NULL DEFAULT false, hunters text NOT NULL DEFAULT '', treasure text NOT NULL DEFAULT '', - hunt_data bytea NOT NULL + hunt_data bytea NOT NULL, + cats_used text NOT NULL ); END; \ No newline at end of file diff --git a/Erupe/server/channelserver/handlers_guild_tresure.go b/Erupe/server/channelserver/handlers_guild_tresure.go index c187f1407..6459c238c 100644 --- a/Erupe/server/channelserver/handlers_guild_tresure.go +++ b/Erupe/server/channelserver/handlers_guild_tresure.go @@ -82,10 +82,12 @@ func handleMsgMhfRegistGuildTresure(s *Session, p mhfpacket.MHFPacket) { level := bf.ReadUint32() huntData.WriteUint32(s.charID) huntData.WriteBytes(stringsupport.PaddedString(s.Name, 18, true)) + catsUsed := "" for i := 0; i < 5; i++ { catID := bf.ReadUint32() huntData.WriteUint32(catID) if catID > 0 { + catsUsed = stringsupport.CSVAdd(catsUsed, int(catID)) for _, cat := range guildCats { if cat.CatID == catID { huntData.WriteBytes(cat.CatName) @@ -95,8 +97,8 @@ func handleMsgMhfRegistGuildTresure(s *Session, p mhfpacket.MHFPacket) { huntData.WriteBytes(bf.ReadBytes(9)) } } - _, err = s.server.db.Exec("INSERT INTO guild_hunts (guild_id, host_id, destination, level, return, hunt_data) VALUES ($1, $2, $3, $4, $5, $6)", - guild.ID, s.charID, destination, level, Time_Current_Adjusted().Unix(), huntData.Data()) + _, err = s.server.db.Exec("INSERT INTO guild_hunts (guild_id, host_id, destination, level, return, hunt_data, cats_used) VALUES ($1, $2, $3, $4, $5, $6, $7)", + guild.ID, s.charID, destination, level, Time_Current_Adjusted().Unix(), huntData.Data(), catsUsed) if err != nil { panic(err) } diff --git a/Erupe/server/channelserver/handlers_mercenary.go b/Erupe/server/channelserver/handlers_mercenary.go index bf7e0d103..af70c55fe 100644 --- a/Erupe/server/channelserver/handlers_mercenary.go +++ b/Erupe/server/channelserver/handlers_mercenary.go @@ -1,21 +1,19 @@ package channelserver import ( - "math/rand" - "os" - "io" - "io/ioutil" - "path/filepath" - - + "erupe-ce/common/byteframe" + "erupe-ce/common/stringsupport" "erupe-ce/network/mhfpacket" "erupe-ce/server/channelserver/compression/deltacomp" "erupe-ce/server/channelserver/compression/nullcomp" - "erupe-ce/common/byteframe" "go.uber.org/zap" + "io" + "io/ioutil" + "math/rand" + "os" + "path/filepath" ) - // THERE ARE [PARTENER] [MERCENARY] [OTOMO AIRU] /////////////////////////////////////////// @@ -236,7 +234,7 @@ func handleMsgMhfSaveOtomoAirou(s *Session, p mhfpacket.MHFPacket) { bf.Seek(-4, io.SeekCurrent) bf.WriteUint32(nextID) } - _ = bf.ReadBytes(uint(dataLen)-4) + _ = bf.ReadBytes(uint(dataLen) - 4) } comp, err := nullcomp.Compress(bf.Data()) if err != nil { @@ -297,8 +295,28 @@ func getGuildAirouList(s *Session) []CatDefinition { return guildCats } + // Get cats used recently + // Retail reset at midday, 12 hours is a midpoint + tempBanDuration := 43200 - (1800) // Minus hunt time + bannedCats := make(map[uint32]int) + var csvTemp string + rows, err := s.server.db.Query(`SELECT cats_used + FROM guild_hunts gh + INNER JOIN characters c + ON gh.host_id = c.id + WHERE c.id=$1 AND gh.return+$2>$3`, s.charID, tempBanDuration, Time_Current_Adjusted().Unix()) + if err != nil { + s.logger.Warn("Failed to get recently used airous", zap.Error(err)) + } + for rows.Next() { + rows.Scan(&csvTemp) + for i, j := range stringsupport.CSVElems(csvTemp) { + bannedCats[uint32(j)] = i + } + } + // ellie's GetGuildMembers didn't seem to pull leader? - rows, err := s.server.db.Query(`SELECT c.otomoairou + rows, err = s.server.db.Query(`SELECT c.otomoairou FROM characters c INNER JOIN guild_characters gc ON gc.character_id = c.id @@ -330,7 +348,8 @@ func getGuildAirouList(s *Session) []CatDefinition { bf := byteframe.NewByteFrameFromBytes(decomp) cats := GetCatDetails(bf) for _, cat := range cats { - if cat.CurrentTask == 4 { + _, exists := bannedCats[cat.CatID] + if cat.CurrentTask == 4 && !exists { guildCats = append(guildCats, cat) } }