Search in sources :

Example 1 with Player

use of com.gamebuster19901.excite.Player in project ExciteBot by TheGameCommunity.

the class Player method addPlayer.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Player addPlayer(MessageContext context, boolean automatic, int playerID, String friendCode, String name) throws SQLException {
    if (context.getEvent() instanceof UnknownPlayer) {
        UnknownPlayer player = (UnknownPlayer) context.getEvent();
        player.name = name;
        player.friendCode = friendCode;
    }
    PreparedStatement ps = Insertion.insertInto(PLAYERS).setColumns(PLAYER_ID, FRIEND_CODE, PLAYER_NAME).to(playerID, friendCode, name).prepare(ConsoleContext.INSTANCE);
    ps.execute();
    Player ret = getPlayerByID(context, playerID);
    DiscoveryAudit.addProfileDiscovery(context, automatic, ret);
    return ret;
}
Also used : PreparedStatement(com.gamebuster19901.excite.bot.database.sql.PreparedStatement)

Example 2 with Player

use of com.gamebuster19901.excite.Player 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)

Example 3 with Player

use of com.gamebuster19901.excite.Player in project ExciteBot by TheGameCommunity.

the class WhoIsCommand method sendResponse.

@SuppressWarnings("serial")
public static int sendResponse(MessageContext context, String lookingFor) {
    if (context.isConsoleMessage() || context.isIngameEvent()) {
        context.sendMessage("You cannot execute this command as " + context.getEvent().getClass().getSimpleName());
        return 1;
    }
    Wiimmfi wiimmfi = Main.discordBot.getWiimmfi();
    EmbedBuilder embed = new EmbedBuilder();
    boolean hasMembers = context.isGuildMessage();
    if (wiimmfi.getError() == null) {
        if (!lookingFor.isEmpty()) {
            HashSet<DiscordUser> users = new HashSet<DiscordUser>() {

                {
                    this.addAll(Arrays.asList(DiscordUser.getDiscordUsersWithUsernameOrID(context, lookingFor)));
                }
            };
            HashSet<Player> players = new HashSet<Player>() {

                {
                    this.addAll(Arrays.asList(Player.getPlayersByAnyIdentifier(context, lookingFor)));
                }
            };
            HashSet<Named> matches = new HashSet<Named>();
            matches.addAll(users);
            matches.addAll(players);
            SimpleDateFormat date = new SimpleDateFormat("yyyy/MM/dd HH:mm z", Locale.ENGLISH);
            if (matches.size() == 1) {
                Named match = matches.iterator().next();
                embed.setTitle("Information about " + match.getIdentifierName());
                if (match instanceof DiscordUser) {
                    DiscordUser user = (DiscordUser) match;
                    Member member;
                    embed.setColor(Color.WHITE);
                    Wii[] wiis = user.getRegisteredWiis();
                    Set<Player> profiles = user.getProfiles(context);
                    Duration timeOnline = Duration.ZERO;
                    Instant lastOnline = TimeUtils.PLAYER_EPOCH;
                    String profileList = "";
                    String wiiList = "";
                    for (Player profile : profiles) {
                        profileList = profileList + profile.toEmbedstring() + "\n";
                        timeOnline = timeOnline.plus(profile.getOnlineDuration());
                        Instant profileLastOnline = profile.getLastOnline();
                        if (profileLastOnline.isAfter(lastOnline)) {
                            lastOnline = profileLastOnline;
                        }
                    }
                    for (Wii wii : wiis) {
                        wiiList = wiiList + wii.getName() + "\n";
                    }
                    if (hasMembers && (member = user.getMember(context.getServer())) != null) {
                        embed.setColor(member.getColor());
                        embed.setThumbnail(user.getJDAUser().getEffectiveAvatarUrl());
                        embed.addField("Username:", user.getJDAUser().getName(), false);
                        embed.addField("Discriminator", user.getJDAUser().getDiscriminator(), false);
                        // embed.addField("Badges:", "", false);
                        embed.addField("ID:", "" + user.getID(), false);
                        embed.addField("Nickname:", member.getNickname() != null ? member.getNickname() : "##Not Nicknamed##", false);
                        embed.addField("Joined Discord:", date.format(member.getTimeCreated().toInstant().toEpochMilli()), false);
                        embed.addField("Joined " + context.getServer().getName() + ":", date.format(member.getTimeJoined().toInstant().toEpochMilli()), false);
                        embed.addField("Member for:", readableDuration(TimeUtils.since(member.getTimeJoined().toInstant()), false), false);
                        embed.addField("Time Online:", readableDuration(timeOnline, true), false);
                        embed.addField(profiles.size() + " registered Profiles:", profileList, false);
                        embed.addField(wiis.length + " registered Wiis:", wiiList, false);
                    } else {
                        embed.setThumbnail(user.getJDAUser().getEffectiveAvatarUrl());
                        embed.addField("Username:", user.getJDAUser().getName(), false);
                        embed.addField("Discriminator", user.getJDAUser().getDiscriminator(), false);
                        embed.addField("ID:", "" + user.getID(), false);
                        embed.addField("Time Online:", readableDuration(timeOnline, true), false);
                        embed.addField(profiles.size() + " registered Profiles:", profileList, false);
                        embed.addField(wiis.length + " registered Wiis:", wiiList, false);
                        embed.appendDescription("For more information, execute this command in a server the user is in.");
                    }
                } else if (match instanceof Player) {
                    Player profile = (Player) match;
                    DiscordUser user = DiscordUser.getDiscordUserTreatingUnknownsAsNobody(context, profile.getDiscord());
                    embed.addField("Name:", profile.getName(), false);
                    embed.addField("ID:", profile.getID() + "", false);
                    embed.addField("FC:", profile.getFriendCode(), false);
                    embed.addField("Owner:", user.toDetailedString(), false);
                    embed.addField("Time Online:", readableDuration(profile.getOnlineDuration(), true), false);
                    embed.addField("First Seen:", date.format(profile.getFirstSeen().toEpochMilli()), false);
                    embed.addField("Last Seen:", date.format(profile.getLastOnline().toEpochMilli()), false);
                }
            } else if (matches.size() == 0) {
                embed.setColor(Color.RED);
                embed.setTitle("Target not found");
                embed.addField("Target:", lookingFor, true);
            } else {
                embed.setTitle("Ambigious target string, supply an ID");
                embed.setColor(Color.RED);
                embed.addField("Target", lookingFor, true);
                embed.addField("Ambiguities", "" + matches.size(), true);
                if (users.size() > 0) {
                    embed.appendDescription("Discord users:\n");
                    String userList = "";
                    for (DiscordUser user : users) {
                        Member member;
                        if (hasMembers && (member = user.getMember(context.getServer())) != null && member.getNickname() != null) {
                            userList = userList + user.toDetailedString() + " AKA " + member.getEffectiveName() + "#" + member.getIdLong() + "\n";
                        } else {
                            userList = userList + user.toDetailedString() + "\n";
                        }
                    }
                    embed.addField("Ambiguous Users:", userList, false);
                }
                if (players.size() > 0) {
                    String playerList = "";
                    for (Player player : players) {
                        playerList = playerList + player.toFullString() + "\n";
                    }
                    embed.addField("Ambiguous Profiles:", playerList, false);
                }
            }
            embed.setTimestamp(Instant.now());
            context.sendMessage(embed.build());
        }
    }
    return 1;
}
Also used : DiscordUser(com.gamebuster19901.excite.bot.user.DiscordUser) Named(com.gamebuster19901.excite.util.Named) Player(com.gamebuster19901.excite.Player) Instant(java.time.Instant) Duration(java.time.Duration) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Wii(com.gamebuster19901.excite.bot.user.Wii) SimpleDateFormat(java.text.SimpleDateFormat) Member(net.dv8tion.jda.api.entities.Member) Wiimmfi(com.gamebuster19901.excite.Wiimmfi) HashSet(java.util.HashSet)

Example 4 with Player

use of com.gamebuster19901.excite.Player in project ExciteBot by TheGameCommunity.

the class LogOutAudit method addLogOutAudit.

public static LogOutAudit addLogOutAudit(MessageContext context, Player player) {
    Audit parent = Audit.addAudit(context, AuditType.LOG_OUT_AUDIT, getMessage(player));
    PreparedStatement st;
    try {
        st = Insertion.insertInto(Table.AUDIT_PROFILE_LOGOUTS).setColumns(AUDIT_ID).to(parent.getID()).prepare(context, true);
        st.execute();
        LogOutAudit ret = getLogOutAuditByAuditID(ConsoleContext.INSTANCE, parent.getID());
        ret.parentData = parent;
        return ret;
    } catch (SQLException e) {
        throw new IOError(e);
    }
}
Also used : SQLException(java.sql.SQLException) IOError(java.io.IOError) PreparedStatement(com.gamebuster19901.excite.bot.database.sql.PreparedStatement)

Example 5 with Player

use of com.gamebuster19901.excite.Player in project ExciteBot by TheGameCommunity.

the class LogInAudit method addLoginAudit.

public static LogInAudit addLoginAudit(MessageContext context, Player player) {
    Audit parent = Audit.addAudit(context, AuditType.LOG_IN_AUDIT, getMessage(player));
    PreparedStatement st;
    try {
        st = Insertion.insertInto(Table.AUDIT_PROFILE_LOGINS).setColumns(AUDIT_ID).to(parent.getID()).prepare(context, true);
        st.execute();
        LogInAudit ret = getLoginAuditByAuditID(ConsoleContext.INSTANCE, parent.getID());
        ret.parentData = parent;
        return ret;
    } catch (SQLException e) {
        throw new IOError(e);
    }
}
Also used : SQLException(java.sql.SQLException) IOError(java.io.IOError) PreparedStatement(com.gamebuster19901.excite.bot.database.sql.PreparedStatement)

Aggregations

Player (com.gamebuster19901.excite.Player)7 SQLException (java.sql.SQLException)6 PreparedStatement (com.gamebuster19901.excite.bot.database.sql.PreparedStatement)5 IOError (java.io.IOError)5 DiscordUser (com.gamebuster19901.excite.bot.user.DiscordUser)4 HashSet (java.util.HashSet)4 Result (com.gamebuster19901.excite.bot.database.Result)2 Duration (java.time.Duration)2 UnknownPlayer (com.gamebuster19901.excite.UnknownPlayer)1 Wiimmfi (com.gamebuster19901.excite.Wiimmfi)1 Ban (com.gamebuster19901.excite.bot.audit.ban.Ban)1 MessageContext (com.gamebuster19901.excite.bot.command.MessageContext)1 Comparison (com.gamebuster19901.excite.bot.database.Comparison)1 Wii (com.gamebuster19901.excite.bot.user.Wii)1 WiimmfiErrorResponse (com.gamebuster19901.excite.exception.WiimmfiErrorResponse)1 WiimmfiResponseException (com.gamebuster19901.excite.exception.WiimmfiResponseException)1 Named (com.gamebuster19901.excite.util.Named)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 SimpleDateFormat (java.text.SimpleDateFormat)1