183 lines
3.5 KiB
Go
183 lines
3.5 KiB
Go
package service
|
||
|
||
import (
|
||
"blazing/common/utils"
|
||
"blazing/cool"
|
||
"blazing/modules/config/service"
|
||
"blazing/modules/player/model"
|
||
"time"
|
||
|
||
"github.com/gogf/gf/v2/util/grand"
|
||
)
|
||
|
||
type EggService struct {
|
||
BaseService
|
||
}
|
||
|
||
func NewEggService(id uint32) *EggService {
|
||
return &EggService{
|
||
|
||
BaseService: BaseService{userid: id,
|
||
|
||
Service: &cool.Service{Model: model.NewEgg()},
|
||
},
|
||
}
|
||
|
||
}
|
||
func (s *EggService) Get() (out *model.Egg) {
|
||
|
||
s.TestModel(s.Model).Scan(&out)
|
||
if out != nil {
|
||
BreedLeftTime := int64(out.Data.StartTime+out.CurEgg.EggID*uint32(time.Hour/1000000000)) - (time.Now().Unix())
|
||
if cool.Config.ServerInfo.IsVip != 0 {
|
||
BreedLeftTime = 0
|
||
}
|
||
//判断是否繁殖完成
|
||
if BreedLeftTime <= 0 && out.Data.HatchState == 1 {
|
||
out.Data.HatchState = 2
|
||
} else {
|
||
out.Data.HatchLeftTime = uint32(BreedLeftTime)
|
||
}
|
||
s.TestModel(s.Model).Save(out)
|
||
}
|
||
|
||
return
|
||
|
||
}
|
||
func (s *EggService) StartBreed(m, f *model.PetInfo) bool {
|
||
if m.Gender != 1 {
|
||
return false
|
||
}
|
||
if f.Gender != 2 {
|
||
return false
|
||
}
|
||
if m.Generation > 9 {
|
||
return false
|
||
}
|
||
if f.Generation > 9 {
|
||
return false
|
||
}
|
||
var tt *model.Egg
|
||
s.TestModel(s.Model).Scan(&tt)
|
||
if tt == nil {
|
||
tt = &model.Egg{}
|
||
tt.IsVip = cool.Config.ServerInfo.IsVip
|
||
}
|
||
//如果正在孵化中或者蛋列表超过4个了,就不能再开始新的繁殖了
|
||
if len(tt.EggList) > 4 {
|
||
return false
|
||
}
|
||
|
||
cureff := model.EggInfo{FeMalePetID: f.ID, MalePetID: m.ID}
|
||
|
||
cureff.EggCatchTime = uint32(time.Now().Unix())
|
||
|
||
cureff.EggID = (uint32(m.Generation)+uint32(f.Generation))/2 + 1
|
||
|
||
cureff.OwnerID = uint32(s.userid)
|
||
|
||
cureff.FeMalePetID = f.ID
|
||
|
||
cureff.MalePetID = m.ID
|
||
tt.PlayerID = uint64(s.userid)
|
||
//tt.CurEgg = cureff
|
||
tt.EggList = append(tt.EggList, cureff)
|
||
|
||
s.TestModel(s.Model).Save(tt)
|
||
return true
|
||
}
|
||
func (s *EggService) StartEgg(owner, eggc uint32) bool {
|
||
|
||
var tt *model.Egg
|
||
s.TestModel(s.Model).Scan(&tt)
|
||
if tt == nil {
|
||
return false
|
||
}
|
||
if tt.Data.HatchState != 0 {
|
||
return false
|
||
|
||
}
|
||
|
||
index, v, ok := utils.FindWithIndex(tt.EggList, func(item model.EggInfo) bool {
|
||
return item.EggCatchTime == eggc
|
||
})
|
||
if !ok {
|
||
return false
|
||
}
|
||
tt.EggList = append(tt.EggList[:index], tt.EggList[index+1:]...) //删除精灵蛋
|
||
tt.Data.StartTime = uint32(time.Now().Unix())
|
||
|
||
tt.Data.HatchState = 1
|
||
|
||
tt.Data.EggID = v.EggID
|
||
|
||
tt.Data.FeMalePetID = v.FeMalePetID
|
||
|
||
tt.Data.MalePetID = v.MalePetID
|
||
|
||
tt.PlayerID = uint64(s.userid)
|
||
tt.CurEgg = *v
|
||
|
||
tt.Data.Intimacy = 1
|
||
s.TestModel(s.Model).Save(tt)
|
||
return true
|
||
|
||
}
|
||
func (s *EggService) EffectHatch() uint32 {
|
||
|
||
var tt *model.Egg
|
||
s.TestModel(s.Model).Scan(&tt)
|
||
if tt == nil {
|
||
return 1
|
||
}
|
||
if tt.Data.HatchState != 1 {
|
||
return 1
|
||
|
||
}
|
||
tt.Data.Intimacy += 1
|
||
if tt.Data.Intimacy > 5 {
|
||
tt.Data.Intimacy = 5
|
||
}
|
||
s.TestModel(s.Model).Save(tt)
|
||
return tt.Data.Intimacy
|
||
}
|
||
func (s *EggService) GetEgg() *model.PetInfo {
|
||
|
||
var tt *model.Egg
|
||
s.TestModel(s.Model).Scan(&tt)
|
||
if tt == nil {
|
||
return nil
|
||
}
|
||
if tt.Data.HatchState != 2 {
|
||
return nil
|
||
|
||
}
|
||
if tt.CurEgg.EggCatchTime == 0 {
|
||
return nil
|
||
|
||
}
|
||
mpets := service.NewEggService().GetResult(tt.CurEgg.MalePetID, tt.CurEgg.FeMalePetID)
|
||
if mpets == 0 {
|
||
return nil
|
||
}
|
||
|
||
dv := grand.N(int(tt.Data.Intimacy+tt.CurEgg.EggID*2), 31)
|
||
p := model.GenPetInfo(int(mpets), dv, -1, 0, 1, nil, -1)
|
||
shinycont := 0
|
||
|
||
if mpets != tt.CurEgg.MalePetID && mpets != tt.CurEgg.FeMalePetID {
|
||
shinycont = int(tt.CurEgg.EggID) * int(tt.CurEgg.EggID)
|
||
}
|
||
|
||
if grand.Meet(shinycont, 100) {
|
||
p.FixShiny()
|
||
}
|
||
//直接覆盖当前蛋
|
||
tt.Data.HatchState = 0
|
||
p.Generation = uint16(tt.CurEgg.EggID) + 1
|
||
tt.CurEgg = model.EggInfo{}
|
||
s.TestModel(s.Model).Save(tt)
|
||
|
||
return p
|
||
}
|