| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package equip
- import (
- capp "f1-game/internal/cherry_app"
- "f1-game/internal/code"
- "f1-game/internal/component/redis"
- "f1-game/internal/constant"
- "f1-game/internal/data"
- "f1-game/internal/enum"
- "f1-game/internal/event"
- "f1-game/internal/extend/rand"
- facade "f1-game/internal/facade"
- nameRoute "f1-game/internal/name/route"
- "f1-game/internal/pb"
- "f1-game/internal/sessions"
- "f1-game/internal/types"
- "f1-game/nodes/game/internal/db"
- dbEquip "f1-game/nodes/game/internal/db/data/equip"
- ao "f1-game/nodes/game/player/asset/origin"
- clog "github.com/cherry-game/cherry/logger"
- cproto "github.com/cherry-game/cherry/net/proto"
- )
- var srv = &service{}
- type service struct {
- facade.PlayerServiceBase[*db.PlayerTable]
- }
- func Service() *service {
- return srv
- }
- // func (p *service) OnLogin(playerTable *db.PlayerTable, session *cproto.Session) {
- // }
- // func (p *service) OnLogined(playerTable *db.PlayerTable, session *cproto.Session) {
- // }
- // func (p *service) OnLogout(playerTable *db.PlayerTable) {
- // }
- // func (p *service) OnReset(playerTable *db.PlayerTable, time *cherryTime.CherryTime, byLogin bool) {
- // }
- func (p *service) Push(playerTable *db.PlayerTable, session *cproto.Session) {
- equipTable := db.GetEquipTable(playerTable.PlayerID)
- proto := equipTable.Equips.ToProto()
- sessions.PushWithSession(session, nameRoute.PushGamePlayer_EquipList, proto)
- }
- // Add 添加装备
- func (p *service) Add(playerID int64, equipID int32, num int64, origin ao.Origin, push bool) (types.Assets, int32) {
- var (
- equipTable = db.GetEquipTable(playerID)
- equipRow, ok = data.Equip.GetByID(equipID)
- equipList types.List[*dbEquip.Equip]
- retAssets = types.NoneAssets
- )
- if !ok {
- return retAssets, code.ConfigNotFound_Equip
- }
- // 随机生成装备
- for range num {
- equip, errCode := p.newEquip(equipRow)
- if errCode != code.OK {
- return retAssets, errCode
- }
- equipTable.Equips.Add(equip)
- equipList.Add(equip)
- retAssets.AddAsset(types.NewAssetWithGUID(equip.EquipID, 1, equip.GUID))
- }
- if !equipList.IsEmpty() {
- equipTable.EquipExtend.AddQualityCount(int32(equipRow.Quality), int32(len(equipList)))
- equipTable.Save2Queue()
- if push {
- // 变更推送
- p.ChangePush(playerID, nil, equipList...)
- }
- // 发送装备获得事件
- capp.PostEvent(event.NewEquipAdd(playerID, retAssets))
- }
- return retAssets, code.OK
- }
- // ChangePush 装备变更推送
- func (p *service) ChangePush(playerID int64, removeEquips []int64, updateEquips ...*dbEquip.Equip) {
- if len(updateEquips) > 0 {
- rsp := &pb.EquipList{}
- for _, equip := range updateEquips {
- rsp.List = append(rsp.List, equip.ToProto())
- }
- sessions.PushPlayer(playerID, nameRoute.PushGamePlayer_EquipChange, rsp)
- }
- if len(removeEquips) > 0 {
- sessions.PushPlayer(playerID, nameRoute.PushGamePlayer_EquipDel, &pb.I64List{
- List: removeEquips,
- })
- }
- }
- // newEquip 创建装备
- func (p *service) newEquip(equipRow *data.EquipRow) (*dbEquip.Equip, int32) {
- equip := &dbEquip.Equip{
- GUID: redis.GUID.NewGUID(),
- EquipID: equipRow.ID,
- Level: 0,
- BaseAttrs: types.Attrs{},
- ExtraAttrs: types.NewPairList[int32, int64](),
- }
- // 随机基础属性
- for _, baseAttrRange := range equipRow.BaseAttrRange {
- randomAttrValue := rand.RangeInt(baseAttrRange.Opt, baseAttrRange.Value)
- equip.BaseAttrs.Add(baseAttrRange.ID, int64(randomAttrValue))
- }
- // 随机额外属性
- extraAttrNum := equipRow.GetExtraAttrNum()
- if extraAttrNum > 0 {
- pool, found := data.EquipAttrExtra.GetWeighter(equipRow.ExtraAttrPool)
- if found {
- rows := pool.SelectUnique(int(extraAttrNum))
- for _, row := range rows {
- equip.ExtraAttrs.Append(types.NewPair(row.Key, row.RandomValue()))
- }
- } else {
- clog.Warnf("equip extra attr pool not found, equipID: %d, pool: %s", equipRow.ID, equipRow.ExtraAttrPool)
- }
- }
- // 随机
- if equipRow.RareSkillProb > 0 {
- // 随机是否触发稀有技能
- randomValue := int32(rand.RangeInt(0, constant.RatioBase))
- if randomValue <= equipRow.RareSkillProb {
- skillPool := equipRow.RandomSingleSkillPool()
- if skillPool > 0 {
- // 随机技能
- skillPoolRow, errCode := data.SkillPool.Random(skillPool)
- if code.IsFail(errCode) {
- return nil, errCode
- }
- equip.SpecialSkill.Key = skillPoolRow.SkillID
- equip.SpecialSkill.Value = 1
- }
- }
- }
- return equip, code.OK
- }
- // 获取装备
- func (p *service) GetEquip(playerID int64, equipGUID int64) (*dbEquip.Equip, int32) {
- equipTable := db.GetEquipTable(playerID)
- equip, found := equipTable.Equips.Get(equipGUID)
- if found {
- return equip, code.OK
- }
- return nil, code.EquipNotFound
- }
- // 更新装备状态
- func (p *service) UpdateEquipStatus(playerID int64, equipGUID int64, state enum.EquipState) int32 {
- equipTable := db.GetEquipTable(playerID)
- equip, found := equipTable.Equips.Get(equipGUID)
- if !found {
- return code.EquipNotFound
- }
- equip.State = state
- equipTable.Save2Queue()
- p.ChangePush(playerID, nil, equip)
- return code.OK
- }
|