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

Commit 4b9e038

Browse files
committed
Servers, Missions and Java 9
- Added Java 9 support - Added mission API - Revamped server API
1 parent 1e05dd5 commit 4b9e038

9 files changed

Lines changed: 247 additions & 82 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
compile group: 'org.json', name: 'json', version: '20090211'
1919
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
2020
compileOnly 'org.projectlombok:lombok:1.16.20'
21+
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
2122
}
2223

2324
shadowJar.destinationDir = file("/build/libs")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package net.olympiccode.vhackos.api.entities.impl;
2+
3+
import lombok.Getter;
4+
import net.olympiccode.vhackos.api.missions.MissionManager;
5+
import net.olympiccode.vhackos.api.requests.Route;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class MissionManagerImpl implements MissionManager {
13+
14+
private final vHackOSAPIImpl api;
15+
16+
int rewardStage;
17+
boolean rewardClaimed;
18+
19+
public MissionManagerImpl(vHackOSAPIImpl api) {
20+
this.api = api;
21+
}
22+
23+
void update() {
24+
JSONObject json = Route.Missions.MISSIONS.compile(api).getResponse().getJSON();
25+
rewardStage = json.optInt("stage", 0);
26+
rewardClaimed = json.optInt("claim", 1) == 0;
27+
}
28+
29+
public boolean claimReward() {
30+
if (rewardClaimed) return false;
31+
JSONObject json = Route.Missions.REWARD_CLAIM.compile(api, "100").getResponse().getJSON();
32+
if (json.optInt("claim") == 0) {
33+
rewardClaimed = true;
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
public int getRewardStage() {
40+
return rewardStage;
41+
}
42+
43+
public boolean isRewardClaimed() {
44+
return rewardClaimed;
45+
}
46+
47+
public List<DailyMission> getDailyMissions() {
48+
List<DailyMission> list = new ArrayList<>();
49+
JSONObject json = Route.Missions.MISSIONS.compile(api).getResponse().getJSON();
50+
JSONArray array = json.optJSONArray("daily");
51+
if (array != null) {
52+
for (int i = 0; i < array.length(); i++) {
53+
JSONObject object = array.optJSONObject(i);
54+
if (object != null) {
55+
list.add(new DailyMissionImpl(i, object.optInt("finished", 0) > 1, object.optInt("finished", 0) > 0,
56+
object.optInt("exp", 0),
57+
object.optInt("rewAmount", 0),
58+
RewardType.valueOf(object.optString("rewType"))));
59+
}
60+
}
61+
}
62+
return list;
63+
}
64+
65+
@Getter
66+
public class DailyMissionImpl implements DailyMission {
67+
boolean finished;
68+
boolean claimed;
69+
int expReward, rewardAmount;
70+
RewardType type;
71+
int id;
72+
public DailyMissionImpl(int id, boolean received, boolean finished, int expRewards, int rewardAmount, RewardType type) {
73+
this.claimed = received;
74+
this.expReward = expRewards;
75+
this.finished = finished;
76+
this.rewardAmount = rewardAmount;
77+
this.type = type;
78+
this.id = id;
79+
}
80+
81+
public boolean claim() {
82+
if (claimed || !finished) return false;
83+
JSONObject object = Route.Missions.DAILY_RECEIVE.compile(api, "200", "" + id).getResponse().getJSON();
84+
if (object.optInt("claimed") == 1) return true;
85+
return false;
86+
}
87+
}
88+
}
Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,104 @@
11
package net.olympiccode.vhackos.api.entities.impl;
22

33
import lombok.Getter;
4+
import lombok.Setter;
45
import net.olympiccode.vhackos.api.requests.Route;
56
import net.olympiccode.vhackos.api.server.Server;
67
import org.json.JSONObject;
78

9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Optional;
12+
import java.util.stream.Collectors;
13+
814
@Getter
915
public class ServerImpl implements Server {
1016

11-
17+
public List<ServerNode> nodes = new ArrayList<>();
1218
int packages;
13-
1419
int serverPieces;
1520
int firewallPieces;
1621
int antivirusPieces;
17-
18-
int serverStrength;
19-
int[] antivirusStrength = new int[3];
20-
int[] firewallStrength = new int[3];
21-
22-
int serverStrengthMax;
23-
int[] antivirusStrengthMax = new int[3];
24-
int[] firewallStrengthMax = new int[3];
25-
26-
int serverStars;
27-
int[] antivirusStars = new int[3];
28-
int[] firewallStars = new int[3];
29-
30-
3122
long lastUpdate = 0;
3223
private vHackOSAPIImpl api;
3324

3425
public ServerImpl(vHackOSAPIImpl api) {
3526
this.api = api;
27+
nodes.add(new ServerNodeImpl(NodeType.AV, 0, 0, 0, 0));
28+
nodes.add(new ServerNodeImpl(NodeType.AV, 1, 0, 0, 0));
29+
nodes.add(new ServerNodeImpl(NodeType.AV, 2, 0, 0, 0));
30+
31+
nodes.add(new ServerNodeImpl(NodeType.FW, 0, 0, 0, 0));
32+
nodes.add(new ServerNodeImpl(NodeType.FW, 1, 0, 0, 0));
33+
nodes.add(new ServerNodeImpl(NodeType.FW, 2, 0, 0, 0));
34+
35+
nodes.add(new ServerNodeImpl(NodeType.SERVER, 0, 0, 0, 0));
3636
}
3737

38-
public boolean upgrade(NODE_TYPE type, int node) {
39-
update();
38+
public boolean upgrade(NodeType type, int node) {
4039
JSONObject object = Route.Server.NODE_ADD.compile(api, node + "", type.getId() + "", "500").getResponse().getJSON();
41-
update();
4240
if (object.optInt("node_updated", 0) == 1) return true;
41+
update();
4342
return false;
4443
}
4544

46-
public OpenResult openAllPacks() {
45+
public boolean upgradeFive(NodeType type, int node) {
46+
JSONObject object = Route.Server.NODE_ADD.compile(api, node + "", type.getId() + "", "600").getResponse().getJSON();
47+
if (object.optInt("node_updated", 0) == 2) return true;
4748
update();
49+
return false;
50+
}
51+
52+
53+
public OpenResult openAllPacks() {
4854
if (packages == 0) return null;
4955
JSONObject object = Route.Server.ACTION.compile(api, "2000").getResponse().getJSON();
5056
update();
5157
return new OpenResultImpl(object.optInt("sServer", 0), object.optInt("sAV", 0), object.optInt("sFW", 0), object.optInt("sBoost", 0));
5258
}
5359

60+
public void update() {
61+
if (lastUpdate > System.currentTimeMillis() - 5000) return;
62+
lastUpdate = System.currentTimeMillis();
63+
JSONObject object = Route.Server.SERVER.compile(api).getResponse().getJSON();
64+
packages = object.optInt("packs", 0);
65+
66+
serverPieces = object.optInt("server_pieces", 0);
67+
firewallPieces = object.optInt("fw_pieces", 0);
68+
antivirusPieces = object.optInt("av_pieces", 0);
69+
70+
((ServerNodeImpl) getNode(NodeType.AV , 0)).setStars(object.optInt("av1_str_stars", 0));
71+
((ServerNodeImpl) getNode(NodeType.AV , 0)).setStrength(object.optInt("av1_str", 0));
72+
((ServerNodeImpl) getNode(NodeType.AV , 0)).setMaxStrength(object.optInt("av1_str_max", 0));
73+
((ServerNodeImpl) getNode(NodeType.AV , 1)).setStars(object.optInt("av2_str_stars", 0));
74+
((ServerNodeImpl) getNode(NodeType.AV , 1)).setStrength(object.optInt("av2_str", 0));
75+
((ServerNodeImpl) getNode(NodeType.AV , 1)).setMaxStrength(object.optInt("av2_str_max", 0));
76+
((ServerNodeImpl) getNode(NodeType.AV , 2)).setStars(object.optInt("av3_str_stars", 0));
77+
((ServerNodeImpl) getNode(NodeType.AV , 2)).setStrength(object.optInt("av3_str", 0));
78+
((ServerNodeImpl) getNode(NodeType.AV , 2)).setMaxStrength(object.optInt("av3_str_max", 0));
79+
80+
((ServerNodeImpl) getNode(NodeType.FW , 0)).setStars(object.optInt("fw1_str_stars", 0));
81+
((ServerNodeImpl) getNode(NodeType.FW , 0)).setStrength(object.optInt("fw1_str", 0));
82+
((ServerNodeImpl) getNode(NodeType.FW , 0)).setMaxStrength(object.optInt("fw1_str_max", 0));
83+
((ServerNodeImpl) getNode(NodeType.FW , 1)).setStars(object.optInt("fw2_str_stars", 0));
84+
((ServerNodeImpl) getNode(NodeType.FW , 1)).setStrength(object.optInt("fw2_str", 0));
85+
((ServerNodeImpl) getNode(NodeType.FW , 1)).setMaxStrength(object.optInt("fw2_str_max", 0));
86+
((ServerNodeImpl) getNode(NodeType.FW , 2)).setStars(object.optInt("fw3_str_stars", 0));
87+
((ServerNodeImpl) getNode(NodeType.FW , 2)).setStrength(object.optInt("fw3_str", 0));
88+
((ServerNodeImpl) getNode(NodeType.FW , 2)).setMaxStrength(object.optInt("fw3_str_max", 0));
89+
90+
((ServerNodeImpl) getNode(NodeType.SERVER, 0)).setStars(object.optInt("server_str_stars", 0));
91+
((ServerNodeImpl) getNode(NodeType.SERVER, 0)).setStrength(object.optInt("server_str", 0));
92+
((ServerNodeImpl) getNode(NodeType.SERVER, 0)).setMaxStrength(object.optInt("server_str_max", 0));
93+
}
94+
95+
public ServerNode getNode(NodeType type, int id) {
96+
update();
97+
return Optional.ofNullable(nodes.stream().filter(serverNode -> serverNode.getType().equals(type)).filter(serverNode -> serverNode.getId() == id).collect(Collectors.toList()).get(0)).orElse(null);
98+
}
99+
54100
@Getter
101+
@Setter
55102
public class OpenResultImpl implements OpenResult {
56103
private final int fw;
57104
private final int server;
@@ -66,43 +113,28 @@ public OpenResultImpl(int server, int av, int fw, int boost) {
66113
}
67114
}
68115

69-
public void update() {
70-
if (lastUpdate > System.currentTimeMillis() - 5000) return;
71-
lastUpdate = System.currentTimeMillis();
72-
JSONObject object = Route.Server.SERVER.compile(api).getResponse().getJSON();
73-
packages = object.optInt("packs", 0);
74-
75-
serverPieces = object.optInt("server_pieces", 0);
76-
firewallPieces = object.optInt("fw_pieces", 0);
77-
antivirusPieces = object.optInt("av_pieces", 0);
78-
79-
antivirusStrength[0] = object.optInt("av1_str", 0);
80-
antivirusStrength[1] = object.optInt("av2_str", 0);
81-
antivirusStrength[2] = object.optInt("av3_str", 0);
82-
83-
firewallStrength[0] = object.optInt("fw1_str", 0);
84-
firewallStrength[1] = object.optInt("fw2_str", 0);
85-
firewallStrength[2] = object.optInt("fw3_str", 0);
86-
87-
serverStrength = object.optInt("server_str", 0);
88-
serverStrengthMax = object.optInt("server_str_max", 0);
89-
serverStars = object.optInt("server_str_stars", 0);
90-
91-
antivirusStrengthMax[0] = object.optInt("av1_str_max", 0);
92-
antivirusStrengthMax[1] = object.optInt("av2_str_max", 0);
93-
antivirusStrengthMax[2] = object.optInt("av3_str_max", 0);
94-
95-
firewallStrengthMax[0] = object.optInt("fw1_str_max", 0);
96-
firewallStrengthMax[1] = object.optInt("fw2_str_max", 0);
97-
firewallStrengthMax[2] = object.optInt("fw3_str_max", 0);
98-
99-
antivirusStars[0] = object.optInt("av1_str_stars", 0);
100-
antivirusStars[1] = object.optInt("av2_str_stars", 0);
101-
antivirusStars[2] = object.optInt("av3_str_stars", 0);
116+
@Getter
117+
@Setter
118+
public class ServerNodeImpl implements ServerNode {
119+
NodeType type;
120+
int id, stars, strength, maxStrength;
121+
122+
public ServerNodeImpl(NodeType type, int id, int stars, int strength, int maxStrength) {
123+
this.type = type;
124+
this.id = id;
125+
this.stars = stars;
126+
this.strength = strength;
127+
this.maxStrength = maxStrength;
128+
if (maxStrength > 0) nodes.add(this);
129+
}
102130

103-
firewallStars[0] = object.optInt("fw1_str_stars", 0);
104-
firewallStars[1] = object.optInt("fw2_str_stars", 0);
105-
firewallStars[2] = object.optInt("fw3_str_stars", 0);
131+
public boolean upgrade() {
132+
return ServerImpl.this.upgrade(type, id + 1);
133+
}
134+
public boolean upgradeFive() {
135+
return ServerImpl.this.upgradeFive(type, id + 1);
136+
}
106137
}
107138

139+
108140
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.olympiccode.vhackos.api.entities.impl;
22

33
import net.olympiccode.vhackos.api.appstore.AppManager;
4+
import net.olympiccode.vhackos.api.entities.AppType;
45
import net.olympiccode.vhackos.api.entities.Stats;
56
import net.olympiccode.vhackos.api.events.Event;
67
import net.olympiccode.vhackos.api.events.EventListener;
@@ -50,6 +51,8 @@ public class vHackOSAPIImpl implements vHackOSAPI {
5051
private MinerImpl miner = new MinerImpl(this);
5152
private Leaderboards leaderboards = new LeaderboardsImpl(this);
5253
private ServerImpl server = new ServerImpl(this);
54+
private MissionManagerImpl missionManager = new MissionManagerImpl(this);
55+
5356
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(corePoolSize, new APIThreadFactory());
5457

5558
public vHackOSAPIImpl(OkHttpClient.Builder httpClientBuilder, boolean autoReconnect, int maxReconnectDelay, int corePoolSize, boolean preLogin, int[] sleepTime) {
@@ -131,11 +134,17 @@ private void setup() {
131134
LOG.info("Loading subsystems...");
132135
updateData();
133136
taskManager.reloadTasks();
137+
server.update();
138+
missionManager.update();
134139
executorService.scheduleAtFixedRate(() -> updateData(), 30000, 30000, TimeUnit.MILLISECONDS);
135140
executorService.scheduleAtFixedRate(() -> taskManager.checkTasks(), 1000, 1000, TimeUnit.MILLISECONDS);
136141
executorService.scheduleAtFixedRate(() -> taskManager.reloadTasks(), 30000, 30000, TimeUnit.MILLISECONDS);
137-
executorService.scheduleAtFixedRate(() -> server.update(), 10000, 10000, TimeUnit.MILLISECONDS);
138-
Runtime.getRuntime().addShutdownHook(new Thread(() -> executorService.shutdownNow()));
142+
if (appManager.getApp(AppType.Server).isInstalled()) executorService.scheduleAtFixedRate(() -> server.update(), 10000, 10000, TimeUnit.MILLISECONDS);
143+
if (appManager.getApp(AppType.Missions).isInstalled()) executorService.scheduleAtFixedRate(() -> missionManager.update(), 10000, 60000 * 10, TimeUnit.MILLISECONDS);
144+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
145+
LOG.info("Shutting down vHackAPI-Java...");
146+
executorService.shutdownNow();
147+
}));
139148
setStatus(Status.INITIALIZED);
140149
}
141150

@@ -256,6 +265,10 @@ public Server getServer() {
256265
return server;
257266
}
258267

268+
public MissionManagerImpl getMissionManager() {
269+
return missionManager;
270+
}
271+
259272
class APIThreadFactory implements ThreadFactory {
260273
private int counter = 0;
261274
private String prefix = "vHackOSAPI";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.olympiccode.vhackos.api.missions;
2+
3+
import net.olympiccode.vhackos.api.entities.impl.MissionManagerImpl;
4+
5+
import java.util.List;
6+
7+
public interface MissionManager {
8+
boolean claimReward();
9+
int getRewardStage();
10+
boolean isRewardClaimed();
11+
List<DailyMission> getDailyMissions();
12+
enum RewardType {
13+
NetCoins, Boosters;
14+
}
15+
16+
interface DailyMission {
17+
boolean claim();
18+
boolean isFinished();
19+
boolean isClaimed();
20+
int getExpReward();
21+
int getRewardAmount();
22+
RewardType getType();
23+
}
24+
25+
26+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ public static class Server {
134134
public static final Route ACTION = new Route("server", "action");
135135
}
136136

137+
public static class Missions {
138+
public static final Route MISSIONS = new Route("missions");
139+
public static final Route DAILY_RECEIVE = new Route("missions", "action", "dailyid");
140+
public static final Route REWARD_CLAIM = new Route("missions", "action");
141+
}
142+
137143
public class CompiledRoute {
138144
private final Route baseRoute;
139145
private final String compiledRoute;

0 commit comments

Comments
 (0)