Search in sources :

Example 1 with WiimmfiErrorResponse

use of com.gamebuster19901.excite.exception.WiimmfiErrorResponse in project ExciteBot by TheGameCommunity.

the class Wiimmfi method updateOnlinePlayers.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Player[] updateOnlinePlayers() throws SQLException, WiimmfiResponseException {
    HashSet<Player> onlinePlayers = new HashSet<Player>();
    if (JSON != null) {
        JsonArray objects = null;
        JsonElement object1;
        HashMap<String, JsonElement> object1Entries = new HashMap<String, JsonElement>();
        if (JSON.isJsonArray()) {
            objects = JSON.getAsJsonArray();
            object1 = objects.get(0);
        } else {
            object1 = JSON;
        }
        for (Entry<String, JsonElement> e : object1.getAsJsonObject().entrySet()) {
            if (object1Entries.put(e.getKey(), e.getValue()) != null) {
                throw new WiimmfiResponseException("Duplicate key in json response:" + e.getKey());
            }
        }
        JsonElement typeJson = object1Entries.get("type");
        JsonElement identifyJson = object1Entries.get("identify");
        JsonElement gameJson = object1Entries.get("game_list");
        String type = null;
        String identify = null;
        String game = null;
        if (typeJson != null) {
            type = typeJson.getAsString();
        }
        if (type == null) {
            throw new WiimmfiResponseException("Unexpected response from wiimmfi api");
        }
        if (type.equals("error")) {
            JsonElement errorJson = object1Entries.get("error");
            JsonElement msgJson = object1Entries.get("msg");
            String error = null;
            String msg = null;
            if (errorJson != null) {
                error = errorJson.getAsString();
            } else {
                error = "No error type received from wiimmfi";
            }
            if (msgJson != null) {
                msg = msgJson.getAsString();
            }
            throw new WiimmfiErrorResponse(error + ": " + msg);
        }
        if (identifyJson != null) {
            identify = identifyJson.getAsString();
        }
        if (gameJson != null) {
            if (gameJson.getAsJsonArray().size() > 0) {
                game = gameJson.getAsJsonArray().get(0).getAsString();
            } else {
                throw new WiimmfiResponseException("No game data response");
            }
        }
        if (!"games".equals(identify)) {
            throw new WiimmfiResponseException("Unexpected response of type: " + identify);
        }
        if (!"exciteracewii".equals(game)) {
            throw new WiimmfiResponseException("Wiimmfi sent player list from incorrect game: " + game);
        }
        elementFinder: for (int i = 1; i < objects.size(); i++) {
            // should be safe to access w/o null check as it should error before now
            JsonElement obj = objects.get(i);
            HashMap<String, JsonElement> entries = new HashMap<String, JsonElement>();
            for (Entry<String, JsonElement> e : obj.getAsJsonObject().entrySet()) {
                if (entries.put(e.getKey(), e.getValue()) != null) {
                    throw new WiimmfiResponseException("Duplicate key in json response:" + e.getKey());
                }
            }
            if ("game-stats".equals(entries.get("type").getAsString())) {
                JsonArray playerList = entries.get("list").getAsJsonArray();
                for (JsonElement e : playerList) {
                    HashMap<String, JsonElement> playerDataEntries = new HashMap<String, JsonElement>();
                    for (Entry<String, JsonElement> e2 : e.getAsJsonObject().entrySet()) {
                        playerDataEntries.put(e2.getKey(), e2.getValue());
                    }
                    int pid = playerDataEntries.get("pid").getAsInt();
                    String fc = playerDataEntries.get("fc").getAsString();
                    int status = playerDataEntries.get("online_status").getAsInt();
                    int host = playerDataEntries.get("hoststate").getAsInt();
                    String name = playerDataEntries.get("name").getAsJsonArray().get(0).getAsString();
                    Player player = Player.getPlayerByID(ConsoleContext.INSTANCE, pid);
                    if (player instanceof UnknownPlayer) {
                        player = Player.addPlayer(new MessageContext(player), true, pid, fc, name);
                    } else {
                        player.setName(name);
                        player.setOnlineStatus(status);
                        player.setHost(host);
                    }
                    onlinePlayers.add(player);
                }
                ;
                break elementFinder;
            }
        }
    }
    for (Player player : onlinePlayers) {
        if (PREV_ONLINE_PLAYERS.contains(player)) {
            if (!(player.isPrivate() || player.isSearching() || player.isFriendsList())) {
                player.updateSecondsPlayed();
            }
            player.updateLastOnline();
        } else {
            LogInAudit.addLoginAudit(new MessageContext(player), player);
            player.updateLastOnline();
        }
        PREV_ONLINE_PLAYERS.remove(player);
    }
    for (Player player : PREV_ONLINE_PLAYERS) {
        LogOutAudit.addLogOutAudit(new MessageContext(player), player);
    }
    ONLINE_PLAYERS = onlinePlayers;
    PREV_ONLINE_PLAYERS = ONLINE_PLAYERS;
    return onlinePlayers.toArray(new Player[] {});
}
Also used : HashMap(java.util.HashMap) JsonArray(com.google.gson.JsonArray) WiimmfiResponseException(com.gamebuster19901.excite.exception.WiimmfiResponseException) Entry(java.util.Map.Entry) JsonElement(com.google.gson.JsonElement) MessageContext(com.gamebuster19901.excite.bot.command.MessageContext) WiimmfiErrorResponse(com.gamebuster19901.excite.exception.WiimmfiErrorResponse) HashSet(java.util.HashSet)

Aggregations

MessageContext (com.gamebuster19901.excite.bot.command.MessageContext)1 WiimmfiErrorResponse (com.gamebuster19901.excite.exception.WiimmfiErrorResponse)1 WiimmfiResponseException (com.gamebuster19901.excite.exception.WiimmfiResponseException)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1