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

Commit

Permalink
Merge pull request #4 from OlympicCode/dev
Browse files Browse the repository at this point in the history
14 werewolves ftw
  • Loading branch information
itsfolf authored Mar 17, 2018
2 parents 93530d1 + 87b465a commit 0dd5343
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package net.olympiccode.vhackos.api.entities.impl;

import net.olympiccode.vhackos.api.misc.Leaderboards;
import net.olympiccode.vhackos.api.requests.Route;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LeaderboardsImpl implements Leaderboards {

private final vHackOSAPIImpl api;
private long tournamentEndTimestamp;
private int tournamentRank;
private int rank;
private List<LeaderboardEntry> data;
private List<TournamentEntry> tournamentData;
private long lastReload = 0;
public LeaderboardsImpl(vHackOSAPIImpl api) {
this.api = api;
}

public long getTournamentEndTimestamp() {
reload();
return tournamentEndTimestamp;
}

public int getTournamentRank() {
reload();
return tournamentRank;
}

public int getRank() {
reload();
return rank;
}

public List<LeaderboardEntry> getData() {
reload();
return data;
}

public List<TournamentEntry> getTournamentData() {
reload();
return tournamentData;
}

public void reload() {
if (lastReload <= System.currentTimeMillis() - 10000) {
lastReload = System.currentTimeMillis();
JSONObject object = Route.Misc.LEADERBOARDS.compile(api).getResponse().getJSON();
tournamentRank = object.optInt("tournamentrank", 0);
rank = object.optInt("myrank", 0);
JSONArray dataa = object.optJSONArray("data");
List<LeaderboardEntry> datal = new ArrayList<>();
for (int i = 0; i < dataa.length(); i++) {
JSONObject entry = dataa.optJSONObject(i);
String user = entry.optString("user");
int level = entry.optInt("level");
double expPorcentage = Double.parseDouble(entry.optString("exp").replace("%", ""));
datal.add(new LeaderboardEntryImpl(user, level, expPorcentage));
}
data = datal;
List<TournamentEntry> datal2 = new ArrayList<>();
for (int i = 0; i < dataa.length(); i++) {
JSONObject entry = dataa.optJSONObject(i);
String user = entry.optString("user");
int level = entry.optInt("level");
long expGain = entry.optLong("expgain");
datal2.add(new TournamentEntryImpl(user, level, expGain));
}
tournamentData = datal2;
tournamentEndTimestamp = System.currentTimeMillis() + (object.optInt("tournamentleft", 0) * 1000);
}
}

class LeaderboardEntryImpl implements Leaderboards.LeaderboardEntry {

private String username;
private int level;
private double expPorcentage;

public LeaderboardEntryImpl(String username, int level, double expPorcentage) {
this.username = username;
this.level = level;
this.expPorcentage = expPorcentage;
}
public String getUsername() {
return username;
}

public int getLevel() {
return level;
}


public double getExpPorcentage() {
return expPorcentage;
}
}

class TournamentEntryImpl implements Leaderboards.TournamentEntry {

private String username;
private int level;
private long expGain;

public TournamentEntryImpl(String username, int level, long expGain) {
this.username = username;
this.level = level;
this.expGain = expGain;
}
public String getUsername() {
return username;
}

public int getLevel() {
return level;
}


public long getExpGain() {
return expGain;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.olympiccode.vhackos.api.events.Event;
import net.olympiccode.vhackos.api.events.EventListener;
import net.olympiccode.vhackos.api.events.StatsUpdateEvent;
import net.olympiccode.vhackos.api.misc.Leaderboards;
import net.olympiccode.vhackos.api.requests.Requester;
import net.olympiccode.vhackos.api.requests.Response;
import net.olympiccode.vhackos.api.requests.Route;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class vHackOSAPIImpl implements vHackOSAPI {
private TaskManagerImpl taskManager = new TaskManagerImpl(this);
private NetworkManagerImpl networkManager = new NetworkManagerImpl(this);
private MinerImpl miner = new MinerImpl(this);
private Leaderboards leaderboards = new LeaderboardsImpl(this);
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(corePoolSize, new APIThreadFactory());

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

public void addEventListener(Object... listener) {
for (Object o : listener) if (o instanceof EventListener) listeners.add((EventListener) o);
}
public void addEventListener(Object... listener) { for (Object o : listener) if (o instanceof EventListener) listeners.add((EventListener) o); }

public Status getStatus() {
return status;
Expand All @@ -160,9 +160,7 @@ private void setPassword(String password) {
this.password = password;
}

public void removeEventListener(Object... listener) {
for (Object o : listener) if (o instanceof EventListener) listeners.remove(o);
}
public void removeEventListener(Object... listener) { for (Object o : listener) if (o instanceof EventListener) listeners.remove(o); }

public void setDebugResponses(boolean debugResponses) {
this.debugResponses = debugResponses;
Expand Down Expand Up @@ -201,6 +199,9 @@ public NetworkManagerImpl getNetworkManager() {
}

public MinerImpl getMiner() { return miner; }

public Leaderboards getLeaderboards() { return leaderboards; }

class APIThreadFactory implements ThreadFactory {
private int counter = 0;
private String prefix = "vHackOSAPI";
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/olympiccode/vhackos/api/misc/Leaderboards.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.olympiccode.vhackos.api.misc;

import java.util.List;

public interface Leaderboards {
long getTournamentEndTimestamp();
int getTournamentRank();
int getRank();
List<LeaderboardEntry> getData();
List<TournamentEntry> getTournamentData();

interface LeaderboardEntry {
String getUsername();
int getLevel();
double getExpPorcentage();
}

interface TournamentEntry {
String getUsername();
int getLevel();
long getExpGain();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public OkHttpClient getHttpClient() {
public Response getResponse(Route.CompiledRoute route) {
if (lastRequest >= System.currentTimeMillis() - 1000) {
try {
Thread.sleep(200);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -101,6 +101,7 @@ public Response getResponse(Route.CompiledRoute route) {
}
} catch (RuntimeException | LoginException e) {
e.printStackTrace();
System.exit(0);
} catch (final Exception e) {
throw new IllegalStateException("An error occurred while processing rest request", e);
} finally {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/olympiccode/vhackos/api/requests/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public CompiledRoute compile(vHackOSAPI apir, String... params) {
try {
arguments.put("uid", api.getUid());
arguments.put("accesstoken", api.getAccessToken());
arguments.put("lang", "en");
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -102,6 +103,7 @@ public static class Misc {
public static final Route UPDATE = new Route("update");
public static final Route MINER = new Route("mining");
public static final Route MINER_ACT = new Route("mining", "action");
public static final Route LEADERBOARDS = new Route("ranking");
}

public static class AppStore {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/olympiccode/vhackos/api/vHackOSAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.olympiccode.vhackos.api.appstore.TaskManager;
import net.olympiccode.vhackos.api.entities.Stats;
import net.olympiccode.vhackos.api.events.EventListener;
import net.olympiccode.vhackos.api.misc.Leaderboards;
import net.olympiccode.vhackos.api.misc.Miner;
import net.olympiccode.vhackos.api.network.NetworkManager;

Expand Down Expand Up @@ -59,5 +60,7 @@ public boolean isInit()

Miner getMiner();

Leaderboards getLeaderboards();

}

2 changes: 1 addition & 1 deletion src/main/java/net/olympiccode/vhackos/api/vHackOSInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.olympiccode.vhackos.api;

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

0 comments on commit 0dd5343

Please sign in to comment.