Skip to content

Commit

Permalink
Enable job merging back (#7)
Browse files Browse the repository at this point in the history
* update bs

* enable job merging

* Add cpu selection when ordering

* -
  • Loading branch information
kuba6000 authored Oct 2, 2024
1 parent 77c0917 commit 2c727a7
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 154 deletions.
8 changes: 4 additions & 4 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
* For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph
*/
dependencies {
api("com.github.GTNewHorizons:GTNHLib:0.3.3:dev")
api("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-415-GTNH:dev")
api("com.github.GTNewHorizons:AE2FluidCraft-Rework:1.3.21-gtnh:dev")
runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.5.4-GTNH:dev")
api("com.github.GTNewHorizons:GTNHLib:0.5.11:dev")
api("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-463-GTNH:dev")
api("com.github.GTNewHorizons:AE2FluidCraft-Rework:1.3.36-gtnh:dev")
runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.6.38-GTNH:dev")
}
90 changes: 82 additions & 8 deletions example_website/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@
const sortOrderDisplay = ['Ascending', 'Descending'];
const storedCraftableDisplay = ['Stored', 'Craftable', 'Stored and craftable'];
const itemsTypeDisplay = ['Items', 'Fluid drops', 'Items and fluid drops'];
currentJob = -1;

const BYTE_UNIT = [ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB" ];
const BYTE_LIMIT = [ 1.0, 1024.0, 1048576.0, 1.073741824E9, 1.099511627776E12, 1.125899906842624E15, 1.15292150460684698E18, 1.1805916207174113E21, 1.2089258196146292E24, 1.2379400392853803E27 ];

currentJob = {
id: -1,
itemHash: -1,
bytesTotal: -1,
}
function searchStringChanged(el){
let text = el.value;
filteringOptions.searchString = text.toLowerCase();
Expand Down Expand Up @@ -313,6 +321,14 @@ function initSettings(){
document.getElementById('showitemid').checked = settings.showItemID;
}
initSettings();
function formatBytes(bytes) {
for (let i = 1; i < 10; i++) {
if (bytes < BYTE_LIMIT[i]) {
return (bytes / BYTE_LIMIT[i - 1]) + " " + BYTE_UNIT[i - 1];
}
}
return (bytes / BYTE_LIMIT[0]) + " " + BYTE_UNIT[0];
}
function formatNumber(num){
switch (settings.numberFormat) {
case 0:
Expand Down Expand Up @@ -485,6 +501,7 @@ function displayCPUDetails(){
popLoadingScreen(message);
});
}
cpuForJob = "";
function updateCPUList(){
$.getJSON('list', function(data) {
if(data.status !== "OK"){
Expand All @@ -494,11 +511,59 @@ function updateCPUList(){
data = data.data;
clusters = data;
globalCPUList = clusters;
if(!globalCPUList[selectedCPU])
if (!globalCPUList[selectedCPU])
selectedCPU = Object.keys(globalCPUList)[0];
displayCPUList();
});
}
function isValidCPUForOrder(cluster) {
if (!cluster['finalOutput']) return true;
if (currentJob.itemHash != cluster['finalOutput']['hashcode']) return false;
if (cluster['usedStorage'] == -1) return false;
return cluster['availableStorage'] >= cluster['usedStorage'] + currentJob.bytesTotal;
}
function updateCPUListForJob() {
let html = "";
for (let key in globalCPUList){
let cluster = globalCPUList[key];
html += "<button onclick='selectCPUForJob(this);' name='" + key + "' class='";
if (cluster['finalOutput'])
{
if (cluster['usedStorage'] != -1 && currentJob.itemHash == cluster['finalOutput']['hashcode'] && cluster['availableStorage'] >= cluster['usedStorage'] + currentJob.bytesTotal){
html += "mergable";
if (!globalCPUList[cpuForJob])
cpuForJob = key;
}
else
html += "invalid";
}
else {
if (!globalCPUList[cpuForJob])
cpuForJob = key;
}
if (cpuForJob == key)
html += " selected";
html += "'>" + key;
if (cluster['finalOutput'])
html += " - " + formatItemName(cluster['finalOutput'], false) + " x" + cluster['finalOutput']['quantity'];
if (cluster['usedStorage'] && cluster['usedStorage'] != -1){
html += "<br> " + formatBytes(cluster['usedStorage']) + " / " + formatBytes(cluster['availableStorage']);
}
else {
html += "<br>" + formatBytes(cluster['availableStorage']);
}
html += "<br>" + cluster['coProcessors'] + " Co-Procs";
html += "</button>";
}
document.getElementById('terminalCPUListForJob').innerHTML = html;
}
function selectCPUForJob(el) {
let cluster = globalCPUList[el.name];
if (!cluster) return;
if (!isValidCPUForOrder(cluster)) return;
cpuForJob = el.name;
updateCPUListForJob();
}
function selectCPU(el) {
selectedCPU = el.name;
displayCPUList();
Expand Down Expand Up @@ -730,11 +795,15 @@ function openTrackingData(id){
}
function beginOrderingItem(hashcode){
console.log(hashcode);
let quantity = Number(window.prompt("How much to order?", "1"));
if (quantity == null || quantity == NaN || quantity < 0 || quantity > Math.pow(2,31)-1){
let answer = window.prompt("How much to order?", "1");
if (answer === null) // cancelled
return;
let quantity = Number(answer);
if (quantity == null || quantity == NaN || quantity <= 0 || quantity > Math.pow(2,31)-1){
return;
}
else {
document.getElementById('terminalCPUListForJob').innerHTML = "...";
let message = "Sending order...";
pushLoadingScreen(message);
$.getJSON('order?item=' + hashcode + "&quantity=" + quantity, function(data){
Expand All @@ -749,7 +818,9 @@ function beginOrderingItem(hashcode){
setCurrentScreen(2);
document.getElementById("terminalcontent").innerHTML = ";)";
document.getElementById("terminalJobHeaderText").innerHTML = "Calculating, please wait...";
currentJob = data['jobID'];
cpuForJob = "";
currentJob.id = data['jobID'];
currentJob.itemHash = hashcode;
setTimeout(updateCraftingPlan, 1000);
}
else{
Expand All @@ -763,7 +834,7 @@ function updateCraftingPlan(){
if(currentWindow != 2){
return;
}
$.getJSON('job?id=' + currentJob, function(data){
$.getJSON('job?id=' + currentJob.id, function(data){
if(currentWindow != 2){
return;
}
Expand All @@ -780,8 +851,10 @@ function updateCraftingPlan(){
htmlHeader += "[Simulation :(] ";
}
htmlHeader += "Crafting Plan - " + data['bytesTotal'] + "bytes";
currentJob.bytesTotal = data['bytesTotal'];
if (!data['isSimulating']){
htmlHeader += "<button onclick='startCurrentJob();'>Start</button>";
updateCPUListForJob();
}
document.getElementById("terminalJobHeaderText").innerHTML = htmlHeader;
if (data['plan']){
Expand Down Expand Up @@ -830,7 +903,7 @@ function cancelCurrentJob(){
}
setCurrentScreen(0);
refreshTerminal();
$.getJSON('job?id=' + currentJob + "&cancel", function(data){
$.getJSON('job?id=' + currentJob.id + "&cancel", function(data){
if(data.status !== "OK"){
alert(data.status + ": " + data.data);
return;
Expand All @@ -844,13 +917,14 @@ function startCurrentJob(){
}
let message = "Submitting job...";
pushLoadingScreen(message);
$.getJSON('job?id=' + currentJob + "&submit", function(data){
$.getJSON('job?id=' + currentJob.id + "&submit" + "&cpu=" + encodeURIComponent(cpuForJob).replace(/'/g,"%27").replace(/"/g,"%22"), function(data){
if (data.status !== "OK"){
alert(data.status + ": " + data.data);
}
popLoadingScreen(message);
setCurrentScreen(0);
refreshTerminal();
updateCPUList();
});
}
function cancelJobOnCPU(selectedCPU){
Expand Down
13 changes: 11 additions & 2 deletions example_website/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,23 @@ button:hover{
margin-bottom: 10px;
}

#terminalCPUList button{
#terminalCPUList button, #terminalCPUListForJob button{
width: 100%;
margin: 0;
border-bottom: black dotted 1px;
}
#terminalCPUList button.selected{
#terminalCPUList button.selected, #terminalCPUListForJob button.selected{
background-color: rgb(0, 137, 155);
}
#terminalCPUListForJob button.invalid{
background-color: rgb(155, 23, 0);
}
#terminalCPUListForJob button.mergable{
background-color: rgb(170, 159, 0);
}
#terminalCPUListForJob button.mergable.selected{
background-color: rgb(0, 165, 194);
}

#terminalHistoryDetails{
padding: 5px;
Expand Down
13 changes: 12 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ accessTransformersFile =
# Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled!
usesMixins = true

# Set to a non-empty string to configure mixins in a separate source set under src/VALUE, instead of src/main.
# This can speed up compile times thanks to not running the mixin annotation processor on all input sources.
# Mixin classes will have access to "main" classes, but not the other way around.
separateMixinSourceSet =

# Adds some debug arguments like verbose output and class export.
usesMixinDebug = false

Expand Down Expand Up @@ -108,9 +113,15 @@ minimizeShadowedDependencies = true
# If disabled, won't rename the shadowed classes.
relocateShadowedDependencies = true

# Adds the GTNH maven, CurseMaven, Modrinth, and some more well-known 1.7.10 repositories.
# Adds CurseMaven, Modrinth, and some more well-known 1.7.10 repositories.
includeWellKnownRepositories = true

# A list of repositories to exclude from the includeWellKnownRepositories setting. Should be a space separated
# list of strings, with the acceptable keys being(case does not matter):
# cursemaven
# modrinth
excludeWellKnownRepositories =

# Change these to your Maven coordinates if you want to publish to a custom Maven repository instead of the default GTNH Maven.
# Authenticate with the MAVEN_USER and MAVEN_PASSWORD environment variables.
# If you need a more complex setup disable maven publishing here and add a publishing repository to addon.gradle.
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.24'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.27'
}
46 changes: 22 additions & 24 deletions src/main/java/com/kuba6000/ae2webintegration/AE2JobTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@

import com.kuba6000.ae2webintegration.discord.DiscordManager;
import com.kuba6000.ae2webintegration.mixins.AE2.CraftingCPUClusterAccessor;
import com.kuba6000.ae2webintegration.mixins.AE2.CraftingLinkAccessor;

import appeng.api.networking.IGrid;
import appeng.api.networking.crafting.ICraftingCPU;
import appeng.api.networking.crafting.ICraftingLink;
import appeng.api.networking.crafting.ICraftingMedium;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.IInterfaceViewable;
import appeng.crafting.CraftingLink;
import appeng.me.Grid;
import appeng.me.cache.CraftingGridCache;
import appeng.me.cluster.implementations.CraftingCPUCluster;
Expand Down Expand Up @@ -127,28 +124,29 @@ public double getShareInCraftingTime(IAEItemStack stack) {

private static int nextFreeTrackingInfoID = 1;

public static void addJob(ICraftingLink link, CraftingGridCache cache, IGrid grid) {
if (link instanceof CraftingLink craftingLink) {
ICraftingCPU cpu = ((CraftingLinkAccessor) craftingLink).callGetCpu();
if (cpu instanceof CraftingCPUCluster cpuCluster) {
if (!AE2Controller.isValid()) {
if (cache.getCpus()
.size() >= 5) AE2Controller.activeGrid = (Grid) grid;
else return;
} else if (AE2Controller.activeGrid != grid) return;
JobTrackingInfo info;
trackingInfoMap.put(cpu, info = new JobTrackingInfo());
info.timeStarted = System.currentTimeMillis();
info.finalOutput = cpu.getFinalOutput()
.copy();
for (IAEItemStack iaeItemStack : ((CraftingCPUClusterAccessor) (Object) cpuCluster).getWaitingFor()) {
info.startedWaitingFor.put(iaeItemStack, System.currentTimeMillis());
info.timeSpentOn.put(iaeItemStack, 0L);
info.craftedTotal.put(iaeItemStack, 0L);
info.waitingFor.put(iaeItemStack, iaeItemStack.getStackSize());
}
}
public static void addJob(CraftingCPUCluster cpuCluster, CraftingGridCache cache, IGrid grid, boolean isMerging) {
if (!AE2Controller.isValid()) {
if (cache.getCpus()
.size() >= 5) AE2Controller.activeGrid = (Grid) grid;
else return;
} else if (AE2Controller.activeGrid != grid) return;
JobTrackingInfo info;
if (isMerging) {
info = trackingInfoMap.get(cpuCluster);
if (info == null) return; // We can't start tracking mid crafting :P
} else {
trackingInfoMap.put(cpuCluster, info = new JobTrackingInfo());
info.timeStarted = System.currentTimeMillis();
}
info.finalOutput = cpuCluster.getFinalOutput()
.copy();
// is this needed?
// for (IAEItemStack iaeItemStack : ((CraftingCPUClusterAccessor) (Object) cpuCluster).getWaitingFor()) {
// info.startedWaitingFor.put(iaeItemStack, System.currentTimeMillis());
// info.timeSpentOn.put(iaeItemStack, 0L);
// info.craftedTotal.put(iaeItemStack, 0L);
// info.waitingFor.put(iaeItemStack, iaeItemStack.getStackSize());
// }
}

public static void updateCraftingStatus(ICraftingCPU cpu, IAEItemStack diff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.kuba6000.ae2webintegration.api.JSON_Item.create;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -17,9 +19,11 @@ public class GetCPUList extends ISyncedRequest {

private static class JSON_CpuInfo {

public long size;
public boolean isBusy;
public JSON_Item finalOutput;
public long availableStorage;
public long usedStorage;
public long coProcessors;
public boolean hasTrackingInfo = false;
public long timeStarted = 0L;
}
Expand Down Expand Up @@ -50,7 +54,9 @@ public void handle(Grid grid) {
for (Map.Entry<String, CraftingCPUCluster> entry : clusters.entrySet()) {
JSON_CpuInfo cpuInfo = new JSON_CpuInfo();
CraftingCPUCluster cluster = entry.getValue();
cpuInfo.size = cluster.getAvailableStorage();
cpuInfo.availableStorage = cluster.getAvailableStorage();
cpuInfo.usedStorage = getUsedStorage(cluster);
cpuInfo.coProcessors = cluster.getCoProcessors();
if (cpuInfo.isBusy = cluster.isBusy()) {
cpuInfo.finalOutput = create(cluster.getFinalOutput());
AE2JobTracker.JobTrackingInfo trackingInfo = AE2JobTracker.trackingInfoMap.get(cluster);
Expand All @@ -64,4 +70,24 @@ public void handle(Grid grid) {
done();
}

private static boolean isUsedStorageAvailable = true;
private static Method getUsedStorageMethod = null;

private static long getUsedStorage(CraftingCPUCluster cluster) {
if (!isUsedStorageAvailable) return -1L;
if (getUsedStorageMethod == null) {
try {
getUsedStorageMethod = CraftingCPUCluster.class.getDeclaredMethod("getUsedStorage");
} catch (NoSuchMethodException e) {
isUsedStorageAvailable = false;
return -1L;
}
}
try {
return (long) getUsedStorageMethod.invoke(cluster);
} catch (IllegalAccessException | InvocationTargetException e) {
return 0L;
}
}

}
Loading

0 comments on commit 2c727a7

Please sign in to comment.