Skip to content

Commit

Permalink
user_experience++
Browse files Browse the repository at this point in the history
  • Loading branch information
xxAROX committed Nov 27, 2023
1 parent 08c2838 commit 3a18509
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/main/java/xxAROX/PresenceMan/Application/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public static void setActivity(APIActivity api_activity, boolean queue) {
if (discord_core != null && discord_create_params != null) {
var activity = api_activity.toDiscord(discord_create_params);
discord_core.activityManager().updateActivity(activity);
System.out.println("Updated discord activity!");
} else {
if (queue) {
APIActivity finalApi_activity = api_activity;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/xxAROX/PresenceMan/Application/AppInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.StringJoiner;

public final class AppInfo {
public final static int[] version = new int[]{ 1,1,3 };
public final static int[] version = new int[]{ 1,1,4 };
public final static String name = "Presence-Man";
public static String icon = "icon.png";
public static long discord_application_id = 1133823892486623344L;
Expand All @@ -20,6 +20,6 @@ public static String getVersion(CharSequence delimiter){
}

public static void main(String[] args) {
System.out.println(getVersion());
System.out.println(getVersion()); // NOTE: DO NOT REMOVE
}
}
11 changes: 5 additions & 6 deletions src/main/java/xxAROX/PresenceMan/Application/RestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import de.jcm.discordgamesdk.GameSDKException;
import lombok.AllArgsConstructor;
import lombok.NonNull;
Expand Down Expand Up @@ -31,18 +32,14 @@ public static void heartbeat(){
if (App.getDiscord_core() == null || !App.getDiscord_core().isOpen()) return;
try {App.getDiscord_core().userManager().getCurrentUser();} catch (GameSDKException ignore) {return;}
if (App.getInstance().xboxUserInfo == null) return;

JsonObject body = new JsonObject();
JsonObject os = new JsonObject();
body.addProperty("xuid", App.getInstance().xboxUserInfo.getXuid());
body.addProperty("gamertag", App.getInstance().xboxUserInfo.getGamertag());
body.addProperty("user_id", String.valueOf(App.getDiscord_core().userManager().getCurrentUser().getUserId()));

os.addProperty("name", String.valueOf(System.getProperty("os.name"))); // TODO: remove this line of code, I don't care about statistics anymore!!
os.addProperty("arch", String.valueOf(System.getProperty("os.arch"))); // TODO: remove this line of code, I don't care about statistics anymore!!
os.addProperty("version", String.valueOf(System.getProperty("os.version"))); // TODO: remove this line of code, I don't care about statistics anymore!!
body.add("os", os); // TODO: remove this line of code, I don't care about statistics anymore!!

JsonObject response = request(Method.POST, RestAPI.Endpoints.heartbeat, new HashMap<>(), body);
Gateway.connected = response != null;
if (response == null) {
App.getInstance().network = null;
App.getInstance().server = null;
Expand Down Expand Up @@ -130,7 +127,9 @@ private static JsonObject request(@NonNull Method method, @NonNull String endpoi
in.close();
con.disconnect();
return new Gson().fromJson(content.toString(), JsonObject.class);
} catch (JsonSyntaxException ignore) {
} catch (IOException e) {
if (Gateway.connected) Gateway.connected = false;
if (!Gateway.broken) Gateway.broken = true;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
public class Gateway {
public static String protocol = "http://";
public static String address = "127.0.0.1";
public static int port = 15151;
public static Integer port = 15151;

public static boolean connected = false;
public static boolean broken = false;
public static boolean broken_popup = false;

public static String getUrl() {
return protocol + address + ":" + port;
return protocol + address + (port == null ? "" : ":" + port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public WaterdogScheduler() {
instance = this;

ThreadFactoryBuilder builder = ThreadFactoryBuilder.builder().format("Scheduler Executor - #%d").build();
this.threadedExecutor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), builder);
this.threadedExecutor = new ThreadPoolExecutor(Math.round(Runtime.getRuntime().availableProcessors() /2f), Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), builder);
}

public static WaterdogScheduler getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public void onRun(int currentTick) {
while ((inputLine = in.readLine()) != null) content.append(inputLine);
in.close();
JsonObject gateway = new Gson().fromJson(content.toString(), JsonObject.class);

Gateway.protocol = gateway.get("protocol").getAsString();
Gateway.address = gateway.get("address").getAsString();
Gateway.port = gateway.get("port").getAsInt();
Gateway.port = gateway.has("port") && !gateway.get("port").isJsonNull() ? gateway.get("port").getAsInt() : null;

ping_backend();
} catch (IOException e) {
System.out.println("Error while fetching gateway information: ");
Expand All @@ -46,14 +48,15 @@ public static void ping_backend() {
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) content.append(inputLine);
in.close();
result = content.toString().toLowerCase().contains("presence-man");
result = content.toString().toLowerCase().contains("jan sohn / xxarox");
} catch (IOException ignore) {
}
Gateway.broken = result;
if (result) {
ReconnectingTask.deactivate();
Gateway.broken = false;
Gateway.broken_popup = false;
System.out.println("Connected to backend successfully!");
} else {
Gateway.broken = true;
ReconnectingTask.activate();
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/xxAROX/PresenceMan/Application/ui/AppUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import java.util.Objects;

public class AppUI extends JFrame {
public static final int BORDER_PADDING = 10;
public static final int BODY_BLOCK_PADDING = 10;

public final JTabbedPane contentPane = new JTabbedPane();
private final List<AUITab> tabs = new ArrayList<>();
private final Tray tray = new Tray();
Expand Down Expand Up @@ -123,11 +120,13 @@ public void openURL(final String url) {
}

public void showException(Throwable t) {
App.getInstance().getLogger().error("Caught exception in thread " + Thread.currentThread().getName(), t);
StringBuilder builder = new StringBuilder("An error occurred:\n");
builder.append("[").append(t.getClass().getSimpleName()).append("] ").append(t.getMessage()).append("\n");
for (StackTraceElement element : t.getStackTrace()) builder.append(element.toString()).append("\n");
this.showError(builder.toString());
t.printStackTrace();
/*
* StringBuilder builder = new StringBuilder("An error occurred:\n");
* builder.append("[").append(t.getClass().getSimpleName()).append("] ").append(t.getMessage()).append("\n");
* for (StackTraceElement element : t.getStackTrace()) builder.append(element.toString()).append("\n");
* showError(builder.toString());
*/
}

public void showInfo(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xxAROX.PresenceMan.Application.ui.tabs;

import xxAROX.PresenceMan.Application.App;
import xxAROX.PresenceMan.Application.entity.Gateway;
import xxAROX.PresenceMan.Application.ui.AUITab;
import xxAROX.PresenceMan.Application.ui.AppUI;

Expand All @@ -9,6 +10,7 @@

public class GeneralTab extends AUITab {
private static final String NOT_CONNECTED = "Not connected";
private static final String HALF_CONNECTED = "Connected to Presence-Man backend!";
private static final String CONNECTED = "Connected to {server} on {network}";

JLabel label_connection_status;
Expand All @@ -27,11 +29,6 @@ protected void init(JPanel contentPane) {
contentPane.add(label_connection_status);
}

public void tick() {
var text = getConnectedMessage();
if (!label_connection_status.getText().equals(text)) label_connection_status.setText(text);
}

@Override
public void setReady() {
}
Expand All @@ -40,13 +37,22 @@ public void setReady() {
public void onClose() {
}

public void tick() {
updateConnectedMessage();
}

public void updateConnectedMessage(){
var text = getConnectedMessage();
if (!label_connection_status.getText().equals(text)) label_connection_status.setText(text);
}

private static String getConnectedMessage(){
var activity = App.getInstance().getApi_activity();
boolean connected = activity != null && App.getInstance().getNetwork() != null && App.getInstance().getServer() != null;
return connected ?
CONNECTED
.replace("{server}", App.getInstance().getServer())
.replace("{network}", App.getInstance().getNetwork())
: NOT_CONNECTED;
: (Gateway.connected ? HALF_CONNECTED : NOT_CONNECTED);
}
}

0 comments on commit 3a18509

Please sign in to comment.