package player

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

func (p *actorPlayer) lottery(req *param.DrawReq) (*param.DrawResp, int32) {
	if !p.isOnline {
		return nil, code.PlayerNotLogin
	}
	var num = 1
	if req.Tp == 2 {
		num = 10
	}
	var resp param.DrawResp
	if p.Player.Item[models.ItemDrawsNumber].Num < num {
		return nil, code.NotEnoughItemsError
	}
	var addItem models.AddItem
	var SubItem models.SubItem
	for i := 0; i < num; i++ {
		draw := p.Player.Draw()
		addItem = addItem.Add(draw.Reward)
		resp.Draws = append(resp.Draws, &param.DrawConfig{ID: draw.ID, ItemID: draw.Reward[0].ItemID, Num: draw.Reward[0].Amount})
	}
	resp.Multi = num
	resp.AddItem = addItem
	si := SubItem.AddById(models.ItemDrawsNumber, num)
	resp.SubItem = si
	p.dirty = true

	p.itemStat(resp.AddItem)

	return &resp, code.OK
}
