Example #1
0
func signProto(day int) []*protodata.RewardData {

	var result []*protodata.RewardData
	Lua, _ := lua.NewLua("conf/sign_reward.lua")

	begin := day/7 - 1
	if begin < 0 {
		begin = 0
	}
	begin = begin*7 + 1
	for i := begin; i <= begin+7; i++ {

		//Lua.L.GetGlobal("signReward")
		Lua.L.DoString(fmt.Sprintf("coin, diamond, action, generalId = signReward(%d)", i))
		coin := Lua.GetInt("coin")
		diamond := Lua.GetInt("diamond")
		action := Lua.GetInt("action")
		generalId := Lua.GetInt("generalId")

		temp := new(protodata.RewardData)
		temp.RewardCoin = proto.Int32(int32(coin))
		temp.RewardDiamond = proto.Int32(int32(diamond))
		temp.Stamina = proto.Int32(int32(action))
		if generalId > 0 {
			config := models.BaseGeneral(generalId, nil)
			temp.General = generalProto(new(models.GeneralData), config)
		}

		result = append(result, temp)
	}

	Lua.Close()
	return result
}
Example #2
0
func (this *Connect) BuyGeneral() error {

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

	baseId := int(request.GetGeneralId())
	baseGeneral := models.BaseGeneral(baseId, nil)
	needDiamond := baseGeneral.BuyDiamond

	if models.General.General(this.Uid, baseId) != nil {
		return this.Send(lineNum(), fmt.Errorf("已有这个英雄: %d", baseId))
	}

	if needDiamond > this.Role.Diamond {
		return this.Send(lineNum(), fmt.Errorf("钻石不足"))
	}

	if err := this.Role.SubDiamond(needDiamond, models.FINANCE_BUY_GENERAL, fmt.Sprintf("genId : %d", baseId)); err != nil {
		return this.Send(lineNum(), err)
	}

	general, err := models.General.Insert(this.Uid, baseGeneral)
	if err != nil {
		return this.Send(lineNum(), err)
	}

	response := &protodata.BuyGeneralResponse{
		Role:    roleProto(this.Role),
		General: generalProto(general, baseGeneral),
	}
	return this.Send(StatusOK, response)
}
Example #3
0
func (this *Connect) GeneralLevelUp() error {

	request := new(protodata.GeneralLevelUpRequest)
	if err := Unmarshal(this.Request.GetSerializedString(), request); err != nil {
		return this.Send(lineNum(), err)
	}
	baseId := int(request.GetGeneralId())

	general := models.General.General(this.Uid, baseId)
	if general == nil {
		return this.Send(lineNum(), fmt.Errorf("英雄数据出错"))
	}

	baseGeneral := models.BaseGeneral(general.BaseId, nil)

	if general.Level >= len(baseGeneral.LevelUpCoin)-1 {
		return this.Send(lineNum(), fmt.Errorf("英雄已是最高等级"))
	}

	coin := baseGeneral.LevelUpCoin[general.Level]
	if coin > this.Role.Coin {
		return this.Send(lineNum(), fmt.Errorf("金币不足"))
	}

	if err := this.Role.SubCoin(coin, models.FINANCE_GENERAL_LEVELUP, baseGeneral.Name); err != nil {
		return this.Send(lineNum(), err)
	}

	if err := general.LevelUp(baseGeneral); err != nil {
		return this.Send(lineNum(), err)
	}

	response := &protodata.GeneralLevelUpResponse{
		Role:    roleProto(this.Role),
		General: generalProto(general, baseGeneral),
	}

	return this.Send(StatusOK, response)
}
Example #4
0
func (this *Connect) Sign() error {

	response := new(protodata.SignRewardResponse)

	if this.Role.SignDate == time.Now().Format("20060102") {

		signDay := this.Role.SignNum % 7
		if signDay == 0 {
			signDay = 7
		}

		response.SignReward = &protodata.SignRewardData{
			SignDay: proto.Int32(int32(signDay)),
			Reward:  signProto(this.Role.SignNum)}
		response.Role = roleProto(this.Role)
		response.SignReward.IsReceive = proto.Bool(true)
		return this.Send(StatusOK, response)
	}

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

	signDay := this.Role.SignNum % 7
	if signDay == 0 {
		signDay = 7
	}

	response.SignReward = &protodata.SignRewardData{
		SignDay: proto.Int32(int32(signDay)),
		Reward:  signProto(this.Role.SignNum)}

	coin, diamond, action, generalId := signReward(this.Role.SignNum)
	if coin > 0 {
		this.Role.AddCoin(coin, models.FINANCE_SIGN_GET, fmt.Sprintf("signDay : %d", signDay))
	} else if diamond > 0 {
		this.Role.AddDiamond(diamond, models.FINANCE_SIGN_GET, fmt.Sprintf("signDay : %d", signDay))
	} else if action > 0 {
		this.Role.SetActionValue(this.Role.ActionValue() + action)
	} else if generalId > 0 {
		var find bool
		for _, val := range models.General.List(this.Uid) {
			if generalId == val.BaseId {
				find = true
				break
			}
		}
		baseGeneral := models.BaseGeneral(generalId, nil)
		if find {
			this.Role.AddDiamond(baseGeneral.BuyDiamond, models.FINANCE_SIGN_GET, fmt.Sprintf("signDay : %d", signDay))
		} else {
			general, err := models.General.Insert(this.Uid, baseGeneral)
			if err != nil {
				return this.Send(lineNum(), response)
			}
			response.General = generalProto(general, baseGeneral)
		}
	}

	response.Role = roleProto(this.Role)
	return this.Send(StatusOK, response)
}
Example #5
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)
}