11package net .olympiccode .vhackos .api .entities .impl ;
22
33import lombok .Getter ;
4+ import lombok .Setter ;
45import net .olympiccode .vhackos .api .requests .Route ;
56import net .olympiccode .vhackos .api .server .Server ;
67import 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
915public 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}
0 commit comments