From bac4e70be41a9fec114872eaa03dda71523ca6df Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 11:44:22 +1100 Subject: [PATCH 1/6] course enumeration concept --- network/mhfpacket/msg_sys_update_right.go | 38 ++++++++++++++++++++--- server/channelserver/handlers.go | 25 +++++---------- server/channelserver/sys_session.go | 12 ++++++- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/network/mhfpacket/msg_sys_update_right.go b/network/mhfpacket/msg_sys_update_right.go index b343dd0c4..9d1dfc52a 100644 --- a/network/mhfpacket/msg_sys_update_right.go +++ b/network/mhfpacket/msg_sys_update_right.go @@ -2,6 +2,7 @@ package mhfpacket import ( "errors" + ps "erupe-ce/common/pascalstring" "erupe-ce/common/byteframe" "erupe-ce/network" @@ -34,6 +35,12 @@ type ClientRight struct { Timestamp uint32 } +type Course struct { + Name string + ID uint16 + Value uint32 +} + // MsgSysUpdateRight represents the MSG_SYS_UPDATE_RIGHT type MsgSysUpdateRight struct { ClientRespAckHandle uint32 // If non-0, requests the client to send back a MSG_SYS_ACK packet with this value. @@ -63,9 +70,32 @@ func (m *MsgSysUpdateRight) Build(bf *byteframe.ByteFrame, ctx *clientctx.Client bf.WriteUint16(v.Unk0) bf.WriteUint32(v.Timestamp) } - - bf.WriteUint16(m.UnkSize) // String of upto 0x800 bytes, update client login token / password in the game's launcherstate struct. - //bf.WriteBytes(m.UpdatedClientLoginToken) - + ps.Uint16(bf, "", false) // update client login token / password in the game's launcherstate struct return nil } + +// GetCourseStruct returns a slice of Course(s) from a rights integer +func GetCourseStruct(rights uint32) []Course { + var courses = []Course{ + {"Trial", 1, 0x00000002}, + {"HunterLife", 2, 0x00000004}, + {"ExtraA", 3, 0x00000008}, + {"ExtraB", 4, 0x00000010}, + {"Mobile", 5, 0x00000020}, + {"Premium", 6, 0x00000040}, + {"Pallone", 7, 0x00000080}, + {"Assist", 8, 0x00000100}, // Legend + {"Netcafe", 9, 0x00000200}, + {"Hiden", 10, 0x00000400}, // Secret + {"HunterSupport", 11, 0x00000800}, // Royal + {"NetcafeBoost", 12, 0x00001000}, + } + var resp []Course + for _, course := range courses { + if rights-course.Value < 0x80000000 { + resp = append(resp, course) + rights -= course.Value + } + } + return resp +} diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index be2d37794..94c32d8bd 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -6,7 +6,6 @@ import ( "erupe-ce/common/stringsupport" "fmt" "io" - "math" "net" "strings" "time" @@ -74,26 +73,18 @@ func doAckSimpleFail(s *Session, ackHandle uint32, data []byte) { } func updateRights(s *Session) { - s.rights = uint32(0x0E) - s.server.db.QueryRow("SELECT rights FROM users u INNER JOIN characters c ON u.id = c.user_id WHERE c.id = $1", s.charID).Scan(&s.rights) + rightsInt := uint32(0x0E) + s.server.db.QueryRow("SELECT rights FROM users u INNER JOIN characters c ON u.id = c.user_id WHERE c.id = $1", s.charID).Scan(&rightsInt) - rights := make([]mhfpacket.ClientRight, 0) - tempRights := s.rights - for i := 30; i > 0; i-- { - right := uint32(math.Pow(2, float64(i))) - if tempRights-right < 0x80000000 { - if i == 1 { - continue - } - rights = append(rights, mhfpacket.ClientRight{ID: uint16(i), Timestamp: 0x70DB59F0}) - tempRights -= right - } + courses := mhfpacket.GetCourseStruct(rightsInt) + rights := []mhfpacket.ClientRight{{1, 0, 0}} + for _, course := range courses { + rights = append(rights, mhfpacket.ClientRight{ID: course.ID, Timestamp: 0x70DB59F0}) } - rights = append(rights, mhfpacket.ClientRight{ID: 1, Timestamp: 0}) update := &mhfpacket.MsgSysUpdateRight{ ClientRespAckHandle: 0, - Bitfield: s.rights, + Bitfield: rightsInt, Rights: rights, UnkSize: 0, } @@ -224,7 +215,7 @@ func logoutPlayer(s *Session) { timePlayed += sessionTime var rpGained int - if s.rights >= 0x40000000 { // N Course + if s.CourseExists("Netcafe") { rpGained = timePlayed / 900 timePlayed = timePlayed % 900 } else { diff --git a/server/channelserver/sys_session.go b/server/channelserver/sys_session.go index 90430f4da..2d3ce9bd1 100644 --- a/server/channelserver/sys_session.go +++ b/server/channelserver/sys_session.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net" + "strings" "sync" "time" @@ -43,7 +44,7 @@ type Session struct { charID uint32 logKey []byte sessionStart int64 - rights uint32 + courses []mhfpacket.Course token string kqf []byte kqfOverride bool @@ -268,3 +269,12 @@ func (s *Session) logMessage(opcode uint16, data []byte, sender string, recipien fmt.Printf("Data [%d bytes]:\n(Too long!)\n\n", len(data)) } } + +func (s *Session) CourseExists(name string) bool { + for _, course := range s.courses { + if strings.ToLower(name) == strings.ToLower(course.Name) { + return true + } + } + return false +} From 31bac7bd68bbe32b8919a2f78c2bfce535c61112 Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 15:03:12 +1100 Subject: [PATCH 2/6] initial course command --- config.json | 14 +++++++ config/config.go | 6 +++ network/mhfpacket/msg_sys_update_right.go | 40 +++++++++++--------- server/channelserver/handlers.go | 6 +-- server/channelserver/handlers_cast_binary.go | 37 ++++++++++++++++++ server/channelserver/sys_session.go | 6 ++- 6 files changed, 85 insertions(+), 24 deletions(-) diff --git a/config.json b/config.json index 4bc422280..a29d7aff4 100644 --- a/config.json +++ b/config.json @@ -55,8 +55,22 @@ "Name": "KeyQuest", "Enabled": false, "Prefix": "!kqf" + }, { + "Name": "Course", + "Enabled": true, + "Prefix": "!course" } ], + "Courses": [ + {"Name": "HunterLife", "Enabled": true}, + {"Name": "ExtraA", "Enabled": true}, + {"Name": "Premium", "Enabled": true}, + {"Name": "Assist", "Enabled": true}, + {"Name": "Netcafe", "Enabled": true}, + {"Name": "Hiden", "Enabled": true}, + {"Name": "HunterSupport", "Enabled": true}, + {"Name": "NetcafeBoost", "Enabled": true} + ], "Database": { "Host": "localhost", "Port": 5432, diff --git a/config/config.go b/config/config.go index 5d21c2240..f15d71276 100644 --- a/config/config.go +++ b/config/config.go @@ -70,6 +70,12 @@ type Command struct { Prefix string } +// Courses is an array of enabled courses +type Courses struct { + Name string + Enabled bool +} + // Database holds the postgres database config. type Database struct { Host string diff --git a/network/mhfpacket/msg_sys_update_right.go b/network/mhfpacket/msg_sys_update_right.go index 9d1dfc52a..cf4232573 100644 --- a/network/mhfpacket/msg_sys_update_right.go +++ b/network/mhfpacket/msg_sys_update_right.go @@ -36,9 +36,9 @@ type ClientRight struct { } type Course struct { - Name string - ID uint16 - Value uint32 + Aliases []string + ID uint16 + Value uint32 } // MsgSysUpdateRight represents the MSG_SYS_UPDATE_RIGHT @@ -74,24 +74,28 @@ func (m *MsgSysUpdateRight) Build(bf *byteframe.ByteFrame, ctx *clientctx.Client return nil } +func Courses() []Course { + var courses = []Course{ + {[]string{"Trial", "TL"}, 1, 0x00000002}, + {[]string{"HunterLife", "HL"}, 2, 0x00000004}, + {[]string{"ExtraA", "Extra", "EX"}, 3, 0x00000008}, + {[]string{"ExtraB"}, 4, 0x00000010}, + {[]string{"Mobile"}, 5, 0x00000020}, + {[]string{"Premium"}, 6, 0x00000040}, + {[]string{"Pallone"}, 7, 0x00000080}, + {[]string{"Assist", "Legend", "Rasta"}, 8, 0x00000100}, // Legend + {[]string{"Netcafe", "N", "Cafe"}, 9, 0x00000200}, + {[]string{"Hiden", "Secret"}, 10, 0x00000400}, // Secret + {[]string{"HunterSupport", "HunterAid", "Support", "Royal", "Aid"}, 11, 0x00000800}, // Royal + {[]string{"NetcafeBoost", "NBoost", "Boost"}, 12, 0x00001000}, + } + return courses +} + // GetCourseStruct returns a slice of Course(s) from a rights integer func GetCourseStruct(rights uint32) []Course { - var courses = []Course{ - {"Trial", 1, 0x00000002}, - {"HunterLife", 2, 0x00000004}, - {"ExtraA", 3, 0x00000008}, - {"ExtraB", 4, 0x00000010}, - {"Mobile", 5, 0x00000020}, - {"Premium", 6, 0x00000040}, - {"Pallone", 7, 0x00000080}, - {"Assist", 8, 0x00000100}, // Legend - {"Netcafe", 9, 0x00000200}, - {"Hiden", 10, 0x00000400}, // Secret - {"HunterSupport", 11, 0x00000800}, // Royal - {"NetcafeBoost", 12, 0x00001000}, - } var resp []Course - for _, course := range courses { + for _, course := range Courses() { if rights-course.Value < 0x80000000 { resp = append(resp, course) rights -= course.Value diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index 94c32d8bd..19547fec8 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -75,13 +75,11 @@ func doAckSimpleFail(s *Session, ackHandle uint32, data []byte) { func updateRights(s *Session) { rightsInt := uint32(0x0E) s.server.db.QueryRow("SELECT rights FROM users u INNER JOIN characters c ON u.id = c.user_id WHERE c.id = $1", s.charID).Scan(&rightsInt) - - courses := mhfpacket.GetCourseStruct(rightsInt) + s.courses = mhfpacket.GetCourseStruct(rightsInt) rights := []mhfpacket.ClientRight{{1, 0, 0}} - for _, course := range courses { + for _, course := range s.courses { rights = append(rights, mhfpacket.ClientRight{ID: course.ID, Timestamp: 0x70DB59F0}) } - update := &mhfpacket.MsgSysUpdateRight{ ClientRespAckHandle: 0, Bitfield: rightsInt, diff --git a/server/channelserver/handlers_cast_binary.go b/server/channelserver/handlers_cast_binary.go index fb0d0331f..05206ad0a 100644 --- a/server/channelserver/handlers_cast_binary.go +++ b/server/channelserver/handlers_cast_binary.go @@ -311,6 +311,43 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) { } } + if strings.HasPrefix(chatMessage.Message, commands["Course"].Prefix) { + if commands["Course"].Enabled { + var name string + n, err := fmt.Sscanf(chatMessage.Message, "!course %s", &name) + if err != nil || n != 1 { + sendServerChatMessage(s, "Error in command. Format: !course ") + } else { + name = strings.ToLower(name) + for _, course := range mhfpacket.Courses() { + for _, alias := range course.Aliases { + if strings.ToLower(name) == strings.ToLower(alias) { + if s.CourseExists(name) { + existingIndex := -1 + for i, course := range s.courses { + for _, alias := range course.Aliases { + if strings.ToLower(name) == strings.ToLower(alias) { + existingIndex = i + } + } + } + if existingIndex >= 0 { + s.courses = append(s.courses[:existingIndex], s.courses[existingIndex+1:]...) + sendServerChatMessage(s, fmt.Sprintf(`%s Course disabled.`, course.Aliases[0])) + } + } else { + s.courses = append(s.courses, course) + sendServerChatMessage(s, fmt.Sprintf(`%s Course enabled.`, course.Aliases[0])) + } + } + } + } + } + } else { + sendDisabledCommandMessage(s, commands["Course"]) + } + } + if strings.HasPrefix(chatMessage.Message, commands["Raviente"].Prefix) { if commands["Raviente"].Enabled { if getRaviSemaphore(s) != "" { diff --git a/server/channelserver/sys_session.go b/server/channelserver/sys_session.go index 2d3ce9bd1..e7c626d09 100644 --- a/server/channelserver/sys_session.go +++ b/server/channelserver/sys_session.go @@ -272,8 +272,10 @@ func (s *Session) logMessage(opcode uint16, data []byte, sender string, recipien func (s *Session) CourseExists(name string) bool { for _, course := range s.courses { - if strings.ToLower(name) == strings.ToLower(course.Name) { - return true + for _, alias := range course.Aliases { + if strings.ToLower(name) == strings.ToLower(alias) { + return true + } } } return false From 8afec7a7dfef7bcd09a201e56cd9f03148817fe8 Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 16:42:10 +1100 Subject: [PATCH 3/6] upgrade to go 1.19 --- go.mod | 33 ++++++++++++++++++++++++--------- go.sum | 26 +++++++++----------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index f4e7f1033..1d7f83e7b 100644 --- a/go.mod +++ b/go.mod @@ -1,23 +1,38 @@ module erupe-ce -go 1.16 +go 1.19 require ( github.com/bwmarrin/discordgo v0.23.2 - github.com/golang/mock v1.6.0 // indirect github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 - github.com/gorilla/websocket v1.4.2 // indirect github.com/jmoiron/sqlx v1.3.4 github.com/lib/pq v1.10.4 - github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/sachaos/lottery v0.0.0-20180520074626-61949d99bd96 github.com/spf13/viper v1.8.1 + go.uber.org/zap v1.18.1 + golang.org/x/crypto v0.1.0 + golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f + golang.org/x/text v0.4.0 +) + +require ( + github.com/felixge/httpsnoop v1.0.1 // indirect + github.com/fsnotify/fsnotify v1.4.9 // indirect + github.com/golang/mock v1.6.0 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/magiconair/properties v1.8.5 // indirect + github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/pelletier/go-toml v1.9.3 // indirect + github.com/spf13/afero v1.6.0 // indirect + github.com/spf13/cast v1.3.1 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.2.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.7.0 // indirect - go.uber.org/zap v1.18.1 - golang.org/x/crypto v0.0.0-20211202192323-5770296d904e - golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect - golang.org/x/text v0.3.7 - golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f // indirect + golang.org/x/sys v0.1.0 // indirect + gopkg.in/ini.v1 v1.62.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 1e7747306..ca2bd44a9 100644 --- a/go.sum +++ b/go.sum @@ -261,7 +261,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ= @@ -291,9 +290,8 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8= -golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= +golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -304,6 +302,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f h1:Al51T6tzvuh3oiwX11vex3QgJ2XTedFPGmbEVh8cdoc= +golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -330,7 +330,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -367,8 +366,6 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -432,12 +429,9 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E= -golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -446,9 +440,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -505,8 +498,7 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg= -golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= +golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From 676bb736bf6ec3773486bf83609a493e1ce775d0 Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 16:43:58 +1100 Subject: [PATCH 4/6] lockable courses via config --- config/config.go | 5 ++-- server/channelserver/handlers.go | 2 +- server/channelserver/handlers_cast_binary.go | 29 ++++++++++++-------- server/channelserver/sys_session.go | 24 ++++++---------- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/config/config.go b/config/config.go index f15d71276..d2804b9fc 100644 --- a/config/config.go +++ b/config/config.go @@ -21,6 +21,7 @@ type Config struct { DevModeOptions DevModeOptions Discord Discord Commands []Command + Courses []Course Database Database Launcher Launcher Sign Sign @@ -70,8 +71,8 @@ type Command struct { Prefix string } -// Courses is an array of enabled courses -type Courses struct { +// Course represents a course within MHF +type Course struct { Name string Enabled bool } diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index 19547fec8..851f4c1df 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -213,7 +213,7 @@ func logoutPlayer(s *Session) { timePlayed += sessionTime var rpGained int - if s.CourseExists("Netcafe") { + if s.FindCourse("Netcafe").Value != 0 { rpGained = timePlayed / 900 timePlayed = timePlayed % 900 } else { diff --git a/server/channelserver/handlers_cast_binary.go b/server/channelserver/handlers_cast_binary.go index 05206ad0a..9372d1572 100644 --- a/server/channelserver/handlers_cast_binary.go +++ b/server/channelserver/handlers_cast_binary.go @@ -7,6 +7,7 @@ import ( "erupe-ce/network/binpacket" "erupe-ce/network/mhfpacket" "fmt" + "golang.org/x/exp/slices" "math" "math/rand" "strings" @@ -322,22 +323,26 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) { for _, course := range mhfpacket.Courses() { for _, alias := range course.Aliases { if strings.ToLower(name) == strings.ToLower(alias) { - if s.CourseExists(name) { - existingIndex := -1 - for i, course := range s.courses { - for _, alias := range course.Aliases { - if strings.ToLower(name) == strings.ToLower(alias) { - existingIndex = i + if slices.Contains(s.server.erupeConfig.Courses, config.Course{Name: course.Aliases[0], Enabled: true}) { + if s.FindCourse(name).Value != 0 { + existingIndex := -1 + for i, course := range s.courses { + for _, alias := range course.Aliases { + if strings.ToLower(name) == strings.ToLower(alias) { + existingIndex = i + } } } - } - if existingIndex >= 0 { - s.courses = append(s.courses[:existingIndex], s.courses[existingIndex+1:]...) - sendServerChatMessage(s, fmt.Sprintf(`%s Course disabled.`, course.Aliases[0])) + if existingIndex >= 0 { + s.courses = append(s.courses[:existingIndex], s.courses[existingIndex+1:]...) + sendServerChatMessage(s, fmt.Sprintf(`%s Course disabled.`, course.Aliases[0])) + } + } else { + s.courses = append(s.courses, course) + sendServerChatMessage(s, fmt.Sprintf(`%s Course enabled.`, course.Aliases[0])) } } else { - s.courses = append(s.courses, course) - sendServerChatMessage(s, fmt.Sprintf(`%s Course enabled.`, course.Aliases[0])) + sendServerChatMessage(s, fmt.Sprintf(`%s Course is locked.`, course.Aliases[0])) } } } diff --git a/server/channelserver/sys_session.go b/server/channelserver/sys_session.go index e7c626d09..4e7429bf8 100644 --- a/server/channelserver/sys_session.go +++ b/server/channelserver/sys_session.go @@ -12,12 +12,10 @@ import ( "erupe-ce/common/byteframe" "erupe-ce/common/stringstack" - "erupe-ce/common/stringsupport" "erupe-ce/network" "erupe-ce/network/clientctx" "erupe-ce/network/mhfpacket" "go.uber.org/zap" - "golang.org/x/text/encoding/japanese" ) type packet struct { @@ -69,16 +67,12 @@ type Session struct { // NewSession creates a new Session type. func NewSession(server *Server, conn net.Conn) *Session { s := &Session{ - logger: server.logger.Named(conn.RemoteAddr().String()), - server: server, - rawConn: conn, - cryptConn: network.NewCryptConn(conn), - sendPackets: make(chan packet, 20), - clientContext: &clientctx.ClientContext{ - StrConv: &stringsupport.StringConverter{ - Encoding: japanese.ShiftJIS, - }, - }, + logger: server.logger.Named(conn.RemoteAddr().String()), + server: server, + rawConn: conn, + cryptConn: network.NewCryptConn(conn), + sendPackets: make(chan packet, 20), + clientContext: &clientctx.ClientContext{}, sessionStart: Time_Current_Adjusted().Unix(), stageMoveStack: stringstack.New(), } @@ -270,13 +264,13 @@ func (s *Session) logMessage(opcode uint16, data []byte, sender string, recipien } } -func (s *Session) CourseExists(name string) bool { +func (s *Session) FindCourse(name string) mhfpacket.Course { for _, course := range s.courses { for _, alias := range course.Aliases { if strings.ToLower(name) == strings.ToLower(alias) { - return true + return course } } } - return false + return mhfpacket.Course{} } From 4390617ccfd90570d5da3cc06dd06784e6be5fa8 Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 23:18:53 +1100 Subject: [PATCH 5/6] update rights integer via struct --- network/mhfpacket/msg_sys_update_right.go | 7 ++++++- server/channelserver/handlers.go | 2 +- server/channelserver/handlers_cast_binary.go | 20 +++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/network/mhfpacket/msg_sys_update_right.go b/network/mhfpacket/msg_sys_update_right.go index cf4232573..0702a5b3e 100644 --- a/network/mhfpacket/msg_sys_update_right.go +++ b/network/mhfpacket/msg_sys_update_right.go @@ -3,6 +3,7 @@ package mhfpacket import ( "errors" ps "erupe-ce/common/pascalstring" + "golang.org/x/exp/slices" "erupe-ce/common/byteframe" "erupe-ce/network" @@ -95,7 +96,11 @@ func Courses() []Course { // GetCourseStruct returns a slice of Course(s) from a rights integer func GetCourseStruct(rights uint32) []Course { var resp []Course - for _, course := range Courses() { + s := Courses() + slices.SortStableFunc(s, func(i, j Course) bool { + return i.ID > j.ID + }) + for _, course := range s { if rights-course.Value < 0x80000000 { resp = append(resp, course) rights -= course.Value diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index 851f4c1df..edd2dc33c 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -213,7 +213,7 @@ func logoutPlayer(s *Session) { timePlayed += sessionTime var rpGained int - if s.FindCourse("Netcafe").Value != 0 { + if s.FindCourse("Netcafe").ID != 0 { rpGained = timePlayed / 900 timePlayed = timePlayed % 900 } else { diff --git a/server/channelserver/handlers_cast_binary.go b/server/channelserver/handlers_cast_binary.go index 9372d1572..64666d9af 100644 --- a/server/channelserver/handlers_cast_binary.go +++ b/server/channelserver/handlers_cast_binary.go @@ -325,22 +325,28 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) { if strings.ToLower(name) == strings.ToLower(alias) { if slices.Contains(s.server.erupeConfig.Courses, config.Course{Name: course.Aliases[0], Enabled: true}) { if s.FindCourse(name).Value != 0 { - existingIndex := -1 - for i, course := range s.courses { - for _, alias := range course.Aliases { + ei := slices.IndexFunc(s.courses, func(c mhfpacket.Course) bool { + for _, alias := range c.Aliases { if strings.ToLower(name) == strings.ToLower(alias) { - existingIndex = i + return true } } - } - if existingIndex >= 0 { - s.courses = append(s.courses[:existingIndex], s.courses[existingIndex+1:]...) + return false + }) + if ei != -1 { + s.courses = append(s.courses[:ei], s.courses[ei+1:]...) sendServerChatMessage(s, fmt.Sprintf(`%s Course disabled.`, course.Aliases[0])) } } else { s.courses = append(s.courses, course) sendServerChatMessage(s, fmt.Sprintf(`%s Course enabled.`, course.Aliases[0])) } + var newInt uint32 + for _, course := range s.courses { + newInt += course.Value + } + s.server.db.Exec("UPDATE users u SET rights=$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", newInt, s.charID) + updateRights(s) } else { sendServerChatMessage(s, fmt.Sprintf(`%s Course is locked.`, course.Aliases[0])) } From fe413b897ac10ff6b8ffca42963b3e2781cd7ad8 Mon Sep 17 00:00:00 2001 From: wish Date: Sun, 30 Oct 2022 23:19:52 +1100 Subject: [PATCH 6/6] update default config --- config.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index a29d7aff4..31ecd8416 100644 --- a/config.json +++ b/config.json @@ -37,7 +37,7 @@ "Commands": [ { "Name": "Rights", - "Enabled": true, + "Enabled": false, "Prefix": "!rights" }, { "Name": "Raviente", @@ -65,11 +65,11 @@ {"Name": "HunterLife", "Enabled": true}, {"Name": "ExtraA", "Enabled": true}, {"Name": "Premium", "Enabled": true}, - {"Name": "Assist", "Enabled": true}, - {"Name": "Netcafe", "Enabled": true}, - {"Name": "Hiden", "Enabled": true}, - {"Name": "HunterSupport", "Enabled": true}, - {"Name": "NetcafeBoost", "Enabled": true} + {"Name": "Assist", "Enabled": false}, + {"Name": "Netcafe", "Enabled": false}, + {"Name": "Hiden", "Enabled": false}, + {"Name": "HunterSupport", "Enabled": false}, + {"Name": "NetcafeBoost", "Enabled": false} ], "Database": { "Host": "localhost",