package player

import (
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
)

func (p *actorPlayer) roll(req *param.RollReq) (*param.RollResp, int32) {
	if !p.isOnline {
		return nil, code.PlayerNotLogin
	}
	if p.Player.Item[models.ItemRoll].Num < req.Multi {
		return nil, code.PlayerItemNumError
	}
	var resp param.RollResp
	var addItem = models.AddItem{}
	var subItem = models.SubItem{}
	var scores int

	ret := p.Player.RollOnce(req.Multi)
	if ret.IsLevelUp == true {
		resp.IsLevelUp = true
	}
	if ret.IsDailyTask == true {
		resp.IsDailyTask = true
	}
	resp.Rolls = append(resp.Rolls, ret.Roll)
	addItem.AddById(models.ItemScore, ret.Roll.Score*req.Multi)
	subItem.AddById(models.ItemRoll, 1*req.Multi)
	scores += ret.Roll.Score * req.Multi

	// 投筛子统计
	p.rollStat()

	resp.AddItem = addItem
	resp.SubItem = subItem
	p.dirty = true
	//设置榜单
	models.SetRank(constant.RankSourceRoll, p.Player.IsRobot, p.Player.UserName, req.Multi)
	resp.Multi = req.Multi

	p.itemStat(resp.AddItem)

	return &resp, code.OK
}
