package com.game.data;

import java.util.Map.Entry;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.util.Utils;

/**
 * 回放数据记录
 * 
 */
public abstract class BasePlayBack {
	/**
	 * 房间信息
	 */
	protected ITObject	info;
	/**
	 * 指令列表
	 */
	protected ITArray	cmdList;

	protected BasePlayBack(Room room) {
		info = TObject.newInstance();
		cmdList = TArray.newInstance();
		info.putUtfString("roomid", room.roomid);
		info.putInt("round", room.round);
		info.putTObject("config", room.config);
		info.putInt("banker_seat", room.bankerSeat);
		info.putInt("active_seat", room.activeSeat);
		info.putInt("game_id", Global.gameId);

		ITArray infoList = new TArray();
		for (Entry<Integer, Player> entry : room.playerMapByPlaying.entrySet()) {
			Player player = entry.getValue();
			if(player.already_round==0)continue;
			ITObject obj = getPlayerInfo(player);
			infoList.addTObject(obj);
		}
		info.putTArray("playerData", infoList);
	}

	protected ITObject getPlayerInfo(Player player) {
		ITObject obj = TObject.newInstance();
		obj.putInt("aid", player.playerid);
		obj.putUtfString("nick", player.nick);
		obj.putInt("sex", player.sex);
		obj.putUtfString("portrait", player.portrait);
		obj.putInt("seat", player.seat);
		player.hp_info(obj);
		return obj;
	}

	protected void addCommand(String cmd, int seat, ITObject data) {
		ITObject cmdObj = TObject.newInstance();
		cmdObj.putUtfString("cmd", cmd);
		cmdObj.putInt("seat", seat);
		cmdObj.putTObject("data", data);
		cmdList.addTObject(cmdObj);
	}

	/**
	 * 获取战绩回放数据,保存到redisd
	 * 
	 * @return
	 */
	public ITObject getData() {
		ITObject data = TObject.newInstance();
		data.putTObject("info", info);
		data.putTArray("cmdList", cmdList);
		return data;
	}
	/**
	 * 给客户端的提示按钮
	 * @param seat
	 * @param cmdData
	 */
	public void addTipCommand(int seat, ITObject cmdData) {
		addCommand("Tip", seat, cmdData);
	}

	/**
	 * 玩家操作点击的提示
	 * @param seat
	 * @param type 
	 * @param weight 
	 */
	public void addClickTipCommand(int seat, int type, int weight) {
		ITObject cmdData = TObject.newInstance();
		cmdData.putInt("weight", weight);
		cmdData.putInt("type", type);
		addCommand("ClickTip", seat, cmdData);
	}
	
	/**
	 * 小结算数据
	 */
	public void addResult(ITObject resultData) {
		ITObject data = TObject.newInstance();
		Utils.objectCopyDeep(resultData, data);
		data.putInt("type", 0);
		addCommand("Result", 0, data);
	}
	
}
