re-implented trailing calculations of train

This commit is contained in:
Stephan Richter
2020-11-05 18:27:12 +01:00
parent f9a3466f33
commit 0ef617aa34
16 changed files with 294 additions and 263 deletions

View File

@@ -22,7 +22,6 @@ public abstract class Block extends StretchableTile{
private static final String ALLOW_TURN = "allowTurn";
public boolean turnAllowed = false;
private Train trailingTrain = null;
@Override
public JSONObject config() {
@@ -30,11 +29,6 @@ public abstract class Block extends StretchableTile{
config.put(NAME, name);
return config;
}
@Override
public boolean isFree() {
return super.isFree() && trailingTrain == null;
}
@Override
public JSONObject json() {
@@ -61,7 +55,7 @@ public abstract class Block extends StretchableTile{
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
Select select = Train.selector(trainHead, null);
Select select = Train.selector(train, null);
select.addTo(new Label(t("Train:")+NBSP)).addTo(new Tag("p")).addTo(form);
return form;
@@ -71,10 +65,10 @@ public abstract class Block extends StretchableTile{
public Tag propMenu() {
Tag window = super.propMenu();
if (trainHead != null) {
window.children().insertElementAt(new Button(t("stop"),"train("+trainHead.id+",'"+ACTION_STOP+"')"), 1);
window.children().insertElementAt(new Button(t("start"),"train("+trainHead.id+",'"+ACTION_START+"')"), 1);
window.children().insertElementAt(trainHead.link("span"), 1);
if (isSet(train)) {
window.children().insertElementAt(new Button(t("stop"),"train("+train.id+",'"+ACTION_STOP+"')"), 1);
window.children().insertElementAt(new Button(t("start"),"train("+train.id+",'"+ACTION_START+"')"), 1);
window.children().insertElementAt(train.link("span"), 1);
window.children().insertElementAt(new Tag("h4").content(t("Train:")), 1);
}
return window;
@@ -84,12 +78,11 @@ public abstract class Block extends StretchableTile{
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
if (replacements == null) replacements = new HashMap<String, Object>();
if (isNull(replacements)) replacements = new HashMap<String, Object>();
replacements.put("%text%",name);
if (trailingTrain != null) replacements.put("%text%","("+trailingTrain.name()+")");
if (trainHead != null) replacements.put("%text%",trainHead.directedName());
if (isSet(train)) replacements.put("%text%",train.directedName());
Tag tag = super.tag(replacements);
if (trainHead != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied");
if (isSet(train)) tag.clazz(tag.get("class")+" occupied");
return tag;
}
@@ -103,32 +96,19 @@ public abstract class Block extends StretchableTile{
return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")";
}
public void trailingTrain(Train train) {
trailingTrain = train;
this.trainHead = null;
plan.place(this);
}
public Train trailingTrain() {
return trailingTrain;
}
@Override
public void unlock() {
trailingTrain = null;
super.unlock();
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
if (params.containsKey(NAME)) name=params.get(NAME);
if (params.containsKey(Train.HEAD)) {
int trainId = Integer.parseInt(params.get(Train.HEAD));
if (trainId == 0) {
trainHead(null);
train = null;
} else {
Train t = Train.get(trainId);
if (t != null) trainHead = t.block(this,true);
Train dummy = Train.get(trainId);
if (isSet(dummy) && dummy != train) {
dummy.set(this);
dummy.dropTrace();
}
}
}
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");

View File

@@ -15,7 +15,7 @@ public class BlockH extends Block{
public Map<Connector, State> connections(Direction from) {
switch (from) {
case WEST:
return Map.of(new Connector(x+len(),y,Direction.WEST),State.UNDEF);
return Map.of(new Connector(x+width(),y,Direction.WEST),State.UNDEF);
case EAST:
return Map.of(new Connector(x-1,y,Direction.EAST),State.UNDEF);
default:
@@ -24,12 +24,17 @@ public class BlockH extends Block{
}
@Override
public int len() {
return length;
public int width() {
return stretch;
}
@Override
public List<Connector> startPoints() {
return List.of(new Connector(x-1, y, Direction.EAST),new Connector(x+len(), y, Direction.WEST));
return List.of(new Connector(x-1, y, Direction.EAST),new Connector(x+width(), y, Direction.WEST));
}
@Override
protected String stretchType() {
return t("Width");
}
}

View File

@@ -25,11 +25,16 @@ public class BlockV extends Block{
@Override
public int height() {
return length;
return stretch;
}
@Override
public List<Connector> startPoints() {
return List.of(new Connector(x,y-1,Direction.SOUTH),new Connector(x,y+height(),Direction.NORTH));
}
@Override
protected String stretchType() {
return t("Height");
}
}

View File

@@ -24,7 +24,7 @@ public class CrossH extends Cross{
}
@Override
public int len() {
public int width() {
return 2;
}

View File

@@ -15,7 +15,7 @@ public class StraightH extends StretchableTile{
if (oneWay == from) return new HashMap<>();
switch (from) {
case WEST:
return Map.of(new Connector(x+len(),y,Direction.WEST),State.UNDEF);
return Map.of(new Connector(x+width(),y,Direction.WEST),State.UNDEF);
case EAST:
return Map.of(new Connector(x-1,y,Direction.EAST),State.UNDEF);
default:
@@ -24,12 +24,17 @@ public class StraightH extends StretchableTile{
}
@Override
public int len() {
return length;
public int width() {
return stretch;
}
@Override
public List<Direction> possibleDirections() {
return List.of(Direction.EAST,Direction.WEST);
}
@Override
protected String stretchType() {
return t("Width");
}
}

View File

@@ -25,11 +25,16 @@ public class StraightV extends StretchableTile{
@Override
public int height() {
return length;
return stretch;
}
@Override
public List<Direction> possibleDirections() {
return List.of(Direction.NORTH,Direction.SOUTH);
}
@Override
protected String stretchType() {
return t("Height");
}
}

View File

@@ -7,61 +7,64 @@ import java.util.Map.Entry;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label;
public abstract class StretchableTile extends Tile {
private static final String LENGTH = "length";
public int length = 1;
private static final String STRETCH_LENGTH = "stretch";
public int stretch = 1;
@Override
public JSONObject config() {
JSONObject config = super.config();
if (length != 1) config.put(LENGTH, length);
if (stretch != 1) config.put(STRETCH_LENGTH, stretch);
return config;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
if (length > 1) json.put(LENGTH, length);
if (stretch > 1) json.put(STRETCH_LENGTH, stretch);
return json;
}
@Override
protected Tile load(JSONObject json) throws IOException {
super.load(json);
if (json.has(LENGTH)) length = json.getInt(LENGTH);
if (json.has(STRETCH_LENGTH)) stretch = json.getInt(STRETCH_LENGTH);
return this;
}
@Override
public Tag propForm(String id) {
Tag form = super.propForm(id);
new Tag("h4").content(t("Length")).addTo(form);
Tag label = new Tag("label").content(t("length:")+NBSP);
new Tag("input").attr("type", "number").attr("name","length").attr("value", length).addTo(label);
label.addTo(new Tag("p")).addTo(form);
new Tag("h4").content(stretchType()).addTo(form);
new Input(STRETCH_LENGTH, stretch).numeric().addTo(new Label(stretchType()+":"+NBSP)).addTo(new Tag("p")).addTo(form);
return form;
}
private void setLength(String value) {
private void stretch(String value) {
try {
setLength(Integer.parseInt(value));
stretch(Integer.parseInt(value));
} catch (NumberFormatException nfe) {
LOG.warn("{} is not a valid length!",value);
}
}
public void setLength(int len) {
this.length = Math.max(1, len);
public void stretch(int len) {
this.stretch = Math.max(1, len);
}
protected abstract String stretchType();
@Override
public Tile update(HashMap<String, String> params) throws IOException {
for (Entry<String, String> entry : params.entrySet()) {
switch (entry.getKey()) {
case LENGTH:
setLength(entry.getValue());
case STRETCH_LENGTH:
stretch(entry.getValue());
break;
}
}

View File

@@ -23,8 +23,8 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Connector;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.Route;
@@ -36,10 +36,12 @@ import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Radio;
public abstract class Tile implements Constants{
public abstract class Tile extends BaseClass{
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
private static int DEFAUT_LENGTH = 5;
public static final String ID = "id";
private static final String LENGTH = "length";
private static final String LOCKED = "locked";
private static final String OCCUPIED = "occupied";
private static final String ONEW_WAY = "one_way";
@@ -48,16 +50,17 @@ public abstract class Tile implements Constants{
private static final String TYPE = "type";
private static final String X = "x";
private static final String Y = "y";
private boolean disabled = false;
private boolean disabled = false;
private int length = DEFAUT_LENGTH;
protected Direction oneWay = null;
protected Plan plan = null;;
protected Route route = null;
private HashSet<Route> routes = new HashSet<>();
protected HashSet<Shadow> shadows = new HashSet<>();
protected Train trainHead = null;
protected Train trainTail = null;
protected Train train = null;
public Integer x = null;
public Integer y = null;
@@ -66,8 +69,8 @@ public abstract class Tile implements Constants{
classes.add("tile");
classes.add(getClass().getSimpleName());
if (isSet(route)) classes.add(LOCKED);
if (isSet(trainHead) || isSet(trainTail)) classes.add(OCCUPIED);
if (disabled) classes.add(DISABLED);
if (isSet(train)) classes.add(OCCUPIED);
if (disabled) classes.add(DISABLED);
return classes;
}
@@ -93,7 +96,7 @@ public abstract class Tile implements Constants{
}
public boolean isFree() {
return !(disabled || isSet(route) || isSet(trainHead) || isSet(trainTail));
return !(disabled || isSet(route) || isSet(train));
}
public int height() {
@@ -115,14 +118,6 @@ public abstract class Tile implements Constants{
tile.load(json);
plan.set(tile.x, tile.y, tile);
}
protected static boolean isNull(Object o) {
return o==null;
}
protected static boolean isSet(Object o) {
return o != null;
}
public JSONObject json() {
JSONObject json = new JSONObject();
@@ -132,13 +127,18 @@ public abstract class Tile implements Constants{
if (isSet(route)) json.put(ROUTE, route.id());
if (isSet(oneWay)) json.put(ONEW_WAY, oneWay);
if (disabled) json.put(DISABLED, true);
if (isSet(trainHead)) json.put(Train.HEAD, trainHead.id);
if (isSet(trainTail)) json.put(Train.TAIL, trainTail.id);
if (isSet(train)) json.put(REALM_TRAIN, train.id);
json.put(LENGTH, length);
return json;
}
public int len() {
return 1;
public int length() {
return length;
}
public Tile length(int newLength) {
length = newLength;
return this;
}
public static void loadAll(String filename, Plan plan) throws IOException {
@@ -163,10 +163,10 @@ public abstract class Tile implements Constants{
JSONObject pos = json.getJSONObject(POS);
x = pos.getInt(X);
y = pos.getInt(Y);
if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY));
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
if (json.has(Train.HEAD)) trainHead = Train.get(json.getInt(Train.HEAD));
if (json.has(Train.TAIL)) trainTail = Train.get(json.getInt(Train.TAIL));
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
if (json.has(LENGTH)) length = json.getInt(LENGTH);
if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY));
if (json.has(REALM_TRAIN)) train = Train.get(json.getInt(REALM_TRAIN));
return this;
}
@@ -195,7 +195,6 @@ public abstract class Tile implements Constants{
new Input(REALM, REALM_PLAN).hideIn(form);
new Input(ID,id()).hideIn(form);
List<Direction> pd = possibleDirections();
if (!pd.isEmpty()) {
new Tag("h4").content(t("One way:")).addTo(form);
@@ -211,6 +210,8 @@ public abstract class Tile implements Constants{
Window window = new Window("tile-properties",t("Properties of {} @ ({},{})",title(),x,y));
String formId = "tile-properties-"+id();
Tag form = propForm(formId);
new Tag("h4").content(t("Length")).addTo(form);
new Input(LENGTH,length).numeric().addTo(form);
new Tag("h4").content(t("Availability")).addTo(form);
new Checkbox(DISABLED, t("disabled"), disabled).addTo(form);
new Button(t("Apply"),"submitForm('"+formId+"')").addTo(form);
@@ -285,7 +286,7 @@ public abstract class Tile implements Constants{
}
public Tag tag(Map<String,Object> replacements) throws IOException {
int width = 100*len();
int width = 100*width();
int height = 100*height();
if (isNull(replacements)) replacements = new HashMap<String, Object>();
replacements.put("%width%",width);
@@ -298,7 +299,7 @@ public abstract class Tile implements Constants{
.attr("name", getClass().getSimpleName())
.attr("viewbox", "0 0 "+width+" "+height);
if (isSet(x)) style="left: "+(30*x)+"px; top: "+(30*y)+"px;";
if (len()>1) style+=" width: "+(30*len())+"px;";
if (width()>1) style+=" width: "+(30*width())+"px;";
if (height()>1) style+=" height: "+(30*height())+"px;";
if (!style.isEmpty()) svg.style(style);
@@ -355,20 +356,19 @@ public abstract class Tile implements Constants{
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
}
public Train trainHead() {
return trainHead;
public Train train() {
return train;
}
public Tile trainHead(Train train) {
if (this.trainHead == train) return this; // nothing to update
this.trainHead = train;
public Tile set(Train newTrain) {
if (newTrain == train) return this; // nothing to update
this.train = newTrain;
return plan.place(this);
}
public void unlock() {
route = null;
trainHead = null;
trainTail = null;
route = null;
train = null;
plan.place(this);
}
@@ -383,7 +383,13 @@ public abstract class Tile implements Constants{
}
}
disabled = "on".equals(params.get(DISABLED));
String len = params.get(LENGTH);
if (isSet(len)) length(Integer.parseInt(len));
plan.stream(tag(null).toString());
return this;
}
public int width() {
return 1;
}
}