implemented push-pull trains
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.3.1</version>
|
||||
<version>0.3.2</version>
|
||||
<name>Web4Rail</name>
|
||||
<description>Java Model Railway Control</description>
|
||||
<url>https://github.com/StephanRichter/Web4Rail</url>
|
||||
|
||||
@@ -68,7 +68,7 @@ public class Plan {
|
||||
public enum Direction{
|
||||
NORTH, SOUTH, EAST, WEST;
|
||||
|
||||
Direction inverse() {
|
||||
public Direction inverse() {
|
||||
switch (this) {
|
||||
case NORTH: return SOUTH;
|
||||
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 String ACTION_SHOW_TRAIN = "showTrain";
|
||||
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 HashSet<Block> blocks = new HashSet<Block>();
|
||||
@@ -416,6 +417,9 @@ public class Plan {
|
||||
return start(train(get(params.get(X),params.get(Y),true)));
|
||||
case ACTION_UPDATE:
|
||||
return update(params);
|
||||
case ACTION_UPDATE_TRAIN:
|
||||
return updateTrain(params);
|
||||
|
||||
default:
|
||||
LOG.warn("Unknown action: {}",action);
|
||||
}
|
||||
@@ -596,6 +600,11 @@ public class Plan {
|
||||
Tile tile = get(x,y,true);
|
||||
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) {
|
||||
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.web4rail.moving;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Vector;
|
||||
@@ -14,11 +15,17 @@ import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
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.Signal;
|
||||
|
||||
public class Train {
|
||||
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<Car> cars = new Vector<Car>();
|
||||
private String name = null;
|
||||
@@ -26,9 +33,13 @@ public class Train {
|
||||
public Route route;
|
||||
public int speed = 0;
|
||||
private Direction direction;
|
||||
private boolean pushPull = false;
|
||||
private int id;
|
||||
|
||||
public Train(Locomotive loco) {
|
||||
id = ++count;
|
||||
add(loco);
|
||||
trains.put(id, this);
|
||||
}
|
||||
|
||||
public void add(Car car) {
|
||||
@@ -41,6 +52,11 @@ public class Train {
|
||||
public void block(Block block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public Train heading(Direction dir) {
|
||||
direction = dir;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
int result = 0;
|
||||
@@ -66,6 +82,14 @@ public class Train {
|
||||
public Tag props() {
|
||||
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 locos = new Tag("li").content(t("Locomotives:"));
|
||||
Tag l2 = new Tag("ul");
|
||||
@@ -94,7 +118,11 @@ public class Train {
|
||||
for (Route rt : routes) {
|
||||
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 (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
|
||||
LOG.debug("{} is not free!",rt);
|
||||
continue;
|
||||
@@ -104,12 +132,13 @@ public class Train {
|
||||
Random rand = new Random();
|
||||
if (availableRoutes.isEmpty()) return t("No free routes from {}",block);
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -117,9 +146,18 @@ public class Train {
|
||||
public String toString() {
|
||||
return name();
|
||||
}
|
||||
|
||||
private void turn() throws IOException {
|
||||
direction = direction.inverse();
|
||||
if (block != null) block.train(this);
|
||||
}
|
||||
|
||||
public Train heading(Direction dir) {
|
||||
direction = dir;
|
||||
return 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.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
|
||||
public abstract class Block extends StretchableTile{
|
||||
private static final String NAME = "name";
|
||||
private static final String ALLOW_TURN = "allowTurn";
|
||||
public String name = "Block";
|
||||
private Train train;
|
||||
public boolean turnAllowed = false;
|
||||
|
||||
@Override
|
||||
public JSONObject config() {
|
||||
@@ -39,8 +42,10 @@ public abstract class Block extends StretchableTile{
|
||||
Tag form = super.propForm();
|
||||
|
||||
Tag label = new Tag("label").content(t("name:"));
|
||||
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);
|
||||
|
||||
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(form);
|
||||
|
||||
return form;
|
||||
}
|
||||
@@ -96,6 +101,7 @@ public abstract class Block extends StretchableTile{
|
||||
public Tile update(HashMap<String, String> params) {
|
||||
super.update(params);
|
||||
if (params.containsKey(NAME)) name=params.get(NAME);
|
||||
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user