This repository has been archived by the owner on Nov 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from OlympicCode/dev
14 werewolves ftw
- Loading branch information
Showing
7 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/main/java/net/olympiccode/vhackos/api/entities/impl/LeaderboardsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/net/olympiccode/vhackos/api/misc/Leaderboards.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |