package com.evt.mgr.handler;

import com.data.util.CountUtil;
import com.evt.mgr.IHandler;
import com.evt.mgr.Utils;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.Logger;

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

public class HandlerGroupRound implements IHandler {
	static Logger log = Logger.getLogger(HandlerGroupRound.class);

	@Override
	public int process(int uid, ITObject param) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int processGroup(int groupId, ITObject param) throws Exception {

		int pid = param.getInt("pid");
		int valid = param.getInt("valid");
		ITArray playerList = param.getTArray("player_list");
		int win_player = param.getInt("win_player");

		int valid_count = param.getInt("valid_count");
		int valid_diamo = 0;
		int all_count = 0;
		if (param.containsKey("valid_diamo")) {
			valid_diamo = param.getInt("valid_diamo");
		}

		if (param.containsKey("all_count")) {
			all_count = param.getInt("all_count");
		}

		String rp_key = String.format("g%s:round:p%s", groupId, pid);
		String round_key = String.format("g%s:round", groupId);

		Jedis jedis13 = Redis.use("group1_db13").getJedis();

		try {

			Pipeline pipeline13 = jedis13.pipelined();

			CountUtil.countLog(rp_key, playerList.size(), pipeline13, false, true);
			CountUtil.countLog(round_key, playerList.size(), pipeline13, true);

			String valid_key = String.format("g%s:valid_room", groupId);
			String p_valid_key = String.format("g%s:valid_room:p%s", groupId, pid);
			CountUtil.countLogByDay(p_valid_key, valid_count==0?2:1, pipeline13, true);
			CountUtil.countLog(valid_key, valid_count==0?2:1, pipeline13);
			Utils.countValidAndTotal(pipeline13, playerList, groupId, pid, valid_count, valid_diamo, all_count,
					win_player);

			pipeline13.sync();

		}catch (Exception e) {
			log.error("统计场次异常",e);

		} finally {
			jedis13.close();
		}
		return 0;
	}

}
