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 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.rule.*;
import extend.zp.player.state.EXPlayerDisCardTipState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.tip.Action;
import extend.zp.tip.IRuleBase;
import extend.zp.tip.TipManager;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;

public class EXRoom extends Room {

	//
	public Map<Integer, TipManager>	tipMap;
	public List<Action>				actionList;

	public RoomCard					card;

	Logger logger = Logger.getLogger(getClass());

	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 						piaoCount 	= 0;
	
	public boolean hh = false;
	public boolean dh = false;
	public boolean hw = false;
	public boolean wh = false;
	public boolean hd = false;
	public boolean wd = false;
	public boolean jhd = false;
	
	public boolean dhdf =false;
	
	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;
		
		hh = config.containsKey("hh") ? (config.getInt("hh") == 0 ? true : false) : true; 
		dh = config.containsKey("dh") ? (config.getInt("dh") == 0 ? true : false) : true; 
		hw = config.containsKey("hw") ? (config.getInt("hw") == 0 ? true : false) : true; 
		wh = config.containsKey("wh") ? (config.getInt("wh") == 0 ? true : false) : true; 
		hd = config.containsKey("hd") ? (config.getInt("hd") == 0 ? true : false) : true; 
		wd = config.containsKey("wd") ? (config.getInt("wd") == 0 ? true : false) : true; 
		jhd = config.containsKey("jhd") ? (config.getInt("jhd") == 0 ? true : false) : true; 
		
		dhdf = config.containsKey("dhdf") ? (config.getInt("dhdf") == 0 ? false : true) : false;
		this.adminSeat = 0;
		if (!this.config.containsKey(Config.GAME_MODE))
		{
			this.config.putInt(Config.GAME_MODE, 2);
		}
		if (!this.config.containsKey(Config.GAME_TUN))
		{
			this.config.putInt(Config.GAME_TUN, 0);
		}
		if (!this.config.containsKey(Config.GAME_TUN))
		{
			this.config.putInt(Config.GAME_TUN, 0);
		}
		if (!this.config.containsKey(Config.GAME_FENGDING))
		{
			this.config.putInt(Config.GAME_FENGDING, 0);
		}
		if (!this.config.containsKey(Config.GAME_TUANYUAN))
		{
			this.config.putInt(Config.GAME_TUANYUAN, 0);
		}
		if (!this.config.containsKey(Config.GAME_TUANYUAN))
		{
			this.config.putInt(Config.GAME_TUANYUAN, 0);
		}
		if (!this.config.containsKey(Config.GAME_HANGHANGXI))
		{
			this.config.putInt(Config.GAME_HANGHANGXI, 0);
		}
		if (!this.config.containsKey(Config.GAME_SHUAHOU))
		{
			this.config.putInt(Config.GAME_SHUAHOU, 0);
		}
		if (!this.config.containsKey(Config.GAME_SHUAHOU))
		{
			this.config.putInt(Config.GAME_SHUAHOU, 0);
		}
		if (!this.config.containsKey(Config.GAME_HUANGFAN))
		{
			this.config.putInt(Config.GAME_HUANGFAN, 0);
		}
		if (!this.config.containsKey(Config.GAME_JIAXINGXING))
		{
			this.config.putInt(Config.GAME_JIAXINGXING, 0);
		}
		if (!this.config.containsKey(Config.GAME_TINGHU))
		{
			this.config.putInt(Config.GAME_TINGHU, 0);
		}
		if (!this.config.containsKey(Config.GAME_SIQI))
		{
			this.config.putInt(Config.GAME_SIQI, 0);
		}
		if (!this.config.containsKey(Config.GAME_DUIZIFU))
		{
			this.config.putInt(Config.GAME_DUIZIFU, 0);
		}
		if (!this.config.containsKey(Config.GAME_YUAN))
		{
			this.config.putInt(Config.GAME_YUAN, 0);
		}
		if (!this.config.containsKey(Config.GAME_BEIKAOBEI))
		{
			this.config.putInt(Config.GAME_BEIKAOBEI, 0);
		}
		if (!this.config.containsKey(Config.GAME_TIANHU))
		{
			this.config.putInt(Config.GAME_TIANHU, 0);
		}
		if (!this.config.containsKey(Config.GAME_DIHU))
		{
			this.config.putInt(Config.GAME_DIHU, 0);
		}
		if (!this.config.containsKey(Config.GAME_HAIHU))
		{
			this.config.putInt(Config.GAME_HAIHU, 0);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_QUPAI))
		{
			this.config.putInt(Config.ROOM_CONFIG_QUPAI, 0);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_KA_WAI))
		{
			this.config.putInt(Config.ROOM_CONFIG_KA_WAI, 0);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_FIRST_RAND_ZHUANG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_FIRST_RAND_ZHUANG, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAO_CONFIG))
		{
			this.config.putInt(Config.ROOM_CONFIG_HAO_CONFIG, 0);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAO_JIABEI))
		{
			this.config.putInt(Config.ROOM_CONFIG_HAO_JIABEI, 0);
		}

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

		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);
		}
		logger.info("jefe kawai:"+this.config.getInt(Config.ROOM_CONFIG_KA_WAI));
		if (this.config.getInt(Config.ROOM_CONFIG_KA_WAI) == 0)
		{
			List<IRuleBase> paoList = new ArrayList<IRuleBase>();
			//paoList.add(new RuleTi());
			paoList.add(new RuleWei());
			EXMainServer.playerRuleMgr.ruleMap.put(PlayerRuleManager.TI_RULE, paoList);

			List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
			drawRuleList.add(new RuleWin());
			drawRuleList.add(new RuleDrawChi());
			//drawRuleList.add(new RuleLiu());
			drawRuleList.add(new RuleWeiHu());
			//drawRuleList.add(new RuleWei());
			drawRuleList.add(new RuleWeiLiu());
			//drawRuleList.add(new RuleDealLiu());

			EXMainServer.playerRuleMgr.ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);
		}
		else
		{
			List<IRuleBase> paoList = new ArrayList<IRuleBase>();
			EXMainServer.playerRuleMgr.ruleMap.put(PlayerRuleManager.TI_RULE, paoList);

			List<IRuleBase> drawRuleList = new ArrayList<IRuleBase>();
			drawRuleList.add(new RuleWin());
			drawRuleList.add(new RuleDrawChi());
			drawRuleList.add(new RuleLiu());
			drawRuleList.add(new RuleWei());
			//drawRuleList.add(new RuleDealLiu());
			drawRuleList.add(new RuleWeiLiu());
			EXMainServer.playerRuleMgr.ruleMap.put(PlayerRuleManager.DRAW_RULE, drawRuleList);
		}

	}

	public void changePaiXing(EXPlayer player, ITObject params) {
		ITArray paixing = params.getTArray("card_list");
		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);
			}
		}
		for (OpCard opCard : player.opCardList) {
			if (opCard.type == RuleWeight.TYPE_TI && opCard.pailie) {
				tmpCardList2.add(opCard.card1);
				tmpCardList2.add(opCard.card2);
				tmpCardList2.add(opCard.card3);
				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;
			//add allpass

			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;
		ArrayList<Integer> tempList = new ArrayList<>();
		tempList.addAll(p.cardInhand);
		for (OpCard opcard : p.opCardList) {
			if (opcard.type == RuleWeight.TYPE_TI && opcard.pailie == true) {
				tempList.add(opcard.card1);
				tempList.add(opcard.card1);
				tempList.add(opcard.card1);
				tempList.add(opcard.card1);
				continue;
			}
			if (opcard.type == RuleWeight.TYPE_KAN && opcard.pailie == true) {
				tempList.add(opcard.card1);
				tempList.add(opcard.card1);
				tempList.add(opcard.card1);
				continue;
			}
		}

		data.putTArray("hand_card", Util.toTArray(tempList));
		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);

		EXPlayer exPlayer = (EXPlayer)player;
		ITArray notPutArray = TArray.newInstance();
		for (Integer notPutCard : exPlayer.notPutSet) {
			notPutArray.addInt(notPutCard);
		}
		data.putTArray("notput", notPutArray);
		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) {
				self = true;
			}
			info_list.addTObject(tempPlayer.getReloadInfo(self));
		}
		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();
				int temp_score = score;
				if(destPlayer.piao > 0) {
					temp_score += destPlayer.piao;
				}

				if(fromPlayer.piao > 0) {
					temp_score += fromPlayer.piao;
				}
				////
				if(multipleScore(temp_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 += temp_score;
					this.addScore(fromPlayer, -temp_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 = 1; //胡算一分
		int huXiNum = 0;
		int kanXi = 0;
		int haoXi = 0;
		int waiXi = 0;
		boolean isDuzi = true;
		for (int i = 0; i < player.tiGroup.size(); i++) {
		    int ti_card = player.tiGroup.get(i)[0];
			CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_TI, ti_card, ti_card, ti_card, 0));

			for (OpCard opCard : player.handCard) {
				if (opCard.card1 == opCard.card2 && opCard.card1 == opCard.card3 && opCard.card1 == ti_card) {
					int[] kanGroup = new int[3];
					kanGroup[0] = ti_card;
					kanGroup[1] = ti_card;
					kanGroup[2] = ti_card;

					player.kanGroup.add(kanGroup);
					player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, ti_card, ti_card, ti_card, Paixing.caclHuxi(RuleWeight.TYPE_KAN, ti_card), true, RuleWeight.HAO_NONE, 0));
				}
			}

			//hao
			if (this.config.getInt(Config.ROOM_CONFIG_HAO_CONFIG) == 0)
			{
				huXi += 30;
				haoXi += 30;
			}
			else {
				huXi += 40;
				haoXi += 40;
			}
		}

		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) {
			    boolean flag = true;
				if (opCard.card1 == opCard.card2 && opCard.card1 == opCard.card3 && opCard.card1 != 0) {
					for (int i = 0; i < player.tiGroup.size(); i++) {
						int ti_card = player.tiGroup.get(i)[0];
						if (ti_card == opCard.card1)
						{
							flag = false;
						}
					}
				}
				if (flag)
				{
					huXi += opCard.huXi;
					huXiNum++;
				}
			}

			//hao
			if (this.config.getInt(Config.ROOM_CONFIG_HAO_CONFIG) == 0)
			{
				if (opCard.hao == RuleWeight.HAO_SHAN)
				{
					huXi += 40;
					haoXi += 40;
				}
				else if (opCard.hao == RuleWeight.HAO_QING)
				{
					huXi += 30;
					haoXi += 30;
				}
				else if (opCard.hao == RuleWeight.HAO_NUI)
				{
					huXi += 20;
					haoXi += 20;
				}
				else if (opCard.hao == RuleWeight.HAO_WAI)
				{
					huXi += 10;
					haoXi += 10;
				}
			}
			else {
				if (opCard.hao == RuleWeight.HAO_SHAN)
				{
					huXi += 50;
					haoXi += 50;
				}
				else if (opCard.hao == RuleWeight.HAO_QING)
				{
					huXi += 40;
					haoXi += 40;
				}
				else if (opCard.hao == RuleWeight.HAO_NUI)
				{
					huXi += 30;
					haoXi += 30;
				}
				else if (opCard.hao == RuleWeight.HAO_WAI)
				{
					huXi += 20;
					haoXi += 20;
				}
			}

			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 == 1) {
				if (opCard.card1 != opCard.card2) {
					isDuzi = false;
				}
			}
			else 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_KAN)
			{
				kanXi += opCard.huXi;
			}
			else if (opCard.type == RuleWeight.TYPE_WEI)
			{
				waiXi += opCard.huXi;
			}
			else if (opCard.type == RuleWeight.TYPE_CHOUWEI)
			{
				waiXi += opCard.huXi;
			}
			else if (opCard.type == RuleWeight.TYPE_TI)
			{
				haoXi += opCard.huXi;
			}

			//hao
			if (this.config.getInt(Config.ROOM_CONFIG_HAO_CONFIG) == 0)
			{
				if (opCard.hao == RuleWeight.HAO_SHAN)
				{
					huXi += 40;
					haoXi += 40;
				}
				else if (opCard.hao == RuleWeight.HAO_QING)
				{
					huXi += 30;
					haoXi += 30;
				}
				else if (opCard.hao == RuleWeight.HAO_NUI)
				{
					huXi += 20;
					haoXi += 20;
				}
				else if (opCard.hao == RuleWeight.HAO_WAI)
				{
					huXi += 10;
					haoXi += 10;
				}
			}
			else {
				if (opCard.hao == RuleWeight.HAO_SHAN)
				{
					huXi += 50;
					haoXi += 50;
				}
				else if (opCard.hao == RuleWeight.HAO_QING)
				{
					huXi += 40;
					haoXi += 40;
				}
				else if (opCard.hao == RuleWeight.HAO_NUI)
				{
					huXi += 30;
					haoXi += 30;
				}
				else if (opCard.hao == RuleWeight.HAO_WAI)
				{
					huXi += 20;
					haoXi += 20;
				}
			}


			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;

		boolean mingtang = false;
		boolean duiFlag = true;
		do
		{
			int duziNum = 0;
			int siduiNum = 0;
			ArrayList<Integer> duiList = new ArrayList<>();
			duiList.addAll(player.cardInhand);
			for (int i = 0; i < player.tiGroup.size(); i++) {
				int card = player.tiGroup.get(i)[0];
				duiList.add(card);
				duiList.add(card);
				duiList.add(card);
				duiList.add(card);
			}
			for (int i = 0; i < player.kanGroup.size(); i++) {
				int card = player.kanGroup.get(i)[0];
				duiList.add(card);
				duiList.add(card);
				duiList.add(card);
			}

			if (duiList.size() < 19)
			{
				break;
			}

			EXPlayer activePlayer = (EXPlayer) player.room.playerMapBySeat.get(player.room.activeSeat);
			if (activePlayer != null & activePlayer.drawCard > 0) {
				duiList.add(activePlayer.drawCard);
			}

			Map<Integer, Integer> cardNumList = CardUtil.getCardNumMap(duiList);
			for (Entry<Integer, Integer> card : cardNumList.entrySet()) {
				if (card.getValue() == 2 || card.getValue() == 3) {
					duziNum++;
				}
				else if (card.getValue() == 4) {
					siduiNum++;
				}
			}

			if (duziNum + siduiNum == 0)
			{
				player.winFan += 120;
				player.mingtangList.add(new MingTang(RuleWeight.NO_DUI, 1, 120));
				mingtang = true;
				duiFlag = false;
				this.isTianHu = false;
				player.isDiHu = false;
			}
			else if (duziNum + siduiNum == 10 || duziNum + siduiNum*2 == 10)
			{
				player.winFan += 120;
				player.mingtangList.add(new MingTang(RuleWeight.SHI_DUI, 1, 120));
				mingtang = true;
				duiFlag = false;
				this.isTianHu = false;
				player.isDiHu = false;
			}
			else if (duziNum + siduiNum == 1 || duziNum + siduiNum*2 == 1)
			{
				if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
				{
					player.winFan += 100;
					player.mingtangList.add(new MingTang(RuleWeight.YI_DUI, 1, 100));
				}
				else {
					player.winFan += 120;
					player.mingtangList.add(new MingTang(RuleWeight.YI_DUI, 1, 120));
				}
				this.isTianHu = false;
				player.isDiHu = false;
				mingtang = true;
				duiFlag = false;
			}
			else if (duziNum + siduiNum == 9 || duziNum + siduiNum*2 == 9)
			{
				if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
				{
					player.winFan += 100;
					player.mingtangList.add(new MingTang(RuleWeight.JIU_DUI, 1, 100));
				}
				else {
					player.winFan += 120;
					player.mingtangList.add(new MingTang(RuleWeight.JIU_DUI, 1, 120));
				}
				this.isTianHu = false;
				player.isDiHu = false;
				mingtang = true;
				duiFlag = false;
			}
		} while(false);

		if (this.isTianHu) {
			if (player.seat == this.bankerSeat) {
				if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
				{
					player.winFan += 100;
					player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 100));
				}
				else {
					player.winFan += 120;
					player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 120));
				}

				mingtang = true;
			}
		}
		else {
			if (player.isDiHu)
			{
				if (player.seat == this.bankerSeat && config.getInt(Config.ROOM_CONFIG_ZHUANG_DI_HU) == 0)
				{
					if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
					{
						player.winFan += 100;
						player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 100));
					}
					else {
						player.winFan += 120;
						player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 120));
					}

					mingtang = true;
				}
				else if (player.seat != this.bankerSeat)
				{
					if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
					{
						player.winFan += 100;
						player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 100));
					}
					else {
						player.winFan += 120;
						player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 120));
					}

					mingtang = true;
				}
			}
		}

		if (duiFlag && !this.isTianHu && !player.isDiHu && player.isTingHu) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				player.winFan += 60;
				player.mingtangList.add(new MingTang(RuleWeight.TING_HU, 1, 60));
			}
			else {
				player.winFan += 80;
				player.mingtangList.add(new MingTang(RuleWeight.TING_HU, 1, 80));
			}

			mingtang = true;
		}

		if (this.card.getCount() == 0) {
			player.winFan += 30;
			player.mingtangList.add(new MingTang(RuleWeight.HAI_HU, 1, 30));
			mingtang = true;
		}

		if (player.cardInhand.size() == 1 && player.kanGroup.size() == 0 && player.tiGroup.size() == 0)
		{
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				player.winFan += 60;
				player.mingtangList.add(new MingTang(RuleWeight.QUAN_QIU_REN, 1, 60));
			}
			else {
				player.winFan += 80;
				player.mingtangList.add(new MingTang(RuleWeight.QUAN_QIU_REN, 1, 80));
			}

			mingtang = true;
		}

		//
		if (redCard == 0) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				int fan = 80;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.WU_HU, 1, fan));
			}
			else {
				int fan = 100;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.WU_HU, 1, fan));
			}

			mingtang = true;
		}
		//
		if (redCard == 1) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				int fan = 60;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.DIAN_HU, 1, fan));
			}
			else {
				int fan = 80;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.DIAN_HU, 1, fan));
			}

			mingtang = true;
		}

		if (redCard >= 13) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				int fan = 80;
				if (this.config.getInt(Config.ROOM_CONFIG_HAO_CONFIG) == 0)
				{
					fan += (redCard - 13) * 10;
				}
				else {
					fan += (redCard - 13) * 20;
				}
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.HONG_HU, 1, fan));
			}
			else {
				int fan = 100;
				if (this.config.getInt(Config.ROOM_CONFIG_HAO_CONFIG) == 0)
				{
					fan += (redCard - 13) * 10;
				}
				else {
					fan += (redCard - 13) * 20;
				}
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.HONG_HU, 1, fan));
			}

			mingtang = true;
		}

		if (smallCard <= 0) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				int fan = 100;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.DA_HU, 1, fan));
			}
			else {
				int fan = 120;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.DA_HU, 1, fan));
			}

			mingtang = true;
		}

		if (bigCard <= 0) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				int fan = 100;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.XIAO_HU, 1, fan));
			}
			else {
				int fan = 120;
				player.winFan += fan;
				player.mingtangList.add(new MingTang(RuleWeight.XIAO_HU, 1, fan));
			}

			mingtang = true;
		}

		//对子胡
		if (isDuzi) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				player.winFan += 100;
				player.mingtangList.add(new MingTang(RuleWeight.DUIZI_HU, 1, 100));
			}
			else {
				player.winFan += 120;
				player.mingtangList.add(new MingTang(RuleWeight.DUIZI_HU, 1, 120));
			}

			mingtang = true;
		}

		if (!isDuzi && huXiNum == 7) {
			if (this.config.getInt(Config.ROOM_CONFIG_MENG_TANG) == 0)
			{
				player.winFan += 60;
				player.mingtangList.add(new MingTang(RuleWeight.XING_XING_XI, 1, 60));
			}
			else {
				player.winFan += 80;
				player.mingtangList.add(new MingTang(RuleWeight.XING_XING_XI, 1, 80));
			}

			mingtang = true;
		}

		if (!mingtang)
		{
			player.huXi = huXi;
			player.total_huxi += player.huXi;
			player.winScore = player.huXi;
			int score = player.winScore;
			return score;
		}
		else
		{
			player.huXi = player.winFan + kanXi + waiXi + haoXi;
			player.total_huxi += player.huXi;
			player.winScore = player.huXi;
			int score = player.winScore;
			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;

		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() {
		this.getPlayBack().addResultData(EXMainServer.gameCtr.getRoomResultData(this));
		super.endGame();
	}

	@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();
		logger.info("jefe clear:"+this.isAllPass);
		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;
	}
}
