go_dreamfactory/modules/caravan/api_buyorsell.go
2023-11-13 17:20:35 +08:00

281 lines
8.1 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 caravan
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
)
//参数校验
func (this *apiComp) BuyOrSellCheck(session comm.IUserSession, req *pb.CaravanBuyOrSellReq) (errdata *pb.ErrorData) {
if len(req.Items) == 0 || req.City == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSellReq) (errdata *pb.ErrorData) {
var (
update map[string]interface{}
addScore int32 // 收益
cityInfo *pb.CityInfo
ok bool
sellValue int32 // 任务统计 贩卖货物价值
buyValue int32 // 任务统计 贩卖货物价值
sellSpValue int32 // 任务统计 向指定X城市贩卖货物贩卖货物价值
profitValue int32 // 盈利
)
update = make(map[string]interface{})
if errdata = this.BuyOrSellCheck(session, req); errdata != nil {
return // 参数校验失败直接返回
}
caravan, _ := this.module.modelCaravan.getCaravanList(session.GetUserId())
if cityInfo, ok = caravan.City[req.City]; !ok {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
}
return
}
c, err := this.module.configure.GetCaravanLv(caravan.Lv)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
caravan.Baglimit = c.Bagtop
// special 城市卖给玩家的商品
// exspecial 城市想要玩家卖给他的商品库
if !req.IsBuy { // 卖给npc
for k, v := range req.Items {
// 校验背包数据够不够
caravan.Items[k].Count -= v
if caravan.Items[k].Count < 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TrollItemNoEnough, // 道具数量不足
Title: pb.ErrorCode_TrollItemNoEnough.ToString(),
}
return
}
items := caravan.Items[k]
var price int32
var prePrice int32 // 之前的价格
price = items.Price
prePrice = items.Price
bFound := false
for _, key := range cityInfo.Special {
if key == k {
if cityConf, err := this.module.configure.GetCaravanCity(req.City); err == nil {
price = cityConf.Specialnum * price / 1000
bFound = true
}
break
}
}
if !bFound {
for _, key := range cityInfo.Exspecial {
if key == k {
bFound = true
price = cityInfo.ExspecialPCT * price / 1000
break
}
}
}
if !bFound {
if cityConf, err := this.module.configure.GetCaravanCity(req.City); err == nil {
price = cityConf.Orspecial * price / 1000
}
}
sellValue += price * v
if price > prePrice {
profitValue = (price - prePrice) * v
}
}
var addRes []*cfg.Gameatn
addRes = append(addRes, &cfg.Gameatn{
A: "attr",
T: "merchantmoney",
N: sellValue,
})
addRes = append(addRes, &cfg.Gameatn{
A: "attr",
T: "profit",
N: profitValue,
})
if errdata = this.module.DispenseRes(session, addRes, true); errdata != nil {
this.module.Errorf("获得虚拟币失败:%v", errdata)
}
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.module.WriteUserLog(session.GetUserId(), "troll sell", addRes)
})
this.module.ArrayBag(caravan)
// 统计 收益
var lvReward []*cfg.Gameatn
caravan.Profit += int64(sellValue)
curLv := this.module.CheckCaravavLvUp(caravan)
update["profit"] = caravan.Profit
update["lv"] = curLv
if curLv > caravan.Lv {
for i := caravan.Lv; i <= curLv-caravan.Lv; i++ {
if c, err := this.module.configure.GetCaravanLv(int32(i)); err == nil {
lvReward = append(lvReward, c.Reward...)
update["baglimit"] = c.Bagtop
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound, // 道具数量不足
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
}
caravan.Lv = curLv
// 修改等级
if errdata = this.module.ModuleUser.ChangeUserCaravanLv(session, curLv); errdata != nil {
this.module.Errorf("ChangeUserCaravanLv err :%v", errdata)
}
}
if len(lvReward) > 0 { // 商队等级奖励 改发邮件
this.module.mail.SendRewardMailByCid(session, comm.CaravanLvReward, lvReward)
}
} else { // 买入
for k, v := range req.Items {
if _, ok := caravan.Items[k]; !ok {
caravan.Items[k] = &pb.BagInfo{
Count: 0,
Price: 0,
}
}
bFound := false
// 计算均价
buyValue = caravan.Items[k].Count * caravan.Items[k].Price
var price int32
// 获取当前节点数据
if len(caravan.Allgoods) == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TrollCityUnSellItem,
Title: pb.ErrorCode_TrollCityUnSellItem.ToString(),
}
return
}
key := configure.Now().Hour() / (24 / len(caravan.Allgoods))
if _, ok := caravan.Allgoods[int32(key)].Goods[k]; !ok {
key = 0
}
price = caravan.Allgoods[int32(key)].Goods[k]
for _, v := range cityInfo.Special {
if v == k {
bFound = true
break
}
}
if !bFound {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TrollCityUnSellItem, // 城市不卖这个物品
Title: pb.ErrorCode_TrollCityUnSellItem.ToString(),
}
return
}
caravan.Items[k].Count += v
buyValue += price * v
caravan.Items[k].Price = buyValue / caravan.Items[k].Count
// 同步更新该城市的 出售货物信息
cityInfo.Count[k] += v
if itemConf, err := this.configure.GetCaravanGoods(k); err == nil { // 更新商店库存
if cityInfo.Count[k] > itemConf.Goodsnum {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TrollBuyMax, // 商品数量不足
Title: pb.ErrorCode_TrollBuyMax.ToString(),
}
return
}
update["city"] = caravan.City
addScore -= price * v
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound, // 商品数量不足
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
}
if this.module.CheckOverweight(caravan) { // 背包满了
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TrollMaxItemCount,
Title: pb.ErrorCode_TrollMaxItemCount.ToString(),
}
return
}
// 减少虚拟币
// caravan.Merchantmoney -= addScore
// update["merchantmoney"] = caravan.Merchantmoney
if errdata = this.module.DispenseRes(session, []*cfg.Gameatn{{
A: "attr",
T: "merchantmoney",
N: addScore,
}}, true); errdata != nil {
this.module.Errorf("获得虚拟币失败:%v", errdata)
}
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.module.WriteUserLog(session.GetUserId(), "troll buy", []*cfg.Gameatn{{
A: "attr",
T: "merchantmoney",
N: addScore,
}})
})
}
update["items"] = caravan.Items
update["baglimit"] = caravan.Baglimit
this.module.modelCaravan.modifyCaravanDataByObjId(session.GetUserId(), update)
session.SendMsg(string(this.module.GetType()), "buyorsell", &pb.CaravanBuyOrSellResp{
Data: caravan,
})
this.module.rank.SetUsrRankList(session.GetUserId())
// 任务统计
var tasks []*pb.BuriedParam
if req.IsBuy {
// 209购买指定城市的急需货物X个
for k, v := range caravan.City { // 找城市
for _, v1 := range v.Exspecial { // 急需的货物
for k2, v2 := range req.Items {
if k2 == v1 {
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype209, v2, k))
break
}
}
}
}
// szTask = append(szTask, comm.GetBuriedParam(comm.Rtype209, req.City, opCount))
} else { // 卖
//向指定X城市贩卖货物贩卖货物价值需要X虚拟币以上
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype210, req.City, sellValue))
// Rtype211 TaskType = 211 // 向指定X城市贩卖价值X虚拟币以上的对应城市急需货物
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype211, req.City, sellSpValue))
}
if len(tasks) > 0 {
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.module.ModuleBuried.TriggerBuried(session, tasks...)
})
}
return
}