package extend.pk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import com.game.data.Player;
import com.game.data.Room;
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 extend.pk.uitl.CardConfig;
import extend.pk.uitl.CardUtil;

public class EXRoom extends Room {
	public RoomCard card;

	/**上次打牌的座位*/
	public int lastDiscardSeat; 

	/**打出牌的集合 */
	public CardGroup discard;

	public EXPlayer win;

	public int firstCard = 0;
	
	public Map<Integer,CardConfig> card_config_map;
	
	public int piaoCount = 0;

	public List<List<CardObj>> supCards;
	public List<CardObj> targetCards;
	public List<EXPlayer> winPlayerList;
	public int adminSeat = 0;

	public int chooseover = 0;

	public int cur510kFen = 0;
	public int cur5Num = 0;
	public int cur10Num = 0;
	public int curkNum = 0;

	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);
		
		card = new RoomCard(this, 30,maxPlayers);
		card_config_map = new HashMap<Integer,CardConfig>();
		card_config_map.put(CardConfig.DAN.type, CardConfig.DAN);
		card_config_map.put(CardConfig.DUIZI.type, CardConfig.DUIZI);
		card_config_map.put(CardConfig.LIANDUI.type, CardConfig.LIANDUI);
		card_config_map.put(CardConfig.SHUNZI.type, CardConfig.SHUNZI);
		card_config_map.put(CardConfig.ZHA.type, CardConfig.ZHA);
		card_config_map.put(CardConfig.FEIJI.type, CardConfig.FEIJI);
		card_config_map.put(CardConfig.SAN_2.type, CardConfig.SAN_2);

		this.config.putInt(Config.ROOM_CONFIG_THREE_A,0);
		this.config.putBoolean(Config.ROOM_CONFIG_SIDAI_2, false);
		this.config.putBoolean(Config.ROOM_CONFIG_SIDAI_3, false);
		this.config.putInt(Config.ROOM_CONFIG_DEMOLITION, 1);
		this.config.putInt(Config.ROOM_CONFIG_THREE_LACK, 0);
		this.config.putInt(Config.ROOM_CONFIG_PLANE_LACK, 0);
		this.config.putInt(Config.ROOM_CONFIG_MINBOOM, 0);
		this.config.putInt(Config.ROOM_CONFIG_SANDAIDAN, 0);
		this.config.putInt(Config.ROOM_CONFIG_HEARTTEN, 0);
		this.config.putInt(Config.ROOM_CONFIG_WILL_BE_OUT, 1);
		this.config.putInt(Config.ROOM_CONFIG_SIDAI, 0);


		boolean sidai_2 = this.config.getBoolean(Config.ROOM_CONFIG_SIDAI_2);
		boolean sidai_3 = this.config.getBoolean(Config.ROOM_CONFIG_SIDAI_3);
		if(sidai_2) {
			card_config_map.put(CardConfig.SI_2.type, CardConfig.SI_2);
		}
		if(sidai_3) {
			card_config_map.put(CardConfig.SI_3.type, CardConfig.SI_3);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_XIPAI_SCORE, 0);
			this.xi_pai_score = 1;
		}
		else {
			this.xi_pai_score = this.config.getInt(Config.ROOM_CONFIG_XIPAI_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIPAI, false);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_510K_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_510K_SCORE, 0);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_510K_SCORE_TYPE))
		{
			this.config.putInt(Config.ROOM_CONFIG_510K_SCORE_TYPE, 0);
		}

		this.isEntrust = true;

		this.supCards = new ArrayList<>();
		this.targetCards = new ArrayList<>();
		this.winPlayerList = new ArrayList<>();
		this.adminSeat = 0;
		this.chooseover = 0;
		this.cur510kFen = 0;
	}

	public void clear() {
		super.clear();
		
	}
	
	public void clearEx() {
		this.activeSeat = 0;
		this.discard = null;
		this.lastDiscardSeat = 0;
		this.win = null;
		this.firstCard = 0;
		this.cur510kFen = 0;
		this.cur5Num = 0;
		this.cur10Num = 0;
		this.curkNum = 0;
		this.winPlayerList.clear();
	}

	public ITObject getReloadInfo(Player player) {
		ITObject data = super.getReloadInfo(player);
		EXPlayer p = (EXPlayer) player;
		
		data.putTArray("hand_card", CardUtil.toTArray(p.cardInhand));

		data.putTArray("handCards", CardUtil.toTArray(p.cardInhand));
		data.putTArray("outCards", CardUtil.toTArray(p.outCards));
		data.putInt("cur510kFen", this.cur510kFen);
		data.putInt("cur5Num", this.cur5Num);
		data.putInt("cur10Num", this.cur10Num);
		data.putInt("curkNum", this.curkNum);

		data.putInt("open", p.open);
		
		data.putInt("last_outcard_seat", this.lastDiscardSeat);
		
		// 游戏结束
		if(this.isplaying == false) {
			
			if(this.win != null) {
				data.putInt("winseat", this.win.seat);
			}
			else
			{
				data.putInt("winseat", 0);
			}
			if(this.card.getCount() > 0) {
				data.putTArray("remaincards", CardUtil.toTArray(this.card.jiangliCardList));
			}
		}
		
		return data;
	}

	/**
	 * 决定总结算方式
	 *
	 * @return true : 总结算
	 */
	protected boolean totalType() {
		for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.cur510kFen >= RoomCard.MAX_510K_SCORE)
			{
				return true;
			}
		}
		return false;
		//return this.round >= this.maxRound;
	}

	public boolean entrustTotal() {
		return false;
	}

	/**
	 *
	 * */
	private int reMultipleScore(int score) {
		if (this.hpData != null) {
			if (this.hpData.containsKey("times")) {
				return Math.round(score / this.hpData.getInt("times"));
			}
		}
		return score;
	}

	public void addScore(Player destPlayer, Player fromPlayer, int score, int type) {

		////
		if(multipleScore(score) > fromPlayer.hp.cur_hp) {
			score = reMultipleScore((int)fromPlayer.hp.cur_hp);
		//	fromPlayer.hp.cur_hp -= multipleScore(score); //不能在这里扣
		}
		//

		destPlayer.score.round_log.put(type, destPlayer.score.round_log.get(type) + score);
		fromPlayer.score.round_log.put(type, fromPlayer.score.round_log.get(type) - score);
		destPlayer.score.round_score += score;
		destPlayer.score.total_score += score;
		fromPlayer.score.round_score -= score;
		fromPlayer.score.total_score -= score;
	}
	
	/**
	 * 变更炸弹积分
	 * @param destPlayer
	 */
	public void addAllBombScore(EXPlayer destPlayer, CardGroup card) {
		if (card.card_list.size() == 7)
		{
			addXiScore(destPlayer, 100);
		}
		else if (card.card_list.size() == 8)
		{
			addXiScore(destPlayer, 200);
		}
		else if (card.card_list.size() == 9)
		{
			addXiScore(destPlayer, 400);
		}
		else if (card.card_list.size() == 10)
		{
			addXiScore(destPlayer, 800);
		}
		else if (card.card_list.size() == 11)
		{
			addXiScore(destPlayer, 1600);
		}
		else if (card.card_list.size() == 12)
		{
			addXiScore(destPlayer, 3200);
		}
	}


	/**
	 * 变更炸弹积分
	 * @param destPlayer
	 */
	public void add510kScore(EXPlayer destPlayer) {
		//destPlayer.cur510kFen += this.cur510kFen;
        if (this.cur510kFen > 0)
		{
			destPlayer.ju510kFen += this.cur510kFen;
			TArray seatData=TArray.newInstance();
			ITObject seatListData = TObject.newInstance();
			seatListData.putInt("add510kfen", this.cur510kFen);
			seatListData.putInt("ju510kfen", destPlayer.ju510kFen);
			seatListData.putInt("all510kfen", destPlayer.cur510kFen);
			seatListData.putInt("aid", destPlayer.playerid);
			seatData.addTObject(seatListData);
			TObject allData=TObject.newInstance();
			allData.putTArray("gold_list", seatData);

			this.cur510kFen = 0;
			this.cur5Num = 0;
			this.cur10Num = 0;
			this.curkNum = 0;
			allData.putInt("cur510kFen", 0);
			allData.putInt("cur5Num", 0);
			allData.putInt("cur10Num", 0);
			allData.putInt("curkNum", 0);

			this.broadCastToClient(0, Config.GAME_EVT_UPDATE_510K_SCORE, allData);

			EXPlayBack pb = (EXPlayBack)destPlayer.getRoom().playBackData;
			pb.add510KFenCommand(destPlayer.seat, destPlayer);
		}
	}

	/**
	 * 变更炸弹积分
	 * @param destPlayer
	 */
	public void addXiScore(EXPlayer destPlayer, int score) {
		destPlayer.curXiFen += score;
		destPlayer.juXiFen += score;
		TArray seatData=TArray.newInstance();
		ITObject seatListData = TObject.newInstance();
		seatListData.putInt("addXiFen", score);
		seatListData.putInt("juXiFen", destPlayer.juXiFen);
		seatListData.putInt("allXiFen", destPlayer.curXiFen);
		seatListData.putInt("aid", destPlayer.playerid);
		seatData.addTObject(seatListData);
		TObject allData=TObject.newInstance();
		allData.putTArray("gold_list", seatData);

		this.broadCastToClient(0, Config.GAME_EVT_UPDATE_XI_SCORE, allData);

		EXPlayBack pb = (EXPlayBack)destPlayer.getRoom().playBackData;
		pb.add510KFenCommand(destPlayer.seat, destPlayer);
	}
	
	/**
	 * 变更游戏积分
	 * @param destPlayer
	 */
	public void addAllScore(EXPlayer destPlayer) {
	    if (true)
		{
			TArray seatData = TArray.newInstance();
			int add510kFen = 0;
			int jiangli = 0;
			//用8替代5
			Map<Integer, List<CardObj>> cardMap =CardUtil.getCardListMap(this.card.jiangliCardList);
			if (cardMap.containsKey(RoomCard.CUR_5_CARD))
			{
				add510kFen += cardMap.get(RoomCard.CUR_5_CARD).size() * 5;
			}
			if (cardMap.containsKey(10))
			{
				add510kFen += cardMap.get(10).size() * 10;
			}
			if (cardMap.containsKey(13))
			{
				add510kFen += cardMap.get(13).size() * 10;
			}
			if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 0 || this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 1)
			{
				jiangli += 100;
			}
			else {
				jiangli += 40;
			}
			destPlayer.jiangli = jiangli;
			destPlayer.leftCardFen = add510kFen;
			destPlayer.cur510kFen += add510kFen;
			destPlayer.cur510kFen += destPlayer.jiangli;
			destPlayer.cur510kFen += destPlayer.ju510kFen;
			//destPlayer.curXiFen += destPlayer.juXiFen;
			destPlayer.allJu510kFen += destPlayer.ju510kFen;
			destPlayer.allJiangLi += destPlayer.jiangli;
			destPlayer.allLeftCardFen += destPlayer.leftCardFen;
			{
				ITObject seatListData = TObject.newInstance();
				seatListData.putInt("add510kfen", add510kFen);
				seatListData.putInt("ju510kfen", destPlayer.ju510kFen);
				seatListData.putInt("all510kfen", destPlayer.cur510kFen);
				seatListData.putInt("juXiFen", destPlayer.juXiFen);
				seatListData.putInt("curXiFen", destPlayer.curXiFen);
				seatListData.putInt("jiangli", destPlayer.jiangli);
				seatListData.putInt("aid", destPlayer.playerid);
				seatData.addTObject(seatListData);
			}

			if (this.playerMapByPlaying.size() == 2) {
				for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {
					EXPlayer fromPlayer = (EXPlayer) entry.getValue();
					if (fromPlayer != destPlayer) {
						int add510kFenFrom = 0;
						int jiangliFrom = 0;
						if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 0 || this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 1)
						{
							jiangliFrom = -100;
						}
						else {
							jiangliFrom = -40;
						}
						fromPlayer.jiangli = jiangliFrom;
						fromPlayer.cur510kFen += add510kFenFrom;
						fromPlayer.cur510kFen += fromPlayer.jiangli;
						fromPlayer.cur510kFen += fromPlayer.ju510kFen;
						//fromPlayer.curXiFen += fromPlayer.juXiFen;
						fromPlayer.allJu510kFen += fromPlayer.ju510kFen;
						fromPlayer.allJiangLi += fromPlayer.jiangli;
						fromPlayer.allLeftCardFen += fromPlayer.leftCardFen;
						ITObject seatListData = TObject.newInstance();
						seatListData.putInt("add510kfen", add510kFenFrom);
						seatListData.putInt("ju510kfen", fromPlayer.ju510kFen);
						seatListData.putInt("all510kfen", fromPlayer.cur510kFen);
						seatListData.putInt("juXiFen", fromPlayer.juXiFen);
						seatListData.putInt("curXiFen", fromPlayer.curXiFen);
						seatListData.putInt("jiangli", fromPlayer.jiangli);
						seatListData.putInt("aid", fromPlayer.playerid);
						seatData.addTObject(seatListData);
					}
				}
			} else {
				EXPlayer second = null;
				EXPlayer third = null;
				if (this.winPlayerList.size() >= 2)
				{
					second = this.winPlayerList.get(1);
					for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {

						EXPlayer fromPlayer = (EXPlayer) entry.getValue();
						if (fromPlayer.playerid != destPlayer.playerid && fromPlayer.playerid != second.playerid) {
						    third = fromPlayer;
						}
					}
				}

				if (second == null || third == null)
				{
					for (Entry<Integer, Player> entry : this.playerMapByPlaying.entrySet()) {

						EXPlayer fromPlayer = (EXPlayer) entry.getValue();
						if (fromPlayer != destPlayer) {
							if (fromPlayer.cardInhand.size() <= 0)
							{
							    second = fromPlayer;
							}
							else {
								third = fromPlayer;
							}
						}
					}
				}


				if (second != null)
				{
					int add510kFenFrom = 0;
					int jiangliFrom = 0;
					if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 0)
					{
						jiangliFrom = -40;
					}
					else if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 1)
					{
						jiangliFrom = -30;
					}
					else {
						jiangliFrom = 0;
					}
					second.jiangli = jiangliFrom;
					second.cur510kFen += add510kFenFrom;
					second.cur510kFen += second.jiangli;
					second.cur510kFen += second.ju510kFen;
					//second.curXiFen += second.juXiFen;
					second.allJu510kFen += second.ju510kFen;
					second.allJiangLi += second.jiangli;
					second.allLeftCardFen += second.leftCardFen;
					ITObject seatListData = TObject.newInstance();
					seatListData.putInt("add510kfen", add510kFenFrom);
					seatListData.putInt("ju510kfen", second.ju510kFen);
					seatListData.putInt("all510kfen", second.cur510kFen);
					seatListData.putInt("juXiFen", second.juXiFen);
					seatListData.putInt("curXiFen", second.curXiFen);
					seatListData.putInt("aid", second.playerid);
					seatListData.putInt("jiangli", second.jiangli);
					seatData.addTObject(seatListData);
				}

				if (third != null) {
					int add510kFenFrom = 0;
					int jiangliFrom = 0;
					if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 0)
					{
						jiangliFrom = -60;
					}
					else if (this.config.getInt(Config.ROOM_CONFIG_510K_SCORE_TYPE) == 1)
					{
						jiangliFrom = -70;
					}
					else {
						jiangliFrom = -40;
					}
					third.jiangli = jiangliFrom;
					third.cur510kFen += add510kFenFrom;
					third.cur510kFen += third.jiangli;
					third.cur510kFen += third.ju510kFen;
					//third.curXiFen += third.juXiFen;
					third.allJu510kFen += third.ju510kFen;
					third.allJiangLi += third.jiangli;
					third.allLeftCardFen += third.leftCardFen;
					ITObject seatListData = TObject.newInstance();
					seatListData.putInt("add510kfen", add510kFenFrom);
					seatListData.putInt("ju510kfen", third.ju510kFen);
					seatListData.putInt("all510kfen", third.cur510kFen);
					seatListData.putInt("juXiFen", third.juXiFen);
					seatListData.putInt("curXiFen", third.curXiFen);
					seatListData.putInt("aid", third.playerid);
					seatListData.putInt("jiangli", third.jiangli);
					seatData.addTObject(seatListData);
				}
			}
			TObject allData = TObject.newInstance();
			allData.putTArray("gold_list", seatData);
			allData.putInt("cur510kFen", 0);
			this.broadCastToClient(0, Config.GAME_EVT_ALL_UPDATE_510K_SCORE, allData);

			if (totalType())
			{
				boolean has = false;
				for(int i = 0; i < this.winPlayerList.size(); i++)
				{
					EXPlayer xxPlayer = this.winPlayerList.get(i);
					if (xxPlayer.cur510kFen >= RoomCard.MAX_510K_SCORE)
					{
						xxPlayer.jieSuanJiangLi += 100;
						xxPlayer.cur510kFen += xxPlayer.jieSuanJiangLi;
						has = true;
						break;
					}
				}

				if (has == false)
				{
					for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
						EXPlayer player = (EXPlayer) entry.getValue();
						if (player.cur510kFen >= RoomCard.MAX_510K_SCORE)
						{
							player.jieSuanJiangLi += 100;
							player.cur510kFen += player.jieSuanJiangLi;
							break;
						}
					}
				}
			}
		}

	    if (totalType())
		{
			EXPlayer winPlayer = this.win;
			int max = RoomCard.MAX_510K_SCORE;
			for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
				EXPlayer fromPlayer = (EXPlayer) entry.getValue();
				if (fromPlayer.cur510kFen+fromPlayer.curXiFen >= max)
				{
					max = fromPlayer.cur510kFen+fromPlayer.curXiFen;
					winPlayer = fromPlayer;
				}
			}

			if (winPlayer != null)
			{
				for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
					EXPlayer fromPlayer = (EXPlayer) entry.getValue();
					if (fromPlayer.playerid != winPlayer.playerid)
					{
						this.addScore(winPlayer, fromPlayer, winPlayer.cur510kFen+winPlayer.curXiFen-fromPlayer.cur510kFen-fromPlayer.curXiFen, EXScore.WIN);
					}
				}
			}
		}
	}
	
	@Override
	protected void roomResult() {
		EXMainServer.gameCtr.roomResult(this);
	}
	

	@Override
	public void saveMilitaryTotal(boolean dissmiss) {
		super.saveMilitaryTotal(dissmiss);
		EXMainServer.gameCtr.roomTotalResult(this, dissmiss);
	}
	
	@Override
	public void endGame() {
		addAllScore(win);

		this.chooseover = 0;

		EXPlayBack pb = (EXPlayBack)this.playBackData;
		
		
		ITObject pbResult = TObject.newInstance();
		
		ITArray info = new TArray();
		for (Entry<Integer, Player> entry : playerMapByPlaying.entrySet()) {
			EXPlayer player=(EXPlayer)entry.getValue();
			ITObject obj = TObject.newInstance();
			obj.putInt("seat", player.seat);
			obj.putInt("score", player.score.round_score);
			obj.putBoolean("entrust", player.entrust);
			obj.putInt("cur510kFen", player.cur510kFen);
			obj.putInt("ju510kFen", player.ju510kFen);
			obj.putInt("curXiFen", player.curXiFen);
			obj.putInt("juXiFen", player.juXiFen);
			obj.putInt("jiangli", player.jiangli);
			obj.putInt("leftCardFen", player.leftCardFen);
			obj.putInt("jieSuanJiangLi", player.jieSuanJiangLi);
			obj.putInt("boomnum", player.boomnum);
			info.addTObject(obj);
		}
		pbResult.putTArray("result", info);
		
		pb.addResultCommand(win.seat, pbResult);
		
		pb.addNewRoundCommand();
		super.endGame();
	}
}
