package hunan;

import com.data.bean.AccountBean;
import com.data.cache.AccountCache;
import com.data.util.ErrorCode;
import com.taurus.core.entity.*;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.Logger;
import com.taurus.web.WebException;
import redis.clients.jedis.Jedis;
import taurus.client.TaurusClient;
import taurus.client.business.AccountBusiness;
import taurus.client.business.GroupRoomBusiness;
import taurus.util.GroupDatas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class DoTest {

    // 扣点点玩家手牌
    public static List<Integer> cardInhand = new ArrayList<Integer>();

    private static final Logger log = Logger.getLogger(DoTest.class);

//
//    //跑得快算法
//    private static PaoDeKuaiSuanFa paoDeKuaiSuanFa;

    //红中其它玩家打的牌
//    public static String hongZhongCard = "";

    //扣点点别人打的牌
    public static String card = "";


    // 玩家座位号
    public static int seat = 0;
    // 玩家ID
    public static int playerId = 0;
    // 登录密码
    public static String password = "123456";
    // 会话标识
    public static String session = "";
    // 访问令牌
    public static String token = "";
    // 玩法ID
    public static String wanfaId = "";

    // 客户端连接对象
    public static TaurusClient client = null;

    public static TObject datas = null;


    //圈子id
    public static int groupId = 0;

    //房间id
    public static String roomId = "";

    public static int count = 0;

    static final void get() {
        TObject data = GroupDatas.getData();
        datas = data;

    }

    public String doTest() throws Exception {
        get();
        ITArray group = datas.getTArray("groupid");
        Jedis jedis11 = Redis.use("group1_db11").getJedis();
        Jedis jedis0 = Redis.use("group1_db0").getJedis();
        try {

            if (group.size() != 0) {
                log.info("数据--------{}", datas);
//                System.out.println("数据--------" + datas);
                ITArray robotIds = datas.getTArray("robotIds");
                int robotIndex = 0;

                for (int i = 0; i < group.size(); i++) {
                    TDataWrapper tDataWrapper = group.get(i);
                    if (tDataWrapper.getTypeId() == TDataType.TOBJECT) {
                        TObject roomObj = (TObject) tDataWrapper.getObject();

                        // 获取圈子id和房间列表
                        Set<String> keySet = roomObj.getKeys();
                        for (String groupIdStr : keySet) {

                            int currentGroupId = Integer.parseInt(groupIdStr);
                            ITArray roomArray = roomObj.getTArray(groupIdStr);//玩法id


                            //为每个房间分配一个机器人
                            for (int j = 0; j < roomArray.size() && robotIndex < robotIds.size(); j++) {
                                int currentPlayId = roomArray.getInt(j);
                                int currentPlayerId = robotIds.getInt(robotIndex);

                                System.out.println("玩家id: " + currentPlayerId + ", 玩法id: " + currentPlayId + ", 圈子id: " + currentGroupId);
                                JiQiRens jiQiRen = new JiQiRens();
                                String playKey = "g{" + groupIdStr + "}:play:" + currentPlayId;

                                String leftover_robot = jedis11.hget(playKey, "leftover_robot");
                                if (jedis11.hget(playKey, "leftover_robot") != null) {
//                                    if (Integer.parseInt(leftover_robot) > 0) {
                                        jiQiRen.tcp_test(currentPlayerId, currentPlayId, currentGroupId);
                                        robotIndex++;
//                                    }
                                }
                            }
                        }
                    }
                }

                // 清空数据
                datas.getTArray("robotIds").clear();
                datas.getTArray("groupid").clear();
                playerId = 0;
                roomId = "";
                groupId = 0;
                count = 0;

            }

        } finally {
            jedis11.close();
            jedis0.close();
        }

        return "";
    }
}