package extend.zp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;

import com.game.Global;
import com.game.Router;
import com.game.Util;
import com.game.data.Player;
import com.game.data.Room;
import com.game.player.state.PlayerWaitState;
import com.taurus.core.entity.*;
import com.taurus.core.util.Logger;

import extend.zp.player.state.EXPlayerDisCardTipState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.tip.Action;
import extend.zp.tip.TipManager;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;
import extend.zp.uitl.WinCard;


public class EXRoom extends Room {
	Logger logger = Logger.getLogger(getClass());
	//
	public Map<Integer, TipManager>	tipMap;
	public List<Action>				actionList;

	public RoomCard					card;

	public int						activeCard;
	public int						drawCard;
	public int						currenDiscardSeat;
	public int						lastDiscardSeat;
	public int						winSeat;
	public int						winCount	= 0;
	public boolean					liuju		= false;
	public int						liujuNum	= 0;
	public boolean					isAllPass	= false;
	public boolean					isTianHu	= true;
	public boolean					isDiHu		= true;

	public int 						step;

	public HashSet<Integer>			throwCardsSet;
	public ArrayList<Integer>		throwCardsList;

	public int						typeface	= 1;

	public ITObject				paiXing;
	public int                       adminSeat;

	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);
		card = new RoomCard(this);

		this.tipMap = new HashMap<Integer, TipManager>();
		this.actionList = new ArrayList<Action>();
		this.paiXing = TObject.newInstance();
		this.throwCardsSet = new HashSet<>();
		this.throwCardsList = new ArrayList<Integer>();
		this.isEntrust = true;

		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);
		}
		this.adminSeat = 0;
	}

	public void changePaiXing(EXPlayer player, ITObject params) {
		ITArray paixing = null;
		try {
			paixing = params.getTArray("card_list");
		}
		catch(Exception e)
		{

		}

		if (paixing == null) {
			paixing = TArray.newInstance();
		}

		ArrayList<Integer> tmpCardList = new ArrayList<Integer>();
		for(int i = 0; i < paixing.size(); i++)
		{
			TDataWrapper card = paixing.get(i);
			TObject map = (TObject)card.getObject();
			tmpCardList.add(map.getInt("card"));
		}

		Collections.sort(tmpCardList);
		ArrayList<Integer> tmpCardList2 = new ArrayList<Integer>();
		tmpCardList2.addAll(player.cardInhand);
		for (OpCard opCard : player.opCardList) {
			if (opCard.type == RuleWeight.TYPE_KAN && opCard.pailie) {
				tmpCardList2.add(opCard.card1);
				tmpCardList2.add(opCard.card2);
				tmpCardList2.add(opCard.card3);
			}
		}
		Collections.sort(tmpCardList2);

		if (!tmpCardList.equals(tmpCardList2))
		{
			ITObject param = new TObject();
			player.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, param);
			return;
		}

		paiXing.putTArray(player.seat + "", paixing);

		ITObject cmdData = TObject.newInstance();
		cmdData.putTArray("card_list", paixing);
		this.getPlayBack().addCommand("ChangePaiXing", player.seat, cmdData);
	}

	public void addAction(Action action) {
		if (this.actionList.size() == 0) {
			this.actionList.add(action);
		} else if (action.tip.weight > this.actionList.get(0).tip.weight) {
			this.actionList.clear();
			this.actionList.add(action);
		} else if (action.tip.weight == this.actionList.get(0).tip.weight) {
			this.actionList.add(action);
		}
		checkAction();
	}

	public void checkAction() {
		boolean isWeightest = this.isWeightestAction();
		if (!isWeightest) {
			return;
		}
		if (this.isAllPass()) {
			this.isAllPass = true;
			Player activePlayer = this.playerMapBySeat.get(this.activeSeat);
			activePlayer.stateMachine.toNextState();
		} else {
			for (Action action : this.actionList) {
				action.run();
			}
			this.actionList.clear();
		}
	}

	public boolean isAllPass() {
		return this.actionList.size() == 0 && this.tipMap.size() == 0;
	}

	public boolean isWeightestAction() {
		if (this.isAllPass()) {
			return true;
		}
		if (this.actionList.size() == 0) {
			return false;
		}
		Action curaction = this.actionList.get(0);
		for (Entry<Integer, TipManager> entry : this.tipMap.entrySet()) {
			TipManager tip = entry.getValue();
			if (curaction.player.seat == tip.owner.seat) {
				continue;
			}
			if (curaction.tip.weight <= tip.getWeightest()) {
				return false;
			}
		}
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			Player player = entry.getValue();
			if (curaction.player.seat == player.seat) {
				continue;
			}
			if (player.stateMachine.curState instanceof EXPlayerDisCardTipState || player.stateMachine.curState instanceof EXPlayerDrawTipState) {
				player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			}
		}
		return true;
	}

	public ITObject getReloadInfo(Player player) {
		ITObject data = getReloadInfoEx(player);
		EXPlayer p = (EXPlayer) player;
		data.putTArray("hand_card", Util.toTArray(p.cardInhand));
		data.putInt("left_card", card.getCount());
		data.putInt("curren_outcard_seat", currenDiscardSeat);
		data.putInt("last_outcard_seat", lastDiscardSeat);
		data.putInt("typeface", typeface);
		data.putInt("discard", this.isDiHu ? 0 : 1);

		ITArray paiXing = this.paiXing.getTArray(player.seat + "");
		if (paiXing == null) {
			paiXing = TArray.newInstance();
		}
		data.putTArray("card_list", paiXing);

		EXPlayer activePlayer = (EXPlayer) playerMapBySeat.get(activeSeat);
		if (activePlayer != null) {
			if (activePlayer.drawCard != 0) {
				data.putInt("acitve_card", activePlayer.drawCard);
			} else {
				data.putInt("acitve_card", activeCard);
			}
		} else {
			data.putInt("acitve_card", 0);
		}
		return data;
	}

	public ITObject getReloadInfoEx(Player player) {
		ITObject data = new TObject();
		data.putInt("active_seat", this.activeSeat);
		data.putInt("banker_seat", this.bankerSeat);
		data.putBoolean("playing", this.isplaying);
		ITArray info_list = TArray.newInstance();
		boolean self = false;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer tempPlayer = (EXPlayer) entry.getValue();
			if (tempPlayer.seat != player.seat) {
				continue;
			}
			Global.logger.info("tempPlayer:"+tempPlayer.playerid);
			info_list.addTObject(tempPlayer.getReloadInfo(true));
		}
		data.putTArray("info_list", info_list);
		return data;
	}

	public void addScore(EXPlayer player, int score, int type) {
		player.score.round_log.put(type, player.score.round_log.get(type) + score);

		player.score.round_score += score;
		player.score.total_score += score;
	}

	public void addScore(EXPlayer destPlayer) {
		int score = caclScore(destPlayer);
		//先判断分够不够
		int tmp_all_score = 0;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (!entry.getKey().equals(destPlayer.playerid)) {
				EXPlayer fromPlayer = (EXPlayer) entry.getValue();
				////
				if(multipleScore(score) > fromPlayer.hp.cur_hp) {
					tmp_all_score += reMultipleScore((int)fromPlayer.hp.cur_hp);
					this.addScore(fromPlayer, -reMultipleScore((int)fromPlayer.hp.cur_hp), EXScore.WIN);
				}else {
					tmp_all_score += score;
					this.addScore(fromPlayer, -score, EXScore.WIN);
				}
			}
		}
		//
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				this.addScore(destPlayer, tmp_all_score, EXScore.WIN);
			}
		}

		////
//		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
//			if (entry.getKey().equals(destPlayer.playerid)) {
//				if (this.maxPlayers == 2) {
//					this.addScore(destPlayer, score, EXScore.WIN);
//				} else {
//					this.addScore(destPlayer, score * 2, EXScore.WIN);
//				}
//			} else {
//				EXPlayer fromPlayer = (EXPlayer) entry.getValue();
//				this.addScore(fromPlayer, -score, EXScore.WIN);
//			}
//		}
	}

	public int caclScore(EXPlayer player) {
		int redCard = 0;
		int bigCard = 0;
		int smallCard = 0;
		int huXi = 0;
		int huXiNum = 0;
		boolean isDuzi = true;
		int huangFan = 0;

		//ssdd
		if (!player.handCardEx.isEmpty()) {
			isDuzi = false;
			for (int card : player.handCardEx) {
				if (CardUtil.isRedCard(card)) {
					redCard++;
				}
				if (CardUtil.cardType(card) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
			}
		}
		for (OpCard opCard : player.handCard) {
			if (opCard.huXi != 0) {
				huXi += opCard.huXi;
				huXiNum++;
			}
			if (CardUtil.isRedCard(opCard.card1)) {
				redCard++;
			}
			if (CardUtil.isRedCard(opCard.card2)) {
				redCard++;
			}
			if (CardUtil.cardType(opCard.card1) == RuleWeight.BIG_CARD) {
				bigCard++;
			} else {
				smallCard++;
			}
			if (CardUtil.cardType(opCard.card2) == RuleWeight.BIG_CARD) {
				bigCard++;
			} else {
				smallCard++;
			}
			if (opCard.type == 2) {
				if (opCard.card1 != opCard.card2 || opCard.card1 != opCard.card3) {
					isDuzi = false;
				}
				if (CardUtil.isRedCard(opCard.card3)) {
					redCard++;
				}
				if (CardUtil.cardType(opCard.card3) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
			}
		}
		for (OpCard opCard : player.opCardList) {
			if (opCard.huXi != 0) {
				huXi += opCard.huXi;
				huXiNum++;
			}
			if (opCard.type == RuleWeight.TYPE_CHOW) {
				isDuzi = false;
				if (CardUtil.isRedCard(opCard.card1)) {
					redCard++;
				}
				if (CardUtil.isRedCard(opCard.card2)) {
					redCard++;
				}
				if (CardUtil.isRedCard(opCard.card3)) {
					redCard++;
				}
				if (CardUtil.cardType(opCard.card1) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
				if (CardUtil.cardType(opCard.card2) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
				if (CardUtil.cardType(opCard.card3) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
			} else {
				if (CardUtil.isRedCard(opCard.card1)) {
					if (opCard.type == RuleWeight.TYPE_PONG || opCard.type == RuleWeight.TYPE_KAN || opCard.type == RuleWeight.TYPE_WEI || opCard.type == RuleWeight.TYPE_CHOUWEI) {
						redCard += 3;
					} else if (opCard.type == RuleWeight.TYPE_PAO || opCard.type == RuleWeight.TYPE_TI) {
						redCard += 4;
					}
				}
				if (CardUtil.cardType(opCard.card1) == RuleWeight.BIG_CARD) {
					if (opCard.type == RuleWeight.TYPE_PONG || opCard.type == RuleWeight.TYPE_KAN || opCard.type == RuleWeight.TYPE_WEI || opCard.type == RuleWeight.TYPE_CHOUWEI) {
						bigCard += 3;
					} else if (opCard.type == RuleWeight.TYPE_PAO || opCard.type == RuleWeight.TYPE_TI) {
						bigCard += 4;
					}
				} else {
					if (opCard.type == RuleWeight.TYPE_PONG || opCard.type == RuleWeight.TYPE_KAN || opCard.type == RuleWeight.TYPE_WEI || opCard.type == RuleWeight.TYPE_CHOUWEI) {
						smallCard += 3;
					} else if (opCard.type == RuleWeight.TYPE_PAO || opCard.type == RuleWeight.TYPE_TI) {
						smallCard += 4;
					}
				}
			}
		}
		player.mingtangList.clear();
		player.winFan = 0;
		player.huXi = huXi;
		player.total_huxi = huXi;
		player.winScore =huXi-huXi%3;//jefe  (huXi - 15) / 3 + 1


		int score = player.winScore + this.config.getInt(Config.GAME_TUN);
		if (!player.isMingTang) {
			if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
				if (player.seat == this.bankerSeat) {
					player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 6));
				} else {
					player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 6));
				}
				player.winFan += 6;
				// �Ʒ�
				if (this.config.getInt(Config.GAME_HUANGFAN) == 1) {
					if (this.liujuNum > 0) {
						huangFan = this.liujuNum;
						player.mingtangList.add(new MingTang(RuleWeight.HUANG_FAN, 3, huangFan + 1));
					}
				}
				// ���
				if (redCard >= 10) {
					int fan = 0;
					if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
						fan = 3 + redCard - 10;
					} else if (this.config.getInt(Config.GAME_MODE) == 2) {
						if (redCard < 13) {
							fan = 2;
						}
					} else {
						fan = 2 + redCard - 10;
					}
					player.winFan += fan;
					player.mingtangList.add(new MingTang(RuleWeight.HONG_HU, 1, fan));
				}
				if (this.config.getInt(Config.GAME_MODE) == 2) {
					// ����
					if (redCard >= 13) {
						player.winFan += 4;
						player.mingtangList.add(new MingTang(RuleWeight.HONG_WU, 1, 4));
					}
				}
				// �ں�
				if (redCard == 0) {
					int fan = 0;
					if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
						fan = 8;
					} else if (this.config.getInt(Config.GAME_MODE) == 2) {
						fan = 5;
					} else {
						fan = 5;
					}
					player.winFan += fan;
					player.mingtangList.add(new MingTang(RuleWeight.WU_HU, 1, fan));
				}
				// ���
				if (redCard == 1) {
					int fan = 0;
					if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
						fan = 6;
					} else if (this.config.getInt(Config.GAME_MODE) == 2) {
						fan = 3;
					} else {
						fan = 3;
					}
					player.winFan += fan;
					player.mingtangList.add(new MingTang(RuleWeight.DIAN_HU, 1, fan));
				}
				// ���ߺ�
				if (this.config.getInt(Config.GAME_SIQI) == 1) {
					if (redCard == 4 || redCard == 7) {
						player.winFan += 2;
						player.mingtangList.add(new MingTang(RuleWeight.SI_QI_HONG, 1, 2));
					}
				}
			} else if (player.room.config.getInt(Config.GAME_MODE) == 1) {//jefe
				if (huXi>=30){
					player.winFan += 2;
				}else {
					if (player.seat == this.bankerSeat) {
						if (this.config.getInt(Config.GAME_TIANHU) == 1) {
							player.winFan += 2;//jefe 4
							player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 4));
						}
					} else {
						if (this.config.getInt(Config.GAME_DIHU) == 1) {
							player.winFan += 2;//jefe 4
							player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 4));
						}
					}
					// �Ʒ�
					if (this.config.getInt(Config.GAME_HUANGFAN) == 1) {
						if (this.liujuNum > 0) {
							huangFan = this.liujuNum;
							player.mingtangList.add(new MingTang(RuleWeight.HUANG_FAN, 3, huangFan + 1));
						}
					}
				}
			}

			if (player.winFan > 0) {
				score *= player.winFan;
			}
			if (huangFan > 0) {
				score *= (huangFan + 1);
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 1) {
				if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
					score = Math.min(score, 100);
				} else if (player.room.config.getInt(Config.GAME_MODE) == 2) {
					score = Math.min(score, 20);
				} else {
					score = Math.min(score, 60);
				}
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 2) {
				if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
					score = Math.min(score, 200);
				} else if (player.room.config.getInt(Config.GAME_MODE) == 2) {
					score = Math.min(score, 40);
				} else {
					score = Math.min(score, 80);
				}
			}

			return score;
		}
		// ���
		if (redCard >= 10) {
			int fan = 0;
			if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
				fan = 3 + redCard - 10;
			} else if (this.config.getInt(Config.GAME_MODE) == 2) {
				if (redCard < 13) {
					fan = 2;
				}
			} else {
				fan = 2 ;//jefe 2 + redCard - 10
			}
			player.winFan += fan;
			player.mingtangList.add(new MingTang(RuleWeight.HONG_HU, 1, fan));
		}
		if (this.config.getInt(Config.GAME_MODE) == 2) {
			// ����
			if (redCard >= 13) {
				player.winFan += 4;
				player.mingtangList.add(new MingTang(RuleWeight.HONG_WU, 1, 4));
			}
		}
		// �ں�
		if (redCard == 0) {
			int fan = 0;
			if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
				fan = 8;
			} else if (this.config.getInt(Config.GAME_MODE) == 2) {
				fan = 5;
			} else {
				fan = 2;//jefe 5
			}
			player.winFan += fan;
			player.mingtangList.add(new MingTang(RuleWeight.WU_HU, 1, fan));
		}
		// ���
		if (redCard == 1) {
			int fan = 0;
			if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
				fan = 6;
			} else if (this.config.getInt(Config.GAME_MODE) == 2) {
				fan = 3;
			} else {
				fan = 2;//jefe 3
			}
			player.winFan += fan;
			player.mingtangList.add(new MingTang(RuleWeight.DIAN_HU, 1, fan));
		}
		// ���Ӻ�
		if (isDuzi) {
			if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
				player.winFan += 8;
				player.mingtangList.add(new MingTang(RuleWeight.DUIZI_HU, 1, 8));
			} else if (this.config.getInt(Config.GAME_MODE) == 2) {
				if (this.config.getInt(Config.GAME_DUIZIFU) == 1) {
					player.winFan += 4;
					player.mingtangList.add(new MingTang(RuleWeight.DUIZI_HU, 1, 4));
				}
			} else {
				player.winFan += 2;//jefe 4
				player.mingtangList.add(new MingTang(RuleWeight.DUIZI_HU, 1, 2));
			}
		}
		// ����
		if (this.winSeat == player.seat) {
			//player.winScore += 1; jefe
			player.mingtangList.add(new MingTang(RuleWeight.ZI_MO, 2, 1*3));//jefe *3
		}
		// ȫ����
		if (this.config.getInt(Config.GAME_MODE) == 3) {//jefe
			if (this.isTianHu) {
				// ���
				if (player.seat == this.bankerSeat) {
					player.winFan += 6;
					player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 6));
				}
				// �غ�
				else {
					player.winFan += 6;
					player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 6));
				}
			}
			// ����
			if (this.card.getCount() == 0) {
				player.winFan += 6;
				player.mingtangList.add(new MingTang(RuleWeight.HAI_HU, 1, 6));
			}
			// ����
			if (this.config.getInt(Config.GAME_TINGHU) == 1) {
				if (player.isTingHu) {
					player.winFan += 6;
					player.mingtangList.add(new MingTang(RuleWeight.TING_HU, 1, 6));
				}
			}
			// ���
			if (bigCard >= 18) {
				int fan = 8 + bigCard - 18;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.DA_HU, 1, fan));
			}

			// С��
			if (smallCard >= 16) {
				int fan = 10 + smallCard - 16;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.XIAO_HU, 1, fan));
			}

			// ����Բ
			if (this.config.getInt(Config.GAME_TUANYUAN) == 1) {
				int duan = 0;
				for (int i = 1; i <= 10; i++) {
					int num = 0;
					for (OpCard opCard : player.opCardList) {
						if (opCard.type != RuleWeight.TYPE_PAO && opCard.type != RuleWeight.TYPE_TI) {
							continue;
						}
						if (opCard.card1 % 100 == i) {
							num++;
						}
					}
					if (num == 2) {
						duan++;
					}
				}
				if (duan > 0) {
					int fan = duan * 8;
					player.winFan += fan;
					player.mingtangList.add(new MingTang(RuleWeight.DA_TUAN_YUAN, 1, fan));
				}
			}
			// ����Ϣ
			if (this.config.getInt(Config.GAME_HANGHANGXI) == 1) {
				if (huXiNum == 7) {
					player.winFan += 8;
					player.mingtangList.add(new MingTang(RuleWeight.XING_XING_XI, 1, 8));
				}
			}
			// ������Ϣ
			if (this.config.getInt(Config.GAME_JIAXINGXING) == 1) {
				if ((player.tiCount + player.kongCount) > 0) {
					if (huXiNum == 6) {
						player.winFan += 4;
						player.mingtangList.add(new MingTang(RuleWeight.JIA_XING_XING, 1, 4));
					}
				}
			}
			// ���ߺ�
			if (this.config.getInt(Config.GAME_SIQI) == 1) {
				if (redCard == 4 || redCard == 7) {
					player.winFan += 2;
					player.mingtangList.add(new MingTang(RuleWeight.SI_QI_HONG, 1, 2));
				}
			}
			// ˣ��
			if (this.config.getInt(Config.GAME_SHUAHOU) == 1) {
				if (player.kanGroup.isEmpty() && player.cardInhand.size() == 1) {
					player.winFan += 8;
					player.mingtangList.add(new MingTang(RuleWeight.SHUA_HOU, 1, 8));
				}
			}
			// �Ʒ�
			if (this.config.getInt(Config.GAME_HUANGFAN) == 1) {
				if (this.liujuNum > 0) {
					huangFan = this.liujuNum;
					player.mingtangList.add(new MingTang(RuleWeight.HUANG_FAN, 3, huangFan + 1));
				}
			}
			// Բ
			if (this.config.getInt(Config.GAME_YUAN) == 1) {
				List<Integer> cardList = new ArrayList<>();
				for (OpCard opCard : player.opCardList) {
					if (opCard.type != RuleWeight.TYPE_PAO && opCard.type != RuleWeight.TYPE_TI) {
						continue;
					}
					cardList.add(opCard.card1);
				}
				if (!cardList.isEmpty()) {
					Collections.sort(cardList);
					int yuan = 0;
					int num = cardList.get(0);
					for (int i = 1; i < cardList.size(); i++) {
						if (num + 1 == cardList.get(i)) {
							yuan++;
						}
						num = cardList.get(i);
					}
					if (yuan > 0) {
						int fan = yuan * 6;
						player.winFan += fan;
						player.mingtangList.add(new MingTang(RuleWeight.YUAN, 1, fan));
					}
				}
			}
			// ������
			if (this.config.getInt(Config.GAME_BEIKAOBEI) == 1) {
				boolean kezi1 = false;
				boolean kezi2 = false;
				boolean isBeiKaoBei = true;
				for (OpCard opCard : player.handCard) {
					if (opCard.type == 1) {
						if (opCard.card1 != player.winCard && opCard.card1 % 100 == player.winCard % 100) {
							kezi1 = true;
							continue;
						}
						break;
					} else {
						if (opCard.card1 != opCard.card2 || opCard.card1 != opCard.card3) {
							if (opCard.card1 == player.winCard || opCard.card2 == player.winCard || opCard.card3 == player.winCard) {
								isBeiKaoBei = false;
								break;
							}
							continue;
						}
						if (opCard.card1 == player.winCard) {
							kezi2 = true;
						}
					}
				}
				if (isBeiKaoBei) {
					for (OpCard opCard : player.opCardList) {
						if (opCard.type != RuleWeight.TYPE_WEI && opCard.type != RuleWeight.TYPE_CHOUWEI && opCard.type != RuleWeight.TYPE_KAN) {
							continue;
						}
						if (opCard.card1 == player.winCard) {
							kezi2 = true;
						}
					}
				}
				if (kezi1 && kezi2) {
					player.winFan += 8;
					player.mingtangList.add(new MingTang(RuleWeight.BEI_KAO_BEI, 1, 8));
				}
			}
		}
		// ���෬
		if (this.config.getInt(Config.GAME_MODE) == 1) {//jefe
			if (this.isTianHu) {
				// ���
				if (player.seat == this.bankerSeat) {
					if (this.config.getInt(Config.GAME_TIANHU) == 0) {//jefe 1
						player.winFan += 2; //jefe 4
						player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 2));
					}
				}
				// �غ�
				else {
					if (this.config.getInt(Config.GAME_DIHU) == 0) { //jefe 1
						player.winFan += 2;// jefe 4
						player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 2));
					}
				}
			}
			// ����
			if (this.config.getInt(Config.GAME_HAIHU) == 1) {//
				if (this.card.getCount() == 0) {
					player.winFan += 2; // //jefe 4
					player.mingtangList.add(new MingTang(RuleWeight.HAI_HU, 1, 2));
				}
			}
			/*
			if (this.config.getInt(Config.GAME_TINGHU) == 1) {
				// ����
				if (player.isTingHu) {
					player.winFan += 2; //jefe 4
					player.mingtangList.add(new MingTang(RuleWeight.TING_HU, 1, 2));
				}
			}
			// �Ʒ�
			if (this.config.getInt(Config.GAME_HUANGFAN) == 1) {
				if (this.liujuNum > 0) {
					huangFan = this.liujuNum;
					player.mingtangList.add(new MingTang(RuleWeight.HUANG_FAN, 3, huangFan + 1));
				}
			}*/

			if (bigCard >= 18) {
				int fan = 8 + bigCard - 18;
				fan = 2;//jefe
				player.winFan += 2;//jefe fan
				player.mingtangList.add(new MingTang(RuleWeight.DA_HU, 1, fan));
			}

			// С��
			if (smallCard >= 18) {
				int fan = 10 + smallCard - 18;
				fan = 2;//jefe
				player.winFan += 2;//jefe fan
				player.mingtangList.add(new MingTang(RuleWeight.XIAO_HU, 1, fan));
			}

			//30息翻倍 jefe
			if(player.winFan<=1&&huXi>=30){
				player.winFan += 2;//jefe fan
				player.mingtangList.add(new MingTang(RuleWeight.SANSHI_XI_FAN, 1, 2));
			}

		}
		score = player.winScore + this.config.getInt(Config.GAME_TUN);
		if (player.winFan > 0) {
			score *= player.winFan;
		}
		if (huangFan > 0) {
			score *= (huangFan + 1);
		}
		if (this.config.getInt(Config.GAME_FENGDING) == 1) {
			if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
				score = Math.min(score, 100);
			} else if (player.room.config.getInt(Config.GAME_MODE) == 2) {
				score = Math.min(score, 20);
			} else {
				score = Math.min(score, 60);
			}
		}
		if (this.config.getInt(Config.GAME_FENGDING) == 2) {
			if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
				score = Math.min(score, 200);
			} else if (player.room.config.getInt(Config.GAME_MODE) == 2) {
				score = Math.min(score, 40);
			} else {
				score = Math.min(score, 80);
			}
		}
		if (this.config.getInt(Config.GAME_FENGDING) == 3) {
			if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
				score = Math.min(score, 300);
			}
		}
		if (this.config.getInt(Config.GAME_FENGDING) == 4) {
			if (player.room.config.getInt(Config.GAME_MODE) == 3) {//jefe
				score = Math.min(score, 500);
			}
		}
		//jefe *3
		if (this.winSeat == player.seat) {
			score += 3;
		}

		return score;
	}

	public boolean checkPao(int eventCard, boolean isDis) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (isDis) {
				for (int i = 0; i < player.kanGroup.size(); i++) {
					if (eventCard == player.kanGroup.get(i)[0]) {
						return true;
					}
				}
				for (int i = 0; i < player.weiGroup.size(); i++) {
					if (eventCard == player.weiGroup.get(i)[0]) {
						return true;
					}
				}
			} else {
				for (int i = 0; i < player.pongGroup.size(); i++) {
					if (eventCard == player.pongGroup.get(i)[0]) {
						return true;
					}
				}
				if (player.seat != this.activeSeat) {
					for (int i = 0; i < player.kanGroup.size(); i++) {
						if (eventCard == player.kanGroup.get(i)[0]) {
							return true;
						}
					}
					for (int i = 0; i < player.weiGroup.size(); i++) {
						if (eventCard == player.weiGroup.get(i)[0]) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	// public void settleRound() {
	// saveMilitaryRound(playBackData.getData(this));
	// if (this.liuju == true) {
	// this.bankerSeat = this.playerMapBySeat.get(this.bankerSeat).nextSeat;
	// }
	// boolean total = this.round >= this.maxRound;
	// int result = this.nonNegative(false);
	// if (!total) {
	// EXMainServer.gameCtr.roomResult(this);
	// if (result == 4) {
	// total = true;
	// this.saveMilitaryTotal(false);
	// }
	// } else {
	// this.saveMilitaryTotal(false);
	// }
	// this.stateMachine.changeState(Global.getState(EXRoomRoundSettleState.class));
	// this.winCount = 0;
	//
	// if (total) {
	// this.stateMachine.changeState(Global.getState(RoomEndGameState.class));
	// return;
	// }
	// }

	public void win(EXPlayer player, EXPlayer activePlayer) {
		if (player.getRoom().drawCard != 0) {
			player.winCard = player.getRoom().drawCard;
		}
		player.winer = 1;
		int scoreSrc = caclScore(player);
		if (player.handCard.isEmpty()) {
			scoreSrc = 0;
		}
		player.resetHuXi();
		int huXi = Paixing.caclHuxi(RuleWeight.TYPE_PAO, activePlayer.drawCard);

		Stack<OpCard> handCard = new Stack<OpCard>();
		List<Integer> handCardEx = new ArrayList<>();
		handCard.addAll(player.handCard);
		handCardEx.addAll(player.handCardEx);

		for (int i = 0; i < player.pongGroup.size(); i++) {
			if (activePlayer.drawCard == player.pongGroup.get(i)[0]) {
				player.kongCount++;
				WinCard win = new WinCard(player.cardInhand, 0);
				if (win.tryWin(player)) {
					int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_PONG, activePlayer.drawCard);
					if (player.huXi - huXi1 + huXi >= 15) {
						player.pongGroup.remove(i);

						CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_PONG, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

						int[] paoGroup = new int[4];
						paoGroup[0] = activePlayer.drawCard;
						paoGroup[1] = activePlayer.drawCard;
						paoGroup[2] = activePlayer.drawCard;
						paoGroup[3] = activePlayer.drawCard;

						player.paoGroup.add(paoGroup);

						huXi = Paixing.caclHuxi(RuleWeight.TYPE_PAO, activePlayer.drawCard);
						player.opCardList.add(new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

						player.handCard.clear();
						player.handCardEx.clear();
						for (int j = 0; j < win.stack.size(); j++) {
							OpCard opcard = new OpCard();
							opcard.card1 = win.stack.get(j).get(0);
							opcard.card2 = win.stack.get(j).get(1);
							opcard.type = 1;
							if (win.stack.get(j).size() > 2) {
								opcard.card3 = win.stack.get(j).get(2);
								opcard.type = 2;
							}
							opcard.huXi = win.stackHuxi.get(j);

							player.handCard.push(opcard);
							if (!win.cardList.isEmpty()) {
								player.handCardEx.addAll(win.cardList);
							}
						}

						int scoreDes = caclScore(player);
						if (scoreDes < scoreSrc) {
							for (int j = 0; j < player.paoGroup.size(); j++) {
								if (activePlayer.drawCard == player.paoGroup.get(j)[0]) {
									player.paoGroup.remove(j);
									break;
								}
							}
							CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

							int[] pongGroup = new int[3];
							pongGroup[0] = activePlayer.drawCard;
							pongGroup[1] = activePlayer.drawCard;
							pongGroup[2] = activePlayer.drawCard;

							player.pongGroup.add(pongGroup);

							huXi = Paixing.caclHuxi(RuleWeight.TYPE_PONG, activePlayer.drawCard);
							player.opCardList.add(new OpCard(RuleWeight.TYPE_PONG, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

							player.handCard.clear();
							player.handCardEx.clear();

							player.handCard.addAll(handCard);
							player.handCardEx.addAll(handCardEx);

							player.kongCount--;
						}
					} else {
						player.kongCount--;
					}
				} else {
					player.kongCount--;
				}
				break;
			}
		}
		if (player.seat != activePlayer.seat) {
			for (int i = 0; i < player.kanGroup.size(); i++) {
				if (activePlayer.drawCard == player.kanGroup.get(i)[0]) {
					player.kongCount++;

					WinCard win = new WinCard(player.cardInhand, 0);
					if (win.tryWin(player)) {
						int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_KAN, activePlayer.drawCard);
						if (player.huXi - huXi1 + huXi >= 15) {
							player.kanGroup.remove(i);

							CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

							int[] paoGroup = new int[4];
							paoGroup[0] = activePlayer.drawCard;
							paoGroup[1] = activePlayer.drawCard;
							paoGroup[2] = activePlayer.drawCard;
							paoGroup[3] = activePlayer.drawCard;

							player.paoGroup.add(paoGroup);

							huXi = Paixing.caclHuxi(RuleWeight.TYPE_PAO, activePlayer.drawCard);
							player.opCardList.add(new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

							player.handCard.clear();
							player.handCardEx.clear();
							for (int j = 0; j < win.stack.size(); j++) {
								OpCard opcard = new OpCard();
								opcard.card1 = win.stack.get(j).get(0);
								opcard.card2 = win.stack.get(j).get(1);
								opcard.type = 1;
								if (win.stack.get(j).size() > 2) {
									opcard.card3 = win.stack.get(j).get(2);
									opcard.type = 2;
								}
								opcard.huXi = win.stackHuxi.get(j);

								player.handCard.push(opcard);
								if (!win.cardList.isEmpty()) {
									player.handCardEx.addAll(win.cardList);
								}
							}

							int scoreDes = caclScore(player);
							if (scoreDes < scoreSrc) {
								for (int j = 0; j < player.paoGroup.size(); j++) {
									if (activePlayer.drawCard == player.paoGroup.get(j)[0]) {
										player.paoGroup.remove(j);
										break;
									}
								}
								CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

								int[] kanGroup = new int[3];
								kanGroup[0] = activePlayer.drawCard;
								kanGroup[1] = activePlayer.drawCard;
								kanGroup[2] = activePlayer.drawCard;

								player.kanGroup.add(kanGroup);

								huXi = Paixing.caclHuxi(RuleWeight.TYPE_KAN, activePlayer.drawCard);
								player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

								player.handCard.clear();
								player.handCardEx.clear();

								player.handCard.addAll(handCard);
								player.handCardEx.addAll(handCardEx);

								player.kongCount--;
							}
						} else {
							player.kongCount--;
						}
					} else {
						player.kongCount--;
					}
					break;
				}
			}
			for (int i = 0; i < player.weiGroup.size(); i++) {
				if (activePlayer.drawCard == player.weiGroup.get(i)[0]) {
					player.kongCount++;

					WinCard win = new WinCard(player.cardInhand, 0);
					if (win.tryWin(player)) {
						int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_WEI, activePlayer.drawCard);
						if (player.huXi - huXi1 + huXi >= 15) {
							player.weiGroup.remove(i);

							CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_WEI, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

							int[] paoGroup = new int[4];
							paoGroup[0] = activePlayer.drawCard;
							paoGroup[1] = activePlayer.drawCard;
							paoGroup[2] = activePlayer.drawCard;
							paoGroup[3] = activePlayer.drawCard;

							player.paoGroup.add(paoGroup);

							huXi = Paixing.caclHuxi(RuleWeight.TYPE_PAO, activePlayer.drawCard);
							player.opCardList.add(new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

							player.handCard.clear();
							player.handCardEx.clear();
							for (int j = 0; j < win.stack.size(); j++) {
								OpCard opcard = new OpCard();
								opcard.card1 = win.stack.get(j).get(0);
								opcard.card2 = win.stack.get(j).get(1);
								opcard.type = 1;
								if (win.stack.get(j).size() > 2) {
									opcard.card3 = win.stack.get(j).get(2);
									opcard.type = 2;
								}
								opcard.huXi = win.stackHuxi.get(j);

								player.handCard.push(opcard);
								if (!win.cardList.isEmpty()) {
									player.handCardEx.addAll(win.cardList);
								}
							}

							int scoreDes = caclScore(player);
							if (scoreDes < scoreSrc) {
								for (int j = 0; j < player.paoGroup.size(); j++) {
									if (activePlayer.drawCard == player.paoGroup.get(j)[0]) {
										player.paoGroup.remove(j);
										break;
									}
								}
								CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_PAO, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, 0));

								int[] weiGroup = new int[3];
								weiGroup[0] = activePlayer.drawCard;
								weiGroup[1] = activePlayer.drawCard;
								weiGroup[2] = activePlayer.drawCard;

								player.weiGroup.add(weiGroup);

								huXi = Paixing.caclHuxi(RuleWeight.TYPE_WEI, activePlayer.drawCard);
								player.opCardList.add(new OpCard(RuleWeight.TYPE_WEI, activePlayer.drawCard, activePlayer.drawCard, activePlayer.drawCard, huXi));

								player.handCard.clear();
								player.handCardEx.clear();

								player.handCard.addAll(handCard);
								player.handCardEx.addAll(handCardEx);

								player.kongCount--;
							}
						} else {
							player.kongCount--;
						}
					} else {
						player.kongCount--;
					}
					break;
				}
			}
		}
		this.addScore(player);
		// this.liuju = false;
		this.liujuNum = 0;

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(player.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			fromPlayer.winer = 2;
		}
		EXMainServer.gameCtr.winEvent(player, player.seat);
		// if (owner.room.config.getInt(Config.GAME_ZHUANG) == 1) {
		// if (room.bankerSeat != owner.seat) {
		// room.bankerSeat = owner.lastSeat;
		// }
		// } else {
		// room.bankerSeat = owner.seat;
		// }
		this.bankerSeat = player.seat;
		player.winCount++;
		this.endGame();
	}

	@Override
	public void endGame() {
		super.endGame();
	}

	public void saveRecRound() {
		this.getPlayBack().addResultData(EXMainServer.gameCtr.getRoomResultData(this));
		super.saveRecRound();
	}

	@Override
	protected void roomResult() {
		EXMainServer.gameCtr.roomResult(this);
	}

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

	public PlayBack getPlayBack() {
		return (PlayBack) playBackData;
	}

	@Override
	public void clear() {
		super.clear();
		this.liuju = false;
		this.activeSeat = this.lastDiscardSeat = this.currenDiscardSeat = 0;
		this.isplaying = this.isAllPass = false;
		this.isTianHu = this.isDiHu = true;
		this.paiXing = TObject.newInstance();
		this.throwCardsSet.clear();
		this.throwCardsList.clear();
	}
	// public void destroy() {
	// super.destroy();
	// }

	/**
	 * 开启倍数后 体力值消耗= 倍数*分数
	 */
	private int reMultipleScore(int score) {
		if (this.hpData != null) {
			if (this.hpData.containsKey("times")) {
				return Math.round(score / this.hpData.getInt("times"));
			}
		}
		return score;
	}
}
