package extend.mj;

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.player.state.EXPlayerDisCardTipState;
import extend.mj.tip.Action;
import extend.mj.tip.TipManager;
import extend.mj.uitl.WinCard;

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

public class EXRoom extends Room {

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

	public RoomCard					card;

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

	public ArrayList<CardNiao>		niao;

	public int						piaoNiaoCount;
	public int                      adminSeat  = 0;
	public int  					lastWinPlayerId = 0;
	public boolean 					isGengZhuang = false;
	public int						first_zhuang_card = 0;
	public int						geng_zhuan_num = 0;
	public int						gent_zhuan_nextSeat = 0;
	/*
	 * 上一次庄家座位号
	 */
	public int 						lastBankerSeat = 0;

	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.niao = new ArrayList<CardNiao>();

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

		if (!this.config.containsKey(Config.ROOM_CONFIG_QINGYISE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QINGYISE, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_PENGPENGHU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_PENGPENGHU, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_QIDUI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QIDUI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAOHUA_QIDUI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_HAOHUA_QIDUI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAOHUA2_QIDUI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_HAOHUA2_QIDUI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAOHUA3_QIDUI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_HAOHUA3_QIDUI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_GANGSHANGKAIHUA_2bei))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_GANGSHANGKAIHUA_2bei, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_SHISHANGYAO))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_SHISHANGYAO, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_TIANDIHU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_TIANDIHU, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_SHIBALUOHAN))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_SHIBALUOHAN, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_XIAOSHANYUAN))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIAOSHANYUAN, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_DASHANYUAN))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_DASHANYUAN, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_DASIXI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_DASIXI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_XIAOSIXI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIAOSIXI, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_LOUHU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_LOUHU, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_GUO_PENG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_GUO_PENG, false);
		}
		if (!this.config.containsKey(Config.ROOM_CONFIG_GUO_GONG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_GUO_GONG, false);
		}

		this.config.putBoolean(Config.ROOM_CONFIG_NONEALL, false);
		this.config.putBoolean(Config.ROOM_CONFIG_ZHUANGXIAN, false);
		this.config.putBoolean(Config.ROOM_CONFIG_QIANGKONG_NIAO, true);

		//可抢杠胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_QIANGKONG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QIANGKONG, false);
		}

		//可抢杠胡2倍
		if (!this.config.containsKey(Config.ROOM_CONFIG_QIANGKONG_2BEI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QIANGKONG_2BEI, false);
		}

		//明杠可抢
		if (!this.config.containsKey(Config.ROOM_CONFIG_QIANGKONG_MING))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QIANGKONG_MING, false);
		}

		//抢杠全包
		if (!this.config.containsKey(Config.ROOM_CONFIG_QG_TYPE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QG_TYPE, false);
		}
		
		//杠爆全包
		if (!this.config.containsKey(Config.ROOM_CONFIG_GANGBAO_TYPE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_GANGBAO_TYPE, false);
		}

		this.config.putBoolean(Config.ROOM_CONFIG_HZ_HU, false);

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

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

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

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

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

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

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

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

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

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

		////幺九
		if (!this.config.containsKey(Config.ROOM_CONFIG_YAO_JIU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_YAO_JIU, false);
		}

		//清幺九
		if (!this.config.containsKey(Config.ROOM_CONFIG_QING_YAO_JIU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QING_YAO_JIU, false);
		}

		//有没有癞子
		if (!this.config.containsKey(Config.ROOM_CONFIG_LAIZI))
		{
			this.config.putInt(Config.ROOM_CONFIG_LAIZI, 0);
		}

		//四鬼胡牌
		if (!this.config.containsKey(Config.ROOM_CONFIG_LAIZI4_HU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_LAIZI4_HU, false);
		}

		//四鬼胡牌两倍
		if (!this.config.containsKey(Config.ROOM_CONFIG_LAIZI4_HU_2BEI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_LAIZI4_HU_2BEI, false);
		}

		//全风
		if (!this.config.containsKey(Config.ROOM_CONFIG_QUAN_FENG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QUAN_FENG, false);
		}

		////12张落地全包
		if (!this.config.containsKey(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO, false);
		}

		////留够马牌
		if (!this.config.containsKey(Config.ROOM_CONFIG_LIU_GOU_MA))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_LIU_GOU_MA, false);
		}

		//全求人
		if (!this.config.containsKey(Config.ROOM_CONFIG_QUAN_FENG_SCORE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_QUAN_FENG_SCORE, false);
		}

		// ROOM_CONFIG_han19 = "han19"; //含19即可
		if (!this.config.containsKey(Config.ROOM_CONFIG_han19))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_han19, false);
		}

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


		//大胡相乘
		if (!this.config.containsKey(Config.ROOM_CONFIG_DAHU_CHENG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_DAHU_CHENG, false);
		}

		//鸡胡不能吃胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_JIHU_NOTCHIHU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_JIHU_NOTCHIHU, false);
		}

		//小胡的倍数另算
		if (!this.config.containsKey(Config.ROOM_CONFIG_XIAOHU_BEI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_XIAOHU_BEI, false);
		}

		//流局两倍
		if (!this.config.containsKey(Config.ROOM_CONFIG_LIUJU_2BEI))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_LIUJU_2BEI, false);
		}

		//荒庄荒杠
		if (!this.config.containsKey(Config.ROOM_CONFIG_HUANGZHUANGHUANGGANG))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_HUANGZHUANGHUANGGANG, false);
		}

		//潮州特殊自摸
		if (!this.config.containsKey(Config.ROOM_CONFIG_SPECAIL_ZIMO))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_SPECAIL_ZIMO, false);
		}

		//必胡，必须胡牌，不能过
		if (!this.config.containsKey(Config.ROOM_CONFIG_MUST_HU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_MUST_HU, false);
		}

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

		//买马,提前预定马，猜谁胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_MAI_NIAO))
		{
			this.config.putInt(Config.ROOM_CONFIG_MAI_NIAO, 0);
		}

		//买马值
		if (!this.config.containsKey(Config.ROOM_CONFIG_MAI_NIAO_OPT))
		{
			this.config.putInt(Config.ROOM_CONFIG_MAI_NIAO_OPT, 0);
		}

		if (!this.config.containsKey(Config.ROOM_CONFIG_MA_GEN_DI_FEN))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_MA_GEN_DI_FEN, 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_OPT))
		{
			this.config.putInt(Config.ROOM_CONFIG_NIAO_OPT, 0);
		}

		//10倍不计分
		if (!this.config.containsKey(Config.ROOM_CONFIG_10_BEI_BUJIFEN))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_10_BEI_BUJIFEN, false);
		}

		//封顶十倍
		if (!this.config.containsKey(Config.ROOM_CONFIG_10_BEI_FENGDING))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_10_BEI_FENGDING, false);
		}

		//自动胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_ZI_DONG_HU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_ZI_DONG_HU, false);
		}

		//海底捞月
		//自动胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_HAI_DI_NAO_YUE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_HAI_DI_NAO_YUE, false);
		}

		////使用默认基础胡分
		if (!this.config.containsKey(Config.ROOM_CONFIG_USE_BASE_HU_SCORE))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_USE_BASE_HU_SCORE, false);
		}

		//门清
		if (!this.config.containsKey(Config.ROOM_CONFIG_MENG_QING))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_MENG_QING, false);
		}

		//没有鸡胡，统一叫平胡
		if (!this.config.containsKey(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU, false);
		}

		//杠上炮2倍
		if (!this.config.containsKey(Config.ROOM_CONFIG_GANGSHANGPAO_2bei))
		{
			this.config.putBoolean(Config.ROOM_CONFIG_GANGSHANGPAO_2bei, false);
		}

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

		if (this.config.getInt(Config.YI_JIAO_LAI_YOU) == 2)
		{
			this.config.putInt(Config.ROOM_CONFIG_LAIZI, 0);
		}

		if (!this.config.containsKey(Config.LAIYOU_DIFEN)) {
			this.config.putInt(Config.LAIYOU_DIFEN, 1);
		}
		else {
			this.config.putInt(Config.LAIYOU_DIFEN, this.config.getInt(Config.LAIYOU_DIFEN)+1);
		}

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

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

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

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

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

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

		this.isEntrust=true;
		this.adminSeat = 0;
		this.lastWinPlayerId =0;

		this.first_zhuang_card = 0;
		this.geng_zhuan_num = 0;
		this.gent_zhuan_nextSeat = 0;

		if (this.config.getBoolean(Config.ROOM_CONFIG_GENGZHUAN))
		{
			this.isGengZhuang = true;
		}

		this.lastBankerSeat = 0;
		this.last_liuju = false;

		//初始化房间的特殊参数
		WinCardType.InitRoomSpecialConfig(this);
	}

	public boolean isLiuJu()
	{
		boolean liugouma = this.config.getBoolean(Config.ROOM_CONFIG_LIU_GOU_MA);
		if (liugouma)
		{
			int num = getNiaoNum(0, true);
			if (card.cardList.size() <= num)
			{
				return true;
			}
		}
		else
		{
			if (card.cardList.size() == 0)
			{
				return true;
			}
		}

	    return false;
	}

	public void endGameByLiuJu()
	{
		if (config.getBoolean(Config.ROOM_CONFIG_HUANGZHUANGHUANGGANG))
		{
			for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
				EXPlayer player = (EXPlayer) entry.getValue();
				int score = player.score.round_log.get(EXScore.KONG);
				player.score.round_score -= score;
				player.score.total_score -= score;
				player.score.round_log.put(EXScore.KONG, 0);
			}
		}
		liuju = true;
		clear_repeatwin();

		endGame();
	}

	public void endGame()
	{
		last_liuju = liuju;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer != 1)
			{
				player.last_winer = 0;
				player.repeat_win = -1;
			}
			else
			{
				player.last_winer = 1;
			}
		}
		super.endGame();
	}

	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 boolean isLaizi(int card) {
		return this.card.isLaizi(card);
	}

	public boolean isFanPai(int card) {
		return this.card.isFanPai(card);
	}

	public void checkAction() {
		boolean isWeightest = this.isWeightestAction();
		if (!isWeightest) {
			return;
		}
		if (this.isAllPass()) {
			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.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;

	}

	/**
	 * 获取房间信息
	 *
	 * @return
	 */
	public ITObject getRoomInfo() {
		ITObject data = super.getRoomInfo();
		if (data != null)
		{
			data.putInt("laiziCard", this.card.laiziCard);
			data.putInt("laiziCard2", this.card.laiziCard2);
			data.putInt("laiziCardBefore", this.card.laiziCardBefore);
			data.putInt("laiziCard2Before", this.card.laiziCard2Before);
		}

		return data;
	}

	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);
		return data;
	}

	public void addScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score, int type) {
		boolean zhuangxian = this.config.getBoolean(Config.ROOM_CONFIG_ZHUANGXIAN);
		if(type == EXScore.WIN) {
			if (zhuangxian) {
				if (destPlayer.seat == this.bankerSeat || fromPlayer.seat == this.bankerSeat) {
					score += 1;
				}
			}
		}

		if (type == EXScore.WIN || type == EXScore.NIAO)
		{
			if (fromPlayer.tinghu10bei)
			{
				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 addMaGengGongScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score) {
		this.addScore(destPlayer, fromPlayer, score, EXScore.MA_GENG_GONG);
	}

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

	public int GetGongScore(EXPlayer destPlayer)
	{
		return destPlayer.score.round_log.get(EXScore.KONG);
	}

	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 addMaiNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer, int score) {
		this.addScore(destPlayer, fromPlayer, score, EXScore.MAI_NIAO);
	}

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

	public void addNiaoScore(EXPlayer destPlayer, EXPlayer fromPlayer, int 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) {
		boolean piao_niao = this.config.getBoolean(Config.ROOM_CONFIG_PIAO_NIAO);
		if(!piao_niao)return;
		this.addScore(destPlayer, fromPlayer,destPlayer.piaoNiao + fromPlayer.piaoNiao , EXScore.PIAO_NIAO);
	}

	public void addAllPiaoNiaoScore(EXPlayer destPlayer) {
		boolean piao_niao = this.config.getBoolean(Config.ROOM_CONFIG_PIAO_NIAO);
		if(!piao_niao)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 CountMaGengGongScore() {
		boolean maGenGong = config.getBoolean(Config.ROOM_CONFIG_MA_GEN_GONG);
		if (maGenGong)
		{
			for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
				EXPlayer player = (EXPlayer) entry.getValue();
				if (player.winer == 1 && player.niaoCount > 0)
				{
				    for(int i = 0; i < player.opCardList.size(); i++)
					{
						OpCard op_card = player.opCardList.get(i);
						//明杠
						if (op_card.type == RuleWeight.TYPE_KONG && op_card.card_seat > 0)
						{
						    EXPlayer gangPlayer = (EXPlayer) this.playerMapBySeat.get(op_card.card_seat);
						    if (gangPlayer != null)
							{
							    addMaGengGongScore(player, gangPlayer, Config.MING_GANG_SCORE * player.niaoCount);
							}
						}
						//补杠
						else if (op_card.type == RuleWeight.TYPE_KONG && op_card.card_seat == 0)
						{
							addAllMaGengGongScore(player, Config.BU_GANG_SCORE * player.niaoCount);
						}
						//暗杠
						else if (op_card.type == RuleWeight.TYPE_SELF_KONG)
						{
							addAllMaGengGongScore(player, Config.AN_GANG_SCORE * player.niaoCount);
						}
					}
				}
			}
		}
	}

	private int getNiaoNum(int card, boolean zimo) {
		int niao_num = 0;
		int niao = this.config.getInt(Config.ROOM_CONFIG_NIAO);
		switch (niao) {
		case Config.NIAO_TYPE_WU:
			niao_num = 0;
			break;
		case Config.NIAO_TYPE_159:
			int opt = this.config.getInt(Config.ROOM_CONFIG_NIAO_OPT);
			niao_num = opt * 2;
			break;
		case Config.NIAO_TYPE_ALL:
			niao_num = 1;
			break;
		case Config.NIAO_TYPE_ZHUAMA:
			if (zimo)
			{
				niao_num = 2;
			}
			else
			{
				niao_num = 1;
			}
		    break;
		}
		return niao_num;
	}

	public void handle_repeatwin(EXPlayer owner)
	{
		if (owner.last_winer == 1)
		{
			owner.repeat_win += 1;
		}
		else
		{
			owner.repeat_win += 1;
		}
	}

	public void clear_repeatwin()
	{
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			player.repeat_win = -1;
			player.last_winer = 0;
		}
	}

	public void niao_tongPao(int card) {
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);

		__zhuaNiao(dianPaoPlayer, getNiaoNum(card, false), true);

		List<EXPlayer> winPlayerList = new ArrayList<EXPlayer>();
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			Player player = entry.getValue();
			if (player.winer == 1) {
				((EXPlayer)player).niaoCount = 0;
				winPlayerList.add((EXPlayer) player);
			}
		}

		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		for (CardNiao cn : niao) {
			if (dianPaoPlayer.playerid == cn.playerId) {
				if (zhua_ma)
				{
					dianPaoPlayer.niaoCount ++;
					cn.score = 0;
					for (EXPlayer winer : winPlayerList) {
						int score = winer.hu_score;
						cn.score += score;
						this.addNiaoScore(winer, dianPaoPlayer, score);
					}
				}
				else
				{
					dianPaoPlayer.niaoCount ++;
					//爆炸马 加倍的情况
					if (dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
					{
						int real_score = 0;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score * winer.hu_score;
							real_score += score;
							this.addNiaoScore(winer, dianPaoPlayer, score);
						}

						cn.score = real_score;
					}
					else if (dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
					{
						int real_score = 0;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score;
							real_score += score;
							this.addNiaoScore(winer, dianPaoPlayer, score);
						}

						cn.score = real_score;
					}
				}
			} else {
				if (zhua_ma)
				{
					int real_score = 0;
					for (EXPlayer winer : winPlayerList) {
						if (winer.playerid == cn.playerId) {
							int score = winer.hu_score;
							real_score += score;
							this.addNiaoScore(winer, dianPaoPlayer, score);
							winer.niaoCount ++;
							break;
						}
					}
					cn.score = real_score;
				}
				//爆炸马 加倍的情况
				else if (dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1) {
				}
				else if (dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && dianPaoPlayer.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0) {
                }
				else
				{
					for (EXPlayer winer : winPlayerList) {
						if (winer.playerid == cn.playerId) {
							int score = cn.score;
							this.addNiaoScore(winer, dianPaoPlayer, score);
							winer.niaoCount ++;
							break;
						}
					}
				}
			}
		}

		CountMaGengGongScore();

		EXMainServer.gameCtr.sendNiaoEvt(dianPaoPlayer, dianPaoPlayer.seat);

		dianpao_countMaiMa(dianPaoPlayer);
	}

	public void niao_tongPao_quanbao(int card, boolean quanbao, boolean zimo) {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);

		__zhuaNiao(dianPaoPlayer, getNiaoNum(card, zimo), true);

		List<EXPlayer> winPlayerList = new ArrayList<EXPlayer>();
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			Player player = entry.getValue();
			if (player.winer == 1) {
				((EXPlayer)player).niaoCount = 0;
				winPlayerList.add((EXPlayer) player);
			}
		}

		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		if (quanbao)
		{
			for (CardNiao cn : niao) {
				if (zhua_ma)
				{
					cn.score = 0;
					for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
						EXPlayer player = (EXPlayer)entry.getValue();
						if (player.playerid == cn.playerId) {
							player.niaoCount ++;
							if (player.winer == 1)
							{
								int score = player.hu_score;
								score = score * (this.maxPlayers - 1);
								cn.score += score;
								this.addNiaoScore(player, dianPaoPlayer, score);
							}
							else
							{
								for (EXPlayer winer : winPlayerList) {
									int score = winer.hu_score;
									score = score * (this.maxPlayers - 1);
									cn.score += score;
									this.addNiaoScore(player, dianPaoPlayer, score);
								}
							}
							break;
						}
					}
				}
				//爆炸马 加倍的情况
				else if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
				{
					if (dianPaoPlayer.playerid == cn.playerId)
					{
						dianPaoPlayer.niaoCount ++;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score * winer.hu_score;
							score = score * (this.maxPlayers - 1);
							this.addNiaoScore(winer, dianPaoPlayer, score);
						}
					}
				}
				else if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
				{
					if (dianPaoPlayer.playerid == cn.playerId)
					{
						dianPaoPlayer.niaoCount ++;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score;
							score = score * (this.maxPlayers - 1);
							this.addNiaoScore(winer, dianPaoPlayer, score);
						}
					}
				}
				else
				{
					for (EXPlayer winer : winPlayerList) {
						if (winer.playerid == cn.playerId) {
							int score = winer.hu_score;
							score = score * (this.maxPlayers - 1);
							this.addNiaoScore(winer, dianPaoPlayer, score);
							winer.niaoCount ++;
							break;
						}
					}
				}
			}
		}
		else
		{
			for (CardNiao cn : niao) {
				if (zhua_ma)
				{
					cn.score = 0;
					for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
						EXPlayer player = (EXPlayer)entry.getValue();
						if (player.playerid == cn.playerId) {
							player.niaoCount ++;
							if (player.winer == 1)
							{
								int score = player.hu_score;
								cn.score += score;
								this.addAllNiaoScore(player, score);
							}
							else
							{
								for (EXPlayer winer : winPlayerList) {
									int score = winer.hu_score;
									cn.score += score;
									this.addNiaoScore(winer, player, score);
								}
							}
							break;
						}
					}
				}
				//爆炸马 加倍的情况
				else if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
				{
					if (dianPaoPlayer.playerid == cn.playerId)
					{
						dianPaoPlayer.niaoCount ++;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score * winer.hu_score;
							this.addAllNiaoScore(winer, score);
						}
					}
				}
				else if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
				{
					if (dianPaoPlayer.playerid == cn.playerId)
					{
						dianPaoPlayer.niaoCount ++;
						for (EXPlayer winer : winPlayerList) {
							int score = cn.score;
							this.addAllNiaoScore(winer, score);
						}
					}
				}
				else
				{
					for (EXPlayer winer : winPlayerList) {
						if (winer.playerid == cn.playerId) {
							int score = cn.score;
							this.addAllNiaoScore(winer, score);
							winer.niaoCount ++;
							break;
						}
					}
				}
			}
		}

		CountMaGengGongScore();

		EXMainServer.gameCtr.sendNiaoEvt(dianPaoPlayer, dianPaoPlayer.seat);

		dianpao_countMaiMa(dianPaoPlayer);
	}

	public void niao_dianPao(EXPlayer owner, int card) {
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer winer = owner;
		EXRoom room = owner.getRoom();
		int num = getNiaoNum(card, false);
		__zhuaNiao(owner, num, false);

		for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
			((EXPlayer)entry.getValue()).niaoCount = 0;
		}

		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		for (CardNiao cn : niao) {
			if (cn.playerId == winer.playerid || dianPaoPlayer.playerid == cn.playerId) {
				if (cn.playerId == winer.playerid) {
					winer.niaoCount++;
				}
				else if (dianPaoPlayer.playerid == cn.playerId)
				{
					dianPaoPlayer.niaoCount++;
				}

				if (zhua_ma)
				{
					this.addNiaoScore(winer, dianPaoPlayer, cn.score);
				}
				//爆炸马 加倍的情况
				else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
				{
					this.addNiaoScore(winer, dianPaoPlayer, cn.score);
				}
				else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
				{
					this.addNiaoScore(winer, dianPaoPlayer, cn.score);
				}
				else
				{
				    this.addNiaoScore(winer, dianPaoPlayer, cn.score);
				}
			}
		}

		CountMaGengGongScore();

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

	public void niao_dianPao_quanbao(EXPlayer owner, int card, boolean quanbao, boolean zimo) {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		EXPlayer dianPaoPlayer = (EXPlayer) this.playerMapBySeat.get(this.activeSeat);
		EXPlayer winer = owner;
		int num = getNiaoNum(card, zimo);
		__zhuaNiao(owner, num, false);

		for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
			((EXPlayer)entry.getValue()).niaoCount = 0;
		}

		if (quanbao == false && zimo == true)
		{
			if (config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && get12ZhangLuoDi(owner) != null)
			{
				EXPlayer badPlayer = get12ZhangLuoDi(owner);
				dianPaoPlayer = badPlayer;
				quanbao = true;
			}
		}

		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		if (quanbao)
		{
			for (CardNiao cn : niao) {
				if (cn.playerId == owner.playerid) {
					if (zhua_ma) {
						this.addNiaoScore(owner, dianPaoPlayer,cn.score * (this.maxPlayers - 1));
					}
					//爆炸马 加倍的情况
					else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1) {
						this.addNiaoScore(owner, dianPaoPlayer,cn.score * (this.maxPlayers - 1));
					} else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0) {
						this.addNiaoScore(owner, dianPaoPlayer,cn.score * (this.maxPlayers - 1));
					} else {
						this.addNiaoScore(owner, dianPaoPlayer,cn.score * (this.maxPlayers - 1));
					}

					owner.niaoCount++;
				} else {
					if (zhua_ma) {
						for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
							EXPlayer player = (EXPlayer) entry.getValue();
							if (player.playerid == cn.playerId) {
								player.niaoCount++;
								this.addNiaoScore(owner, dianPaoPlayer, cn.score);
								break;
							}
						}
					}
				}
			}
		}
		else
		{
			for (CardNiao cn : niao) {
				if (cn.playerId == owner.playerid) {
					if (zhua_ma) {
						this.addAllNiaoScore(owner, cn.score);
					}
					//爆炸马 加倍的情况
					else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
					{
						this.addAllNiaoScore(owner, cn.score);
					}
					else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
					{
						this.addAllNiaoScore(owner, cn.score);
					}
					else
					{
						this.addAllNiaoScore(owner, cn.score);
					}

					owner.niaoCount++;
				} else {
					if (zhua_ma) {
						for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
							EXPlayer player = (EXPlayer) entry.getValue();
							if (player.playerid == cn.playerId) {
								player.niaoCount ++;
								this.addNiaoScore(owner, player, cn.score);
								break;
							}
						}
					}
				}
			}
		}

		CountMaGengGongScore();

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

	public void niao_selfWin(EXPlayer owner, int card) {
		if (this.config.getInt(Config.ROOM_CONFIG_NIAO) == 0) {
			return;
		}
		int num = getNiaoNum(card, true);

		__zhuaNiao(owner, num, false);

		for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
			((EXPlayer)entry.getValue()).niaoCount = 0;
		}

		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		for (CardNiao cn : niao) {
			if (cn.playerId == owner.playerid) {
				if (zhua_ma)
				{
					if (this.config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && this.get12ZhangLuoDi(owner) != null)
					{
						EXPlayer badPlayer = this.get12ZhangLuoDi(owner);
						this.addScore(owner,badPlayer, cn.score * (this.maxPlayers - 1), EXScore.WIN);
					}
					else
					{
						this.addAllNiaoScore(owner, cn.score);
					}
				}
				//爆炸马 加倍的情况
				else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 1)
				{
					if (this.config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && this.get12ZhangLuoDi(owner) != null)
					{
						EXPlayer badPlayer = this.get12ZhangLuoDi(owner);
						this.addScore(owner,badPlayer, cn.score * (this.maxPlayers - 1), EXScore.WIN);
					}
					else
					{
						this.addAllNiaoScore(owner, cn.score);
					}
				}
				else if (owner.room.config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL && owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT) == 0)
				{
					if (this.config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && this.get12ZhangLuoDi(owner) != null)
					{
						EXPlayer badPlayer = this.get12ZhangLuoDi(owner);
						this.addScore(owner,badPlayer, cn.score * (this.maxPlayers - 1), EXScore.WIN);
					}
					else
					{
						this.addAllNiaoScore(owner, cn.score);
					}
				}
				else
				{
					if (this.config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && this.get12ZhangLuoDi(owner) != null)
					{
						EXPlayer badPlayer = this.get12ZhangLuoDi(owner);
						this.addScore(owner,badPlayer, cn.score * (this.maxPlayers - 1), EXScore.WIN);
					}
					else
					{
						this.addAllNiaoScore(owner, cn.score);
					}
				}
				owner.niaoCount++;
			} else {
				if (zhua_ma)
				{
					for (Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
						EXPlayer player = (EXPlayer) entry.getValue();
						if (player.playerid == cn.playerId) {
							player.niaoCount ++;
							if (this.config.getBoolean(Config.ROOM_CONFIG_12ZHANG_LUODI_QUANBAO) && this.get12ZhangLuoDi(owner) != null) {
								EXPlayer badPlayer = this.get12ZhangLuoDi(owner);
								this.addNiaoScore(owner, badPlayer, cn.score);
							}
							else {
								this.addNiaoScore(owner, player, cn.score);
							}
							break;
						}
					}
				}
			}
		}

		CountMaGengGongScore();

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

		zimo_countMaiMa(owner);
	}

	private void dianpao_countMaiMa(EXPlayer diaoPaoPlayer)
	{
		if (this.config.getInt(Config.ROOM_CONFIG_MAI_NIAO) <= 0)
		{
			return;
		}

		int winer_num = 0;
		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.winer == 1)
			{
			    winer_num++;
			}
		}

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.playerid != diaoPaoPlayer.playerid)
			{
				for (Entry<Integer, Player> win_entry : this.playerMapById.entrySet()) {
					EXPlayer win_player = (EXPlayer) win_entry.getValue();
					if (win_player.winer == 1)
					{
						int seat = 0;
						if (winer_num == 1)
						{
							seat = this.lastBankerSeat;
						}
						else if (winer_num > 1)
						{
						    seat = diaoPaoPlayer.seat;
						}

						int zhong_ma_num = countPlayerMaiMaZhongMaNum(player, win_player, seat);
						if (zhong_ma_num > 0)
						{
							addMaiNiaoScore(player, diaoPaoPlayer, win_player.hu_score*zhong_ma_num);
						}
					}
				}
			}

			if (player.winer != 1)
			{
				int seat = 0;
				if (winer_num == 1)
				{
					seat = this.lastBankerSeat;
				}
				else if (winer_num > 1)
				{
					seat = diaoPaoPlayer.seat;
				}

				int zhong_ma_num = countPlayerMaiMaZhongMaNum(player, diaoPaoPlayer, seat);
				if (zhong_ma_num > 0)
				{
					for (Entry<Integer, Player> win_entry : this.playerMapById.entrySet()) {
						EXPlayer win_player = (EXPlayer) win_entry.getValue();
						if (win_player.winer == 1) {
							addMaiNiaoScore(win_player, player, win_player.hu_score*zhong_ma_num);
						}
					}
				}
			}
		}

		EXMainServer.gameCtr.sendMaiNiaoEvt(diaoPaoPlayer, diaoPaoPlayer.seat);
	}

	private void zimo_countMaiMa(EXPlayer owner)
	{
		if (this.config.getInt(Config.ROOM_CONFIG_MAI_NIAO) <= 0)
		{
			return;
		}

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (owner.playerid == player.playerid)
			{
			    int zhong_ma_num = countPlayerMaiMaZhongMaNum(owner, owner, this.lastBankerSeat);
			    if (zhong_ma_num > 0)
				{
					addAllMaiNiaoScore(owner, owner.hu_score*zhong_ma_num);
				}
			}
			else if (player.winer != 1)
			{
				int zhong_ma_num = countPlayerMaiMaZhongMaNum(player, owner, this.lastBankerSeat);
				if (zhong_ma_num > 0)
				{
					for (Entry<Integer, Player> entry2 : this.playerMapById.entrySet()) {
						EXPlayer player2 = (EXPlayer) entry2.getValue();
						if (player2.winer != 1 && player2.playerid != owner.playerid && player2.playerid != player.playerid)
						{
							addMaiNiaoScore(player, player2, owner.hu_score*zhong_ma_num);
						}
					}
				}

				for (Entry<Integer, Player> entry2 : this.playerMapById.entrySet()) {
					EXPlayer player2 = (EXPlayer) entry2.getValue();
					if (player2.playerid != owner.playerid)
					{
						int zhong_ma_num2 = countPlayerMaiMaZhongMaNum(player, player2, this.lastBankerSeat);
						if (zhong_ma_num2 > 0)
						{
							addMaiNiaoScore(owner, player, owner.hu_score*zhong_ma_num2);
						}
					}
				}
			}
		}

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

	private int countPlayerMaiMaZhongMaNum(EXPlayer owner, EXPlayer other, int start_seat) {
		int zhong_ma_num = 0;
		if (this.maxPlayers == 3)
		{
			for (int i = 0; i < owner.mai_niao.size(); i++) {
				CardNiao cn = owner.mai_niao.get(i);
				int card = cn.card;
				int tem = card % 100;
				if (start_seat == other.seat) {
					if (card < 400) {
						if (tem == 1 || tem == 4 || tem == 7) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.DONGFENG || card == Config.BEIFENG || card == Config.BAIBAN) {
							cn.score = 1;
							zhong_ma_num++;
						}
					}
				} else if ((start_seat) % this.maxPlayers + 1 == other.seat && this.maxPlayers >= 2) {
					if (card < 400) {
						if (tem == 2 || tem == 5 || tem == 8) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.NANFENG || card == Config.HONGZHONG) {
							cn.score = other.hu_score;
							zhong_ma_num++;
						}
					}
				} else if ((start_seat + 1) % this.maxPlayers + 1 == other.seat && this.maxPlayers >= 3) {
					if (card < 400) {
						if (tem == 3 || tem == 6 || tem == 9) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.XIFENG || card == Config.FACAI) {
							cn.score = 1;
							zhong_ma_num++;
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < owner.mai_niao.size(); i++) {
				CardNiao cn = owner.mai_niao.get(i);
				int card = cn.card;
				int tem = card % 100;
				if (start_seat == other.seat) {
					if (card < 400) {
						if (tem == 1 || tem == 5 || tem == 9) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.DONGFENG || card == Config.HONGZHONG) {
							cn.score = 1;
							zhong_ma_num++;
						}
					}
				} else if ((start_seat) % this.maxPlayers + 1 == other.seat && this.maxPlayers >= 2) {
					if (card < 400) {
						if (tem == 2 || tem == 6) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.NANFENG || card == Config.FACAI) {
							cn.score = other.hu_score;
							zhong_ma_num++;
						}
					}
				} else if ((start_seat + 1) % this.maxPlayers + 1 == other.seat && this.maxPlayers >= 3) {
					if (card < 400) {
						if (tem == 3 || tem == 7) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.XIFENG || card == Config.BAIBAN) {
							cn.score = 1;
							zhong_ma_num++;
						}
					}
				} else if ((start_seat + 2) % this.maxPlayers + 1 == other.seat && this.maxPlayers >= 4) {
					if (card < 400) {
						if (tem == 4 || tem == 8) {
							cn.score = 1;
							zhong_ma_num++;
						}
					} else {
						if (card == Config.BEIFENG) {
							cn.score = 1;
							zhong_ma_num++;
						}
					}
				}
			}
		}

		return zhong_ma_num;
	}

	private final void baozhama_jiabei_zhuaNiao(EXPlayer owner, int niao_num, int allscore) {
		this.niao.clear();
		if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_WU)
		{
			return;
		}

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

			int card = this.card.pop();
			CardNiao cn = new CardNiao();
			cn.card = card;
			this.niao.add(cn);
			int tem = card % 100;
			if (niao_all) {
				cn.playerId = owner.playerid;
				cn.seat = owner.seat;
				if (card >= 400 || card == this.card.laiziCard || card == this.card.laiziCard2) {
					cn.score = 10 * allscore;
				} else {
					cn.score = tem * allscore;
				}
			}
		}
	}

	private final void __zhuaNiao(EXPlayer owner, int niao_num, boolean isYiPaoDuoXiang) {
		this.niao.clear();
		if (config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_WU)
		{
			return;
		}

		int cur_niao_score = owner.niao_score;
		boolean magengdi = config.getBoolean(Config.ROOM_CONFIG_MA_GEN_DI_FEN);
		boolean zhua_ma = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ZHUAMA;
		boolean niao_all = config.getInt(Config.ROOM_CONFIG_NIAO) == Config.NIAO_TYPE_ALL;
		int niao_all_opt = owner.room.config.getInt(Config.ROOM_CONFIG_NIAO_OPT);
		if (magengdi || zhua_ma)
		{
			cur_niao_score = owner.hu_score;
		}

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

			int card = this.card.pop();
			CardNiao cn = new CardNiao();
			cn.card = card;
			this.niao.add(cn);
			int tem = card % 100;
			if (niao_all) {
				cn.playerId = owner.playerid;
				cn.seat = owner.seat;
				if (niao_all_opt == 0)
				{
					if (card >= 400 || card == this.card.laiziCard || card == this.card.laiziCard2) {
						cn.score = 10 * cur_niao_score;
					} else {
						cn.score = tem * cur_niao_score;
					}
				}
				else if (niao_all_opt == 1)
				{
					if (isYiPaoDuoXiang)
					{
						if (card >= 400 || card == this.card.laiziCard || card == this.card.laiziCard2) {
							cn.score = 10;
						} else {
							cn.score = tem;
						}
					}
					else
					{
						if (card >= 400 || card == this.card.laiziCard || card == this.card.laiziCard2) {
							cn.score = 10 * owner.hu_score;
						} else {
							cn.score = tem * owner.hu_score;
						}
					}
				}
			}
			else if (zhua_ma)
			{
				EXPlayer bankerPlayer = (EXPlayer)this.playerMapBySeat.get(this.lastBankerSeat);
				if (this.maxPlayers == 2)
				{
					if (card < 400)
					{
						if (tem == 1 || tem == 3 || tem == 5 || tem == 7 || tem == 9) {
							if (bankerPlayer != null)
							{
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
						else if (tem == 2 || tem == 4 || tem == 6 || tem == 8) {
							EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get(bankerPlayer.nextSeat);
							if (otherPlayer != null)
							{
								cn.playerId = otherPlayer.playerid;
								cn.seat = otherPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
					}
					else
					{
						if (card == Config.NANFENG || card == Config.BEIFENG || card == Config.FACAI)
						{
							if (bankerPlayer != null)
							{
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
						else if (card == Config.DONGFENG || card == Config.XIFENG || card == Config.HONGZHONG || card == Config.BAIBAN)
						{
							EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get(bankerPlayer.nextSeat);
							if (otherPlayer != null)
							{
								cn.playerId = otherPlayer.playerid;
								cn.seat = otherPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
					}
				}
				else if (this.maxPlayers == 3) {
					if (card < 400) {
						if (tem == 1 || tem == 4 || tem == 7) {
							if (bankerPlayer != null) {
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						} else if (tem == 2 || tem == 5 || tem == 8) {
							if (this.maxPlayers >= 2) {
								EXPlayer otherPlayer = (EXPlayer) this.playerMapBySeat.get((this.lastBankerSeat) % this.maxPlayers + 1);
								if (otherPlayer != null) {
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						} else if (tem == 3 || tem == 6 || tem == 9) {
							if (this.maxPlayers >= 3) {
								EXPlayer otherPlayer = (EXPlayer) this.playerMapBySeat.get((this.lastBankerSeat + 1) % this.maxPlayers + 1);
								if (otherPlayer != null) {
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
					} else {
						if (card == Config.DONGFENG || card == Config.BEIFENG || card == Config.BAIBAN) {
							if (bankerPlayer != null) {
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						} else if (card == Config.NANFENG || card == Config.HONGZHONG) {
							if (this.maxPlayers >= 2) {
								EXPlayer otherPlayer = (EXPlayer) this.playerMapBySeat.get((this.lastBankerSeat) % this.maxPlayers + 1);
								if (otherPlayer != null) {
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						} else if (card == Config.XIFENG || card == Config.FACAI) {
							if (this.maxPlayers >= 3) {
								EXPlayer otherPlayer = (EXPlayer) this.playerMapBySeat.get((this.lastBankerSeat + 1) % this.maxPlayers + 1);
								if (otherPlayer != null) {
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
					}
				}
				else
				{
					if (card < 400)
					{
						if (tem == 1 || tem == 5 || tem == 9) {
							if (bankerPlayer != null)
							{
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
						else if (tem == 2 || tem == 6) {
							if (this.maxPlayers >= 2)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
						else if (tem == 3 || tem == 7) {
							if (this.maxPlayers >= 3)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
						else if (tem == 4 || tem == 8) {
							if (this.maxPlayers >= 4)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+2)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
					}
					else
					{
						if (card == Config.DONGFENG || card == Config.HONGZHONG)
						{
							if (bankerPlayer != null)
							{
								cn.playerId = bankerPlayer.playerid;
								cn.seat = bankerPlayer.seat;
								cn.score = owner.hu_score;
							}
						}
						else if (card == Config.NANFENG || card == Config.FACAI)
						{
							if (this.maxPlayers >= 2)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
						else if (card == Config.XIFENG || card == Config.BAIBAN)
						{
							if (this.maxPlayers >= 3)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
						else if (card == Config.BEIFENG)
						{
							if (this.maxPlayers >= 4)
							{
								EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+2)%this.maxPlayers+1);
								if (otherPlayer != null)
								{
									cn.playerId = otherPlayer.playerid;
									cn.seat = otherPlayer.seat;
									cn.score = owner.hu_score;
								}
							}
						}
					}
				}
			}
			else {
				if (isYiPaoDuoXiang)
				{
				    if (this.maxPlayers == 2)
					{
						EXPlayer bankerPlayer = (EXPlayer)this.playerMapBySeat.get(this.lastBankerSeat);
						if (card < 400)
						{
							if (tem == 1 || tem == 3 || tem == 5 || tem == 7 || tem == 9) {
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							}
							else if (tem == 2 || tem == 4 || tem == 6 || tem == 8) {
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
						else
						{
							if (card == Config.NANFENG || card == Config.BEIFENG || card == Config.FACAI)
							{
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							}
							else if (card == Config.DONGFENG || card == Config.XIFENG || card == Config.HONGZHONG || card == Config.BAIBAN)
							{
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
					}
				    else if (this.maxPlayers == 3)
					{
						EXPlayer bankerPlayer = (EXPlayer)this.playerMapBySeat.get(this.lastBankerSeat);
						if (card < 400)
						{
							if (tem == 1 || tem == 4 || tem == 7) {
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							} else if (tem == 2 || tem == 5 || tem == 8) {
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
					    	} else if (tem == 3 || tem == 6 || tem == 9) {
								if (this.maxPlayers >= 3)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
						else
						{
							if (card == Config.DONGFENG || card == Config.BEIFENG || card == Config.BAIBAN)
							{
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							} else if (card == Config.NANFENG || card == Config.HONGZHONG)
							{
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
							else if (card == Config.XIFENG || card == Config.FACAI)
							{
								if (this.maxPlayers >= 3)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
					}
				    else
					{
						EXPlayer bankerPlayer = (EXPlayer)this.playerMapBySeat.get(this.lastBankerSeat);
						if (card < 400)
						{
							if (tem == 1 || tem == 5 || tem == 9) {
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							}
							else if (tem == 2 || tem == 6) {
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
							else if (tem == 3 || tem == 7) {
								if (this.maxPlayers >= 3)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
							else if (tem == 4 || tem == 8) {
								if (this.maxPlayers >= 4)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+2)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
						else
						{
							if (card == Config.DONGFENG || card == Config.HONGZHONG)
							{
								if (bankerPlayer != null && bankerPlayer.winer == 1)
								{
									cn.playerId = bankerPlayer.playerid;
									cn.seat = bankerPlayer.seat;
									if (magengdi)
									{
										cn.score = bankerPlayer.hu_score;
									}
									else {
										cn.score = bankerPlayer.niao_score;
									}
								}
							}
							else if (card == Config.NANFENG || card == Config.FACAI)
							{
								if (this.maxPlayers >= 2)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
							else if (card == Config.XIFENG || card == Config.BAIBAN)
							{
								if (this.maxPlayers >= 3)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+1)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
							else if (card == Config.BEIFENG)
							{
								if (this.maxPlayers >= 4)
								{
									EXPlayer otherPlayer = (EXPlayer)this.playerMapBySeat.get((this.lastBankerSeat+2)%this.maxPlayers+1);
									if (otherPlayer != null && otherPlayer.winer == 1)
									{
										cn.playerId = otherPlayer.playerid;
										cn.seat = otherPlayer.seat;
										if (magengdi)
										{
											cn.score = otherPlayer.hu_score;
										}
										else {
											cn.score = otherPlayer.niao_score;
										}
									}
								}
							}
						}
					}
				}
				else
				{
					if (this.maxPlayers == 2)
					{
						if (this.lastBankerSeat == owner.seat)
						{
							if (card < 400)
							{
								if (tem == 1 || tem == 3 || tem == 5 || tem == 7 || tem == 9) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.NANFENG || card == Config.BEIFENG|| card == Config.FACAI)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 2)
						{
							if (card < 400)
							{
								if (tem == 2 || tem == 4|| tem == 6|| tem == 8) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.DONGFENG || card == Config.XIFENG || card == Config.HONGZHONG || card == Config.BAIBAN)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
					}
					else if (this.maxPlayers == 3)
					{
						if (this.lastBankerSeat == owner.seat)
						{
							if (card < 400)
							{
								if (tem == 1 || tem == 4 || tem == 7) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.DONGFENG || card == Config.BEIFENG || card == Config.BAIBAN)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 2)
						{
							if (card < 400)
							{
								if (tem == 2 || tem == 5 || tem == 8) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.NANFENG || card == Config.HONGZHONG)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat+1)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 3)
						{
							if (card < 400)
							{
								if (tem == 3 || tem == 6 || tem == 9) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.XIFENG || card == Config.FACAI)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
					}
					else
					{
						if (this.lastBankerSeat == owner.seat)
						{
							if (card < 400)
							{
								if (tem == 1 || tem == 5 || tem == 9) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.DONGFENG || card == Config.HONGZHONG)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 2)
						{
							if (card < 400)
							{
								if (tem == 2 || tem == 6) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.NANFENG || card == Config.FACAI)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat+1)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 3)
						{
							if (card < 400)
							{
								if (tem == 3 || tem == 7) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.XIFENG || card == Config.BAIBAN)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
						else if ((this.lastBankerSeat+2)%this.maxPlayers+1 == owner.seat && this.maxPlayers >= 4)
						{
							if (card < 400)
							{
								if (tem == 4 || tem == 8) {
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
							else
							{
								if (card == Config.BEIFENG)
								{
									cn.playerId = owner.playerid;
									cn.seat = owner.seat;
									cn.score = cur_niao_score;
								}
							}
						}
					}
				}
			}
		}
	}

	public void winCallback(EXPlayer owner, int card) {
		if (this.winCount >= this.actionList.size()) {
			if (actionList.size() > 1) {
				this.bankerSeat = this.activeSeat;
				niao_tongPao(card);
			} else {
				niao_dianPao(owner, card);
			}

			this.endGame();
		}
	}

	public void winCallback_qiangkong(EXPlayer owner, int card) {
		boolean qiangkong_niao = config.getBoolean(Config.ROOM_CONFIG_QIANGKONG_NIAO);
		boolean qg_type = this.config.getBoolean(Config.ROOM_CONFIG_QG_TYPE);
		if (this.winCount >= this.actionList.size()) {
			if (actionList.size() > 1) {
				this.bankerSeat = this.activeSeat;
				if (qiangkong_niao)
					niao_tongPao_quanbao(card, qg_type, false);
			} else {
				if (qiangkong_niao) {
					niao_dianPao_quanbao(owner, card, qg_type, false);
				}
			}

			this.endGame();
		}
	}

//	public void settleRound() {
//		saveMilitaryRound(playBackData.getData());
//		boolean total = this.round >= this.maxRound;
//		if(this.entrusResultType ==EXRoom.ENTRUST_CURREN_RESULT) {
//			for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) { 
//				if(entry.getValue().isEntrust()) {
//					total = true;
//					break;
//				}
//			}
//		}
//		if (!total)
//			EXMainServer.gameCtr.roomResult(this);
//		else {
//			this.saveMilitaryTotal(false);
//		}
//		this.stateMachine.changeState(Global.getState(EXRoomRoundSettleState.class));
//		this.winCount = 0;
//		
//		
//		if (total) {
//			this.stateMachine.changeState(Global.getState(RoomDestoryGameState.class));
//			return;
//		}
//	}

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

	@Override
	public void clear() {
		super.clear();

		this.liuju = false;
		this.activeSeat = this.lastDiscardSeat = this.currenDiscardSeat = 0;
		this.winCount = this.piaoNiaoCount = 0;
		this.niao.clear();

		this.isGengZhuang = false;
		this.first_zhuang_card = 0;
		this.geng_zhuan_num = 0;
		this.gent_zhuan_nextSeat = 0;
		if (this.config.getBoolean(Config.ROOM_CONFIG_GENGZHUAN))
		{
			this.isGengZhuang = true;
		}
		this.lastBankerSeat = 0;
	}

	public void handle10BeiBuJiFen(EXPlayer winner)
	{
		if (!this.config.getBoolean(Config.ROOM_CONFIG_10_BEI_BUJIFEN))
		{
		    return;
		}

		for (Entry<Integer, Player> entry : this.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (winner.playerid != player.playerid)
			{
				if (IsTingHu10Bei(player))
				{
					player.tinghu10bei = true;
				}
			}
		}
	}

	public boolean IsTingHu10Bei(EXPlayer player)
	{
		List<Integer> tempList = new ArrayList<Integer>();
		boolean nowan = this.config.getBoolean(Config.ROOM_CONFIG_NO_WAN);
		if (nowan)
		{
			for (int index = 1; index <= 9; index++) {
				tempList.add(200 + index);
				tempList.add(300 + index);
			}
		}
		else {
			for (int index = 1; index <= 9; index++) {
				tempList.add(100 + index);
				tempList.add(200 + index);
				tempList.add(300 + index);
			}
		}

		//400 东风 403南风 406西风 409北风 412红中 415发财 418白板
		boolean wufeng = this.config.getBoolean(Config.ROOM_CONFIG_WU_FENG);
		if (!wufeng)
		{
			tempList.add(Config.DONGFENG);
			tempList.add(Config.NANFENG);
			tempList.add(Config.XIFENG);
			tempList.add(Config.BEIFENG);
			tempList.add(Config.HONGZHONG);
			tempList.add(Config.FACAI);
			tempList.add(Config.BAIBAN);
		}

		for(int i = 0; i < tempList.size(); i++)
		{
			int card = tempList.get(i);
			int laizi = config.getInt(Config.ROOM_CONFIG_LAIZI);
			HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
			WinCard win = new WinCard(player.cardInhand, card, this.card.laiziCard, this.card.laiziCard2, laizi > 0 ? true : false, false);
			if (win.checkWin(map, player, this, true)) {
				int dahu_score = 0;
				int cheng_score = 1;
				for (Entry<Integer, WinCardType> entry : map.entrySet()) {
					WinCardType cardType = entry.getValue();
					if (this.config.getBoolean(Config.ROOM_CONFIG_DAHU_CHENG))
					{
						cheng_score *= cardType.score;
					}
					else
					{
						if (cardType.IsDaHu())
						{
							dahu_score += cardType.score*WinCardType.DA_HU_BEI_SCORE;
						}
						else
						{
							cheng_score *=  cardType.score;
						}
					}
				}

				if (dahu_score * cheng_score >= 10)
				{
					return true;
				}
			}
		}

		return false;
	}

	public EXPlayer get12ZhangLuoDi(EXPlayer owner)
	{
		if (owner.opCardList.size() >= 4)
		{
			OpCard opCard = owner.opCardList.get(owner.opCardList.size() - 1);
			if (opCard.type == RuleWeight.TYPE_PONG || opCard.type == RuleWeight.KONG)
			{
				if (opCard.card_seat > 0)
				{
					EXPlayer badPlayer = (EXPlayer)this.playerMapBySeat.get(opCard.card_seat);
					if (badPlayer != null)
					{
					    return badPlayer;
					}
				}
			}
		}
		return null;
	}
}
