package com.mgr.group.data;

import java.util.Map;

import com.data.bean.BaseBean;
import com.data.cache.GroupMemberCache;
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.plugin.redis.Redis;
import com.taurus.core.util.StringUtil;
import com.taurus.core.util.Utils;

/**
 * 基本房间对象
 * 
 *  
 */
public class Room extends BaseBean {
	/**
	 * 房间ID
	 */
	public String			roomid			= "";
	/**
	 * 玩家列表ID映射
	 */
	public final ITArray	players			= TArray.newInstance();
	/**
	 * 房间最大人数
	 */
	public int				maxPlayers;

	public int              limitInRoom;

	/**
	 * 圈子玩法ID
	 */
	public int				groupPlayId;
	/**
	 * 局數
	 */
	public int				round			= 0;
	/**
	 * 最大回合数
	 */
	public int				maxRound		= 0;
	/**
	 * 0 未开始    1开始    2标记需要删除  3删除
	 */
	public int				status			= 0;
	/**
	 * 0 关闭  1打开
	 */
	public int				open			= 1;

	/**
	 * 房间匹配权重
	 * pid    open    status     剩余空位
	 * 11      1       1          00
	 */
	public int				priorityValue	= 1;

	/*
	假房间
	 */
	public boolean         fake = false;
	/**
	 * 最后操作时间
	 */
	public long				lastTime		= 0;

	public ITObject		data			= TObject.newInstance();

	private void setInfo() {
		data.putUtfString("id", roomid);
		data.putInt("round", round);
		data.putInt("times", maxRound);
		data.putInt("status", status);
		data.putInt("maxPlayers", maxPlayers);
		data.putInt("pid", groupPlayId);
		ITArray arr = TArray.newInstance();
		Utils.arrayCopy(players, arr);
		data.putTArray("plist", arr);
		data.putInt("limitInRoom", limitInRoom);

	}

	public void fillData(Map<String, String> redis_map) {
		String _id = redis_map.get("id");
		String _status = redis_map.get("status");
		if (StringUtil.isEmpty(_id)) {
			this.del = true;
			return;
		}
		if(StringUtil.isNotEmpty(_status)) {
			this.status = Integer.parseInt(_status);
		}
		
		if (this.status == 2 || this.status == 3) {
			this.del = true;
			return;
		}
		this.roomid = _id;
		this.id = Integer.parseInt(_id);
		this.groupPlayId = Integer.parseInt(redis_map.get("gpid"));
		this.round = Integer.parseInt(redis_map.get("round"));
		this.maxRound = Integer.parseInt(redis_map.get("times"));
		this.maxPlayers = Integer.parseInt(redis_map.get("maxPlayers"));
		this.limitInRoom = Integer.parseInt(redis_map.get("limitInRoom"));
		this.open = Integer.parseInt(redis_map.get("open"));
		if (StringUtil.isEmpty(redis_map.get("fake")))
		{
			this.fake = false;
		}
		else {
			this.fake = true;
		}
		
		this.loadRedisPlayer(redis_map.get("players"), redis_map.get("seats"),redis_map);
		setInfo();
		updatePriority();
	}

	/**
	 * 重新加载玩家
	 */
	private void loadRedisPlayer(String players_json, String seats_json,Map<String, String> redis_map) {
		players.clear();
		if (StringUtil.isEmpty(players_json))
			return;
		ITArray players = TArray.newFromJsonData(players_json);
		
		ITArray seats = TArray.newFromJsonData(seats_json);
		for (int i = 0; i < players.size(); i++) {
			int player_id = players.getInt(i);
			Player player = PlayerCache.getPlayer(player_id);
			player.seat = seats.getInt(i);
			Object tem = redis_map.get("net_"+player_id);
			player.off_time =tem ==null?0:(int)(Long.parseLong((String)tem)/1000);

			String strGroup = redis_map.get("group");
			int groupId = strGroup ==null?0:(Integer.parseInt((String)strGroup));
			String gm_key1 = GroupMemberCache.genKey(groupId, player.id);
			if (this.fake)
			{
				String strHp = Redis.use("group1_db10").hget("fake_"+player.id, "fake_hp");
				player.hp = strHp ==null?0:(Long.parseLong((String)strHp));
			}
			else {
				String strHp = Redis.use("group1_db10").hget(gm_key1, "hp");
				player.hp = strHp ==null?0:(Long.parseLong((String)strHp));
			}

			String strQuid = Redis.use("group1_db10").hget(gm_key1, "queueid");
			player.queueid = strQuid ==null?0:(Integer.parseInt((String)strQuid));

			String strseeid = Redis.use("group1_db10").hget(gm_key1, "seeid");
			player.seeid = strseeid ==null?0:(Integer.parseInt((String)strseeid));

			this.players.addTObject(player.getInfo());
		}
	}

	/**
	 * 房间匹配权重
	 */
	public void updatePriority() {
		if (this.status == 2 || this.status == 3) {
			this.priorityValue = 0;
			return;
		}
		int t_status = this.status == 0 ? 1 : 0;
		int t_mc = this.players.size() + 1;
		t_mc = this.players.size() == this.maxPlayers ? 0 : t_mc;
		this.priorityValue = this.groupPlayId * 10000 + this.open * 1000 + t_status * 100 + t_mc;
	}

}
