implemented push-pull trains
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>0.3.1</version>
|
<version>0.3.2</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
<url>https://github.com/StephanRichter/Web4Rail</url>
|
<url>https://github.com/StephanRichter/Web4Rail</url>
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ public class Plan {
|
|||||||
public enum Direction{
|
public enum Direction{
|
||||||
NORTH, SOUTH, EAST, WEST;
|
NORTH, SOUTH, EAST, WEST;
|
||||||
|
|
||||||
Direction inverse() {
|
public Direction inverse() {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case NORTH: return SOUTH;
|
case NORTH: return SOUTH;
|
||||||
case SOUTH: return NORTH;
|
case SOUTH: return NORTH;
|
||||||
@@ -112,6 +112,7 @@ public class Plan {
|
|||||||
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
||||||
private static final String ACTION_SHOW_TRAIN = "showTrain";
|
private static final String ACTION_SHOW_TRAIN = "showTrain";
|
||||||
private static final String ACTION_START_TRAIN = "startTrain";
|
private static final String ACTION_START_TRAIN = "startTrain";
|
||||||
|
private static final String ACTION_UPDATE_TRAIN = "updateTrain";
|
||||||
|
|
||||||
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
|
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
|
||||||
private HashSet<Block> blocks = new HashSet<Block>();
|
private HashSet<Block> blocks = new HashSet<Block>();
|
||||||
@@ -416,6 +417,9 @@ public class Plan {
|
|||||||
return start(train(get(params.get(X),params.get(Y),true)));
|
return start(train(get(params.get(X),params.get(Y),true)));
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return update(params);
|
return update(params);
|
||||||
|
case ACTION_UPDATE_TRAIN:
|
||||||
|
return updateTrain(params);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LOG.warn("Unknown action: {}",action);
|
LOG.warn("Unknown action: {}",action);
|
||||||
}
|
}
|
||||||
@@ -597,6 +601,11 @@ public class Plan {
|
|||||||
if (tile != null) set(x,y,tile.update(params));
|
if (tile != null) set(x,y,tile.update(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object updateTrain(HashMap<String, String> params) throws IOException {
|
||||||
|
Train.update(params);
|
||||||
|
return this.html();
|
||||||
|
}
|
||||||
|
|
||||||
public void warn(Contact contact) {
|
public void warn(Contact contact) {
|
||||||
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.srsoftware.web4rail.moving;
|
package de.srsoftware.web4rail.moving;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
@@ -14,11 +15,17 @@ import de.srsoftware.web4rail.Application;
|
|||||||
import de.srsoftware.web4rail.Plan.Direction;
|
import de.srsoftware.web4rail.Plan.Direction;
|
||||||
import de.srsoftware.web4rail.Route;
|
import de.srsoftware.web4rail.Route;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
|
import de.srsoftware.web4rail.tags.Checkbox;
|
||||||
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tiles.Block;
|
import de.srsoftware.web4rail.tiles.Block;
|
||||||
import de.srsoftware.web4rail.tiles.Signal;
|
import de.srsoftware.web4rail.tiles.Signal;
|
||||||
|
|
||||||
public class Train {
|
public class Train {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
||||||
|
private static final String PUSH_PULL = "pushPull";
|
||||||
|
private static int count = 0;
|
||||||
|
private static final HashMap<Integer, Train> trains = new HashMap<Integer, Train>();
|
||||||
|
private static final String ID = "id";
|
||||||
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
||||||
private Vector<Car> cars = new Vector<Car>();
|
private Vector<Car> cars = new Vector<Car>();
|
||||||
private String name = null;
|
private String name = null;
|
||||||
@@ -26,9 +33,13 @@ public class Train {
|
|||||||
public Route route;
|
public Route route;
|
||||||
public int speed = 0;
|
public int speed = 0;
|
||||||
private Direction direction;
|
private Direction direction;
|
||||||
|
private boolean pushPull = false;
|
||||||
|
private int id;
|
||||||
|
|
||||||
public Train(Locomotive loco) {
|
public Train(Locomotive loco) {
|
||||||
|
id = ++count;
|
||||||
add(loco);
|
add(loco);
|
||||||
|
trains.put(id, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Car car) {
|
public void add(Car car) {
|
||||||
@@ -42,6 +53,11 @@ public class Train {
|
|||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Train heading(Direction dir) {
|
||||||
|
direction = dir;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public int length() {
|
public int length() {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (Locomotive loco : locos) result += loco.length;
|
for (Locomotive loco : locos) result += loco.length;
|
||||||
@@ -66,6 +82,14 @@ public class Train {
|
|||||||
public Tag props() {
|
public Tag props() {
|
||||||
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
||||||
|
|
||||||
|
Form form = new Form();
|
||||||
|
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "updateTrain").addTo(form);
|
||||||
|
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", id).addTo(form);
|
||||||
|
|
||||||
|
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
||||||
|
pp.addTo(form);
|
||||||
|
new Tag("button").attr("type", "submit").content(t("save")).addTo(form).addTo(window);
|
||||||
|
|
||||||
Tag list = new Tag("ul");
|
Tag list = new Tag("ul");
|
||||||
Tag locos = new Tag("li").content(t("Locomotives:"));
|
Tag locos = new Tag("li").content(t("Locomotives:"));
|
||||||
Tag l2 = new Tag("ul");
|
Tag l2 = new Tag("ul");
|
||||||
@@ -94,7 +118,11 @@ public class Train {
|
|||||||
for (Route rt : routes) {
|
for (Route rt : routes) {
|
||||||
if (rt == route) continue; // andere Route als zuvor wählen
|
if (rt == route) continue; // andere Route als zuvor wählen
|
||||||
if (rt.path().firstElement() != block) continue; // keine Route wählen, die nicht vom aktuellen Block des Zuges startet
|
if (rt.path().firstElement() != block) continue; // keine Route wählen, die nicht vom aktuellen Block des Zuges startet
|
||||||
if (direction != null && rt.startDirection != direction) continue; // keine Routen entgegen der Fahrtrichtung wählen
|
if (direction != null && rt.startDirection != direction) { // Route ist entgegen der Startrichtung des Zuges
|
||||||
|
if (!pushPull || !block.turnAllowed) { // Zug ist kein Wendezug oder Block erlaubt kein Wenden
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!rt.free()) { // keine belegten Routen wählen
|
if (!rt.free()) { // keine belegten Routen wählen
|
||||||
LOG.debug("{} is not free!",rt);
|
LOG.debug("{} is not free!",rt);
|
||||||
continue;
|
continue;
|
||||||
@@ -104,12 +132,13 @@ public class Train {
|
|||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
if (availableRoutes.isEmpty()) return t("No free routes from {}",block);
|
if (availableRoutes.isEmpty()) return t("No free routes from {}",block);
|
||||||
int sel = rand.nextInt(availableRoutes.size());
|
int sel = rand.nextInt(availableRoutes.size());
|
||||||
this.route = availableRoutes.get(sel).lock(this).setSignals(null);
|
route = availableRoutes.get(sel).lock(this).setSignals(null);
|
||||||
|
if (direction != route.startDirection) turn();
|
||||||
setSpeed(100);
|
setSpeed(100);
|
||||||
return t("started {}",this);
|
return t("started {}",this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String t(String message, Object...fills) {
|
private static String t(String message, Object...fills) {
|
||||||
return Translation.get(Application.class, message, fills);
|
return Translation.get(Application.class, message, fills);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,8 +147,17 @@ public class Train {
|
|||||||
return name();
|
return name();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Train heading(Direction dir) {
|
private void turn() throws IOException {
|
||||||
direction = dir;
|
direction = direction.inverse();
|
||||||
return this;
|
if (block != null) block.train(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void update(HashMap<String, String> params) {
|
||||||
|
LOG.debug("update({})",params);
|
||||||
|
int id = Integer.parseInt(params.get(ID));
|
||||||
|
Train train = trains.get(id);
|
||||||
|
if (train == null) return;
|
||||||
|
train.pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/main/java/de/srsoftware/web4rail/tags/Checkbox.java
Normal file
18
src/main/java/de/srsoftware/web4rail/tags/Checkbox.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package de.srsoftware.web4rail.tags;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Tag;
|
||||||
|
|
||||||
|
public class Checkbox extends Tag {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -7294673319021862994L;
|
||||||
|
|
||||||
|
public Checkbox(String name, String label, boolean preCheck) {
|
||||||
|
super("label");
|
||||||
|
Tag checkbox = new Tag("input").attr("type", "checkbox").attr("name", name);
|
||||||
|
if (preCheck) checkbox.attr("checked", "checked");
|
||||||
|
checkbox.addTo(this);
|
||||||
|
content(label);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,11 +10,14 @@ import org.json.JSONObject;
|
|||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Connector;
|
import de.srsoftware.web4rail.Connector;
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
|
import de.srsoftware.web4rail.tags.Checkbox;
|
||||||
|
|
||||||
public abstract class Block extends StretchableTile{
|
public abstract class Block extends StretchableTile{
|
||||||
private static final String NAME = "name";
|
private static final String NAME = "name";
|
||||||
|
private static final String ALLOW_TURN = "allowTurn";
|
||||||
public String name = "Block";
|
public String name = "Block";
|
||||||
private Train train;
|
private Train train;
|
||||||
|
public boolean turnAllowed = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject config() {
|
public JSONObject config() {
|
||||||
@@ -42,6 +45,8 @@ public abstract class Block extends StretchableTile{
|
|||||||
new Tag("input").attr("type", "text").attr(NAME,"name").attr("value", name).addTo(label);
|
new Tag("input").attr("type", "text").attr(NAME,"name").attr("value", name).addTo(label);
|
||||||
label.addTo(form);
|
label.addTo(form);
|
||||||
|
|
||||||
|
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(form);
|
||||||
|
|
||||||
return form;
|
return form;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +101,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
public Tile update(HashMap<String, String> params) {
|
public Tile update(HashMap<String, String> params) {
|
||||||
super.update(params);
|
super.update(params);
|
||||||
if (params.containsKey(NAME)) name=params.get(NAME);
|
if (params.containsKey(NAME)) name=params.get(NAME);
|
||||||
|
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user