| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package model
- import (
- "math"
- "roserver/baseserver/model"
- "roserver/serverproto"
- )
- const KeepSakeTicket = 31
- type RoleKeepSake struct {
- SaveObject
- keepSake map[int32]*serverproto.KeepSake
- material map[int32]*serverproto.KeyValueType
- changed map[int32]int32
- // keepSakePiece int32
- }
- func newRoleKeepSake(r *Role) *RoleKeepSake {
- roleKeepSake := &RoleKeepSake{
- SaveObject: SaveObject{
- role: r,
- },
- }
- roleKeepSake.keepSake = make(map[int32]*serverproto.KeepSake)
- roleKeepSake.material = make(map[int32]*serverproto.KeyValueType)
- roleKeepSake.changed = make(map[int32]int32)
- // roleKeepSake.keepSakePiece = 0
- return roleKeepSake
- }
- func (this *RoleKeepSake) Load(msg interface{}) bool {
- proRole := msg.(*serverproto.Role)
- if proRole.RoleKeepSake != nil {
- // this.keepSakePiece = proRole.RoleKeepSake.KeepSakePiece
- for _, data := range proRole.RoleKeepSake.KeepSake {
- this.keepSake[data.KeepSakeId] = data
- }
- for _, data := range proRole.RoleKeepSake.Material {
- this.material[data.Key] = data
- }
- }
- return true
- }
- func (this *RoleKeepSake) Save() {
- this.SetDirty(false)
- saveMsg := &serverproto.SSKeepSakeSaveReq{
- RoleKeepSake: &serverproto.RoleKeepSake{},
- }
- if len(this.keepSake) > 0 {
- for _, data := range this.keepSake {
- saveMsg.RoleKeepSake.KeepSake = append(saveMsg.RoleKeepSake.KeepSake, data)
- }
- }
- if len(this.material) > 0 {
- for _, data2 := range this.material {
- saveMsg.RoleKeepSake.Material = append(saveMsg.RoleKeepSake.Material, data2)
- }
- }
- this.role.SendDb(saveMsg)
- }
- func (this *RoleKeepSake) KeepSakeChangeNtf(keepSakeId int32, materialList []int32) {
- ntfMsg := &serverproto.SCKeepSakeChangeNtf{}
- keepSake, ok := this.keepSake[keepSakeId]
- if ok {
- ntfMsg.KeepSake = keepSake
- }
- for _, data := range materialList {
- bFind := false
- for index, material := range this.material {
- if material.Key == data {
- ntfMsg.Materials = append(ntfMsg.Materials, this.material[index])
- bFind = true
- }
- }
- if !bFind {
- ntfMsg.Materials = append(ntfMsg.Materials, &serverproto.KeyValueType{
- Key: data,
- Value: 0,
- })
- }
- }
- this.role.ReplayGate(ntfMsg, true)
- }
- func (this *RoleKeepSake) IsAllMeetLvl(Lvl int32) (ret bool) {
- ret = false
- argLvl := float64(Lvl)
- for _, sake := range model.ConvertKeepSake {
- data, ok := this.keepSake[sake.KeepSakeId]
- if !ok {
- return false
- }
- curLvl := int32(math.Min(float64(sake.MaxLevel), argLvl))
- if data.KeepSakeLevel < curLvl {
- return false
- }
- ret = true
- }
- return
- }
- func (this *RoleKeepSake) KeepSakeLevelUp(keepSakeId int32) serverproto.ErrorCode {
- keepSakeCfg, ok := model.ConvertKeepSake[keepSakeId]
- if !ok {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //是否有这个信物
- var nextLevel int32 = 0
- keepSake, ok := this.keepSake[keepSakeId]
- if ok {
- if keepSake.KeepSakeLevel >= keepSakeCfg.MaxLevel {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- nextLevel = keepSake.KeepSakeLevel + 1
- } else {
- nextLevel = 1
- }
- //招到对应等级的材料消耗,不够的是否可以用碎片补齐
- levelData, ok2 := keepSakeCfg.LevelData[nextLevel]
- if !ok2 {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //计算所需要材料,材料不够,用水晶补齐//扣除材料
- var needCrystal int32 = 0
- needRes := map[int32]int32{}
- for key, value := range levelData.Materials {
- data, ok3 := this.material[key]
- var needItem int32 = value
- if ok3 {
- if data.Value >= value {
- needRes[key] = value
- needItem = 0
- } else {
- needRes[key] = data.Value
- needItem = value - data.Value
- }
- }
- if needItem > 0 {
- //计算需要的水晶
- itemCfg, ok4 := serverproto.ItemCfgLoader[key]
- if !ok4 {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- ratio, ok := model.GlobalKeepSakeCrystalToMaterial[itemCfg.Quality]
- if !ok {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- needCrystal += needItem * ratio
- }
- }
- nCount := this.role.GetResNum(KeepSakeTicket)
- if nCount < uint64(needCrystal) {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //扣除信物材料
- var changeList []int32
- for key, value := range needRes {
- for _, data := range this.material {
- if key == data.Key {
- if data.Value > value {
- data.Value -= value
- } else {
- delete(this.material, key)
- }
- changeList = append(changeList, key)
- }
- }
- }
- //扣除卷轴材料
- removeList := map[int32]int32{}
- removeList[KeepSakeTicket] = needCrystal
- this.role.DelItemList(removeList, AddItemST{AddFrom: AddFrom_KeepSake})
- //升级
- if nextLevel == 1 {
- this.keepSake[keepSakeId] = &serverproto.KeepSake{
- KeepSakeId: keepSakeId,
- KeepSakeLevel: 1,
- }
- } else {
- keepSake.KeepSakeLevel++
- }
- //通知客户端
- this.KeepSakeChangeNtf(keepSakeId, changeList)
- // maxLevel := model.GetMaxKeepSakeLevel(keepSakeId)
- //当前升级到最高等级
- if keepSakeCfg.MaxLevel <= this.keepSake[keepSakeId].KeepSakeLevel {
- this.SendKeepSakeDataToRank(keepSakeId)
- this.ChangeMaterialToPiece(keepSakeId, keepSakeCfg)
- }
- //计算战力
- //this.role.GetRoleFightPower().OnKeepSakeChange(keepSakeId, this.keepSake[keepSakeId].KeepSakeLevel)
- this.SetDirty(true)
- this.onKeepSakeLevelUp(keepSakeCfg)
- TaskMagCheck(this.role, serverproto.TaskType_Eve_Keepsake_lvl_All, keepSakeCfg.MaxLevel)
- return serverproto.ErrorCode_ERROR_OK
- }
- func (this *RoleKeepSake) onKeepSakeLevelUp(sakeCfgData *model.KeepSakeData) {
- this.role.roleBattleAttr.AttrChange(AttrChangeST{
- ChangeType: Attr_Change_Sake,
- ChangeHeroData: this.role.GetRoleHero().GetMainHero(),
- ChangeJobType: sakeCfgData.Job,
- })
- }
- func (this *RoleKeepSake) AddKeepSakeMaterial(itemCfg *serverproto.ItemCfg, materialCont int32) {
- if itemCfg == nil {
- return
- }
- materialId := itemCfg.Id
- keepSakeId, ok := model.ConvertMaterialToKeepSake[materialId]
- if !ok {
- //日志记录
- return
- }
- data, ok2 := this.keepSake[keepSakeId]
- if ok2 {
- //判定是否是最高等级//是,则转换成碎片
- maxLevel := model.GetMaxKeepSakeLevel(keepSakeId)
- if maxLevel <= data.KeepSakeLevel {
- this.KeepSakeMaterialToPiece(itemCfg, materialCont)
- this.SetDirty(true)
- return
- }
- }
- //否,则加入到材料列表中
- _, ok3 := this.material[materialId]
- if ok3 {
- this.material[materialId].Value += materialCont
- } else {
- this.material[materialId] = &serverproto.KeyValueType{
- Key: materialId,
- Value: materialCont,
- }
- }
- this.KeepSakeChangeNtf(0, []int32{materialId})
- this.changed[materialId] = 1
- this.SetDirty(true)
- }
- func (this *RoleKeepSake) ChangeMaterialToPiece(keepSakeId int32, keepSakeCfg *model.KeepSakeData) {
- if keepSakeCfg == nil {
- return
- }
- var totalPieces int32 = 0
- bChanged := false
- for key, _ := range keepSakeCfg.AllMaterials { //遍历每个等级需要的材料
- material, ok1 := this.material[key]
- if !ok1 {
- continue
- }
- itemCfg, ok2 := serverproto.ItemCfgLoader[key]
- if !ok2 {
- continue
- }
- ratio, ok3 := model.GlobalKeepSakeMaterialToCrystal[itemCfg.Quality]
- if !ok3 {
- return
- }
- totalPieces += ratio * material.Value
- delete(this.material, key)
- bChanged = true
- }
- if totalPieces > 0 {
- addItemList := make(map[int32]int32)
- addItemList[KeepSakeTicket] = totalPieces
- this.role.AddItemList(addItemList, AddFrom_KeepSake, true)
- }
- if bChanged {
- this.SetDirty(true)
- }
- }
- //信物材料转换成碎片
- func (this *RoleKeepSake) KeepSakeMaterialToPiece(itemCfg *serverproto.ItemCfg, materialCont int32) {
- if itemCfg == nil {
- return
- }
- ratio, ok := model.GlobalKeepSakeMaterialToCrystal[itemCfg.Quality]
- if !ok {
- return
- }
- addItemList := make(map[int32]int32)
- addItemList[KeepSakeTicket] = materialCont * ratio
- this.role.AddItemList(addItemList, AddFrom_KeepSake, true)
- }
- //请求信物数据
- func (this *RoleKeepSake) GetKeepSakeData(data *serverproto.RoleKeepSake) {
- if data == nil {
- return
- }
- for _, keepSake := range this.keepSake {
- data.KeepSake = append(data.KeepSake, keepSake)
- }
- for _, material := range this.material {
- data.Material = append(data.Material, material)
- }
- }
- func (this *RoleKeepSake) SendKeepSakeDataToRank(keepSakeId int32) {
- ntfMsg := &serverproto.SSUpdateKeepSakeRankNtf{}
- ntfMsg.Uid = this.role.GetUUid()
- ntfMsg.KeepSakeId = keepSakeId
- this.role.SendRank(ntfMsg)
- }
- func (this *RoleKeepSake) OnKeepSakeChangeNtf() {
- if len(this.changed) >= 0 {
- var changeList []int32
- for key, _ := range this.changed {
- changeList = append(changeList, key)
- }
- this.KeepSakeChangeNtf(0, changeList)
- this.changed = make(map[int32]int32)
- }
- }
|