Skip to content

Commit

Permalink
versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
quiquelhappy committed Mar 10, 2021
1 parent 046febd commit 21d0bb4
Show file tree
Hide file tree
Showing 15 changed files with 425 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>

</dependencies>

<build>
Expand Down
89 changes: 82 additions & 7 deletions src/io/purecore/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.purecore.api.call.ApiException;
import io.purecore.api.call.Call;
import io.purecore.api.call.Param;
import io.purecore.api.event.Handler;
import io.purecore.api.instance.Instance;
import io.purecore.api.key.Key;
import io.purecore.api.versioning.Game;
import io.purecore.api.versioning.GameSoftware;
import io.purecore.api.versioning.Version;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import okhttp3.OkHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -22,9 +31,18 @@ public class Core {
protected static OkHttpClient client = new OkHttpClient();

protected static Socket socket;
protected static String clientVersion;
protected static boolean connected = false;
protected static ArrayList<Handler> handlers = new ArrayList<>();

public static String getClientVersion(){
return Core.clientVersion;
}

public static void setClientVersion(String version){
Core.clientVersion=version;
}

public Core(String key){
Core.key = new Key(key);
if(Core.client==null){
Expand Down Expand Up @@ -57,24 +75,40 @@ public static boolean isConnected(){
public static void emit(String key, String string){
Core.socket.emit(key,string);
}
public static void emit(String key, JsonElement data){
Core.socket.emit(key,new JSONObject(data));
public static void emit(String key, JsonElement data) throws JSONException {
Core.socket.emit(key,new JSONObject(new Gson().toJson(data)));
}
public static void emit(String key, HashMap<String,String> data){
Core.emit(key, new Gson().toJson(data));
public static void emit(String key, HashMap<String,String> data) throws JSONException {
Core.emit(key, new Gson().toJsonTree(data));
}

public Instance getInstance(){
return new Instance();
}

public Version getVersionCandidate(Game game, GameSoftware gameSoftware, String gameVersion) throws IOException, ApiException {
JsonElement element = Core.call("/version/get/candidate/")
.addParam(Param.GameVersion,gameVersion)
.addParam(Param.Game, String.valueOf(game))
.addParam(Param.GameSoftware, String.valueOf(gameSoftware))
.commit();
return new Version(element.getAsJsonObject());
}

private static void connect(){
URI uri = URI.create("https://socket.purecore.io/instances/");
Core.socket = IO.socket(uri);

URI uri = URI.create("https://socket.purecore.io:443/");
IO.Options options = new IO.Options();
options.path="/instances/";

Core.socket = IO.socket(uri,options);
Core.socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
Core.socket.emit("handshake",Core.key.getHash());
for (Handler handler:Core.handlers){
handler.onAuthenticating();
}
Core.emit("handshake",Core.key.getHash());
}
}).on("connected", new Emitter.Listener() {
@Override
Expand All @@ -100,7 +134,48 @@ public void call(Object... objects) {
handler.onNewExecutions();
}
}
}).on("checkVersion", new Emitter.Listener() {
@Override
public void call(Object... objects) {
JsonElement body = new Gson().fromJson(String.valueOf(objects[0]), JsonElement.class);
Version version = new Version(body.getAsJsonObject());
for (Handler handler:Core.handlers){
handler.onNewVersion(version);
}
}
}).on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
@Override
public void call(Object... objects) {
for (Handler handler:Core.handlers){
handler.onConnecting();
}
}
}).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... objects) {
for (Handler handler:Core.handlers){
handler.onConnectError();
}
}
}).on(Socket.EVENT_RECONNECTING, new Emitter.Listener() {
@Override
public void call(Object... objects) {
for (Handler handler:Core.handlers){
handler.onReconnecting();
}
}
}).on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
for (Handler handler:Core.handlers){
handler.onAuthenticating();
}
Core.socket.emit("handshake",Core.key.getHash());
}
});
for (Handler handler:Core.handlers){
handler.onSocketCreated();
}
Core.socket.connect();
}

Expand Down
1 change: 1 addition & 0 deletions src/io/purecore/api/call/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Call(String path, Key key, OkHttpClient client){
this.endpoint=path;
this.hash=key.getHash();
this.client=client;
this.params=new HashMap<>();
}

public Call addParam(Param param, String value){
Expand Down
34 changes: 31 additions & 3 deletions src/io/purecore/api/call/Param.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package io.purecore.api.call;

public enum Param {
Version("v"),
GameVersion("gv"),
Token("tok"),
SinceVersion("sv"),
UntilVersion("uv"),
Repo("rep"),
Tag("ta"),
FileName("f"),
Notify("noti"),
Stability("st"),
GameSoftwares("gss"),
GameSoftware("gs"),
Year("ye"),
Month("mo"),
Week("we"),
Day("da"),
Hour("ho"),
Epoch("ep"),
Page("pa"),
Address("ad"),
PaymentMethod("pm"),
Key("k"),
Expand All @@ -21,11 +34,13 @@ public enum Param {
Cname("cnm"),
Game("gme"),
PlatformId("plid"),
SubjectPlatformId("splid"),
PlatformName("plnm"),
Quantity("qty"),
Value("val"),
Ip("ip"),
DeviceFingerprint("dv"),
Url("u"),
ForumCategory("fc"),
ForumEmote("fe"),
ForumObject("fo"),
Expand Down Expand Up @@ -73,12 +88,25 @@ public enum Param {
Price("pr"),
Amount("am"),
ExecutionType("et"),
PunishmentSystem("ps"),
Punishment("pn"),
Appeal("a"),
Report("r"),
Offence("o"),
Offences("os"),
PunishmentAction("pa"),
PunishmentType("pt"),
Reason("re"),
Until("ut"),
Trigger("trg"),
LocalId("lid"),
BanCmd("bcmd"),
TempbanCmd("tbcmd"),
UnbanCmd("ubcmd"),
MuteCmd("mcmd"),
TempmuteCmd("tmcmd"),
UnmuteCmd("umcmd"),
WarnCmd("wcmd"),
UnwarnCmd("uwcmd"),
TempwarnCmd("twcmd"),
KickCmd("kcmd"),
VotingSite("vs"),
Ticket("t"),
TicketCategory("tc"),
Expand Down
8 changes: 8 additions & 0 deletions src/io/purecore/api/event/Handler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package io.purecore.api.event;

import io.purecore.api.versioning.Version;

public interface Handler {

void onConnected();
void onConnecting();
void onAuthenticating();
void onReconnecting();
void onConnectError();
void onSocketCreated();
void onDisconnected();
void onNewExecutions();
void onNewVersion(Version version);

}
4 changes: 2 additions & 2 deletions src/io/purecore/api/execution/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public ExecutionContext(JsonObject object){
this.username=object.get("username").getAsString();
this.id=object.get("id").getAsString();
this.packageName=object.get("packageName").getAsString();
if(object.get("quantity").isJsonNull()){
if(!object.get("quantity").isJsonNull()){
this.quantity=object.get("quantity").getAsFloat();
}
if(object.get("quantityDifference").isJsonNull()){
if(!object.get("quantityDifference").isJsonNull()){
this.quantityDifference=object.get("quantityDifference").getAsFloat();
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/io/purecore/api/execution/ExecutionSuccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.purecore.api.execution;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.purecore.api.Core;
import io.purecore.api.call.ApiException;
import io.purecore.api.call.Param;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExecutionSuccess {

public List<SimplifiedExecution> success;
public List<SimplifiedExecution> fail;

public ExecutionSuccess(JsonObject object){
this.success=new ArrayList<>();
for (JsonElement element: object.get("success").getAsJsonArray()){
this.success.add(new SimplifiedExecution(element.getAsJsonObject()));
}

this.fail=new ArrayList<>();
for (JsonElement element: object.get("fail").getAsJsonArray()){
this.fail.add(new SimplifiedExecution(element.getAsJsonObject()));
}
}

public ExecutionSuccess(List<SimplifiedExecution> executions) throws IOException, ApiException {
List<String> ids = new ArrayList<>();
for (SimplifiedExecution execution:executions){
ids.add(execution.getId());
}
String jsonIds = new Gson().toJson(ids);
JsonObject response = Core.call("/execution/batch/mark/").addParam(Param.SimplifiedCommandExecutions,jsonIds).commit().getAsJsonObject();
this.success=new ArrayList<>();
for (JsonElement element: response.get("success").getAsJsonArray()){
this.success.add(new SimplifiedExecution(element.getAsJsonObject()));
}

this.fail=new ArrayList<>();
for (JsonElement element: response.get("fail").getAsJsonArray()){
this.fail.add(new SimplifiedExecution(element.getAsJsonObject()));
}
}

}
Loading

0 comments on commit 21d0bb4

Please sign in to comment.