Skip to content

Commit

Permalink
WPR-15 - Add mission details, general refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-yo committed Jan 23, 2024
1 parent 1455e51 commit 1f09a9a
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 74 deletions.
38 changes: 38 additions & 0 deletions src/main/java/org/faulty/wpreplace/models/MissionDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.faulty.wpreplace.models;

import lombok.Data;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

@Data
public class MissionDetails {
private final Date date;
private final String theatre;
private final MissionWeather weather;

public static MissionDetails fromLuaMission(LuaValue missionLua) {
return new MissionDetails(getDate(missionLua.get("date").checktable()),
missionLua.get("theatre").tojstring(),
MissionWeather.fromLua(missionLua.get("weather").checktable()));
}

private static Date getDate(LuaTable luaDate) {
return new Date(
luaDate.get("Year").toint(),
luaDate.get("Month").toint(),
luaDate.get("Day").toint()
);
}

@Data
public static class Date {
private final int year;
private final int month;
private final int day;

@Override
public String toString() {
return day + "/" + month + "/" + year;
}
}
}
71 changes: 71 additions & 0 deletions src/main/java/org/faulty/wpreplace/models/MissionWeather.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.faulty.wpreplace.models;

import lombok.Data;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

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

@Data
public class MissionWeather {
private final int atmosphereType;
private final List<Wind> winds;
private final boolean enableFog;
private final int fogThickness;
private final int fogVisibility;
private final int seasonTemperature;
private final String name;
private final List<Item> clouds;

public static MissionWeather fromLua(LuaTable weatherLua) {
return new MissionWeather(
weatherLua.get("atmosphere_type").toint(),
getWinds(weatherLua.get("wind").checktable()),
weatherLua.get("enable_fog").toboolean(),
weatherLua.get("fog").get("thickness").toint(),
weatherLua.get("fog").get("visibility").toint(),
weatherLua.get("season").get("temperature").toint(),
weatherLua.get("name").tojstring(),
getClouds(weatherLua.get("clouds").checktable())
);
}

private static List<Item> getClouds(LuaTable cloudsLua) {
List<Item> pylonsPayload = new ArrayList<>();
for (LuaValue key : cloudsLua.keys()) {
pylonsPayload.add(new Item(key.tojstring(), cloudsLua.get(key).tojstring()));
}
return pylonsPayload;
}

private static List<Wind> getWinds(LuaTable windLua) {
List<Wind> winds = new ArrayList<>();
for (LuaValue key : windLua.keys()) {
winds.add(new Wind(
key.tojstring(),
windLua.get(key).get("speed").toint(),
windLua.get(key).get("dir").toint()
));
}
return winds;
}

@Data
public static class Wind {
private final String location;
private final int speed;
private final int dir;
}

@Data
public static final class Item {
private final String key;
private final String value;

public Item(String key, Object value) {
this.key = key;
this.value = String.valueOf(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,44 @@

@Data
@AllArgsConstructor
public final class SlimRoute {
public final class RouteDetails {
private int id;
private double x;
private double y;
private int alt;
private double speed;
private double eta;
private String type;
private String task;

public static List<SlimRoute> fromLuaRoute(LuaTable points) {
List<SlimRoute> slimRoutes = new ArrayList<>();

public static List<RouteDetails> fromLuaRoute(LuaTable points) {
List<RouteDetails> slimRoutes = new ArrayList<>();
for (LuaValue key : points.keys()) {
LuaValue luaValue = points.get(key);
SlimRoute slimRoute = new SlimRoute(
RouteDetails slimRoute = new RouteDetails(
key.toint(),
luaValue.get("x").todouble(),
luaValue.get("y").todouble(),
luaValue.get("alt").toint(),
luaValue.get("speed").todouble(),
luaValue.get("ETA").todouble()
luaValue.get("ETA").todouble(),
luaValue.get("type").tojstring(),
getTask(luaValue.get("task"))
);
slimRoutes.add(slimRoute);
}
return slimRoutes;
}

public void updateEta(SlimRoute source) {
private static String getTask(LuaValue luaValue) {
if (luaValue.isstring()) {
return luaValue.tojstring();
}
return luaValue.checktable().get("id").tojstring();
}

public void updateEta(RouteDetails source) {
Point3D point1 = new Point3D(source.x, source.y, source.alt);
Point3D point2 = new Point3D(this.x, this.y, this.alt);
double distance = point1.distance(point2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Log4j2
@Getter
@Component
public class MissionMizService {
public class MissionService {

LuaValue mission;
String mizFilePath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.faulty.wpreplace.services;

import lombok.Getter;
import org.faulty.wpreplace.models.SlimRoute;
import org.faulty.wpreplace.models.RouteDetails;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RouteContext {
public class RouteService {

@Getter
LuaValue route;
Expand All @@ -30,8 +30,8 @@ public String printDetails() {
return String.format("coalition %s, country %d, unitType %s group %d ", coalition, countryId, unitType, groupId);
}

public void updateRoute(List<SlimRoute> items) {
SlimRoute last = null;
public void updateRoute(List<RouteDetails> items) {
RouteDetails last = null;
LuaTable points = route.get("points").checktable();
items.forEach(item -> {
LuaValue luaValue = points.get(item.getId());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/faulty/wpreplace/ui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void start(Stage primaryStage) throws IOException {
}

public static void addIcons(Stage stage) {
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon256.ico"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon48.png"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon32.png"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icons/icon256.ico"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icons/icon48.png"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icons/icon32.png"));
}


Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/faulty/wpreplace/ui/CopyRouteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import javafx.scene.control.*;
import javafx.scene.text.Text;
import org.faulty.wpreplace.models.Entry;
import org.faulty.wpreplace.services.MissionMizService;
import org.faulty.wpreplace.services.RouteContext;
import org.faulty.wpreplace.services.MissionService;
import org.faulty.wpreplace.services.RouteService;
import org.faulty.wpreplace.utils.MessageUtils;
import org.faulty.wpreplace.utils.RouteUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,9 +17,9 @@
@Component
public class CopyRouteController {
@Autowired
private RouteContext routeContext;
private RouteService routeService;
@Autowired
private MissionMizService missionContext;
private MissionService missionContext;

@FXML
public Text routeDetails;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void initialize() {
initCountryList();
initUnitType();
initGroupIds();
routeDetails.setText("Source: " + routeContext.printDetails());
routeDetails.setText("Source: " + routeService.printDetails());
selectBlueCoalition();
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public void copyRouteData() {
int countryId = countryIdListView.getSelectionModel().getSelectedItem().getLocation();
List<Integer> groupIds = groupIdListView.getSelectionModel().getSelectedItems();
String unitType = ((RadioButton) unitTypeToggleGroup.getSelectedToggle()).getUserData().toString();
groupIds.forEach(group -> RouteUtils.setRoute(missionContext.getMission(), routeContext.getRoute(),
groupIds.forEach(group -> RouteUtils.setRoute(missionContext.getMission(), routeService.getRoute(),
coalition, countryId, unitType));
String groups = groupIds.stream().map(String::valueOf).collect(Collectors.joining(","));
String msg = String.format("Route set for coalition %s, country %d, unit '%s' with groups %s", coalition, countryId, unitType, groups);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.faulty.wpreplace.ui;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import lombok.extern.log4j.Log4j2;
import org.faulty.wpreplace.models.MissionDetails;
import org.faulty.wpreplace.models.MissionWeather;
import org.faulty.wpreplace.services.MissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Log4j2
@Component
public class GeneralMissionDetailsController {
@Autowired
private MissionService missionService;

@FXML
public Text dateText;
@FXML
public Text theatreText;

@FXML
private TableView<MissionWeather.Item> dataTable;
@FXML
public TableView<MissionWeather.Wind> windTable;
@FXML
public TableView<MissionWeather.Item> cloudsTable;

public void initialize() {
MissionDetails missionDetails = MissionDetails.fromLuaMission(missionService.getMission());
dateText.setText(missionDetails.getDate().toString());
theatreText.setText(missionDetails.getTheatre());
initGeneralDate(missionDetails);
initWindTable(missionDetails);
initCloudsTable(missionDetails);


}

private void initCloudsTable(MissionDetails missionDetails) {
TableColumn<MissionWeather.Item, String> keyColumn = new TableColumn<>("Key");
keyColumn.setCellValueFactory(new PropertyValueFactory<>("key"));

TableColumn<MissionWeather.Item, String> valColumn = new TableColumn<>("Value");
valColumn.setCellValueFactory(new PropertyValueFactory<>("value"));

cloudsTable.getColumns().addAll(keyColumn, valColumn);
cloudsTable.getItems().addAll(missionDetails.getWeather().getClouds());
}

private void initWindTable(MissionDetails missionDetails) {
TableColumn<MissionWeather.Wind, String> altColumn = new TableColumn<>("Altitude");
altColumn.setCellValueFactory(new PropertyValueFactory<>("location"));

TableColumn<MissionWeather.Wind, String> speedColumn = new TableColumn<>("Speed");
speedColumn.setCellValueFactory(new PropertyValueFactory<>("speed"));

TableColumn<MissionWeather.Wind, String> dirColumn = new TableColumn<>("Direction");
dirColumn.setCellValueFactory(new PropertyValueFactory<>("dir"));

windTable.getColumns().addAll(altColumn, speedColumn, dirColumn);
windTable.getItems().addAll(missionDetails.getWeather().getWinds());
}

private void initGeneralDate(MissionDetails missionDetails) {
TableColumn<MissionWeather.Item, String> typeColumn = new TableColumn<>("Key");
typeColumn.setCellValueFactory(new PropertyValueFactory<>("key"));

TableColumn<MissionWeather.Item, String> valueColumn = new TableColumn<>("Value");
valueColumn.setCellValueFactory(new PropertyValueFactory<>("value"));

dataTable.getColumns().addAll(typeColumn, valueColumn);
MissionWeather weather = missionDetails.getWeather();
dataTable.setItems(FXCollections.observableArrayList(List.of(
new MissionWeather.Item("Name", weather.getName()),
new MissionWeather.Item("Atmosphere Type", weather.getAtmosphereType()),
new MissionWeather.Item("Enable Fog", weather.isEnableFog()),
new MissionWeather.Item("Fog Thickness", weather.getFogThickness()),
new MissionWeather.Item("Fog Visibility", weather.getFogVisibility()),
new MissionWeather.Item("Season Temperature", weather.getSeasonTemperature())
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.faulty.wpreplace.models.UnitDetails;
import org.faulty.wpreplace.models.UnitPayload;
import org.faulty.wpreplace.models.UnitRadio;
import org.faulty.wpreplace.services.MissionMizService;
import org.faulty.wpreplace.services.MissionService;
import org.faulty.wpreplace.utils.MessageUtils;
import org.faulty.wpreplace.utils.RouteUtils;
import org.luaj.vm2.LuaTable;
Expand All @@ -36,7 +36,7 @@
@Component
public class GroupDetailsController {
@Autowired
private MissionMizService missionMizService;
private MissionService missionService;
@FXML
public Text groupDetailText;
@FXML
Expand All @@ -47,11 +47,15 @@ public void initialize() {
}

public void setGroup(String coalition, int countryId, String unitType, int groupId) {
LuaTable group = RouteUtils.getGroup(missionMizService.getMission(), coalition, countryId, unitType, groupId);
LuaTable group = RouteUtils.getGroup(missionService.getMission(), coalition, countryId, unitType, groupId);
if (group == null) {
MessageUtils.showError("Error!", "Unable to load group");
return;
}
initUnitTable(coalition, countryId, unitType, groupId, group);
}

private void initUnitTable(String coalition, int countryId, String unitType, int groupId, LuaTable group) {
groupDetailText.setText(String.format("Showing Group info for: coalition %s, country %d, unitType %s group %d ",
coalition, countryId, unitType, groupId));
ObservableList<UnitDetails> routes = FXCollections.observableArrayList(UnitDetails.fromLuaGroup(group));
Expand Down
Loading

0 comments on commit 1f09a9a

Please sign in to comment.