mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-12 23:14:36 +01:00
simplify reused code
This commit is contained in:
@@ -53,6 +53,30 @@ func ReadWarehouseItem(bf *byteframe.ByteFrame) MHFItemStack {
|
|||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DiffItemStacks(o []MHFItemStack, u []MHFItemStack) []MHFItemStack {
|
||||||
|
// o = old, u = update, f = final
|
||||||
|
var f []MHFItemStack
|
||||||
|
for _, uItem := range u {
|
||||||
|
exists := false
|
||||||
|
for i := range o {
|
||||||
|
if o[i].WarehouseID == uItem.WarehouseID {
|
||||||
|
exists = true
|
||||||
|
o[i].Quantity = uItem.Quantity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
uItem.WarehouseID = token.RNG.Uint32()
|
||||||
|
f = append(f, uItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, oItem := range o {
|
||||||
|
if oItem.Quantity > 0 {
|
||||||
|
f = append(f, oItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
func (is MHFItemStack) ToBytes() []byte {
|
func (is MHFItemStack) ToBytes() []byte {
|
||||||
bf := byteframe.NewByteFrame()
|
bf := byteframe.NewByteFrame()
|
||||||
bf.WriteUint32(is.WarehouseID)
|
bf.WriteUint32(is.WarehouseID)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"erupe-ce/common/mhfmon"
|
"erupe-ce/common/mhfmon"
|
||||||
ps "erupe-ce/common/pascalstring"
|
ps "erupe-ce/common/pascalstring"
|
||||||
"erupe-ce/common/stringsupport"
|
"erupe-ce/common/stringsupport"
|
||||||
"erupe-ce/common/token"
|
|
||||||
_config "erupe-ce/config"
|
_config "erupe-ce/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -844,28 +843,8 @@ func handleMsgMhfEnumerateUnionItem(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
|
|
||||||
func handleMsgMhfUpdateUnionItem(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgMhfUpdateUnionItem(s *Session, p mhfpacket.MHFPacket) {
|
||||||
pkt := p.(*mhfpacket.MsgMhfUpdateUnionItem)
|
pkt := p.(*mhfpacket.MsgMhfUpdateUnionItem)
|
||||||
// o = old, u = update, f = final
|
newStacks := mhfitem.DiffItemStacks(userGetItems(s), pkt.UpdatedItems)
|
||||||
var fItems []mhfitem.MHFItemStack
|
s.server.db.Exec(`UPDATE users u SET item_box=$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)`, mhfitem.SerializeWarehouseItems(newStacks), s.charID)
|
||||||
oItems := userGetItems(s)
|
|
||||||
for _, uItem := range pkt.UpdatedItems {
|
|
||||||
exists := false
|
|
||||||
for i := range oItems {
|
|
||||||
if oItems[i].WarehouseID == uItem.WarehouseID {
|
|
||||||
exists = true
|
|
||||||
oItems[i].Quantity = uItem.Quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
uItem.WarehouseID = token.RNG.Uint32()
|
|
||||||
fItems = append(fItems, uItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, oItem := range oItems {
|
|
||||||
if oItem.Quantity > 0 {
|
|
||||||
fItems = append(fItems, oItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.server.db.Exec(`UPDATE users u SET item_box=$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)`, mhfitem.SerializeWarehouseItems(fItems), s.charID)
|
|
||||||
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"erupe-ce/common/mhfitem"
|
"erupe-ce/common/mhfitem"
|
||||||
"erupe-ce/common/token"
|
|
||||||
_config "erupe-ce/config"
|
_config "erupe-ce/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
@@ -1580,28 +1579,8 @@ func handleMsgMhfEnumerateGuildItem(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
|
|
||||||
func handleMsgMhfUpdateGuildItem(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgMhfUpdateGuildItem(s *Session, p mhfpacket.MHFPacket) {
|
||||||
pkt := p.(*mhfpacket.MsgMhfUpdateGuildItem)
|
pkt := p.(*mhfpacket.MsgMhfUpdateGuildItem)
|
||||||
// o = old, u = update, f = final
|
newStacks := mhfitem.DiffItemStacks(guildGetItems(s, pkt.GuildID), pkt.UpdatedItems)
|
||||||
var fItems []mhfitem.MHFItemStack
|
s.server.db.Exec(`UPDATE guilds SET item_box=$1 WHERE id=$2`, mhfitem.SerializeWarehouseItems(newStacks), pkt.GuildID)
|
||||||
oItems := guildGetItems(s, pkt.GuildID)
|
|
||||||
for _, uItem := range pkt.UpdatedItems {
|
|
||||||
exists := false
|
|
||||||
for i := range oItems {
|
|
||||||
if oItems[i].WarehouseID == uItem.WarehouseID {
|
|
||||||
exists = true
|
|
||||||
oItems[i].Quantity = uItem.Quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
uItem.WarehouseID = token.RNG.Uint32()
|
|
||||||
fItems = append(fItems, uItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, oItem := range oItems {
|
|
||||||
if oItem.Quantity > 0 {
|
|
||||||
fItems = append(fItems, oItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.server.db.Exec(`UPDATE guilds SET item_box=$1 WHERE id=$2`, mhfitem.SerializeWarehouseItems(fItems), pkt.GuildID)
|
|
||||||
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -495,32 +495,12 @@ func handleMsgMhfEnumerateWarehouse(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
|
|
||||||
func handleMsgMhfUpdateWarehouse(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgMhfUpdateWarehouse(s *Session, p mhfpacket.MHFPacket) {
|
||||||
pkt := p.(*mhfpacket.MsgMhfUpdateWarehouse)
|
pkt := p.(*mhfpacket.MsgMhfUpdateWarehouse)
|
||||||
// o = old, u = update, f = final
|
|
||||||
var fItems []mhfitem.MHFItemStack
|
|
||||||
var fEquip []mhfitem.MHFEquipment
|
|
||||||
switch pkt.BoxType {
|
switch pkt.BoxType {
|
||||||
case 0:
|
case 0:
|
||||||
oItems := warehouseGetItems(s, pkt.BoxIndex)
|
newStacks := mhfitem.DiffItemStacks(warehouseGetItems(s, pkt.BoxIndex), pkt.UpdatedItems)
|
||||||
for _, uItem := range pkt.UpdatedItems {
|
s.server.db.Exec(fmt.Sprintf(`UPDATE warehouse SET item%d=$1 WHERE character_id=$2`, pkt.BoxIndex), mhfitem.SerializeWarehouseItems(newStacks), s.charID)
|
||||||
exists := false
|
|
||||||
for i := range oItems {
|
|
||||||
if oItems[i].WarehouseID == uItem.WarehouseID {
|
|
||||||
exists = true
|
|
||||||
oItems[i].Quantity = uItem.Quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
uItem.WarehouseID = token.RNG.Uint32()
|
|
||||||
fItems = append(fItems, uItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, oItem := range oItems {
|
|
||||||
if oItem.Quantity > 0 {
|
|
||||||
fItems = append(fItems, oItem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.server.db.Exec(fmt.Sprintf(`UPDATE warehouse SET item%d=$1 WHERE character_id=$2`, pkt.BoxIndex), mhfitem.SerializeWarehouseItems(fItems), s.charID)
|
|
||||||
case 1:
|
case 1:
|
||||||
|
var fEquip []mhfitem.MHFEquipment
|
||||||
oEquips := warehouseGetEquipment(s, pkt.BoxIndex)
|
oEquips := warehouseGetEquipment(s, pkt.BoxIndex)
|
||||||
for _, uEquip := range pkt.UpdatedEquipment {
|
for _, uEquip := range pkt.UpdatedEquipment {
|
||||||
exists := false
|
exists := false
|
||||||
|
|||||||
Reference in New Issue
Block a user