package com.game.player.state;

import com.data.bean.GroupBean;
import com.data.cache.GroupCache;
import com.data.cache.GroupMemberCache;
import com.data.util.ConsumeCode;
import com.data.util.CountUtil;
import com.data.util.Utility;
import com.game.ActionEvent;
import com.game.Global;
import com.game.data.Player;
import com.game.state.StateBase;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

/**
 * 玩家准备状态
 * 
 *
 */
public class PlayerReadyState extends StateBase<Player> {

	@Override
	public void enter(Player owner) {
		owner.ready = true;
		owner.room.stateMachine.execute(ActionEvent.EVENT_READY, 0, null);
	}

	@Override
	public void execute(Player owner, String cmd, int gid, Object param) {
		if(owner.room.round>0)return;
		if(cmd.equals(ActionEvent.EVENT_EXIT_ROOM)) {
//			if(owner.room.round==0) {
//				String gm_key = GroupMemberCache.genKey(owner.room.groupId, owner.playerid);
//				Redis.use("group1_db10").hincrBy(gm_key, "hp", owner.xi_pai_total);
//				String sql = String.format("{call sp_update_hp(%s,%s,%s,%s,%s,%s,'%s',%s,'%s')}", owner.room.groupId, owner.playerid, owner.xi_pai_total, owner.hp.cur_hp, ConsumeCode.HP_PARTNER_XIPAI_REWARD_BACK,
//						owner.room.groupPid, owner.room.roomid, System.currentTimeMillis()/1000, "洗牌退回");
//				Utility.evtdb(gid, 2, sql);
//				
//				Jedis jedis9 = Redis.use("group1_db13").getJedis();
//				try {
//					//处理盟主退回操作
//					GroupBean gb = GroupCache.getGroup(owner.room.groupId);
//					String desc = "洗牌退回";
//					long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s", owner.room.groupId, gb.owner), -owner.xi_pai_total,
//							jedis9);
//					String mengzhuSql = String.format(
//							"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
//									+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
//									gb.id, gb.owner, ConsumeCode.HP_PARTNER_XIPAI_REWARD_BACK, -owner.xi_pai_total, cur_hp, owner.room.groupPid, owner.room.roomid, System.currentTimeMillis()/1000,
//							0, desc);
//					Pipeline pipeline = jedis9.pipelined();
//					String key = String.format("g{%s}:m%s:d_xipai_reward", owner.room.groupId, gb.owner);
//					CountUtil.countLogByDay30(key, -owner.xi_pai_total, pipeline);
//					Utility.evtdbLog(gid, 1,mengzhuSql);
//					String rewardsql = String.format(
//							"update group_member set reward_hp=reward_hp+ %s where uid = %s AND groupId = %s", -owner.xi_pai_total,
//							gb.owner, owner.room.groupId);
//
//					Utility.evtdb(owner.room.groupId, 1, rewardsql);
//
//				}catch (Exception e) {
//					e.printStackTrace();
//				} finally {
//					jedis9.close();
//				}
//
//			}
			Global.gameCtr.exitRoom(owner, gid,false);
		}
	}

}
