Files
bl/logic/controller/item.go
2025-11-18 20:52:04 +00:00

167 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"blazing/common/data/xmlres"
"blazing/common/socket/errorcode"
"math/rand"
"time"
"blazing/logic/service/item"
"blazing/logic/service/player"
"blazing/modules/blazing/model"
)
func (h Controller) UserItemList(data *item.ItemListInboundInfo, c *player.Player) (result *item.ItemListOutboundInfo, err errorcode.ErrorCode) {
result = &item.ItemListOutboundInfo{}
result.ItemList = make([]model.SingleItemInfo, 0)
item := c.Service.Item.Get(data.Param1, data.Param2)
for _, v := range item {
var vv model.SingleItemInfo
vv.ItemId = v.ItemId
vv.ItemCnt = v.ItemCnt
vv.LeftTime = 360000
result.ItemList = append(result.ItemList, vv)
}
return result, 0
}
func (h Controller) PlayerGoldCount(data *item.GoldOnlineRemainInboundInfo, c *player.Player) (result *item.GoldOnlineRemainOutboundInfo, err errorcode.ErrorCode) {
return &item.GoldOnlineRemainOutboundInfo{
GoldNumber: uint32(c.Info.GoldBean) * 100,
Coin: c.Info.Coins,
}, 0
}
func (h Controller) PlayerExp(data *item.ExpTotalRemainInboundInfo, c *player.Player) (result *item.ExpTotalRemainOutboundInfo, err errorcode.ErrorCode) {
return &item.ExpTotalRemainOutboundInfo{
TotalExp: uint32(c.Info.ExpPool),
}, 0
}
func (h Controller) BuyItem(data *item.BuyInboundInfo, c *player.Player) (result *item.BuyOutboundInfo, err errorcode.ErrorCode) {
tt, ok := xmlres.ItemsMAP[int(data.ItemId)]
if ok && tt.Price != 0 && c.UseCoins(data.Count*uint32(tt.Price)) {
r := c.ItemAdd(model.SingleItemInfo{ItemId: data.ItemId, ItemCnt: data.Count})
if len(r) != 0 {
return &item.BuyOutboundInfo{
ItemId: data.ItemId,
Level: 1,
Count: data.Count,
Coins: c.Info.Coins,
}, 0
}
}
return &item.BuyOutboundInfo{
Coins: c.Info.Coins,
}, 0
}
func (h Controller) ChangePlayerCloth(data *item.ChangePlayerClothInboundInfo, c *player.Player) (result *item.ChangePlayerClothOutboundInfo, err errorcode.ErrorCode) {
result = &item.ChangePlayerClothOutboundInfo{
UserID: c.Info.UserID,
ClothList: make([]model.PeopleItemInfo, 0),
}
for _, v := range data.ClothList {
result.ClothList = append(result.ClothList, model.PeopleItemInfo{ID: v, Level: 1})
}
c.Info.Clothes = result.ClothList
c.GetSpace().Broadcast(c, data.Head.CMD, result)
return
}
func (h Controller) Talk(data *item.TalkCountInboundInfo, c *player.Player) (result *item.TalkCountOutboundInfo, err errorcode.ErrorCode) {
result = &item.TalkCountOutboundInfo{}
c.Service.Talk.Exec(func(t map[uint32]uint32) bool {
tt, ok := t[data.ID]
if ok {
result.GiftCount = tt
}
return false
})
return result, 0
}
var talkcacche = make(map[string]uint32)
func (h Controller) TalkCate(data *item.TalkCateInboundInfo, c *player.Player) (result *item.DayTalkInfo, err errorcode.ErrorCode) {
result = &item.DayTalkInfo{}
result.OutList = make([]item.CateInfo, 0)
for _, te := range xmlres.TalkConfig.Energies {
if te.MapID == uint64(c.Info.MapID) && te.Type == uint64(data.ID) { //
_, ok := talkcacche[te.Name]
if !ok {
for _, v := range xmlres.ItemsMAP {
if v.Name == te.Name {
talkcacche[te.Name] = uint32(v.ID)
}
}
}
rand.Seed(time.Now().UnixNano()) // UnixNano 精度更高,避免短时间内种子重复
// 2. 生成 1-10 的随机数rand.Intn(10) → 0-9+1 后范围变为 1-10
randomNum := rand.Intn(10) + 1
c.Service.Talk.Exec(func(t map[uint32]uint32) bool {
if t == nil {
t = make(map[uint32]uint32)
}
_, ok := t[data.ID]
if !ok {
t[data.ID] = 0
}
t[data.ID] += 1
if t[data.ID] < uint32(te.CollectCnt) {
result.OutList = append(result.OutList, item.CateInfo{ID: uint32(talkcacche[te.Name]), Count: uint32(randomNum)})
c.ItemAdd(model.SingleItemInfo{ItemId: uint32(talkcacche[te.Name]), ItemCnt: uint32(randomNum)})
}
return true
})
break
}
}
return result, 0
}
func (h Controller) BuyMItem(data *item.BuyMultiInboundInfo, c *player.Player) (result *item.BuyMultiOutboundInfo, err errorcode.ErrorCode) {
var rrr []model.SingleItemInfo
for _, v := range data.ItemIds {
_, ok := xmlres.ItemsMAP[int(v)]
if ok {
rrr = append(rrr, model.SingleItemInfo{ItemId: uint32(v), ItemCnt: 1})
}
}
r := c.ItemAdd(rrr...)
if len(r) != 0 {
return &item.BuyMultiOutboundInfo{
Coins: c.Info.Coins,
}, 0
}
return &item.BuyMultiOutboundInfo{
Coins: c.Info.Coins,
}, 0
}