From 56cad91261476921797cac7505ff78c2b4f24c04 Mon Sep 17 00:00:00 2001 From: wishu Date: Sun, 10 Jul 2022 00:53:41 +1000 Subject: [PATCH] add support for guild meals --- Erupe/guild-food.sql | 12 ++++ .../mhfpacket/msg_mhf_load_guild_cooking.go | 8 +-- .../mhfpacket/msg_mhf_regist_guild_cooking.go | 18 +++--- Erupe/server/channelserver/handlers_guild.go | 61 +++++++++++++------ 4 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 Erupe/guild-food.sql diff --git a/Erupe/guild-food.sql b/Erupe/guild-food.sql new file mode 100644 index 000000000..34a2dec08 --- /dev/null +++ b/Erupe/guild-food.sql @@ -0,0 +1,12 @@ +BEGIN; + +CREATE TABLE IF NOT EXISTS public.guild_meals +( + id serial NOT NULL PRIMARY KEY, + guild_id int NOT NULL, + meal_id int NOT NULL, + level int NOT NULL, + expires int NOT NULL +); + +END; \ No newline at end of file diff --git a/Erupe/network/mhfpacket/msg_mhf_load_guild_cooking.go b/Erupe/network/mhfpacket/msg_mhf_load_guild_cooking.go index 30acccf2a..c1c288502 100644 --- a/Erupe/network/mhfpacket/msg_mhf_load_guild_cooking.go +++ b/Erupe/network/mhfpacket/msg_mhf_load_guild_cooking.go @@ -1,7 +1,7 @@ package mhfpacket -import ( - "errors" +import ( + "errors" "erupe-ce/network/clientctx" "erupe-ce/network" @@ -11,7 +11,7 @@ import ( // MsgMhfLoadGuildCooking represents the MSG_MHF_LOAD_GUILD_COOKING type MsgMhfLoadGuildCooking struct{ AckHandle uint32 - Unk0 uint8 + MaxMeals uint8 } // Opcode returns the ID associated with this packet type. @@ -22,7 +22,7 @@ func (m *MsgMhfLoadGuildCooking) Opcode() network.PacketID { // Parse parses the packet from binary func (m *MsgMhfLoadGuildCooking) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { m.AckHandle = bf.ReadUint32() - m.Unk0 = bf.ReadUint8() + _ = bf.ReadUint8() return nil } diff --git a/Erupe/network/mhfpacket/msg_mhf_regist_guild_cooking.go b/Erupe/network/mhfpacket/msg_mhf_regist_guild_cooking.go index c97ae8797..3ab0dd535 100644 --- a/Erupe/network/mhfpacket/msg_mhf_regist_guild_cooking.go +++ b/Erupe/network/mhfpacket/msg_mhf_regist_guild_cooking.go @@ -1,7 +1,7 @@ package mhfpacket -import ( - "errors" +import ( + "errors" "erupe-ce/network/clientctx" "erupe-ce/network" @@ -10,10 +10,10 @@ import ( // MsgMhfRegistGuildCooking represents the MSG_MHF_REGIST_GUILD_COOKING type MsgMhfRegistGuildCooking struct{ - AckHandle uint32 - Unk0 uint32 - Unk1 uint16 - Unk2 uint8 + AckHandle uint32 + OverwriteID uint32 + MealID uint16 + Success uint8 } // Opcode returns the ID associated with this packet type. @@ -24,9 +24,9 @@ func (m *MsgMhfRegistGuildCooking) Opcode() network.PacketID { // Parse parses the packet from binary func (m *MsgMhfRegistGuildCooking) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { m.AckHandle = bf.ReadUint32() - m.Unk0 = bf.ReadUint32() - m.Unk1 = bf.ReadUint16() - m.Unk2 = bf.ReadUint8() + m.OverwriteID = bf.ReadUint32() + m.MealID = bf.ReadUint16() + m.Success = bf.ReadUint8() return nil } diff --git a/Erupe/server/channelserver/handlers_guild.go b/Erupe/server/channelserver/handlers_guild.go index a0d56a58b..9a978c306 100644 --- a/Erupe/server/channelserver/handlers_guild.go +++ b/Erupe/server/channelserver/handlers_guild.go @@ -1637,31 +1637,56 @@ func handleMsgMhfGetGuildMissionRecord(s *Session, p mhfpacket.MHFPacket) { doAckBufSucceed(s, pkt.AckHandle, make([]byte, 0x190)) } +type GuildMeal struct { + ID uint32 `db:"id"` + MealID uint32 `db:"meal_id"` + Level uint32 `db:"level"` + Expires uint32 `db:"expires"` +} + func handleMsgMhfLoadGuildCooking(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfLoadGuildCooking) - // MealData - // uint16 meal count - // Meal - // uint32 hash? - // uint32 unk - // uint32 success level - // uint32 expiration timestamp - // encourage food - data := []byte{0x00, 0x06, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x02, 0xc4, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x0B, 0x51, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x02, 0x9c, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x0B, 0x52, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x02, 0x07, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x0B, 0x51, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x01, 0x8b, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFD, 0x0B, 0x51, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x02, 0x54, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFC, 0x0B, 0x51, - 0x0F, 0x51, 0x97, 0xFF, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x03, 0xF3, 0xFC, 0x0B, 0x51} - doAckBufSucceed(s, pkt.AckHandle, data) - //data := []byte{0x00, 0x01, 0x1C, 0x72, 0x54, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x5F, 0xF8, 0x2F, 0xE1} - //doAckBufSucceed(s, pkt.AckHandle, data) + guild, _ := GetGuildInfoByCharacterId(s, s.charID) + data, err := s.server.db.Queryx("SELECT id, meal_id, level, expires FROM guild_meals WHERE guild_id = $1", guild.ID) + if err != nil { + s.logger.Fatal("Failed to get guild meals from db", zap.Error(err)) + } + temp := byteframe.NewByteFrame() + count := 0 + for data.Next() { + mealData := &GuildMeal{} + err = data.StructScan(&mealData) + if err != nil { + s.logger.Fatal("Failed to scan meal data", zap.Error(err)) + } + if mealData.Expires > uint32(Time_Current_Adjusted().Add(-60 * time.Minute).Unix()) { + count++ + temp.WriteUint32(mealData.ID) + temp.WriteUint32(mealData.MealID) + temp.WriteUint32(mealData.Level) + temp.WriteUint32(mealData.Expires) + } + } + bf := byteframe.NewByteFrame() + bf.WriteUint16(uint16(count)) + bf.WriteBytes(temp.Data()) + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } func handleMsgMhfRegistGuildCooking(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfRegistGuildCooking) + guild, _ := GetGuildInfoByCharacterId(s, s.charID) + if pkt.OverwriteID != 0 { + _, err := s.server.db.Exec("DELETE FROM guild_meals WHERE id = $1", pkt.OverwriteID) + if err != nil { + s.logger.Fatal("Failed to delete meal in db", zap.Error(err)) + } + } + _, err := s.server.db.Exec("INSERT INTO guild_meals (guild_id, meal_id, level, expires) VALUES ($1, $2, $3, $4)", guild.ID, pkt.MealID, pkt.Success, Time_Current_Adjusted().Add(30 * time.Minute).Unix()) + if err != nil { + s.logger.Fatal("Failed to register meal in db", zap.Error(err)) + } doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x01, 0x00}) }