diff --git a/network/mhfpacket/msg_mhf_check_weekly_stamp.go b/network/mhfpacket/msg_mhf_check_weekly_stamp.go index 83f6e5ce8..f03b1d1e7 100644 --- a/network/mhfpacket/msg_mhf_check_weekly_stamp.go +++ b/network/mhfpacket/msg_mhf_check_weekly_stamp.go @@ -1,15 +1,16 @@ package mhfpacket import ( + "errors" + "erupe-ce/common/byteframe" "erupe-ce/network" "erupe-ce/network/clientctx" - "erupe-ce/common/byteframe" ) // MsgMhfCheckWeeklyStamp represents the MSG_MHF_CHECK_WEEKLY_STAMP type MsgMhfCheckWeeklyStamp struct { AckHandle uint32 - Unk0 uint8 + StampType string Unk1 bool Unk2 uint16 // Hardcoded 0 in the binary } @@ -22,7 +23,13 @@ func (m *MsgMhfCheckWeeklyStamp) Opcode() network.PacketID { // Parse parses the packet from binary func (m *MsgMhfCheckWeeklyStamp) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { m.AckHandle = bf.ReadUint32() - m.Unk0 = bf.ReadUint8() + stampType := bf.ReadUint8() + switch stampType { + case 1: + m.StampType = "hl" + case 2: + m.StampType = "ex" + } m.Unk1 = bf.ReadBool() m.Unk2 = bf.ReadUint16() return nil @@ -30,9 +37,5 @@ func (m *MsgMhfCheckWeeklyStamp) Parse(bf *byteframe.ByteFrame, ctx *clientctx.C // Build builds a binary packet from the current data. func (m *MsgMhfCheckWeeklyStamp) Build(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { - bf.WriteUint32(m.AckHandle) - bf.WriteUint8(m.Unk0) - bf.WriteBool(m.Unk1) - bf.WriteUint16(m.Unk2) - return nil + return errors.New("NOT IMPLEMENTED") } diff --git a/network/mhfpacket/msg_mhf_exchange_weekly_stamp.go b/network/mhfpacket/msg_mhf_exchange_weekly_stamp.go index 6cce19147..758925936 100644 --- a/network/mhfpacket/msg_mhf_exchange_weekly_stamp.go +++ b/network/mhfpacket/msg_mhf_exchange_weekly_stamp.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" ) // MsgMhfExchangeWeeklyStamp represents the MSG_MHF_EXCHANGE_WEEKLY_STAMP -type MsgMhfExchangeWeeklyStamp struct{} +type MsgMhfExchangeWeeklyStamp struct { + AckHandle uint32 + StampType string + Unk1 bool + Unk2 uint16 +} // Opcode returns the ID associated with this packet type. func (m *MsgMhfExchangeWeeklyStamp) Opcode() network.PacketID { @@ -18,7 +23,17 @@ func (m *MsgMhfExchangeWeeklyStamp) Opcode() network.PacketID { // Parse parses the packet from binary func (m *MsgMhfExchangeWeeklyStamp) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error { - return errors.New("NOT IMPLEMENTED") + m.AckHandle = bf.ReadUint32() + stampType := bf.ReadUint8() + switch stampType { + case 1: + m.StampType = "hl" + case 2: + m.StampType = "ex" + } + m.Unk1 = bf.ReadBool() + m.Unk2 = bf.ReadUint16() + return nil } // Build builds a binary packet from the current data. diff --git a/patch-schema/stamps.sql b/patch-schema/stamps.sql new file mode 100644 index 000000000..2b940fa8c --- /dev/null +++ b/patch-schema/stamps.sql @@ -0,0 +1,13 @@ +BEGIN; + +CREATE TABLE IF NOT EXISTS public.stamps ( + character_id integer PRIMARY KEY, + hl_total integer DEFAULT 0, + hl_redeemed integer DEFAULT 0, + hl_next timestamp without time zone, + ex_total integer DEFAULT 0, + ex_redeemed integer DEFAULT 0, + ex_next timestamp without time zone +); + +END; \ No newline at end of file diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index 26447a2fb..fa2bbf2f4 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -646,19 +646,41 @@ func handleMsgMhfGetCogInfo(s *Session, p mhfpacket.MHFPacket) {} func handleMsgMhfCheckWeeklyStamp(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfCheckWeeklyStamp) - - resp := byteframe.NewByteFrame() - resp.WriteUint16(0x000E) - resp.WriteUint16(0x0001) - resp.WriteUint16(0x0000) - resp.WriteUint16(0x0000) // 0x0000 stops the vaguely annoying log in pop up - resp.WriteUint32(0) - resp.WriteUint32(0x5dddcbb3) // Timestamp - - doAckBufSucceed(s, pkt.AckHandle, resp.Data()) + weekCurrentStart := TimeWeekStart() + weekNextStart := TimeWeekNext() + var total, redeemed, updated uint16 + var nextClaim time.Time + err := s.server.db.QueryRow(fmt.Sprintf("SELECT %s_next FROM stamps WHERE character_id=$1", pkt.StampType), s.charID).Scan(&nextClaim) + if err != nil { + s.server.db.Exec("INSERT INTO stamps (character_id, hl_next, ex_next) VALUES ($1, $2, $2)", s.charID, weekNextStart) + nextClaim = weekNextStart + } + if nextClaim.Before(weekCurrentStart) { + s.server.db.Exec(fmt.Sprintf("UPDATE stamps SET %s_total=%s_total+1, %s_next=$1 WHERE character_id=$2", pkt.StampType, pkt.StampType, pkt.StampType), weekNextStart, s.charID) + updated = 1 + } + s.server.db.QueryRow(fmt.Sprintf("SELECT %s_total, %s_redeemed FROM stamps WHERE character_id=$1", pkt.StampType, pkt.StampType), s.charID).Scan(&total, &redeemed) + bf := byteframe.NewByteFrame() + bf.WriteUint16(total) + bf.WriteUint16(redeemed) + bf.WriteUint16(updated) + bf.WriteUint32(0) // Unk + bf.WriteUint32(uint32(weekCurrentStart.Unix())) + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } -func handleMsgMhfExchangeWeeklyStamp(s *Session, p mhfpacket.MHFPacket) {} +func handleMsgMhfExchangeWeeklyStamp(s *Session, p mhfpacket.MHFPacket) { + pkt := p.(*mhfpacket.MsgMhfExchangeWeeklyStamp) + var total, redeemed uint16 + s.server.db.QueryRow(fmt.Sprintf("UPDATE stamps SET %s_redeemed=%s_redeemed+8 WHERE character_id=$1 RETURNING %s_total, %s_redeemed", pkt.StampType, pkt.StampType, pkt.StampType, pkt.StampType), s.charID).Scan(&total, &redeemed) + bf := byteframe.NewByteFrame() + bf.WriteUint16(total) + bf.WriteUint16(redeemed) + bf.WriteUint16(0) + bf.WriteUint32(0) // Unk, but has possible values + bf.WriteUint32(uint32(TimeWeekStart().Unix())) + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) +} func getGookData(s *Session, cid uint32) (uint16, []byte) { var data []byte diff --git a/server/channelserver/sys_timefix.go b/server/channelserver/sys_timefix.go index 839382053..4cde6a319 100644 --- a/server/channelserver/sys_timefix.go +++ b/server/channelserver/sys_timefix.go @@ -31,6 +31,16 @@ func Time_Current_Midnight() time.Time { return time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), 0, 0, 0, 0, baseTime.Location()) } +func TimeWeekStart() time.Time { + midnight := Time_Current_Midnight() + offset := (int(midnight.Weekday()) - 1) * -24 + return midnight.Add(time.Hour * time.Duration(offset)) +} + +func TimeWeekNext() time.Time { + return TimeWeekStart().Add(time.Hour * 24 * 7) +} + func Time_Current_Week_uint8() uint8 { baseTime := time.Now().In(time.FixedZone(fmt.Sprintf("UTC+%d", Offset), Offset*60*60)).AddDate(YearAdjust, MonthAdjust, DayAdjust)