package extend.mj;

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

import com.game.Global;
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.ITObject;

import extend.mj.EXScore.HUScore;
import extend.mj.player.state.EXPlayerDisCardTipState;
import extend.mj.room.state.EXRoomSetpState;
import extend.mj.tip.Action;
import extend.mj.tip.TipManager;
import extend.mj.uitl.CardUtil;

public class EXRoom extends Room {

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

	public int						activeCard;
	public int						currenDiscardSeat;
	public int						lastDiscardSeat;
	public int						winCount			= 0;
	public boolean					liuju				= false;
	public int						tongpao				= 0;

	public boolean					activeHaidi			= false;
	public int						haidiCard;


	public OpenKong					activeKong			= null;

	public List<CardNiao>			niao;
	public int adminSeat                                = 0;


	public int						qsWinCount			= 0;

	public int						haidiCount			= 0;

	public int						piaoNiaoCount		= 0;
	public int						tianTingCount		= 0;
	private boolean					closeWinCallBack	= false;
	public int						fengdingScore = 0;
	public int						difen_score = 0;
	public int						niao_score = 0;


	public EXRoom(String roomid, Map<String, String> redis_room_map) {
		super(roomid, redis_room_map);

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_NO_JIANG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_NO_JIANG, true);
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_FENGDING_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_FENGDING_SCORE, 0);
		}
		else {
			int fengding_score = this.config.getInt(Config.ROOM_CONFIG_FENGDING_SCORE);
		    if (fengding_score == 0)
			{
				this.fengdingScore = 0;
			}
		    else if (fengding_score == 1)
			{
				this.fengdingScore = 35;
			}
			else if (fengding_score == 2)
			{
				this.fengdingScore = 40;
			}
			else if (fengding_score == 3)
			{
				this.fengdingScore = 45;
			}
		}

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

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

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_PIAO_NIAO_OPT))
		{
			this.config.putInt(Config.ROOM_CONFIG_PIAO_NIAO_OPT, 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);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_DIFEN_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_DIFEN_SCORE, 0);
			this.difen_score = 0;
		}
		else {
			this.difen_score = this.config.getInt(Config.ROOM_CONFIG_DIFEN_SCORE);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_NIAOFEN_SCORE))
		{
			this.config.putInt(Config.ROOM_CONFIG_NIAOFEN_SCORE, 0);
			this.niao_score = 0;
		} else {
			this.niao_score = this.config.getInt(Config.ROOM_CONFIG_NIAOFEN_SCORE);
		}

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

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

		card = new RoomCard(this);

		this.tipMap = new HashMap<Integer, TipManager>();
		this.actionList = new ArrayList<Action>();
		this.actionWinList = new ArrayList<Action>();
		this.niao = new ArrayList<CardNiao>();
		this.isEntrust=true;
		this.adminSeat = 0;
	}

	public void addAction(Action action) {
		addAction(action, true);
	}

	public void addAction(Action action, boolean check) {
		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) {
			boolean add = true;
			if (activeKong != null) {
				if (action.tip.weight < RuleWeight.WIN) {
					if (activeKong.tipActionSeat == -1) {
						activeKong.tipActionSeat = actionList.get(0).player.seat;
					}
					if (activeKong.tipActionSeat != -1) {
						int pos = Util.getPos(activeKong.kongSeat, action.player.seat, this.maxPlayers);
						int pos1 = Util.getPos(activeKong.kongSeat, activeKong.tipActionSeat, this.maxPlayers);
						if (pos < pos1) {
							actionList.clear();
							activeKong.tipActionSeat = action.player.seat;
						} else {
							add = false;
						}
					}
				}
			}
			if (add)
				this.actionList.add(action);
		}

		if (check)
			checkAction();
	}

	public void checkAction() {
		boolean isWeightest = this.isWeightestAction();
		if (!isWeightest) {
			return;
		}
		if (this.isAllPass()) {
			this.actionWinList.clear();
			EXPlayer activePlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
			if (activePlayer.getRoom().activeKong != null) {
				activePlayer.getRoom().activeKong = null;
				activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
				stateMachine.changeState(Global.getState(EXRoomSetpState.class));
			} else {
				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.tip.weight <= tip.getWeightest()) {
				return false;
			}
		}

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.stateMachine.curState instanceof EXPlayerDisCardTipState) {
				player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			}
		}
		return true;
	}

	public ITObject getReloadInfo(Player player) {
		ITObject data = super.getReloadInfo(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);
		if (this.activeKong != null) {
			data.putTObject("okinfo", this.activeKong.getTObject());
		}
		return data;
	}

	public boolean checkZhuangxian(EXPlayer destPlayer, EXPlayer fromPlayer, int type) {
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		if (zhuangxian) {
			if (destPlayer.seat == this.bankerSeat || fromPlayer.seat == this.bankerSeat) {
				return true;
			}
		}
		if (!zhuangxian)
			return true;
		return false;
	}

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

		if (type == EXScore.WIN) {
//			if (checkZhuangxian(destPlayer, fromPlayer, type)) {
//				score += 1;
//			}

			EXScore exscore = (EXScore) destPlayer.score;
			HUScore hu_score = exscore.getHuScore(fromPlayer);
			if (hu_score == null) {
				hu_score = new HUScore();
				hu_score.player = fromPlayer;
				exscore.hu_score.add(hu_score);
			}

			hu_score.score += score;
		}

		if (type == EXScore.WIN || type == EXScore.NIAO) {
			return;
		}

		destPlayer.score.round_log.put(type, destPlayer.score.round_log.get(type) + score);
		fromPlayer.score.round_log.put(type, fromPlayer.score.round_log.get(type) - score);

		destPlayer.score.round_score += score;
		destPlayer.score.total_score += score;

		fromPlayer.score.round_score -= score;
		fromPlayer.score.total_score -= score;

	}

	public void addQSScore(EXPlayer destPlayer, int score, int id, QSLog qs_log) {

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			int tem = score;
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			if (checkZhuangxian(destPlayer, fromPlayer, EXScore.QS)) {
				tem += 1;
			}
			EXScore exscore = (EXScore) destPlayer.score;
			HUScore hu_score = exscore.getHuScore(fromPlayer);
			if (hu_score == null) {
				hu_score = new HUScore();
				hu_score.player = fromPlayer;
				exscore.hu_score.add(hu_score);
			}
			int sum = 0;
			if (hu_score.qs_score_map.containsKey(id)) {
				sum = hu_score.qs_score_map.get(id);
			}
			sum += tem;
			hu_score.qs_score_map.put(id, sum);
			qs_log.score += tem;
			this.addScore(destPlayer, fromPlayer, tem, EXScore.QS);
		}
	}

	public void addAllScore(EXPlayer destPlayer, int socre, int type) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			this.addScore(destPlayer, fromPlayer, socre, type);
		}
	}

	public void addNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score) {
		int niao_type = config.getInt(Config.ROOM_CONFIG_NIAO_TYPE);
		HUScore hs = ((EXScore) destPlayer.score).getHuScore(fromPlayer);
		if (niao_type == Config.NIAO_TYPE_DOUBLE) {
			score = hs.score * (int) Math.round(Math.pow(2, hs.niao_double_num));
			hs.niao_double_num++;
		} else if (niao_type == Config.NIAO_TYPE_CS2NIAO) {
			score = hs.score;
		}
		hs.niao_score += score;
		this.addScore(destPlayer, fromPlayer, score, EXScore.NIAO);
	}

	public void addAllNiaoScore(EXPlayer destPlayer, int score) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			addNiaoScore(destPlayer, fromPlayer, score);
		}
	}

	public void addPiaoNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer) {
		int piao_niao = this.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		if (piao_niao == 0)
			return;
		this.addScore(destPlayer, fromPlayer, destPlayer.piaoNiao + fromPlayer.piaoNiao, EXScore.PIAO_NIAO);
	}

	public void addAllPiaoNiaoScore(EXPlayer destPlayer) {
		int piao_niao = this.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		if (piao_niao == 0)
			return;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(destPlayer.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			addPiaoNiaoScore(destPlayer, fromPlayer);
		}
	}

	public void niao_tongPao() {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		__zhuaNiao_Win(zhuangxian ? banker : dianPaoPlayer);
		if (niao.size() == 0)
			return;
		List<EXPlayer> winPlayerList = new ArrayList<EXPlayer>();
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1) {
				winPlayerList.add((EXPlayer) player);
			} else {
				player.win_count = 0;
			}
		}
		for (CardNiao cn : niao) {
			boolean not_niao = true;
			if (dianPaoPlayer.playerid == cn.playerId) {
				for (EXPlayer winer : winPlayerList) {
					this.addNiaoScore(winer, dianPaoPlayer, cn.score);
				}
				not_niao = false;
			} else {
				for (EXPlayer winer : winPlayerList) {
					if (winer.playerid == cn.playerId) {
						this.addNiaoScore(winer, dianPaoPlayer, cn.score);
						not_niao = false;
						break;
					}
				}
			}
			if (not_niao)
				cn.score = 0;
		}
		EXMainServer.gameCtr.sendNiaoEvt(dianPaoPlayer, dianPaoPlayer.seat);
	}

	public void niao_dianPao(EXPlayer winer) {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		__zhuaNiao_Win(zhuangxian ? banker : winer);
		if (niao.size() == 0)
			return;

		for (CardNiao cn : niao) {
			if (cn.playerId == winer.playerid || dianPaoPlayer.playerid == cn.playerId) {
				this.addNiaoScore(winer, dianPaoPlayer, cn.score);
			} else {
				cn.score = 0;
			}
		}

		EXMainServer.gameCtr.sendNiaoEvt(winer, winer.seat);
	}

	public void niao_selfWin(EXPlayer owner) {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		__zhuaNiao_Win(zhuangxian ? banker : owner);
		if (niao.size() == 0)
			return;

		for (CardNiao cn : niao) {
			boolean not_niao = true;
			if (cn.playerId == owner.playerid) {
				this.addAllNiaoScore(owner, cn.score);
				not_niao = false;
			} else {
				for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					if (player.playerid == cn.playerId) {
						this.addNiaoScore(owner, player, cn.score);
						not_niao = false;
						break;
					}
				}
			}
			if (not_niao)
				cn.score = 0;
		}

		EXMainServer.gameCtr.sendNiaoEvt(owner, owner.seat);
	}

	public void niao_qsWin(EXPlayer owner, int id, QSLog qs_log) {
		EXPlayer banker = (EXPlayer) this.playerMapBySeat.get(this.bankerSeat);
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		qs_log.niaoList.clear();
		for (int k = 0; k < 2; ++k) {
			int card = CardUtil.randomDiceNum();
			this.__zhuaNiao(zhuangxian ? banker : owner, qs_log.niaoList, card);
		}

		for (CardNiao cn : qs_log.niaoList) {
			boolean not_niao = true;
			if (cn.playerId == owner.playerid) {
				for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
					if (entry.getKey().equals(owner.playerid)) {
						continue;
					}
					EXPlayer fromPlayer = (EXPlayer) entry.getValue();
					__addQsWinNiao(owner, fromPlayer, id, qs_log);
				}
				not_niao = false;
			} else {
				for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					if (player.playerid == cn.playerId) {
						__addQsWinNiao(owner, player, id, qs_log);
						not_niao = false;
						break;
					}
				}
			}
			if (not_niao)
				cn.score = 0;
		}
	}

	public void banban_qsWin(EXPlayer owner, int id, QSLog qs_log) {
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			if (entry.getKey().equals(owner.playerid)) {
				continue;
			}
			EXPlayer fromPlayer = (EXPlayer) entry.getValue();
			__addQsWinNiao(owner, fromPlayer, id, qs_log);
		}
	}

	private void __addQsWinNiao(EXPlayer destPlayer, EXPlayer fromPlayer, int id, QSLog qs_log) {
		HUScore hs = ((EXScore) destPlayer.score).getHuScore(fromPlayer);
		int qs_score = hs.qs_score_map.get(id);
		qs_log.niao_score += qs_score;
		this.addScore(destPlayer, fromPlayer, qs_score, EXScore.QS);
	}

	private final void __zhuaNiao(EXPlayer owner, List<CardNiao> list, int card) {
		CardNiao cn = new CardNiao();
		cn.card = card;
		list.add(cn);
		int tem = card % 100;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			int pos = Util.getPos(owner.seat, entry.getValue().seat, this.maxPlayers);
			Player player = entry.getValue();
			boolean result = false;
			switch (pos) {
			case 0:
				result = tem == 1 || tem == 5 || tem == 9;
				break;
			case 1:
				if (this.maxPlayers == 2) {
					result = (tem == 3 || tem == 7);
				} else {
					result = (tem == 2 || tem == 6);
				}
				break;
			case 2:
				if (this.maxPlayers == 3) {
					result = (tem == 4 || tem == 8);
				} else {
					result = (tem == 3 || tem == 7);
				}
				break;
			case 3:
				result = (tem == 4 || tem == 8);
				break;
			}
			if (result) {
				cn.playerId = player.playerid;
				cn.score = this.niao_score;
			}
		}
	}

	private final void __zhuaNiao_Win(EXPlayer owner) {
		this.niao.clear();
		if (this.activeHaidi) {
			card.cardList.add(this.haidiCard);
		} else {
			if (card.cardList.size() == 0) {
				return;
			}
		}

		int niao_num = config.getInt(Config.ROOM_CONFIG_NIAO);
		for (int index = 0; index < niao_num; index++) {
			if (card.cardList.size() == 0) {
				break;
			}

			int card = this.card.pop();
			this.__zhuaNiao(owner, this.niao, card);
		}
	}

	private void winAction(EXPlayer owner, boolean tongpao) {
		for (Entry<Integer, Player> entry : playerMapById.entrySet()) {
			EXPlayer p = (EXPlayer) entry.getValue();
			if (p.winer != 1) {
				p.win_count = 0;
			}
		}
		if (tongpao) {
			niao_tongPao();
			this.bankerSeat = this.activeSeat;
		} else {
			niao_dianPao(owner);
			this.bankerSeat = owner.seat;
		}
		this.endGame();
	}

	public void winCallback(EXPlayer owner) {
		if (closeWinCallBack)
			return;
		if (this.tongpao > 1) {
			this.closeWinCallBack = true;
			List<TipManager> tlist = new ArrayList<TipManager>(this.tipMap.values());
			for (TipManager tipmgr : tlist) {
				tipmgr.winAction();
			}
			for (Action action : this.actionWinList) {
				action.run();
			}
			this.actionWinList.clear();
			// this.actionList.clear();
			winAction(owner, true);
			this.closeWinCallBack = false;
		} else {
			winAction(owner, false);
		}
	}

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

	@Override
	public void endGame() {
		boolean fengding = this.config.getBoolean(Config.ROOM_CONFIG_FENGDING);
		boolean four_win = this.config.getBoolean(Config.ROOM_CONFIG_FOUR_WIN);
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1) {
				List<HUScore> list = ((EXScore) player.score).hu_score;
				for (HUScore hu_score : list) {
					int hu_num = hu_score.score;
					int niao_num = hu_score.niao_score;
					if (four_win && player.win_count >= 4) {
						hu_num = hu_num * 2;
						niao_num = niao_num * 2;
					}
					int sum_score = hu_num + niao_num;
					EXPlayer destPlayer = player;
					EXPlayer fromPlayer = hu_score.player;

					destPlayer.score.round_log.put(EXScore.WIN, destPlayer.score.round_log.get(EXScore.WIN) + hu_num);
					fromPlayer.score.round_log.put(EXScore.WIN, fromPlayer.score.round_log.get(EXScore.WIN) - hu_num);
					destPlayer.score.round_log.put(EXScore.NIAO, destPlayer.score.round_log.get(EXScore.NIAO) + niao_num);
					fromPlayer.score.round_log.put(EXScore.NIAO, fromPlayer.score.round_log.get(EXScore.NIAO) - niao_num);
					//if (fengding) {
					//	if (sum_score > Config.FENGDING_SCORE) {
					//		sum_score = Config.FENGDING_SCORE;
					//		player.fengding = true;
					//	}
					//}
					if (fengding && this.fengdingScore > 0)
					{
						if (sum_score > this.fengdingScore) {
							sum_score = this.fengdingScore;
							player.fengding = true;
						}
					}
					destPlayer.score.round_score += sum_score;
					destPlayer.score.total_score += sum_score;
					fromPlayer.score.round_score -= sum_score;
					fromPlayer.score.total_score -= sum_score;
				}
			}
		}
		super.endGame();
	}

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

	@Override
	public void clear() {
		super.clear();
		this.liuju = false;
		this.haidiCard = 0;
		this.closeWinCallBack = false;
		this.tongpao = 0;
		this.activeSeat = this.lastDiscardSeat = this.currenDiscardSeat = 0;
		this.qsWinCount = this.haidiCount = this.winCount = this.piaoNiaoCount = 0;
		this.activeKong = null;
		this.activeHaidi = false;
		this.niao.clear();
		this.actionWinList.clear();
	}

}
