package extend.zp;

import java.util.*;
import java.util.Map.Entry;

import com.game.Global;
import com.game.Router;
import com.game.Util;
import com.game.data.Hp;
import com.game.data.Player;
import com.game.data.Room;
import com.game.player.state.PlayerWaitState;
import com.game.room.state.RoomDestoryGameState;
import com.game.room.state.RoomEndState;
import com.taurus.core.entity.*;
import com.taurus.core.plugin.redis.Redis;
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;
import extend.zp.uitl.WinCardNew;
import redis.clients.jedis.Jedis;

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 boolean isFanPao = false;

	public int tiandiflag = 0;

	public int baiXi = 0;

	public boolean pingju = false;

	public boolean yikuaibianflag = false;
	public boolean yikuaibiandes = false;

	public int daNiaoCount = 0;

	public int fangpaosubvalue = 0;// 放炮扣除的分数

	public boolean endflag = false;

	public boolean endrecord = false;
	public int daniaoflag = 0; // 当前是否参与打鸟事件

	public int step;

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

	public List<Integer> roomoutcardlist;

	public List<Integer> paochucardlist;

	public int typeface = 1;
	public boolean endsendflag = false;

	public List<int[]> weiRoomGroup; // 记录当前已经偎过的牌 为了确定放跑

	public ITObject paiXing;
	public int adminSeat;

	public boolean jiesanflag = false;

	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.weiRoomGroup = new ArrayList<>();
		this.paiXing = TObject.newInstance();
		this.throwCardsSet = new HashSet<>();
		this.roomoutcardlist = new ArrayList<>();
		this.paochucardlist = new ArrayList<>();
		this.throwCardsList = new ArrayList<Integer>();
		this.isEntrust = true;

		if (!this.config.containsKey(Config.ROOM_CONFIG_XIPAI_SCORE)) {
			this.config.putFloat(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() {
		//// System.out.println("isWeightestAction");
		if (this.isAllPass()) {
			return true;
		}
		if (this.actionList.size() == 0) {
			return false;
		}
		Action curaction = this.actionList.get(0);
		//// System.out.println("isWeightestAction2");
		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) {
				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 finalsettle() {
		int winid = 0;
		int maxCount = 0;// 最大赢家分
		int otherCount = 0; // 输家
		int allniaofen = 0; // 总鸟分
		int tmp_all_score = 0;

		int[] tmparrs = { 0, 0 };
		int i = 0;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			tmparrs[i] = player.total_huxi;
			allniaofen = allniaofen + player.niaoValue * player.daNiao;
		}
		Arrays.sort(tmparrs);
		maxCount = tmparrs[1];
		otherCount = tmparrs[0];

		// 四舍五入
		double maxc = 0;
		maxc = (double) maxCount / 10;
		maxCount = (int) Math.round(maxc) * 10;
		double maxo = 0;
		if (otherCount < 0) {
			maxo = (double) -otherCount / 10L;
			otherCount = -(int) Math.round(maxo) * 10;
		} else {
			maxo = (double) otherCount / 10L;
			otherCount = (int) Math.round(maxo) * 10;
		}

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();

			if (player.playerid == winid) {
				// 加分
				if (maxCount < otherCount + 10) {
					this.pingju = true;
					// 平局
					player.score.round_score = 0;// allniaofen*this.hpData.getInt("times");
					player.score.total_score = 0;// allniaofen*this.hpData.getInt("times");
					// player.score.round_log.put(EXScore.WIN,
					// player.score.round_log.get(EXScore.WIN) + player.score.round_score);
					player.hp.total_hp = 0;// allniaofen*this.hpData.getInt("times");
				} else {
					player.score.round_score = (maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
					player.score.total_score = (maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
					player.score.round_log.put(EXScore.WIN,
							player.score.round_log.get(EXScore.WIN) + player.score.round_score);
					player.hp.total_hp = (maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
				}

			} else {
				// 输分
				if (maxCount < otherCount + 10) {
					// player.score.round_log.put(EXScore.WIN,
					// player.score.round_log.get(EXScore.WIN) -(maxCount - otherCount +
					// allniaofen));
					player.score.round_score = 0;// - allniaofen*this.hpData.getInt("times");
					player.score.total_score = 0;// -allniaofen*this.hpData.getInt("times");
					player.hp.total_hp = 0;// -allniaofen*this.hpData.getInt("times");
				} else {
					player.score.round_log.put(EXScore.WIN,
							player.score.round_log.get(EXScore.WIN) - (maxCount - otherCount + allniaofen));
					player.score.round_score = -(maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
					player.score.total_score = -(maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
					player.hp.total_hp = -(maxCount - otherCount + allniaofen) * this.hpData.getInt("times");
				}

			}
		}
	}

	public int caclScore(EXPlayer player) {
		int redCard = 0;
		int bigCard = 0;
		int smallCard = 0;
		int huXi = 0;
		int huXiNum = 0;
		int huangFan = 0;
		int kuaibiannum = 0;
		int checkbn = 0;
		boolean mingtangflag = false;
		// 结算
		if (!player.handCardEx.isEmpty()) {
			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 (CardUtil.isRedCard(opCard.card3) && opCard.card3 != 0) {
				redCard++;
			}
			if (opCard.type == 2) {

				if (CardUtil.cardType(opCard.card3) == RuleWeight.BIG_CARD) {
					bigCard++;
				} else {
					smallCard++;
				}
			}
			if (CardUtil.isRedCard(opCard.card2) && CardUtil.isRedCard(opCard.card1)) {
				kuaibiannum++;
			}

			if (CardUtil.isRedCard(opCard.card2) || CardUtil.isRedCard(opCard.card1)
					|| CardUtil.isRedCard(opCard.card3)) {
				checkbn++;
			}

		}

		for (OpCard opCard : player.opCardList) {
			if (opCard.huXi != 0) {
				huXi += opCard.huXi;
				huXiNum++;
			}
			if (opCard.type == RuleWeight.TYPE_CHOW) {

				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++;
				}
				if (CardUtil.isRedCard(opCard.card1) && CardUtil.isRedCard(opCard.card2)
						&& CardUtil.isRedCard(opCard.card3)) {
					kuaibiannum++;
				}
				if (CardUtil.isRedCard(opCard.card1) || CardUtil.isRedCard(opCard.card2)
						|| CardUtil.isRedCard(opCard.card3)) {
					checkbn++;
				}

			} 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;
						kuaibiannum++;
						checkbn++;
					} else if (opCard.type == RuleWeight.TYPE_PAO || opCard.type == RuleWeight.TYPE_TI) {
						redCard += 4;
						kuaibiannum++;
						checkbn++;
					}
				}
				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 = 1;
		int score = 0;

		if (this.isTianHu) {
			// 天胡
			if (player.seat == this.bankerSeat) {
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.TIAN_HU, 1, 100));
			} else {
				// 地胡
				this.isDiHu = true;
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 100));
			}
		}
		// 判断房间是否有出牌

		if (player.getRoom().roomoutcardlist.size() == 1 && player.outcardList.size() == 0) {
			if (player.getRoom().activeCard == player.getRoom().roomoutcardlist.get(0)) {
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.DI_HU, 1, 100));
			}
		}
		if (this.isTianHu || this.isDiHu) {

		} else {
			// 看看十三红
			if (redCard >= 13) {
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.SHISANHONG_HU, 1, 100));
			}

			// 乌胡
			if (redCard == 0) {
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.WU_HU, 1, 100));
			}

			// 红胡
			if (redCard >= 10 && redCard < 13) {
				player.winFan = player.winFan * 2;
				player.mingtangList.add(new MingTang(RuleWeight.HONG_HU, 2, 2));
			}

			// 一点朱
			if (redCard == 1) {
				player.winFan = player.winFan * 2;
				player.mingtangList.add(new MingTang(RuleWeight.DIAN_HU, 2, 2));
			}
			// 一块扁
			if (kuaibiannum == 1 && redCard >= 2 && redCard <= 4 && checkbn == 1) {
				player.winFan = player.winFan * 2;
				this.yikuaibiandes = true;
				player.mingtangList.add(new MingTang(RuleWeight.YIKUAIBIAN_HU, 2, 2));
			}

			// 30胡
			if (huXi == 30) {
				mingtangflag = true;
				player.mingtangList.add(new MingTang(RuleWeight.KA30_HU, 1, 100));
			}

			// 20胡
			if (huXi == 20) {
				player.winFan = player.winFan * 2;
				player.mingtangList.add(new MingTang(RuleWeight.KA20_HU, 2, 2));
			}

			// 海底胡
			if (this.card.getCount() == 0 && !this.isFanPao) {
				player.winFan = player.winFan * 2;
				player.mingtangList.add(new MingTang(RuleWeight.HAIDI_HU, 2, 2));
			}

			// 自摸
			if (this.winSeat == player.seat && player.ispengchiwin == false && !this.isFanPao) {
				player.winFan = player.winFan * 2;
				player.mingtangList.add(new MingTang(RuleWeight.ZI_MO, 2, 2));
			}
		}

		if (player.mingtangList.size() > 0) {
			if (mingtangflag) {
				for (MingTang mingTang : player.mingtangList) {
					if (mingTang.type == 1) {
						// 计算息
						score = score + mingTang.score;
					}
				}
			}
		}
		if (mingtangflag) {
			if (player.winFan > 0) {
				score = score * player.winFan;
			}
		} else {
			if (player.winFan > 0) {
				score = huXi * player.winFan;
			} else {
				score = huXi;
			}
		}

		if (this.isFanPao) {
			this.fangpaosubvalue = score;
		}
		if (player.winer == 1) {
			player.cur_huxi = score; // 当前胡息
		}

		if (this.isFanPao) {
			score = Math.min(score, 100);
		} else {
			if (this.config.getInt(Config.GAME_FENGDING) == 1) {
				score = Math.min(score, 200);
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 2) {
				score = Math.min(score, 400);
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 3) {
				score = Math.min(score, 800);
			}
		}

		player.winScore = score;
		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 int caclScoreNew(EXPlayer player, int eventcard, boolean isjiang) {
		int huxi = 0;
		int tmpscore = 0;

		List<Integer> huxiScore = new ArrayList<>();

		WinCardNew winCard2 = new WinCardNew(player.cardInhand, eventcard, huxi, isjiang, player.opCardList);
		if (winCard2.checkHu()) {

			if (winCard2.opCardsList.size() == 0) {
				return 0;
			}
			List<List<MingTang>> st = new ArrayList<>();
			// 计算名堂
			player.mingtangListtmp.clear();
			int okop = 0;
			for (int i = 0; i < winCard2.opCardsList.size(); i++) {
				int score = 0;
				int redCard = 0;
				int kuaibiannum = 0;
				this.yikuaibianflag = false;
				if (winCard2.opCardsList.size() > 0) {
					for (int j = 0; j < winCard2.opCardsList.get(i).size(); j++) {
						OpCard tmpopcard = winCard2.opCardsList.get(i).get(j);

						huxi += tmpopcard.huXi;
						if (CardUtil.isRedCard(tmpopcard.card1)) {
							if (tmpopcard.type == RuleWeight.TYPE_JIANG) {
								redCard = redCard + 2;
								kuaibiannum++;
							}
							if (tmpopcard.type == RuleWeight.TYPE_TI || tmpopcard.type == RuleWeight.TYPE_PAO) {
								redCard = redCard + 4;
								kuaibiannum++;
							}
							if (tmpopcard.type == RuleWeight.TYPE_KAN || tmpopcard.type == RuleWeight.TYPE_CHOUWEI
									|| tmpopcard.type == RuleWeight.TYPE_WEI
									|| tmpopcard.type == RuleWeight.TYPE_PONG) {
								redCard = redCard + 3;
								kuaibiannum++;
							}
						}
						if (tmpopcard.type == RuleWeight.TYPE_CHOW) {
							if (CardUtil.isRedCard(tmpopcard.card1)) {
								redCard++;
							}
							if (CardUtil.isRedCard(tmpopcard.card2)) {
								redCard++;
							}
							if (CardUtil.isRedCard(tmpopcard.card3)) {
								redCard++;
							}
							if (CardUtil.isRedCard(tmpopcard.card1) || CardUtil.isRedCard(tmpopcard.card2)
									|| CardUtil.isRedCard(tmpopcard.card3)) {
								kuaibiannum++;
							}
						}

						//
					}

					player.winFan = 1;
					List<MingTang> tmpmt = new ArrayList<>();
					boolean mingtangflag = false;

					if (this.isTianHu) {
						// 天胡
						if (player.seat == this.bankerSeat) {
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.TIAN_HU, 1, 100));
						} else {
							// 地胡
							this.isDiHu = true;
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.DI_HU, 1, 100));
						}
					}
					// 判断房间是否有出牌

					if (player.getRoom().roomoutcardlist.size() == 1 && player.outcardList.size() == 0) {
						if (player.getRoom().activeCard == player.getRoom().roomoutcardlist.get(0)) {
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.DI_HU, 1, 100));
						}
					}
					if (this.isTianHu || this.isDiHu) {

					} else {
						// 看看十三红
						if (redCard >= 13) {
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.SHISANHONG_HU, 1, 100));
						}

						// 乌胡
						if (redCard == 0) {
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.WU_HU, 1, 100));
						}

						// 红胡
						if (redCard >= 10 && redCard < 13) {
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.HONG_HU, 2, 2));
						}

						// 一点朱
						if (redCard == 1) {
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.DIAN_HU, 2, 2));
						}
						// 一块扁
						if (redCard >= 2 && redCard <= 4 && kuaibiannum == 1) {
							this.yikuaibianflag = true;
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.YIKUAIBIAN_HU, 2, 2));
						}

						// 30胡
						if (huxi == 30) {
							mingtangflag = true;
							tmpmt.add(new MingTang(RuleWeight.KA30_HU, 1, 100));
						}

						// 20胡
						if (huxi == 20) {
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.KA20_HU, 2, 2));
						}

						// 海底胡
						if (this.card.getCount() == 0 && !this.isFanPao) {
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.HAIDI_HU, 2, 2));
						}

						// 自摸
						if (this.winSeat == player.seat && player.ispengchiwin == false && !this.isFanPao) {
							player.winFan = player.winFan * 2;
							tmpmt.add(new MingTang(RuleWeight.ZI_MO, 2, 2));
						}
					}

					if (tmpmt.size() > 0) {
						if (mingtangflag) {
							for (MingTang mingTang : tmpmt) {
								if (mingTang.type == 1) {
									// 计算息
									score = score + mingTang.score;
								}
							}
						}
					}
					if (mingtangflag) {
						if (player.winFan > 0) {
							score = score * player.winFan;
						}
					} else {
						if (player.winFan > 0) {
							score = huxi * player.winFan;
						} else {
							score = huxi;
						}
					}
					st.add(tmpmt);

					if (tmpscore < score && score > 0) {
						okop = i;
						tmpscore = score;
						player.tmpopCardList = winCard2.opCardsList.get(i);
						player.mingtangListtmp.clear();
						player.mingtangListtmp = st.get(i);
					}

					huxi = 0;
				}
			}

			if (tmpscore > 0) {
				player.mingtangListtmp = st.get(okop);
			}

			st.clear();
		}

		return tmpscore;
	}

	// 胜利操作
	public void winnew(EXPlayer player, EXPlayer activePlayer) {
		if (player.getRoom().drawCard != 0) {
			if (player.winCard == 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);
		// System.out.println("activePlayer.drawCard:"+activePlayer.drawCard);
		// 计算胡息

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

		boolean checkpaoflag = false;

		for (int i = 0; i < player.pongGroup.size(); i++) {

			if (activePlayer.drawCard == player.pongGroup.get(i)[0]
					&& !this.paochucardlist.contains(activePlayer.drawCard)) {
				player.kongCount++;
				WinCard win = new WinCard(player.cardInhand, 0);
				if (win.tryWin(player)) {
					checkpaoflag = true;
					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);
								if (opcard.card1 == opcard.card2 && opcard.card1 == opcard.card3
										&& opcard.card3 == opcard.card2) {
									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)) {
						checkpaoflag = true;
						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);
									if (opcard.card1 == opcard.card2 && opcard.card1 == opcard.card3
											&& opcard.card3 == opcard.card2) {
										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)) {
						checkpaoflag = true;
						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);
									if (opcard.card1 == opcard.card2 && opcard.card1 == opcard.card3
											&& opcard.card3 == opcard.card2) {
										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;
				}
			}
		}

		// System.out.println("scoreSrc:");
		// 记录小结算
		scoreSrc = caclScore(player);
		// System.out.println("scoreSrc:"+scoreSrc);

		// 用于标识一块扁
		int scoreSrcnew = caclScoreNew(player,
				checkpaoflag ? 0 : (this.activeCard == 0 ? activePlayer.drawCard : this.activeCard), false);
		if (scoreSrcnew == 0) {
			scoreSrcnew = caclScoreNew(player,
					checkpaoflag ? 0 : (this.activeCard == 0 ? activePlayer.drawCard : this.activeCard), true);
		}
		if (scoreSrcnew < scoreSrc || scoreSrcnew < 15) {
			scoreSrcnew = caclScoreNew(player,
					!checkpaoflag ? 0 : (this.activeCard == 0 ? activePlayer.drawCard : this.activeCard), false);
			if (scoreSrcnew == 0) {
				scoreSrcnew = caclScoreNew(player,
						!checkpaoflag ? 0 : (this.activeCard == 0 ? activePlayer.drawCard : this.activeCard), true);
			}
		}

		if (scoreSrcnew >= 15) {

			player.newcalc = 1;
			// if (scoreSrc == 0){
			// scoreSrc = scoreSrcnew;
			// player.mingtangList = player.mingtangListtmp;
			// }
			// if (this.yikuaibiandes==false&&this.yikuaibianflag){
			scoreSrc = scoreSrcnew;
			player.mingtangList = player.mingtangListtmp;
			// }
			// scoreSrc = scoreSrcnew;
			// player.mingtangList = player.mingtangListtmp;
			player.cur_huxi = scoreSrc; // 当前胡息
			player.opCardList = player.tmpopCardList;
			player.handCardEx.clear();
			player.handCard.clear();
			player.cardInhand.clear();
		} else {
			for (OpCard opCard : player.handCard) {
				player.opCardList.add(opCard);
			}
		}

		if (this.isFanPao) {
			scoreSrc = Math.min(scoreSrc, 100);
		} else {
			if (this.config.getInt(Config.GAME_FENGDING) == 1) {
				scoreSrc = Math.min(scoreSrc, 200);
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 2) {
				scoreSrc = Math.min(scoreSrc, 400);
			}
			if (this.config.getInt(Config.GAME_FENGDING) == 3) {
				scoreSrc = Math.min(scoreSrc, 800);
			}
		}
		if (this.isFanPao) {
			this.fangpaosubvalue = scoreSrc;
		}
		player.winScore = scoreSrc;
		player.cur_huxi = scoreSrc;
		this.liujuNum = 0;

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			if (fromPlayer.winer == 1) {
				fromPlayer.total_huxi = fromPlayer.total_huxi + scoreSrc;
			} else {
				fromPlayer.winer = 2;
				// 如果是放炮，则罚分
				if (this.isFanPao) {
					fromPlayer.total_huxi = fromPlayer.total_huxi - scoreSrc;
					fromPlayer.cur_huxi = -scoreSrc;
				}
			}

		}
		// 推送胡牌
		EXMainServer.gameCtr.winEvent(player, player.seat);

		this.bankerSeat = player.seat;
		player.winCount++;
		this.endGame();

	}

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

	public void addScoreInto(EXPlayer destPlayer, EXPlayer fromPlayer, int score, int type) {
		if (score <= 0) {
			return;
		}
		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;
		fromPlayer.score.round_score -= score;
		destPlayer.score.total_score += score;
		fromPlayer.score.total_score -= score;

	}

	public void finalScore() {

		int winid = 0;
		int maxCount = 0;// 最大赢家分
		int otherCount = 0; // 输家
		int allniaofen = 0; // 总鸟分
		int tmp_all_score = 0;

		int[] tmparrs = { 0, 0 };
		int i = 0;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			tmparrs[i] = player.total_huxi;
			allniaofen = allniaofen + player.niaoValue * player.daNiao;
			i++;
		}
		Arrays.sort(tmparrs);
		maxCount = tmparrs[1];
		otherCount = tmparrs[0];
		// 获取赢家
		if (maxCount == 0 && otherCount == 0) {
			return;
		}
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.total_huxi == maxCount) {
				player.winer = 1;
			}
		}
		// 四舍五入
		double maxc = 0;
		maxc = (double) maxCount / 10;
		maxCount = (int) Math.round(maxc) * 10;
		double maxo = 0;
		if (otherCount < 0) {
			maxo = (double) -otherCount / 10L;
			otherCount = -(int) Math.round(maxo) * 10;
		} else {
			maxo = (double) otherCount / 10L;
			otherCount = (int) Math.round(maxo) * 10;
		}
		EXPlayer destPlayer = null;
		EXPlayer fromPlayer = null;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1) {
				destPlayer = player;
			} else {
				fromPlayer = player;
			}
		}

		this.addScoreInto(destPlayer, fromPlayer, allniaofen + maxCount - otherCount, EXScore.WIN);
	}

	public void overHuxi() {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.total_huxi >= 100) {
				this.endflag = true;
				this.maxRound = this.round;
			}
		}
	}

	public void endGame() {
		// this.endflag = this.entrustTotal();
		// 如果是流局则：给庄家-10
		if (this.liuju) {
			for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
				EXPlayer player = (EXPlayer) entry.getValue();
				if (player.seat == this.bankerSeat) {
					player.total_huxi = player.total_huxi - 10;
					player.cur_huxi = -10;
				} else {
					player.cur_huxi = 0;
				}
			}
		}
		this.overHuxi();
		this.jiesanflag = true;
		// this.maxRound = this.round;

		super.endGame();
		if (!this.endrecord) {
			updateRecresult();
		}

	}

	/*
	 * 更新当前记录
	 */
	public void updateRecresult() {
		// System.out.println("updateRecresult");
		ITArray plist = TArray.newInstance();
		Jedis jedis5 = Redis.use("group1_db5").getJedis();
		try {
			TObject pdata;
			for (Iterator var10 = this.playerMapBySeat.entrySet().iterator(); var10.hasNext(); plist
					.addTObject(pdata)) {
				Map.Entry<Integer, Player> entry = (Map.Entry) var10.next();
				EXPlayer player = (EXPlayer) entry.getValue();
				pdata = TObject.newInstance();
				pdata.putUtfString("nick", player.nick);
				if (jiesanflag) {
					pdata.putInt("score", player.cur_huxi * 100);
					pdata.putInt("hp", player.cur_huxi * 100);
				} else {
					pdata.putInt("score", 0);
					pdata.putInt("hp", 0);
				}
				// player.cur_huxi = 0;
			}

			jedis5.hset(this.military_key, "round_" + this.round, plist.toJson());
		} finally {
			jedis5.close();
		}

	}

	public void saveRecRound() {
		if (this.getPlayBack() != null) {
			this.getPlayBack().addResultData(EXMainServer.gameCtr.getRoomResultData(this));
		}

		super.saveRecRound();

	}

	@Override
	protected void roomResult() {
		// 小结算
		if (!endsendflag) {
			endsendflag = true;
			EXMainServer.gameCtr.roomResult(this);
		}

	}

	@Override
	public void saveMilitaryTotal(boolean dissmiss) {

		this.endrecord = true;
		// 桌子结算时候调用
		this.finalScore();
		// 强行解散
		boolean settlement = this.redisUpdateHp();
		// 计算是否有发牌
		boolean isdrawCard = false;
		for (Entry<Integer, Player> entry : this.playerMapBySeat.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.cardInhand.size() != 0) {
				isdrawCard = true;
			}
		}
		if (this.round == 1 && !isdrawCard) {
			// 如果第一轮所有玩家没抓牌则不计入记录
		} else {
			super.saveMilitaryTotal(dissmiss);
			updateRecresult();
			EXMainServer.gameCtr.roomTotalResult(this, dissmiss);

		}
	}

	public PlayBack getPlayBack() {
		return (PlayBack) this.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();
		this.weiRoomGroup.clear();
		this.roomoutcardlist.clear();
		this.paochucardlist.clear();

		this.isFanPao = false;
		this.endflag = false;
		this.fangpaosubvalue = 0;
		this.yikuaibianflag = false;
	}
}
