-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
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,2 @@ | ||
endpoint: https://api.ball.one/balances | ||
filename: ./bookkeeper.txt |
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,99 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation=" | ||
http://maven.apache.org/POM/4.0.0 | ||
http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.spiralstudio.mod</groupId> | ||
<artifactId>root</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>bookkeeper</artifactId> | ||
<version>1.0.0</version> | ||
<name>Bookkeeper</name> | ||
|
||
<properties> | ||
<mainClass>com.spiralstudio.mod.bookkeeper.Main</mainClass> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.threerings</groupId> | ||
<artifactId>projectx-pcode</artifactId> | ||
<version>1.0.0</version> | ||
<scope>system</scope> | ||
<systemPath>${basedir}/../lib/projectx-pcode.jar</systemPath> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.spiralstudio.mod</groupId> | ||
<artifactId>core</artifactId> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.javassist</groupId> | ||
<artifactId>javassist</artifactId> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>${mainClass}</mainClass> | ||
</manifest> | ||
<manifestEntries> | ||
<Name>${project.name}</Name> | ||
<Version>${project.version}</Version> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>${mainClass}</mainClass> | ||
</manifest> | ||
<manifestEntries> | ||
<Name>${project.name}</Name> | ||
<Version>${project.version}</Version> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
|
||
</project> |
81 changes: 81 additions & 0 deletions
81
bookkeeper/src/main/java/com/spiralstudio/mod/bookkeeper/BookkeeperHandler.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,81 @@ | ||
package com.spiralstudio.mod.bookkeeper; | ||
|
||
import com.spiralstudio.mod.core.FileUtils; | ||
import com.spiralstudio.mod.core.HttpUtils; | ||
import com.spiralstudio.mod.core.JsonUtils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* @author Leego Yih | ||
*/ | ||
public class BookkeeperHandler { | ||
public static String endpoint = ""; | ||
public static String filename = ""; | ||
public static final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
|
||
public static void dump(com.threerings.projectx.util.A ctx, String type, int value) { | ||
String name = getKnightName(ctx); | ||
if (name != null) { | ||
executor.submit(() -> { | ||
callAPI(name, type, value); | ||
writeFile(name, type, value); | ||
}); | ||
} | ||
} | ||
|
||
public static void callAPI(String name, String type, int value) { | ||
String body = null; | ||
try { | ||
if (endpoint == null || endpoint.isEmpty()) { | ||
return; | ||
} | ||
Map<String, Object> obj = new HashMap<>(); | ||
obj.put("knight", name); | ||
obj.put("type", type); | ||
obj.put("value", value); | ||
obj.put("timestamp", System.currentTimeMillis()); | ||
body = JsonUtils.toString(obj); | ||
HttpUtils.post(endpoint, Collections.emptyMap(), body); | ||
} catch (Exception e) { | ||
System.out.println("[Bookkeeper] Failed to call api: " + endpoint + ", data: " + body); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void writeFile(String name, String type, int value) { | ||
String body = null; | ||
try { | ||
if (filename == null || filename.isEmpty()) { | ||
return; | ||
} | ||
body = "[" + LocalDateTime.now() + "] " + name + ": " + type + "=" + value + "\n"; | ||
FileUtils.write(filename, body + "\n", true); | ||
} catch (Exception e) { | ||
System.out.println("[Bookkeeper] Failed to write file: " + filename + ", data: " + body); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static String getKnightName(com.threerings.projectx.util.A ctx) { | ||
Object name = null; | ||
try { | ||
Field field = Class.forName("com.threerings.util.Name").getDeclaredField("_name"); | ||
field.setAccessible(true); | ||
name = field.get(ctx.uk().knight); | ||
} catch (Exception e) { | ||
System.out.println("[Bookkeeper] Failed to get knight name"); | ||
} | ||
if (name != null) { | ||
return (String) name; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
bookkeeper/src/main/java/com/spiralstudio/mod/bookkeeper/Main.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,71 @@ | ||
package com.spiralstudio.mod.bookkeeper; | ||
|
||
import com.spiralstudio.mod.core.ClassPool; | ||
import com.spiralstudio.mod.core.Configs; | ||
import com.spiralstudio.mod.core.MethodModifier; | ||
import com.spiralstudio.mod.core.Registers; | ||
import lombok.Data; | ||
|
||
/** | ||
* @author Leego Yih | ||
* @see com.threerings.projectx.client.aq EnergyPanel | ||
* @see com.threerings.projectx.item.client.g BaseItemListPanel | ||
*/ | ||
public class Main { | ||
private static boolean mounted = false; | ||
|
||
static { | ||
Registers.add(Main.class); | ||
} | ||
|
||
public static void mount() { | ||
if (mounted) { | ||
return; | ||
} | ||
mounted = true; | ||
Config config = getConfig(); | ||
initConfig(config); | ||
redefineEnergyPanel(); | ||
redefineBaseItemListPanel(); | ||
} | ||
|
||
static void initConfig(Config config) { | ||
if (config == null) { | ||
return; | ||
} | ||
BookkeeperHandler.endpoint = config.endpoint; | ||
BookkeeperHandler.filename = config.filename; | ||
} | ||
|
||
static void redefineEnergyPanel() { | ||
ClassPool.from("com.threerings.projectx.client.aq") | ||
.modifyMethod(new MethodModifier() | ||
.methodName("uv") | ||
.insertAfter("com.spiralstudio.mod.bookkeeper.BookkeeperHandler.dump(this._ctx, \"energy\", this._ctx.uk().energy);")); | ||
} | ||
|
||
static void redefineBaseItemListPanel() { | ||
ClassPool.from("com.threerings.projectx.item.client.g") | ||
.modifyMethod(new MethodModifier() | ||
.methodName("updateCrowns") | ||
.insertAfter("com.spiralstudio.mod.bookkeeper.BookkeeperHandler.dump(this._ctx, \"crowns\", this._ctx.uk().crowns);")); | ||
} | ||
|
||
static Config getConfig() { | ||
try { | ||
return Configs.readYaml("bookkeeper.yml", Config.class); | ||
} catch (Exception e) { | ||
System.out.println("Failed to read config: " + e.getMessage()); | ||
return null; | ||
} | ||
} | ||
|
||
@Data | ||
public static class Config { | ||
private String endpoint; | ||
private String filename; | ||
} | ||
|
||
public static void main(String[] args) { | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"mod": { | ||
"name": "Bookkeeper", | ||
"description": "", | ||
"author": "Leego", | ||
"version": "1.0.0", | ||
"compatibility": "Yes" | ||
} | ||
} |
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