package fi.flexplex.lib; import java.io.IOException; import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.UUID; import com.google.gson.JsonElement; import com.google.gson.JsonObject; public final class PlayerFriends { private final GraphQLApi graphQLApi; private final String token; private final String player; private final Set friends = new HashSet<>(); private final Set incomingFriendRequests = new HashSet<>(); private final Set outcomingFriendRequests = new HashSet<>(); protected PlayerFriends(final GraphQLApi graphQLApi, final String token, final String player) throws IOException { this.graphQLApi = graphQLApi; this.token = token; this.player = player; if (!this.update()) { throw new IOException(); } } protected boolean update() { final Optional result = this.graphQLApi.execute( "query{" + "player(uuid:\"" + this.player + "\"){" + "friends{uuid}" + "incomingFriendRequests(token:\"" + this.token + "\"){uuid}" + "outcomingFriendRequests(token:\"" + this.token + "\"){uuid}" + "}" + "}" ); if (result.isEmpty()) { return false; } final JsonObject playerObject = result.get().get("player").getAsJsonObject(); this.friends.clear(); for (final JsonElement element : playerObject.get("friends").getAsJsonArray()) { this.friends.add(UUID.fromString(element.getAsJsonObject().get("uuid").getAsString())); } this.incomingFriendRequests.clear(); for (final JsonElement element : playerObject.get("incomingFriendRequests").getAsJsonArray()) { this.incomingFriendRequests.add(UUID.fromString(element.getAsJsonObject().get("uuid").getAsString())); } this.outcomingFriendRequests.clear(); for (final JsonElement element : playerObject.get("outcomingFriendRequests").getAsJsonArray()) { this.outcomingFriendRequests.add(UUID.fromString(element.getAsJsonObject().get("uuid").getAsString())); } return true; } /** * Get friends * @return friends */ public Set getFriends() { return this.friends; } /** * Get incoming friend requests * @return friend requests */ public Set getIncomingFriendRequests() { return this.incomingFriendRequests; } /** * Get outcoming friend requests * @return friend requests */ public Set getOutcomingFriendRequests() { return this.outcomingFriendRequests; } /** * Create new friend request * @param player from * @param targetPlayer to * @return true if was created successfully */ public boolean createFriendRequest(final String player, final String targetPlayer) { final Optional result = this.graphQLApi.execute( "mutation{" + "createFriendRequest(" + "token:\"" + this.token + "\"," + "player:\"" + player + "\"," + "targetPlayer:\"" + targetPlayer + "\"" + ")" + "}" ); return result.isPresent() && result.get().get("createFriendRequest").getAsBoolean(); } /** * Accept incoming friend request * @param player coming from * @param targetPlayer who is accepting * @return true if was accepted successfully */ public boolean acceptFriendRequest(final String player, final String targetPlayer) { final Optional result = this.graphQLApi.execute( "mutation{" + "acceptFriendRequest(" + "token:\"" + this.token + "\"," + "player:\"" + player + "\"," + "targetPlayer:\"" + targetPlayer + "\"" + ")" + "}" ); return result.isPresent() && result.get().get("acceptFriendRequest").getAsBoolean(); } /** * Delete friend or friend request * @param player * @param targetPlayer * @return true if was deleted successfully */ public boolean deleteFriend(final String player, final String targetPlayer) { final Optional result = this.graphQLApi.execute( "mutation{" + "deleteFriend(" + "token:\"" + this.token + "\"," + "player:\"" + player + "\"," + "targetPlayer:\"" + targetPlayer + "\"" + ")" + "}" ); return result.isPresent() && result.get().get("deleteFriend").getAsBoolean(); } /** * Change settings does player accept incoming friend requests * @param player * @param enabled * @return true if was changed successfully */ public boolean setFriendRequestsEnabled(final String player, final boolean enabled) { final Optional result = this.graphQLApi.execute( "mutation{" + "updateAcceptFriendRequests(" + "token:\"" + this.token + "\"," + "player:\"" + player + "\"," + "acceptRequests:\"" + (enabled ? "true" : "false") + "\"" + ")" + "}" ); return result.isPresent() && result.get().get("updateAcceptFriendRequests").getAsBoolean(); } }