|
| 1 | +package material.clan; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import material.user.BungieUser; |
| 7 | +import material.DestinyAPI; |
| 8 | +import utils.HttpUtils; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class Clan { |
| 15 | + |
| 16 | + private String apiKey = DestinyAPI.getApiKey(); |
| 17 | + private HttpUtils hu = new HttpUtils(); |
| 18 | + private JsonObject jo; // The entire Clan response |
| 19 | + private JsonObject cjo; // "detail" |
| 20 | + |
| 21 | + private long clanId; |
| 22 | + private String clanName = null; |
| 23 | + |
| 24 | + // Details about the clan (more or less in the order they appear in the JSON response) |
| 25 | + private String clanDescription; |
| 26 | + private Date creationDate; |
| 27 | + private int memberCount; |
| 28 | + private boolean isPublic; |
| 29 | + private String motto; |
| 30 | + |
| 31 | + private boolean allowChat; |
| 32 | + |
| 33 | + private BungieUser founder; |
| 34 | + private List<BungieUser> admins; |
| 35 | + private List<BungieUser> members = new ArrayList<>(); |
| 36 | + |
| 37 | + public Clan(long clanId) { |
| 38 | + this.clanId = clanId; |
| 39 | + cjo = hu.urlRequestGET("https://www.bungie.net/platform/GroupV2/" + clanId +"/?components=200").get("Response").getAsJsonObject().get("detail").getAsJsonObject(); |
| 40 | + assignValues(); |
| 41 | + } |
| 42 | + |
| 43 | + public Clan(String clanName) { |
| 44 | + this.clanName = clanName; |
| 45 | + jo = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/Name/" + clanName.replace(" ", "%20") +"/1/?components=200").get("Response").getAsJsonObject(); |
| 46 | + cjo = jo.get("detail").getAsJsonObject(); |
| 47 | + assignValues(); |
| 48 | + } |
| 49 | + |
| 50 | + private void assignValues() { |
| 51 | + if(clanName == null) { // If the clan object was created via ID then the clanName would be null by default |
| 52 | + clanName = cjo.get("name").getAsString(); |
| 53 | + } else { // Opposite of previous reason |
| 54 | + clanId = cjo.get("groupId").getAsLong(); |
| 55 | + } |
| 56 | + // creationDate = StringUtils.valueOfZTime(cjo.get("creationDate").getAsString()); // The date the clan was created |
| 57 | + clanDescription = cjo.get("about").getAsString(); |
| 58 | + memberCount = cjo.get("memberCount").getAsInt(); |
| 59 | + isPublic = cjo.get("isPublic").getAsBoolean(); |
| 60 | + motto = cjo.get("motto").getAsString(); |
| 61 | + allowChat = cjo.get("allowChat").getAsBoolean(); |
| 62 | + |
| 63 | + // founder = new BungieUser(jo.get("founder").getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString()); |
| 64 | + } |
| 65 | + |
| 66 | + public String getClanID() { |
| 67 | + return clanId + ""; |
| 68 | + } |
| 69 | + |
| 70 | + public String getClanName() { |
| 71 | + return clanName; |
| 72 | + } |
| 73 | + |
| 74 | + public String getClanDescription() { return clanDescription; } |
| 75 | + |
| 76 | + public Date getCreationDate() { return creationDate; } |
| 77 | + |
| 78 | + public int getMemberCount() { return memberCount; } |
| 79 | + |
| 80 | + public boolean isPublic() { return isPublic; } |
| 81 | + |
| 82 | + public String getMotto() { return motto; } |
| 83 | + |
| 84 | + public boolean isAllowChat() { return allowChat; } |
| 85 | + |
| 86 | + public BungieUser getFounder() { return founder; } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a list of the founder and the admins of the clan |
| 90 | + * The founder is always the first in this list? |
| 91 | + * Followed by the admins in the order they were promoted |
| 92 | + */ |
| 93 | + public List<BungieUser> getAdmins() { |
| 94 | + if(admins != null) return admins; |
| 95 | + |
| 96 | + List<BungieUser> temp = new ArrayList<>(); |
| 97 | + JsonArray ja = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/" + clanId + "/AdminsAndFounder/?componenets=200").get("Response").getAsJsonObject().get("results").getAsJsonArray(); |
| 98 | + |
| 99 | + for(JsonElement je : ja) { |
| 100 | + temp.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString())); |
| 101 | + } |
| 102 | + |
| 103 | + return temp; |
| 104 | + } |
| 105 | + |
| 106 | + public List<BungieUser> getMembers() { |
| 107 | + if(!members.isEmpty()) return members; |
| 108 | + |
| 109 | + JsonObject jo = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/" + clanId + "/Members/").get("Response").getAsJsonObject(); |
| 110 | + |
| 111 | + for(JsonElement je : jo.get("results").getAsJsonArray()) { |
| 112 | + members.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString())); |
| 113 | + } |
| 114 | + |
| 115 | + return members; |
| 116 | + } |
| 117 | +} |
0 commit comments