Skip to content

Commit

Permalink
WPR-7 - Improve help and route show
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-yo committed Jan 21, 2024
1 parent 7c65f4d commit c2ebe4c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Before running the script, ensure that you have the following:
To run the script, use the following command:

```bash
java -jar waypoint-replace-0.0.8.jar
java -jar waypoint-replace-0.0.9.jar
```
<b> Note! <br/>
* Replace 0.0.8 with the actual version downloaded
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/faulty/wpreplace/commands/RouteCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.faulty.wpreplace.commands;

import java.util.ArrayList;
import java.util.List;

import org.faulty.wpreplace.context.MissionMizService;
import org.faulty.wpreplace.context.RouteContext;
import org.faulty.wpreplace.utils.LuaWriter;
import org.faulty.wpreplace.utils.RouteUtils;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,7 +23,7 @@ public class RouteCommand extends BaseCommand {
private RouteContext routeContext;

@ShellMethodAvailability("isMissionLoaded")
@ShellMethod(key = "route get", value = "Single selector", group = "Route")
@ShellMethod(key = "route get", value = "Select a source route", group = "Route")
public String routeGet() {
String coalition = getCoalition();
int countryId = getIntInput("country id:", "1");
Expand All @@ -41,36 +39,26 @@ public String routeGet() {
}

@ShellMethodAvailability("isMissionAndRouteLoaded")
@ShellMethod(key = "route show", value = "Multi selector", group = "Route")
@ShellMethod(key = "route show", value = "Show source route coordinates", group = "Route")
public String showRoute() {
LuaTable route = routeContext.getRoute().get("points").checktable();
return "Showing results for " + routeContext.printDetails() + "\n"
+ LuaWriter.luaTableToString(compactPoints(route));
}

public LuaTable compactPoints(LuaTable points) {
List<LuaValue> newPoints = new ArrayList<>();
for (LuaValue key : points.keys()) {
LuaString point = LuaString.valueOf(String.format("x=%s, y=%s, alt=%s",
points.get(key).get("x"),
points.get(key).get("y"),
points.get(key).get("alt")));
newPoints.add(point);
}
return LuaValue.listOf(newPoints.toArray(new LuaValue[] {}));
return String.format("Showing %d points for %s\n%s", route.length(), routeContext.printDetails(),
LuaWriter.luaTableToString(RouteUtils.compactPoints(route)));
}

@ShellMethodAvailability("isMissionAndRouteLoaded")
@ShellMethod(key = "route copy", value = "Multi selector", group = "Route")
@ShellMethod(key = "route copy", value = "Copy source route to choosen destinations", group = "Route")
public String routeCopy() {
String coalition = getCoalition();
int countryId = Integer.parseInt(getStringInput("country id:", "1"));
List<String> unitTypes = selectMultipleUnitTypes();
if (unitTypes.isEmpty()) {
return "No units selected";
}
unitTypes.forEach(unitType -> RouteUtils.setRoute(missionContext.getMission(), routeContext.getRoute(), coalition, countryId, unitType));
return String.format("Route set for coalition %s, country %d, units '%s'", coalition, countryId, String.join(",", unitTypes));
unitTypes.forEach(unitType -> RouteUtils.setRoute(missionContext.getMission(), routeContext.getRoute(),
coalition, countryId, unitType));
return String.format("Route set for coalition %s, country %d, units '%s'", coalition, countryId,
String.join(",", unitTypes));
}

private Availability isMissionLoaded() {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/faulty/wpreplace/utils/RouteUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.faulty.wpreplace.utils;

import java.util.ArrayList;
import java.util.List;

import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

Expand Down Expand Up @@ -30,4 +34,20 @@ public static void setRoute(LuaValue mission, LuaValue route, String coalition,
}
}
}

/**
* Compact a route points to x,y,alt only
*/
public static LuaTable compactPoints(LuaTable points) {
List<LuaValue> newPoints = new ArrayList<>();
for (LuaValue key : points.keys()) {
LuaString point = LuaString.valueOf(String.format("x=%s, y=%s, alt=%s",
points.get(key).get("x"),
points.get(key).get("y"),
points.get(key).get("alt")));
newPoints.add(point);
}
return LuaValue.listOf(newPoints.toArray(new LuaValue[] {}));
}

}

0 comments on commit c2ebe4c

Please sign in to comment.