Пример #1
0
func duplicateProtoList(duplicates []*models.DuplicateData, configs []*models.ConfigDuplicate) []*protodata.ChapterData {

	//list := models.ConfigDuplicateList()

	var result []*protodata.ChapterData
	result = append(result, &protodata.ChapterData{
		ChapterId:   proto.Int32(int32(configs[0].Chapter)),
		ChapterName: proto.String(configs[0].ChapterName),
		ChapterDesc: proto.String(""),
		IsUnlock:    proto.Bool(true),
	})

	for index, section := range configs {

		var sectionProto protodata.SectionData
		sectionProto.SectionId = proto.Int32(int32(section.Section))
		sectionProto.SectionName = proto.String(section.SectionName)
		sectionProto.SectionDesc = proto.String("")
		sectionProto.IsUnlock = proto.Bool(true)

		var find bool
		if index > 0 {
			for _, d := range duplicates {
				if d.Chapter == configs[index-1].Chapter && d.Section == configs[index-1].Section {
					find = true
					break
				} else {
					find = false
				}
			}
			if !find {
				sectionProto.IsUnlock = proto.Bool(false)
			}
		}

		if section.Chapter != int(*result[len(result)-1].ChapterId) {

			result = append(result, &protodata.ChapterData{
				ChapterId:   proto.Int32(int32(section.Chapter)),
				ChapterName: proto.String(section.ChapterName),
				ChapterDesc: proto.String(""),
				IsUnlock:    proto.Bool(find),
			})
		}
		result[len(result)-1].Sections = append(result[len(result)-1].Sections, &sectionProto)
	}

	return result
}
Пример #2
0
func (this *Connect) BattleResult() error {

	request := &protodata.FightEndRequest{}
	if err := Unmarshal(this.Request.GetSerializedString(), request); err != nil {
		return this.Send(lineNum(), err)
	}

	coin := int(request.GetCoinNum())
	diamond := int(request.GetDiamondNum())
	killNum := int(request.GetKillNum())

	BattleLogModel := models.LastBattleLog(this.Uid)

	if BattleLogModel == nil || BattleLogModel.Result != 0 {
		return this.Send(lineNum(), fmt.Errorf("战斗数据非法:没有战斗初始化"))
	}

	if err := BattleLogModel.SetResult(request.GetIsWin(), killNum); err != nil {
		return this.Send(lineNum(), err)
	}

	response := new(protodata.FightEndResponse)

	if killNum > 0 {
		general := models.General.General(this.Uid, this.Role.GeneralBaseId)
		general.AddKillNum(killNum)
		response.General = generalProto(general, models.BaseGeneral(general.BaseId, nil))
	}

	var generalData *protodata.GeneralData
	if BattleLogModel.Type == 0 && request.GetIsWin() {

		var find bool
		DuplicateModel := models.NewDuplicateModel(this.Uid)
		//duplicateNum := len(DuplicateModel.List())
		for _, val := range DuplicateModel.List() {
			if val.Chapter == BattleLogModel.Chapter && val.Section == BattleLogModel.Section {
				find = true
				break
			}
		}

		if !find {
			if nil == DuplicateModel.Insert(BattleLogModel.Chapter, BattleLogModel.Section) {
				return this.Send(lineNum(), fmt.Errorf("数据库错误:新增数据失败"))
			}
		}

		//var length int = 1
		//if len(DuplicateModel.List()) != duplicateNum {
		//	length = 2
		//}
		//duplicateNum = len(DuplicateModel.List())
		//begin := duplicateNum - length
		//if begin < 0 {
		//	begin = 0
		//}

		list := models.ConfigDuplicateList()

		last := DuplicateModel.List()[len(DuplicateModel.List())-1]
		var base *models.ConfigDuplicate
		find = false
		for _, duplicate := range list {
			if find == true {
				base = duplicate
				break
			}
			if duplicate.Chapter == last.Chapter && duplicate.Section == last.Section {
				base = duplicate
				find = true
			}
		}

		section := new(protodata.SectionData)
		section.SectionId = proto.Int32(int32(base.Section))
		section.SectionName = proto.String(base.SectionName)
		section.IsUnlock = proto.Bool(true)
		chapter := new(protodata.ChapterData)
		chapter.ChapterId = proto.Int32(int32(base.Chapter))
		chapter.ChapterName = proto.String(base.ChapterName)
		chapter.IsUnlock = proto.Bool(true)
		chapter.Sections = append(chapter.Sections, section)
		response.Chapter = append(response.Chapter, chapter)

		// 奖励英雄
		baseId := 0
		for _, val := range list {
			if val.Chapter == BattleLogModel.Chapter && val.Section == BattleLogModel.Section {
				if val.GenId > 0 {
					baseId = val.GenId
				}
				break
			}
		}

		if baseId > 0 {
			if general := models.General.General(this.Uid, baseId); general == nil {
				baseGeneral := models.BaseGeneral(baseId, nil)
				if general, err := models.General.Insert(this.Uid, baseGeneral); err != nil {
					return this.Send(lineNum(), err)
				} else {
					generalData = generalProto(general, baseGeneral)
				}
			}
		}
	} else {
		if request.GetIsWin() {
			this.Role.UnlimitedNum += 1
			if this.Role.UnlimitedNum > this.Role.UnlimitedMaxNum {
				this.Role.UnlimitedMaxNum = this.Role.UnlimitedNum
			}
		} else if this.Role.UnlimitedNum != 0 {
			this.Role.UnlimitedNum = 0
		}
	}

	response.Reward = rewardProto(coin, diamond, 0, generalData)

	oldCoin, oldDiamond := this.Role.Coin, this.Role.Diamond

	if killNum > 0 {
		this.Role.KillNum += killNum
	}
	if coin > 0 {
		this.Role.Coin += coin
	}
	if diamond > 0 {
		this.Role.Diamond += diamond
	}

	if err := this.Role.Set(); err != nil {
		this.Role = nil
		return this.Send(lineNum(), err)
	}

	if coin > 0 {
		models.InsertAddCoinFinanceLog(this.Uid, models.FINANCE_DUPLICATE_GET, oldCoin, this.Role.Coin, "")
	}
	if diamond > 0 {
		models.InsertAddDiamondFinanceLog(this.Uid, models.FINANCE_DUPLICATE_GET, oldDiamond, this.Role.Diamond, "")
	}

	response.Role = roleProto(this.Role)
	return this.Send(StatusOK, response)
}