Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Commit 0dd5343

Browse files
authored
Merge pull request #4 from OlympicCode/dev
14 werewolves ftw
2 parents 93530d1 + 87b465a commit 0dd5343

File tree

7 files changed

+165
-8
lines changed

7 files changed

+165
-8
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package net.olympiccode.vhackos.api.entities.impl;
2+
3+
import net.olympiccode.vhackos.api.misc.Leaderboards;
4+
import net.olympiccode.vhackos.api.requests.Route;
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class LeaderboardsImpl implements Leaderboards {
12+
13+
private final vHackOSAPIImpl api;
14+
private long tournamentEndTimestamp;
15+
private int tournamentRank;
16+
private int rank;
17+
private List<LeaderboardEntry> data;
18+
private List<TournamentEntry> tournamentData;
19+
private long lastReload = 0;
20+
public LeaderboardsImpl(vHackOSAPIImpl api) {
21+
this.api = api;
22+
}
23+
24+
public long getTournamentEndTimestamp() {
25+
reload();
26+
return tournamentEndTimestamp;
27+
}
28+
29+
public int getTournamentRank() {
30+
reload();
31+
return tournamentRank;
32+
}
33+
34+
public int getRank() {
35+
reload();
36+
return rank;
37+
}
38+
39+
public List<LeaderboardEntry> getData() {
40+
reload();
41+
return data;
42+
}
43+
44+
public List<TournamentEntry> getTournamentData() {
45+
reload();
46+
return tournamentData;
47+
}
48+
49+
public void reload() {
50+
if (lastReload <= System.currentTimeMillis() - 10000) {
51+
lastReload = System.currentTimeMillis();
52+
JSONObject object = Route.Misc.LEADERBOARDS.compile(api).getResponse().getJSON();
53+
tournamentRank = object.optInt("tournamentrank", 0);
54+
rank = object.optInt("myrank", 0);
55+
JSONArray dataa = object.optJSONArray("data");
56+
List<LeaderboardEntry> datal = new ArrayList<>();
57+
for (int i = 0; i < dataa.length(); i++) {
58+
JSONObject entry = dataa.optJSONObject(i);
59+
String user = entry.optString("user");
60+
int level = entry.optInt("level");
61+
double expPorcentage = Double.parseDouble(entry.optString("exp").replace("%", ""));
62+
datal.add(new LeaderboardEntryImpl(user, level, expPorcentage));
63+
}
64+
data = datal;
65+
List<TournamentEntry> datal2 = new ArrayList<>();
66+
for (int i = 0; i < dataa.length(); i++) {
67+
JSONObject entry = dataa.optJSONObject(i);
68+
String user = entry.optString("user");
69+
int level = entry.optInt("level");
70+
long expGain = entry.optLong("expgain");
71+
datal2.add(new TournamentEntryImpl(user, level, expGain));
72+
}
73+
tournamentData = datal2;
74+
tournamentEndTimestamp = System.currentTimeMillis() + (object.optInt("tournamentleft", 0) * 1000);
75+
}
76+
}
77+
78+
class LeaderboardEntryImpl implements Leaderboards.LeaderboardEntry {
79+
80+
private String username;
81+
private int level;
82+
private double expPorcentage;
83+
84+
public LeaderboardEntryImpl(String username, int level, double expPorcentage) {
85+
this.username = username;
86+
this.level = level;
87+
this.expPorcentage = expPorcentage;
88+
}
89+
public String getUsername() {
90+
return username;
91+
}
92+
93+
public int getLevel() {
94+
return level;
95+
}
96+
97+
98+
public double getExpPorcentage() {
99+
return expPorcentage;
100+
}
101+
}
102+
103+
class TournamentEntryImpl implements Leaderboards.TournamentEntry {
104+
105+
private String username;
106+
private int level;
107+
private long expGain;
108+
109+
public TournamentEntryImpl(String username, int level, long expGain) {
110+
this.username = username;
111+
this.level = level;
112+
this.expGain = expGain;
113+
}
114+
public String getUsername() {
115+
return username;
116+
}
117+
118+
public int getLevel() {
119+
return level;
120+
}
121+
122+
123+
public long getExpGain() {
124+
return expGain;
125+
}
126+
}
127+
}

src/main/java/net/olympiccode/vhackos/api/entities/impl/vHackOSAPIImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.olympiccode.vhackos.api.events.Event;
66
import net.olympiccode.vhackos.api.events.EventListener;
77
import net.olympiccode.vhackos.api.events.StatsUpdateEvent;
8+
import net.olympiccode.vhackos.api.misc.Leaderboards;
89
import net.olympiccode.vhackos.api.requests.Requester;
910
import net.olympiccode.vhackos.api.requests.Response;
1011
import net.olympiccode.vhackos.api.requests.Route;
@@ -44,6 +45,7 @@ public class vHackOSAPIImpl implements vHackOSAPI {
4445
private TaskManagerImpl taskManager = new TaskManagerImpl(this);
4546
private NetworkManagerImpl networkManager = new NetworkManagerImpl(this);
4647
private MinerImpl miner = new MinerImpl(this);
48+
private Leaderboards leaderboards = new LeaderboardsImpl(this);
4749
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(corePoolSize, new APIThreadFactory());
4850

4951
public vHackOSAPIImpl(OkHttpClient.Builder httpClientBuilder, boolean autoReconnect, int maxReconnectDelay, int corePoolSize) {
@@ -132,9 +134,7 @@ public void setStatus(Status status) {
132134
this.status = status;
133135
}
134136

135-
public void addEventListener(Object... listener) {
136-
for (Object o : listener) if (o instanceof EventListener) listeners.add((EventListener) o);
137-
}
137+
public void addEventListener(Object... listener) { for (Object o : listener) if (o instanceof EventListener) listeners.add((EventListener) o); }
138138

139139
public Status getStatus() {
140140
return status;
@@ -160,9 +160,7 @@ private void setPassword(String password) {
160160
this.password = password;
161161
}
162162

163-
public void removeEventListener(Object... listener) {
164-
for (Object o : listener) if (o instanceof EventListener) listeners.remove(o);
165-
}
163+
public void removeEventListener(Object... listener) { for (Object o : listener) if (o instanceof EventListener) listeners.remove(o); }
166164

167165
public void setDebugResponses(boolean debugResponses) {
168166
this.debugResponses = debugResponses;
@@ -201,6 +199,9 @@ public NetworkManagerImpl getNetworkManager() {
201199
}
202200

203201
public MinerImpl getMiner() { return miner; }
202+
203+
public Leaderboards getLeaderboards() { return leaderboards; }
204+
204205
class APIThreadFactory implements ThreadFactory {
205206
private int counter = 0;
206207
private String prefix = "vHackOSAPI";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.olympiccode.vhackos.api.misc;
2+
3+
import java.util.List;
4+
5+
public interface Leaderboards {
6+
long getTournamentEndTimestamp();
7+
int getTournamentRank();
8+
int getRank();
9+
List<LeaderboardEntry> getData();
10+
List<TournamentEntry> getTournamentData();
11+
12+
interface LeaderboardEntry {
13+
String getUsername();
14+
int getLevel();
15+
double getExpPorcentage();
16+
}
17+
18+
interface TournamentEntry {
19+
String getUsername();
20+
int getLevel();
21+
long getExpGain();
22+
}
23+
}

src/main/java/net/olympiccode/vhackos/api/requests/Requester.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public OkHttpClient getHttpClient() {
4949
public Response getResponse(Route.CompiledRoute route) {
5050
if (lastRequest >= System.currentTimeMillis() - 1000) {
5151
try {
52-
Thread.sleep(200);
52+
Thread.sleep(1000);
5353
} catch (InterruptedException e) {
5454
e.printStackTrace();
5555
}
@@ -101,6 +101,7 @@ public Response getResponse(Route.CompiledRoute route) {
101101
}
102102
} catch (RuntimeException | LoginException e) {
103103
e.printStackTrace();
104+
System.exit(0);
104105
} catch (final Exception e) {
105106
throw new IllegalStateException("An error occurred while processing rest request", e);
106107
} finally {

src/main/java/net/olympiccode/vhackos/api/requests/Route.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public CompiledRoute compile(vHackOSAPI apir, String... params) {
5050
try {
5151
arguments.put("uid", api.getUid());
5252
arguments.put("accesstoken", api.getAccessToken());
53+
arguments.put("lang", "en");
5354
} catch (JSONException e) {
5455
e.printStackTrace();
5556
}
@@ -102,6 +103,7 @@ public static class Misc {
102103
public static final Route UPDATE = new Route("update");
103104
public static final Route MINER = new Route("mining");
104105
public static final Route MINER_ACT = new Route("mining", "action");
106+
public static final Route LEADERBOARDS = new Route("ranking");
105107
}
106108

107109
public static class AppStore {

src/main/java/net/olympiccode/vhackos/api/vHackOSAPI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.olympiccode.vhackos.api.appstore.TaskManager;
55
import net.olympiccode.vhackos.api.entities.Stats;
66
import net.olympiccode.vhackos.api.events.EventListener;
7+
import net.olympiccode.vhackos.api.misc.Leaderboards;
78
import net.olympiccode.vhackos.api.misc.Miner;
89
import net.olympiccode.vhackos.api.network.NetworkManager;
910

@@ -59,5 +60,7 @@ public boolean isInit()
5960

6061
Miner getMiner();
6162

63+
Leaderboards getLeaderboards();
64+
6265
}
6366

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.olympiccode.vhackos.api;
22

33
public class vHackOSInfo {
4-
private static final int REST_VERSION = 10;
4+
private static final int REST_VERSION = 11;
55
public static final String API_PREFIX = String.format("https://api.vhack.cc/mobile/%d/", REST_VERSION);
66
}

0 commit comments

Comments
 (0)