216 lines
6.4 KiB
Go
216 lines
6.4 KiB
Go
package caravan
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/utils"
|
|
"math"
|
|
)
|
|
|
|
type Caravan struct {
|
|
modules.ModuleBase
|
|
modelCaravan *modelCaravan
|
|
api *apiComp
|
|
configure *configureComp
|
|
}
|
|
|
|
func NewModule() core.IModule {
|
|
return &Caravan{}
|
|
}
|
|
|
|
func (this *Caravan) GetType() core.M_Modules {
|
|
return comm.ModuleTroll
|
|
}
|
|
|
|
func (this *Caravan) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
|
err = this.ModuleBase.Init(service, module, options)
|
|
|
|
return
|
|
}
|
|
|
|
func (this *Caravan) OnInstallComp() {
|
|
this.ModuleBase.OnInstallComp()
|
|
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.modelCaravan = this.RegisterComp(new(modelCaravan)).(*modelCaravan)
|
|
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
|
}
|
|
|
|
// 接口信息 修改数据
|
|
func (this *Caravan) ModifyCaravanData(uid string, data map[string]interface{}) (code pb.ErrorCode) {
|
|
err := this.modelCaravan.modifyCaravanDataByObjId(uid, data)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
}
|
|
return
|
|
}
|
|
|
|
// 初始化城市信息
|
|
func (this *Caravan) InitCaravanCityData(uid string, data *pb.DBCaravan) {
|
|
list := this.configure.GetAllCaravanCity()
|
|
data.City = make(map[int32]*pb.CityInfo, 0)
|
|
for _, v := range list {
|
|
city := &pb.CityInfo{
|
|
Like: []int32{}, // 城市卖给玩家的商品 (注意 这里有库存 必须初始化 Count 字段数据)
|
|
Unlike: []int32{}, // 城市想要玩家卖给他的商品库
|
|
Count: map[int32]int32{}, // key 货物ID
|
|
Rtime: configure.Now().Unix(), // 初始化城市货物刷新时间
|
|
}
|
|
|
|
if len(v.Special) > int(v.Citytypenum) {
|
|
ids := utils.RandomNumbers(0, len(v.Special), int(v.Citytypenum))
|
|
for _, id := range ids {
|
|
city.Like = append(city.Like, v.Special[id])
|
|
|
|
}
|
|
} else {
|
|
city.Like = append(city.Like, v.Special...)
|
|
}
|
|
for _, v := range city.Like {
|
|
city.Count[v] = 40 // 配置暂无 后面走配置
|
|
}
|
|
city.Unlike = append(city.Like, v.Exspecial...)
|
|
|
|
data.City[v.Id] = city
|
|
}
|
|
}
|
|
|
|
// 初始化货物信息
|
|
func (this *Caravan) InitCaravanItemData(uid string, data *pb.DBCaravan) {
|
|
items := this.configure.GetAllCaravanItem()
|
|
data.Goods = make(map[int32]*pb.Goods, 0)
|
|
for _, v := range items {
|
|
goods := &pb.Goods{
|
|
Period: 0, // 变动周期
|
|
CurPeriod: 1, // 当前变动周期
|
|
Price: 0, // 当前价格
|
|
Time: configure.Now().Unix(), // 刷新时间
|
|
}
|
|
if len(v.Changeperiod) == 2 {
|
|
goods.Period = comm.GetRandNum(v.Changeperiod[0], v.Changeperiod[1])
|
|
goods.Price = v.Goodsprice
|
|
data.Goods[v.Id] = goods
|
|
}
|
|
}
|
|
}
|
|
|
|
// 初始化门票和虚拟币
|
|
func (this *Caravan) InitCaravanTicket(session comm.IUserSession, lv int32) (code pb.ErrorCode) {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
if conf := this.modelCaravan.module.configure.GetCaravanLv(lv); conf != nil {
|
|
var res []*cfg.Gameatn
|
|
res = append(res, conf.Tickettop)
|
|
res = append(res, conf.Moneynumtop)
|
|
code = this.DispenseRes(session, res, true) // 推送门票和虚拟币资源
|
|
}
|
|
return
|
|
}
|
|
|
|
// 刷新城市货物信息
|
|
func (this *Caravan) refreshCaravanCityInfo(uid string, data *pb.DBCaravan) {
|
|
var (
|
|
bChange bool
|
|
update map[string]interface{}
|
|
)
|
|
update = make(map[string]interface{})
|
|
for k, v := range data.City {
|
|
if c := this.configure.GetCaravanCity(k); c != nil {
|
|
if configure.Now().Unix()-v.Rtime >= int64(c.Checktime) {
|
|
v.Rtime = configure.Now().Unix() - (configure.Now().Unix()-v.Rtime)%int64(c.Checktime)
|
|
v.Count = make(map[int32]int32) // 初始化城市信息
|
|
v.Like = []int32{}
|
|
v.Unlike = []int32{}
|
|
if len(c.Special) > int(c.Citytypenum) {
|
|
ids := utils.RandomNumbers(0, len(c.Special), int(c.Citytypenum))
|
|
for _, id := range ids {
|
|
v.Like = append(v.Like, c.Special[id])
|
|
|
|
}
|
|
} else {
|
|
v.Like = append(v.Like, c.Special...)
|
|
}
|
|
for _, v1 := range v.Like {
|
|
if itemConf := this.configure.GetCaravanGoods(v1); itemConf != nil { // 更新商店库存
|
|
v.Count[v1] = itemConf.Goodsnum
|
|
}
|
|
}
|
|
v.Unlike = append(v.Like, c.Exspecial...)
|
|
bChange = true
|
|
}
|
|
}
|
|
}
|
|
if bChange {
|
|
update["city"] = data.City
|
|
this.modelCaravan.modifyCaravanDataByObjId(uid, update)
|
|
}
|
|
}
|
|
|
|
func (this *Caravan) refreshCaravanItemInfo(uid string, data *pb.DBCaravan) {
|
|
var (
|
|
bChange bool
|
|
update map[string]interface{}
|
|
)
|
|
for k, v := range data.Goods {
|
|
if c := this.configure.GetCaravanGoods(k); c != nil {
|
|
if configure.Now().Unix()-v.Time > int64(c.Changetime) {
|
|
bChange = true
|
|
subTime := configure.Now().Unix() - v.Time
|
|
icount := int32(subTime / int64(c.Changetime)) // 循环周期
|
|
if icount > 50 { //超过一定的周期 则不计算
|
|
// 随机出新的变动周期
|
|
v.Period = comm.GetRandNum(c.Changeperiod[0], c.Changeperiod[1])
|
|
v.CurPeriod = 0
|
|
v.Time = configure.Now().Unix()
|
|
} else {
|
|
for i := 0; i < int(icount); i++ { // 计算当前的价格
|
|
// 价格涨跌权重 PriceChangeWeight
|
|
bUp := false // true 涨
|
|
ipos := comm.GetRandW(c.PriceChangeWeight)
|
|
if ipos == 1 {
|
|
if comm.GetRandW(c.PriceChangeWeightOne) == 0 {
|
|
bUp = true
|
|
}
|
|
} else if ipos == 2 {
|
|
if comm.GetRandW(c.PriceChangeWeightTwo) == 0 {
|
|
bUp = true
|
|
}
|
|
} else if ipos == 3 {
|
|
if comm.GetRandW(c.PriceChangeWeightThree) == 0 {
|
|
bUp = true
|
|
}
|
|
}
|
|
|
|
if bUp { // 价格上涨
|
|
v.Price = int32(math.Floor(float64(v.Price) * (1.0 + float64(c.FluctuationRange)/1000.0)))
|
|
} else {
|
|
v.Price = int32(math.Floor(float64(v.Price) * (1.0 - float64(c.FluctuationRange)/1000.0)))
|
|
}
|
|
|
|
if v.Price < c.Pricemin { // 设置最小值
|
|
v.Price = c.Pricemin
|
|
}
|
|
|
|
if v.Price > c.Pricemax { // 设置最大值
|
|
v.Price = c.Pricemax
|
|
}
|
|
v.CurPeriod += 1 // 更新周期+1
|
|
if v.CurPeriod+icount > v.Period {
|
|
// 随机出新的变动周期
|
|
v.Period = comm.GetRandNum(c.Changeperiod[0], c.Changeperiod[1])
|
|
v.CurPeriod = 0
|
|
}
|
|
}
|
|
v.Time = configure.Now().Unix() - (subTime % int64(c.Changetime)) // 写入刷新时间
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if bChange {
|
|
update["goods"] = data.Goods
|
|
this.modelCaravan.modifyCaravanDataByObjId(uid, update)
|
|
}
|
|
}
|