working on saving and loading of route actions
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
|
||||
public abstract class Action {
|
||||
private static final String TYPE = "type";
|
||||
|
||||
public abstract void fire() throws IOException;
|
||||
public abstract void fire(Plan plan) throws IOException;
|
||||
|
||||
public JSONObject json() {
|
||||
JSONObject json = new JSONObject();
|
||||
@@ -19,4 +22,19 @@ public abstract class Action {
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
|
||||
String clazz = json.getString(TYPE);
|
||||
switch (clazz) {
|
||||
case "ActivateRoute":
|
||||
return new ActivateRoute(json.getInt(RouteAction.ROUTE));
|
||||
case "FinishRoute":
|
||||
return new FinishRoute(json.getInt(RouteAction.ROUTE));
|
||||
case "SetSignalsToStop":
|
||||
return new SetSignalsToStop(json.getInt(RouteAction.ROUTE));
|
||||
case "SpeedReduction":
|
||||
return new SpeedReduction(json.getInt(RouteAction.ROUTE), json.getInt(SpeedReduction.MAX_SPEED));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ import de.srsoftware.web4rail.Route;
|
||||
public class ActivateRoute extends RouteAction {
|
||||
|
||||
|
||||
public ActivateRoute(Route route) {
|
||||
super(route);
|
||||
public ActivateRoute(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() throws IOException {
|
||||
public void fire(Route route) throws IOException {
|
||||
route.activate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ import de.srsoftware.web4rail.Route;
|
||||
|
||||
public class FinishRoute extends RouteAction {
|
||||
|
||||
public FinishRoute(Route route) {
|
||||
super(route);
|
||||
public FinishRoute(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() throws IOException {
|
||||
public void fire(Route route) throws IOException {
|
||||
route.finish();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
|
||||
public abstract class RouteAction extends Action {
|
||||
|
||||
private static final String ROUTE = "route";
|
||||
protected Route route;
|
||||
static final String ROUTE = "route";
|
||||
protected int routeId;
|
||||
|
||||
public RouteAction(Route route) {
|
||||
this.route = route;
|
||||
public RouteAction(int routeId) {
|
||||
this.routeId = routeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(ROUTE, route.id());
|
||||
json.put(ROUTE, routeId);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire(Plan plan) throws IOException {
|
||||
fire(plan.route(routeId));
|
||||
}
|
||||
|
||||
protected abstract void fire(Route route) throws IOException;
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ import de.srsoftware.web4rail.tiles.Signal;
|
||||
public class SetSignalsToStop extends RouteAction {
|
||||
|
||||
|
||||
public SetSignalsToStop(Route route) {
|
||||
super(route);
|
||||
public SetSignalsToStop(int routeId) {
|
||||
super(routeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() throws IOException {
|
||||
public void fire(Route route) throws IOException {
|
||||
route.setSignals(Signal.STOP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,16 +7,16 @@ import de.srsoftware.web4rail.moving.Train;
|
||||
|
||||
public class SpeedReduction extends RouteAction {
|
||||
|
||||
private static final String MAX_SPEED = "max_speed";
|
||||
static final String MAX_SPEED = "max_speed";
|
||||
private int maxSpeed;
|
||||
|
||||
public SpeedReduction(Route route, int kmh) {
|
||||
super(route);
|
||||
public SpeedReduction(int routeId, int kmh) {
|
||||
super(routeId);
|
||||
maxSpeed = kmh;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
public void fire(Route route) {
|
||||
Train train = route.train;
|
||||
if (train != null && train.speed > maxSpeed) train.setSpeed(maxSpeed);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user