implemented storing of routes

This commit is contained in:
Stephan Richter
2020-09-21 14:27:21 +02:00
parent 67623f49bc
commit 5c67e6089d
9 changed files with 156 additions and 66 deletions

View File

@@ -2,9 +2,19 @@ package de.srsoftware.web4rail.actions;
import java.io.IOException;
import org.json.JSONObject;
public abstract class Action {
private static final String TYPE = "type";
public abstract void fire() throws IOException;
public JSONObject json() {
JSONObject json = new JSONObject();
json.put(TYPE, getClass().getSimpleName());
return json;
}
@Override
public String toString() {
return getClass().getSimpleName();

View File

@@ -4,12 +4,11 @@ import java.io.IOException;
import de.srsoftware.web4rail.Route;
public class ActivateRoute extends Action {
public class ActivateRoute extends RouteAction {
private Route route;
public ActivateRoute(Route route) {
this.route = route;
super(route);
}
@Override

View File

@@ -4,17 +4,14 @@ import java.io.IOException;
import de.srsoftware.web4rail.Route;
public class FinishRoute extends Action {
private Route route;
public class FinishRoute extends RouteAction {
public FinishRoute(Route route) {
this.route = route;
super(route);
}
@Override
public void fire() throws IOException {
route.finish();
}
}

View File

@@ -0,0 +1,22 @@
package de.srsoftware.web4rail.actions;
import org.json.JSONObject;
import de.srsoftware.web4rail.Route;
public abstract class RouteAction extends Action {
private static final String ROUTE = "route";
protected Route route;
public RouteAction(Route route) {
this.route = route;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
json.put(ROUTE, route.id());
return json;
}
}

View File

@@ -5,17 +5,15 @@ import java.io.IOException;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.tiles.Signal;
public class SetSignalsToStop extends Action {
public class SetSignalsToStop extends RouteAction {
private Route route;
public SetSignalsToStop(Route route) {
this.route = route;
super(route);
}
@Override
public void fire() throws IOException {
route.setSignals(Signal.STOP);
}
}

View File

@@ -1,15 +1,17 @@
package de.srsoftware.web4rail.actions;
import org.json.JSONObject;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.moving.Train;
public class SpeedReduction extends Action {
public class SpeedReduction extends RouteAction {
private static final String MAX_SPEED = "max_speed";
private int maxSpeed;
private Route route;
public SpeedReduction(Route route, int kmh) {
this.route = route;
super(route);
maxSpeed = kmh;
}
@@ -18,4 +20,11 @@ public class SpeedReduction extends Action {
Train train = route.train;
if (train != null && train.speed > maxSpeed) train.setSpeed(maxSpeed);
}
@Override
public JSONObject json() {
JSONObject json = super.json();
json.put(MAX_SPEED, maxSpeed);
return json;
}
}