Skip to content

Commit 4286fff

Browse files
committed
Clean up DestinyCharacter and streamline abstraction
1 parent fa69a54 commit 4286fff

File tree

5 files changed

+148
-62
lines changed

5 files changed

+148
-62
lines changed

src/main/java/material/stats/Activity.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ public class Activity extends ContentFramework {
3232
private int[] modes;
3333

3434
public Activity(String activityId) {
35-
super("https://stats.bungie.net/Platform/Destiny2/Stats/PostGameCarnageReport/" + activityId + "/");
35+
super("https://stats.bungie.net/Platform/Destiny2/Stats/PostGameCarnageReport/" + activityId + "/", source -> {
36+
return source.getAsJsonObject("Response");
37+
});
3638
this.activityId = activityId;
3739
}
3840

3941
/**
4042
* Initialize an activity with more information which could improve load times
4143
*/
4244
public Activity(String activityId, String referenceId, String directoryActivityHash, String rawDate, int mode, int[] modes) {
43-
super("https://stats.bungie.net/Platform/Destiny2/Stats/PostGameCarnageReport/" + activityId + "/");
45+
super("https://stats.bungie.net/Platform/Destiny2/Stats/PostGameCarnageReport/" + activityId + "/", source -> {
46+
return source.getAsJsonObject("Response");
47+
});
4448
this.activityId = activityId;
4549
this.referenceId = referenceId;
4650
this.directoryActivityHash = directoryActivityHash;
@@ -55,7 +59,7 @@ public Activity(String activityId, String referenceId, String directoryActivity
5559
*/
5660
public Date getDatePlayed() {
5761
checkJO();
58-
return time == null ? time = StringUtils.valueOfZTime(jo.get("period").getAsString()) : time;
62+
return time == null ? time = StringUtils.valueOfZTime(getJO().get("period").getAsString()) : time;
5963
}
6064

6165
/**
@@ -64,31 +68,31 @@ public Date getDatePlayed() {
6468
*/
6569
public String getReferenceId() {
6670
checkJO();
67-
return referenceId == null ? referenceId = jo.getAsJsonObject("activityDetails").get("referenceId").getAsString() : referenceId;
71+
return referenceId == null ? referenceId = getJO().getAsJsonObject("activityDetails").get("referenceId").getAsString() : referenceId;
6872
}
6973

7074
/**
7175
* Get the director activity hash (the type of activity played?)
7276
*/
7377
public String getDirectoryActivityHash() {
7478
checkJO();
75-
return directoryActivityHash == null ? directoryActivityHash = jo.getAsJsonObject("activityDetails").get("directorActivityHash").getAsString() : directoryActivityHash;
79+
return directoryActivityHash == null ? directoryActivityHash = getJO().getAsJsonObject("activityDetails").get("directorActivityHash").getAsString() : directoryActivityHash;
7680
}
7781

7882
/**
7983
* Gets the instance id, which happens to be the same as the activityId :)
8084
= */
8185
public String getInstanceId() {
8286
checkJO();
83-
return instanceId == null ? instanceId = jo.get("instanceId").getAsString() : instanceId;
87+
return instanceId == null ? instanceId = getJO().get("instanceId").getAsString() : instanceId;
8488
}
8589

8690
/**
8791
* Get the mode number of the Activity
8892
*/
8993
private int getModeNumber() {
9094
checkJO();
91-
return mode == -1 ? mode = jo.getAsJsonObject("activityDetails").get("mode").getAsInt() : mode;
95+
return mode == -1 ? mode = getJO().getAsJsonObject("activityDetails").get("mode").getAsInt() : mode;
9296
}
9397

9498
/**
@@ -114,7 +118,7 @@ public List<ActivityParticipant> getParticipants() {
114118
checkJO();
115119

116120
List<ActivityParticipant> temp = new ArrayList<>();
117-
for(JsonElement je : jo.get("entries").getAsJsonArray()) {
121+
for(JsonElement je : getJO().get("entries").getAsJsonArray()) {
118122
temp.add(new ActivityParticipant(je.getAsJsonObject()));
119123
}
120124
return temp;
@@ -125,6 +129,6 @@ public List<ActivityParticipant> getParticipants() {
125129
*/
126130
public JsonObject getJsonObject() {
127131
checkJO();
128-
return jo;
132+
return getJO();
129133
}
130134
}

src/main/java/material/user/BungieUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public List<DestinyCharacter> getCharacters() {
170170
public int getTimePlayed() {
171171
if (playTime != -1) { return playTime; }
172172
for (DestinyCharacter c : getCharacters()) {
173-
playTime += Integer.parseInt(c.getMinutesPlayedTotal());
173+
playTime += c.getMinutesPlayedTotal();
174174
}
175175
return playTime;
176176
}

src/main/java/material/user/DestinyCharacter.java

Lines changed: 101 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,127 @@
1616
import material.stats.Activity;
1717
import utils.HttpUtils;
1818
import utils.StringUtils;
19+
import utils.framework.ContentFramework;
20+
import utils.framework.ContentInterface;
1921

2022
import java.util.ArrayList;
2123
import java.util.Date;
2224
import java.util.List;
2325

24-
public class DestinyCharacter {
26+
public class DestinyCharacter extends ContentFramework {
27+
28+
private BungieUser bungieUser;
2529

26-
private String membershipID;
2730
private String characterID;
2831
private Date lastPlayed;
29-
private String minutesPlayedThisSession;
30-
private String minutesPlayedTotal;
31-
private String lightLevel;
32-
private String membershipType;
32+
private int minutesPlayedThisSession, minutesPlayedTotal, lightLevel = -1;
3333

3434
private Gender gender;
3535
private DestinyClass d2class;
3636
private Race race;
3737

38-
private String emblemPath;
39-
private String emblemBackgroundPath;
40-
private String emblemHash;
38+
private String emblemPath, emblemBackgroundPath, emblemHash;
4139

4240
private List<Activity> allActivities;
4341

4442
HttpUtils hu = new HttpUtils();
45-
private JsonObject jo;
4643

4744
public DestinyCharacter(BungieUser bungieUser, String characterID) {
45+
super("https://www.bungie.net/Platform/Destiny2/" + bungieUser.getMembershipType() + "/Profile/" + bungieUser.getBungieMembershipID() + "/Character/" + characterID + "/?components=200",
46+
source -> source.getAsJsonObject("Response").getAsJsonObject("character").getAsJsonObject("data"));
4847
this.characterID = characterID;
49-
jo = hu.urlRequestGET("https://www.bungie.net/Platform/Destiny2/" + bungieUser.getMembershipType() + "/Profile/" + bungieUser.getBungieMembershipID() + "/Character/" + characterID + "/?components=200").getAsJsonObject("Response").getAsJsonObject("character").getAsJsonObject("data");
50-
assignValues();
48+
this.bungieUser = bungieUser;
5149
}
5250

53-
private void assignValues() {
54-
membershipID = jo.get("membershipId").getAsString();
55-
membershipType = jo.get("membershipType").getAsString();
56-
characterID = jo.get("characterId").getAsString();
57-
lastPlayed = StringUtils.valueOfZTime(jo.get("dateLastPlayed").getAsString());
58-
minutesPlayedThisSession = jo.get("minutesPlayedThisSession").getAsString();
59-
minutesPlayedTotal = jo.get("minutesPlayedTotal").getAsString();
60-
lightLevel = jo.get("light").getAsString();
61-
62-
race = evaluateRace(jo.get("raceHash").getAsString());
63-
gender = evaluateGender(jo.get("genderHash").getAsString());
64-
d2class = evaluateClass(jo.get("classHash").getAsString());
51+
public String getMembershipID() {
52+
return bungieUser.getBungieMembershipID();
53+
}
6554

66-
emblemPath = jo.get("emblemPath").getAsString();
67-
emblemBackgroundPath = jo.get("emblemBackgroundPath").getAsString();
68-
emblemHash = jo.get("emblemHash").getAsString();
55+
public int getMembershipType() {
56+
return bungieUser.getMembershipType();
6957
}
7058

71-
public String getMembershipID() { return membershipID; }
72-
public String getMembershipType() { return membershipType; }
7359
public String getCharacterID() { return characterID; }
74-
public Date getLastPlayed() { return lastPlayed; }
75-
public String getMinutesPlayedThisSession() { return minutesPlayedThisSession; }
76-
public String getMinutesPlayedTotal() { return minutesPlayedTotal; }
77-
public String getLightLevel() { return lightLevel; }
7860

79-
public Race getRace() { return race; }
80-
public Gender getGender() { return gender; }
81-
public DestinyClass getD2class() { return d2class; }
61+
public Date getLastPlayed() {
62+
if (lastPlayed == null) {
63+
lastPlayed = StringUtils.valueOfZTime(getJO().get("dateLastPlayed").getAsString());
64+
}
65+
return lastPlayed;
66+
}
67+
68+
public int getMinutesPlayedThisSession() {
69+
if (minutesPlayedThisSession == -1) {
70+
minutesPlayedThisSession = getJO().get("minutesPlayedThisSession").getAsInt();
71+
}
72+
return minutesPlayedThisSession;
73+
}
74+
75+
public int getMinutesPlayedTotal() {
76+
if (minutesPlayedTotal == -1) {
77+
minutesPlayedTotal = getJO().get("minutesPlayedTotal").getAsInt();
78+
}
79+
return minutesPlayedTotal;
80+
}
81+
82+
public int getLightLevel() {
83+
if (lightLevel == -1) {
84+
lightLevel = getJO().get("light").getAsInt();
85+
}
86+
return lightLevel;
87+
}
88+
89+
public Race getRace() {
90+
if (race == null) {
91+
race = evaluateRace(getJO().get("raceHash").getAsString());
92+
}
93+
return race;
94+
}
95+
96+
public Gender getGender() {
97+
if (gender == null) {
98+
gender = evaluateGender(getJO().get("genderHash").getAsString());
99+
}
100+
return gender;
101+
}
102+
103+
public DestinyClass getD2class() {
104+
if (d2class == null) {
105+
d2class = evaluateClass(getJO().get("classHash").getAsString());
106+
}
107+
return d2class;
108+
}
109+
110+
public String getEmblemPath() {
111+
if (emblemPath == null) {
112+
emblemPath = getJO().get("emblemPath").getAsString();
113+
}
114+
return emblemPath;
115+
}
82116

83-
public String getEmblemPath() { return emblemPath; }
84-
public String getEmblemBackgroundPath() { return emblemBackgroundPath; }
85-
public String getEmblemHash() { return emblemHash; }
86-
public JsonObject getJsonObject() { return jo; }
117+
public String getEmblemBackgroundPath() {
118+
if (emblemBackgroundPath == null) {
119+
emblemBackgroundPath = getJO().get("emblemBackgroundPath").getAsString();
120+
}
121+
return emblemBackgroundPath;
122+
}
123+
124+
public String getEmblemHash() {
125+
if (emblemHash == null) {
126+
emblemHash = getJO().get("emblemHash").getAsString();
127+
}
128+
return emblemHash;
129+
}
130+
131+
public JsonObject getJsonObject() { return getJO(); }
87132

88133
public List<DestinyItem> getEquippedItems() {
89-
JsonArray jsonArray = new HttpUtils().urlRequestGET("https://www.bungie.net/Platform/Destiny2/" + getMembershipType() + "/Profile/" + membershipID + "/Character/"
90-
+ getCharacterID() + "/?components=205").getAsJsonObject("Response").getAsJsonObject("equipment").getAsJsonObject("data").getAsJsonArray("items");
134+
JsonArray jsonArray = new HttpUtils().urlRequestGET("https://www.bungie.net/Platform/Destiny2/" + getMembershipType() + "/Profile/" + bungieUser.getBungieMembershipID() + "/Character/"
135+
+ getCharacterID() + "/?components=205").getAsJsonObject("Response").getAsJsonObject("equipment").getAsJsonObject("data").getAsJsonArray("items");
91136

92137
List<DestinyItem> destinyItems = new ArrayList<>();
93138

94-
for(JsonElement jsonElement : jsonArray) {
139+
for (JsonElement jsonElement : jsonArray) {
95140
destinyItems.add(new DestinyItem(jsonElement.getAsJsonObject().get("itemHash").getAsString()));
96141
}
97142

@@ -101,20 +146,21 @@ public List<DestinyItem> getEquippedItems() {
101146
/**
102147
* A very resource intensive task, use at your own risk
103148
* Needs work because not all activities return the same JSON info
104-
= */
149+
* =
150+
*/
105151
public List<Activity> getAllActivities() {
106-
if(allActivities != null) return allActivities;
152+
if (allActivities != null) { return allActivities; }
107153
allActivities = new ArrayList<>();
108154
JsonObject jj = hu.urlRequestGET("https://www.bungie.net/Platform/Destiny2/" + getMembershipType() + "/Account/" + getMembershipID() + "/Character/" + getCharacterID() + "/Stats/AggregateActivityStats/");
109-
for(JsonElement je : jj.getAsJsonObject("Response").getAsJsonArray("activities")) {
155+
for (JsonElement je : jj.getAsJsonObject("Response").getAsJsonArray("activities")) {
110156
allActivities.add(new Activity(je.getAsJsonObject().getAsJsonObject("values").getAsJsonObject("fastestCompletionMsForActivity").get("activityId").getAsString()));
111157
}
112158
return allActivities;
113159
}
114160

115161
private Gender evaluateGender(String genderHash) {
116162
JsonObject jj = hu.manifestGET(ManifestEntityTypes.GENDER, genderHash).getAsJsonObject("Response");
117-
switch(jj.get("genderType").getAsString()) {
163+
switch (jj.get("genderType").getAsString()) {
118164
case "0":
119165
return Gender.MALE;
120166
case "1":
@@ -125,7 +171,7 @@ private Gender evaluateGender(String genderHash) {
125171

126172
private DestinyClass evaluateClass(String classHash) {
127173
JsonObject jj = hu.manifestGET(ManifestEntityTypes.CLASS, classHash).getAsJsonObject("Response");
128-
switch(jj.getAsJsonObject("displayProperties").get("name").getAsString()) {
174+
switch (jj.getAsJsonObject("displayProperties").get("name").getAsString()) {
129175
case "Warlock":
130176
return DestinyClass.WARLOCK;
131177
case "Titan":
@@ -138,7 +184,7 @@ private DestinyClass evaluateClass(String classHash) {
138184

139185
private Race evaluateRace(String raceHash) {
140186
JsonObject jj = hu.manifestGET(ManifestEntityTypes.RACE, raceHash).getAsJsonObject("Response");
141-
switch(jj.getAsJsonObject("displayProperties").get("name").getAsString()) {
187+
switch (jj.getAsJsonObject("displayProperties").get("name").getAsString()) {
142188
case "Exo":
143189
return Race.EXO;
144190
case "Awoken":
@@ -154,9 +200,11 @@ public enum Gender {
154200
FEMALE("Female");
155201

156202
private String value;
203+
157204
private Gender(String value) {
158205
this.value = value;
159206
}
207+
160208
public String getValue() { return value; }
161209

162210
}
@@ -167,9 +215,11 @@ public enum DestinyClass {
167215
WARLOCK("Warlock");
168216

169217
private String value;
218+
170219
private DestinyClass(String value) {
171220
this.value = value;
172221
}
222+
173223
public String getValue() { return value; }
174224
}
175225

@@ -179,9 +229,11 @@ public enum Race {
179229
HUMAN("Human");
180230

181231
private String value;
232+
182233
private Race(String value) {
183234
this.value = value;
184235
}
236+
185237
public String getValue() { return value; }
186238
}
187239
}

src/main/java/utils/framework/ContentFramework.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,29 @@
1111
import com.google.gson.JsonObject;
1212
import utils.HttpUtils;
1313

14+
/**
15+
* Used as a standard framework for most stuff that bases its content off of a single request
16+
*/
1417
public class ContentFramework implements ContentInterface {
1518

1619
private String url;
17-
protected JsonObject jo = null;
20+
private JsonObject jo = null;
21+
private JsonObjectModifier jsonObjectModifier;
1822

19-
public ContentFramework(String url) {
23+
public ContentFramework(String url, JsonObjectModifier jsonObjectModifier) {
2024
this.url = url;
25+
this.jsonObjectModifier = jsonObjectModifier;
2126
}
2227

2328
@Override
2429
public void checkJO() {
2530
if(jo == null) {
26-
jo = new HttpUtils().urlRequestGET(url).getAsJsonObject("Response");
31+
jo = jsonObjectModifier.modify(new HttpUtils().urlRequestGET(url));
2732
}
2833
}
34+
35+
public JsonObject getJO() {
36+
checkJO();
37+
return jo;
38+
}
2939
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) dec4234 2021. Access is granted, without any express warranties or guarantees of
3+
* any kind, to all wishing to use this software for their benefit. No one may specifically claim credit, or
4+
* ownership of this software without the explicit permission of the author.
5+
*
6+
* GitHub -> https://github.com/dec4234/JavaDestinyAPI
7+
*/
8+
9+
package utils.framework;
10+
11+
import com.google.gson.JsonObject;
12+
13+
/**
14+
* Used to modify JsonObjects returned by HttpRequests
15+
*/
16+
@FunctionalInterface
17+
public interface JsonObjectModifier {
18+
19+
JsonObject modify(JsonObject source);
20+
}

0 commit comments

Comments
 (0)