mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-02-04 09:15:08 +01:00
initial distributions rework
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
|||||||
// MsgMhfEnumerateDistItem represents the MSG_MHF_ENUMERATE_DIST_ITEM
|
// MsgMhfEnumerateDistItem represents the MSG_MHF_ENUMERATE_DIST_ITEM
|
||||||
type MsgMhfEnumerateDistItem struct {
|
type MsgMhfEnumerateDistItem struct {
|
||||||
AckHandle uint32
|
AckHandle uint32
|
||||||
Unk0 uint8
|
DistType uint8
|
||||||
Unk1 uint8
|
Unk1 uint8
|
||||||
Unk2 uint16
|
Unk2 uint16
|
||||||
Unk3 []byte
|
Unk3 []byte
|
||||||
@@ -24,7 +24,7 @@ func (m *MsgMhfEnumerateDistItem) Opcode() network.PacketID {
|
|||||||
// Parse parses the packet from binary
|
// Parse parses the packet from binary
|
||||||
func (m *MsgMhfEnumerateDistItem) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error {
|
func (m *MsgMhfEnumerateDistItem) Parse(bf *byteframe.ByteFrame, ctx *clientctx.ClientContext) error {
|
||||||
m.AckHandle = bf.ReadUint32()
|
m.AckHandle = bf.ReadUint32()
|
||||||
m.Unk0 = bf.ReadUint8()
|
m.DistType = bf.ReadUint8()
|
||||||
m.Unk1 = bf.ReadUint8()
|
m.Unk1 = bf.ReadUint8()
|
||||||
m.Unk2 = bf.ReadUint16()
|
m.Unk2 = bf.ReadUint16()
|
||||||
m.Unk3 = bf.ReadBytes(uint(bf.ReadUint8()))
|
m.Unk3 = bf.ReadBytes(uint(bf.ReadUint8()))
|
||||||
|
|||||||
15
patch-schema/10-rework-distributions.sql
Normal file
15
patch-schema/10-rework-distributions.sql
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- This will delete all of your old distribution data!
|
||||||
|
--ALTER TABLE IF EXISTS public.distribution DROP COLUMN IF EXISTS data;
|
||||||
|
|
||||||
|
CREATE TABLE public.distribution_items
|
||||||
|
(
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
distribution_id integer,
|
||||||
|
item_type integer,
|
||||||
|
item_id integer,
|
||||||
|
quantity integer
|
||||||
|
);
|
||||||
|
|
||||||
|
END;
|
||||||
@@ -4,24 +4,25 @@ import (
|
|||||||
"erupe-ce/common/byteframe"
|
"erupe-ce/common/byteframe"
|
||||||
ps "erupe-ce/common/pascalstring"
|
ps "erupe-ce/common/pascalstring"
|
||||||
"erupe-ce/network/mhfpacket"
|
"erupe-ce/network/mhfpacket"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ItemDist struct {
|
type ItemDist struct {
|
||||||
ID uint32 `db:"id"`
|
ID uint32 `db:"id"`
|
||||||
Deadline uint32 `db:"deadline"`
|
Deadline time.Time `db:"deadline"`
|
||||||
TimesAcceptable uint16 `db:"times_acceptable"`
|
TimesAcceptable uint16 `db:"times_acceptable"`
|
||||||
TimesAccepted uint16 `db:"times_accepted"`
|
TimesAccepted uint16 `db:"times_accepted"`
|
||||||
MinHR uint16 `db:"min_hr"`
|
MinHR uint16 `db:"min_hr"`
|
||||||
MaxHR uint16 `db:"max_hr"`
|
MaxHR uint16 `db:"max_hr"`
|
||||||
MinSR uint16 `db:"min_sr"`
|
MinSR uint16 `db:"min_sr"`
|
||||||
MaxSR uint16 `db:"max_sr"`
|
MaxSR uint16 `db:"max_sr"`
|
||||||
MinGR uint16 `db:"min_gr"`
|
MinGR uint16 `db:"min_gr"`
|
||||||
MaxGR uint16 `db:"max_gr"`
|
MaxGR uint16 `db:"max_gr"`
|
||||||
EventName string `db:"event_name"`
|
EventName string `db:"event_name"`
|
||||||
Description string `db:"description"`
|
Description string `db:"description"`
|
||||||
Data []byte `db:"data"`
|
Data []byte `db:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
|
||||||
@@ -37,13 +38,10 @@ func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
WHERE d.id = da.distribution_id
|
WHERE d.id = da.distribution_id
|
||||||
AND da.character_id = $1
|
AND da.character_id = $1
|
||||||
) AS times_accepted,
|
) AS times_accepted,
|
||||||
CASE
|
COALESCE(deadline, TO_TIMESTAMP(0)) AS deadline
|
||||||
WHEN (EXTRACT(epoch FROM deadline)::int) IS NULL THEN 0
|
|
||||||
ELSE (EXTRACT(epoch FROM deadline)::int)
|
|
||||||
END deadline
|
|
||||||
FROM distribution d
|
FROM distribution d
|
||||||
WHERE character_id = $1 AND type = $2 OR character_id IS NULL AND type = $2 ORDER BY id DESC;
|
WHERE character_id = $1 AND type = $2 OR character_id IS NULL AND type = $2 ORDER BY id DESC;
|
||||||
`, s.charID, pkt.Unk0)
|
`, s.charID, pkt.DistType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Error getting distribution data from db", zap.Error(err))
|
s.logger.Error("Error getting distribution data from db", zap.Error(err))
|
||||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4))
|
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||||
@@ -56,7 +54,7 @@ func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
s.logger.Error("Error parsing item distribution data", zap.Error(err))
|
s.logger.Error("Error parsing item distribution data", zap.Error(err))
|
||||||
}
|
}
|
||||||
bf.WriteUint32(distData.ID)
|
bf.WriteUint32(distData.ID)
|
||||||
bf.WriteUint32(distData.Deadline)
|
bf.WriteUint32(uint32(distData.Deadline.Unix()))
|
||||||
bf.WriteUint32(0) // Unk
|
bf.WriteUint32(0) // Unk
|
||||||
bf.WriteUint16(distData.TimesAcceptable)
|
bf.WriteUint16(distData.TimesAcceptable)
|
||||||
bf.WriteUint16(distData.TimesAccepted)
|
bf.WriteUint16(distData.TimesAccepted)
|
||||||
@@ -93,67 +91,61 @@ func handleMsgMhfEnumerateDistItem(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
resp := byteframe.NewByteFrame()
|
resp := byteframe.NewByteFrame()
|
||||||
resp.WriteUint16(uint16(distCount))
|
resp.WriteUint16(uint16(distCount))
|
||||||
resp.WriteBytes(bf.Data())
|
resp.WriteBytes(bf.Data())
|
||||||
resp.WriteUint8(0)
|
|
||||||
doAckBufSucceed(s, pkt.AckHandle, resp.Data())
|
doAckBufSucceed(s, pkt.AckHandle, resp.Data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DistributionItem struct {
|
||||||
|
ItemType uint8 `db:"item_type"`
|
||||||
|
ID uint32 `db:"id"`
|
||||||
|
ItemID uint32 `db:"item_id"`
|
||||||
|
Quantity uint32 `db:"quantity"`
|
||||||
|
}
|
||||||
|
|
||||||
func handleMsgMhfApplyDistItem(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgMhfApplyDistItem(s *Session, p mhfpacket.MHFPacket) {
|
||||||
pkt := p.(*mhfpacket.MsgMhfApplyDistItem)
|
pkt := p.(*mhfpacket.MsgMhfApplyDistItem)
|
||||||
|
|
||||||
if pkt.DistributionID == 0 {
|
bf := byteframe.NewByteFrame()
|
||||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 6))
|
bf.WriteUint32(pkt.DistributionID)
|
||||||
} else {
|
var distItems []DistributionItem
|
||||||
row := s.server.db.QueryRowx("SELECT data FROM distribution WHERE id = $1", pkt.DistributionID)
|
rows, err := s.server.db.Queryx(`SELECT id, item_id, item_type, quantity FROM distribution_items WHERE distribution_id=$1`, pkt.DistributionID)
|
||||||
dist := &ItemDist{}
|
if err == nil {
|
||||||
err := row.StructScan(dist)
|
var distItem DistributionItem
|
||||||
if err != nil {
|
for rows.Next() {
|
||||||
s.logger.Error("Error parsing item distribution data", zap.Error(err))
|
err = rows.StructScan(&distItem)
|
||||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 6))
|
if err != nil {
|
||||||
return
|
continue
|
||||||
|
}
|
||||||
|
distItems = append(distItems, distItem)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if len(dist.Data) >= 2 {
|
bf.WriteUint16(uint16(len(distItems)))
|
||||||
distData := byteframe.NewByteFrameFromBytes(dist.Data)
|
for _, item := range distItems {
|
||||||
distItems := int(distData.ReadUint16())
|
bf.WriteUint8(item.ItemType)
|
||||||
for i := 0; i < distItems; i++ {
|
bf.WriteUint32(item.ItemID)
|
||||||
if len(dist.Data) >= 2+(i*13) {
|
bf.WriteUint32(item.Quantity)
|
||||||
itemType := distData.ReadUint8()
|
bf.WriteUint32(item.ID)
|
||||||
_ = distData.ReadBytes(6)
|
switch item.ItemType {
|
||||||
quantity := int(distData.ReadUint16())
|
case 17:
|
||||||
_ = distData.ReadBytes(4)
|
_ = addPointNetcafe(s, int(item.Quantity))
|
||||||
switch itemType {
|
case 19:
|
||||||
case 17:
|
s.server.db.Exec("UPDATE users u SET gacha_premium=gacha_premium+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", item.Quantity, s.charID)
|
||||||
_ = addPointNetcafe(s, quantity)
|
case 20:
|
||||||
case 19:
|
s.server.db.Exec("UPDATE users u SET gacha_trial=gacha_trial+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", item.Quantity, s.charID)
|
||||||
s.server.db.Exec("UPDATE users u SET gacha_premium=gacha_premium+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", quantity, s.charID)
|
case 21:
|
||||||
case 20:
|
s.server.db.Exec("UPDATE users u SET frontier_points=frontier_points+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", item.Quantity, s.charID)
|
||||||
s.server.db.Exec("UPDATE users u SET gacha_trial=gacha_trial+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", quantity, s.charID)
|
case 23:
|
||||||
case 21:
|
saveData, err := GetCharacterSaveData(s, s.charID)
|
||||||
s.server.db.Exec("UPDATE users u SET frontier_points=frontier_points+$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)", quantity, s.charID)
|
if err == nil {
|
||||||
case 23:
|
saveData.RP += uint16(item.Quantity)
|
||||||
saveData, err := GetCharacterSaveData(s, s.charID)
|
saveData.Save(s)
|
||||||
if err == nil {
|
|
||||||
saveData.RP += uint16(quantity)
|
|
||||||
saveData.Save(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
doAckBufSucceed(s, pkt.AckHandle, bf.Data())
|
||||||
|
|
||||||
bf := byteframe.NewByteFrame()
|
if pkt.DistributionID > 0 {
|
||||||
bf.WriteUint32(pkt.DistributionID)
|
_, err = s.server.db.Exec(`INSERT INTO public.distributions_accepted VALUES ($1, $2)`, pkt.DistributionID, s.charID)
|
||||||
bf.WriteBytes(dist.Data)
|
|
||||||
doAckBufSucceed(s, pkt.AckHandle, bf.Data())
|
|
||||||
|
|
||||||
_, err = s.server.db.Exec(`
|
|
||||||
INSERT INTO public.distributions_accepted
|
|
||||||
VALUES ($1, $2)
|
|
||||||
`, pkt.DistributionID, s.charID)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("Error updating accepted dist count", zap.Error(err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user