From 9fe1b1d04ae3bdd332c7f60b7442ac4887224e88 Mon Sep 17 00:00:00 2001 From: wish Date: Thu, 11 Aug 2022 02:50:59 +1000 Subject: [PATCH 1/2] initial titles implementation --- network/mhfpacket/msg_mhf_acquire_title.go | 21 ++++++--- patch-schema/titles.sql | 11 +++++ server/channelserver/handlers.go | 4 -- server/channelserver/handlers_house.go | 54 +++++++++++++++++----- 4 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 patch-schema/titles.sql diff --git a/network/mhfpacket/msg_mhf_acquire_title.go b/network/mhfpacket/msg_mhf_acquire_title.go index e521734f8..fe3a5ca95 100644 --- a/network/mhfpacket/msg_mhf_acquire_title.go +++ b/network/mhfpacket/msg_mhf_acquire_title.go @@ -1,15 +1,20 @@ package mhfpacket -import ( - "errors" +import ( + "errors" - "erupe-ce/network/clientctx" - "erupe-ce/network" "erupe-ce/common/byteframe" + "erupe-ce/network" + "erupe-ce/network/clientctx" ) // MsgMhfAcquireTitle represents the MSG_MHF_ACQUIRE_TITLE -type MsgMhfAcquireTitle struct{} +type MsgMhfAcquireTitle struct { + AckHandle uint32 + Unk0 uint16 + Unk1 uint16 + TitleID uint16 +} // Opcode returns the ID associated with this packet type. func (m *MsgMhfAcquireTitle) Opcode() network.PacketID { @@ -18,7 +23,11 @@ func (m *MsgMhfAcquireTitle) Opcode() network.PacketID { // Parse parses the packet from binary func (m *MsgMhfAcquireTitle) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { - return errors.New("NOT IMPLEMENTED") + m.AckHandle = bf.ReadUint32() + m.Unk0 = bf.ReadUint16() + m.Unk1 = bf.ReadUint16() + m.TitleID = bf.ReadUint16() + return nil } // Build builds a binary packet from the current data. diff --git a/patch-schema/titles.sql b/patch-schema/titles.sql new file mode 100644 index 000000000..e4a87dc86 --- /dev/null +++ b/patch-schema/titles.sql @@ -0,0 +1,11 @@ +BEGIN; + +CREATE TABLE IF NOT EXISTS public.titles +( + id int NOT NULL, + char_id int NOT NULL, + unlocked_at timestamp without time zone, + updated_at timestamp without time zone +); + +END; \ No newline at end of file diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index 1cfe9e3ff..58ea6b401 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -500,8 +500,6 @@ func handleMsgMhfTransitMessage(s *Session, p mhfpacket.MHFPacket) { func handleMsgCaExchangeItem(s *Session, p mhfpacket.MHFPacket) {} -func handleMsgMhfResetTitle(s *Session, p mhfpacket.MHFPacket) {} - func handleMsgMhfPresentBox(s *Session, p mhfpacket.MHFPacket) {} func handleMsgMhfServerCommand(s *Session, p mhfpacket.MHFPacket) {} @@ -556,8 +554,6 @@ func handleMsgMhfEnumerateOrder(s *Session, p mhfpacket.MHFPacket) { func handleMsgMhfGetExtraInfo(s *Session, p mhfpacket.MHFPacket) {} -func handleMsgMhfAcquireTitle(s *Session, p mhfpacket.MHFPacket) {} - func handleMsgMhfEnumerateUnionItem(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfEnumerateUnionItem) var boxContents []byte diff --git a/server/channelserver/handlers_house.go b/server/channelserver/handlers_house.go index 92d7c853e..7b28582d7 100644 --- a/server/channelserver/handlers_house.go +++ b/server/channelserver/handlers_house.go @@ -6,6 +6,8 @@ import ( "erupe-ce/common/stringsupport" "erupe-ce/network/mhfpacket" "go.uber.org/zap" + "io" + "time" ) func handleMsgMhfUpdateInterior(s *Session, p mhfpacket.MHFPacket) { @@ -291,25 +293,53 @@ func handleMsgMhfSaveDecoMyset(s *Session, p mhfpacket.MHFPacket) { doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00}) } +type Title struct { + ID uint16 `db:"id"` + Acquired time.Time `db:"unlocked_at"` + Updated time.Time `db:"updated_at"` +} + func handleMsgMhfEnumerateTitle(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfEnumerateTitle) + var count uint16 bf := byteframe.NewByteFrame() - if pkt.CharID == 0 { - titleCount := 114 // all titles unlocked - bf.WriteUint16(uint16(titleCount)) // title count - bf.WriteUint16(0) // unk - for i := 0; i < titleCount; i++ { - bf.WriteUint16(uint16(i)) - bf.WriteUint16(0) // unk - bf.WriteUint32(0) // timestamp acquired - bf.WriteUint32(0) // timestamp updated - } - } else { - bf.WriteUint16(0) + bf.WriteUint16(0) + bf.WriteUint16(0) // Unk + rows, err := s.server.db.Queryx("SELECT id, unlocked_at, updated_at FROM titles WHERE char_id=$1", s.charID) + if err != nil { + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) + return } + for rows.Next() { + title := &Title{} + err = rows.StructScan(&title) + if err != nil { + continue + } + count++ + bf.WriteUint16(title.ID) + bf.WriteUint16(0) // Unk + bf.WriteUint32(uint32(title.Acquired.Unix())) + bf.WriteUint32(uint32(title.Updated.Unix())) + } + bf.Seek(0, io.SeekStart) + bf.WriteUint16(count) doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } +func handleMsgMhfAcquireTitle(s *Session, p mhfpacket.MHFPacket) { + pkt := p.(*mhfpacket.MsgMhfAcquireTitle) + err := s.server.db.QueryRow("SELECT count(*) FROM titles WHERE id=$1 AND char_id=$2", pkt.TitleID, s.charID) + if err != nil { + s.server.db.Exec("INSERT INTO titles VALUES ($1, $2, now(), now())", pkt.TitleID, s.charID) + } else { + s.server.db.Exec("UPDATE titles SET updated_at=now()") + } + doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4)) +} + +func handleMsgMhfResetTitle(s *Session, p mhfpacket.MHFPacket) {} + func handleMsgMhfOperateWarehouse(s *Session, p mhfpacket.MHFPacket) {} func handleMsgMhfEnumerateWarehouse(s *Session, p mhfpacket.MHFPacket) {} From a0dbe9d8c6043c0e496bc800ba7fd774c2bed4f1 Mon Sep 17 00:00:00 2001 From: wish Date: Thu, 11 Aug 2022 18:47:50 +1000 Subject: [PATCH 2/2] change title db syntax --- server/channelserver/handlers_house.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/channelserver/handlers_house.go b/server/channelserver/handlers_house.go index 7b28582d7..401347b76 100644 --- a/server/channelserver/handlers_house.go +++ b/server/channelserver/handlers_house.go @@ -329,8 +329,9 @@ func handleMsgMhfEnumerateTitle(s *Session, p mhfpacket.MHFPacket) { func handleMsgMhfAcquireTitle(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfAcquireTitle) - err := s.server.db.QueryRow("SELECT count(*) FROM titles WHERE id=$1 AND char_id=$2", pkt.TitleID, s.charID) - if err != nil { + var exists int + err := s.server.db.QueryRow("SELECT count(*) FROM titles WHERE id=$1 AND char_id=$2", pkt.TitleID, s.charID).Scan(&exists) + if err != nil || exists == 0 { s.server.db.Exec("INSERT INTO titles VALUES ($1, $2, now(), now())", pkt.TitleID, s.charID) } else { s.server.db.Exec("UPDATE titles SET updated_at=now()")