bugfixes + added new condition TrainWasInBlock
This commit is contained in:
@@ -32,7 +32,7 @@ public class BlockFree extends Condition {
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
block(Block.get(new Id(json.getString(BLOCK))));
|
||||
if (json.has(BLOCK)) block(Block.get(new Id(json.getString(BLOCK))));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class BlockFree extends Condition {
|
||||
if (!params.containsKey(BLOCK)) return t("No block id passed to BlockFree.update()!");
|
||||
Id bid = new Id(params.get(BLOCK));
|
||||
Block block = Block.get(bid);
|
||||
if (block == null) return t("No block with id {} found!",bid);
|
||||
if (isNull(block)) return t("No block with id {} found!",bid);
|
||||
this.block = block;
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,8 @@ public abstract class Condition extends BaseClass {
|
||||
TrainHasTag.class,
|
||||
TrainLength.class,
|
||||
TrainSelect.class,
|
||||
TrainSpeed.class);
|
||||
TrainSpeed.class,
|
||||
TrainWasInBlock.class);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package de.srsoftware.web4rail.conditions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
public class TrainWasInBlock extends Condition {
|
||||
|
||||
private static final String BLOCK = Block.class.getSimpleName();
|
||||
private static final String COUNT = "count";
|
||||
private Block block;
|
||||
private int count = 5;
|
||||
|
||||
private TrainWasInBlock block(Block block) {
|
||||
this.block = block;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
Train train = context.train();
|
||||
if (isNull(train)) return false;
|
||||
return train.lastBlocks(count).contains(block) != inverted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
return super.json().put(BLOCK, block.id()).put(COUNT, count);
|
||||
}
|
||||
|
||||
public Condition load(JSONObject json) {
|
||||
super.load(json);
|
||||
if (json.has(BLOCK)) block(Block.get(new Id(json.getString(BLOCK))));
|
||||
if (json.has(COUNT)) count = json.getInt(COUNT);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Select block"), Block.selector(block, null));
|
||||
formInputs.add(t("Seek in last"), new Input(COUNT, count).numeric().addTo(new Tag("span")).content(NBSP+t("blocks of train")));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeChild(BaseClass child) {
|
||||
if (child == block) block = null;
|
||||
super.removeChild(child);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (block == null) return "["+t("Click here to select block!")+"]";
|
||||
return t(inverted ? "{} not within last {} blocks of train":"{} within last {} blocks of train",block,count);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
if (!params.containsKey(BLOCK)) return t("No block id passed to TrainWasInBlock.update()!");
|
||||
Id bid = new Id(params.get(BLOCK));
|
||||
Block block = Block.get(bid);
|
||||
if (isNull(block)) return t("No block with id {} found!",bid);
|
||||
this.block = block;
|
||||
if (params.containsKey(COUNT)) count=Integer.parseInt(params.get(COUNT));
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private Block currentBlock,destination = null;
|
||||
LinkedList<Tile> trace = new LinkedList<Tile>();
|
||||
private Vector<Block> lastBlocks = new Vector<Block>();
|
||||
|
||||
public int speed = 0;
|
||||
private Autopilot autopilot = null;
|
||||
@@ -227,6 +228,16 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return t("{} now in auto-mode",this);
|
||||
}
|
||||
|
||||
private Fieldset blockHistory() {
|
||||
Fieldset fieldset = new Fieldset(t("Last blocks"));
|
||||
Tag list = new Tag("ol");
|
||||
for (int i=lastBlocks.size(); i>0; i--) {
|
||||
lastBlocks.get(i-1).link().addTo(new Tag("li")).addTo(list);
|
||||
}
|
||||
return list.addTo(fieldset);
|
||||
}
|
||||
|
||||
|
||||
public String brakeId() {
|
||||
return brakeId(false);
|
||||
}
|
||||
@@ -405,6 +416,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (!tags.isEmpty()) json.put(TAGS, tags);
|
||||
return json;
|
||||
}
|
||||
|
||||
public Collection<Block> lastBlocks(int count) {
|
||||
Vector<Block> blocks = new Vector<Block>(count);
|
||||
for (int i=0; i<count && i<lastBlocks.size(); i++) blocks.add(lastBlocks.get(i));
|
||||
return blocks;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
int result = 0;
|
||||
@@ -593,6 +610,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
preForm.add(Locomotive.cockpit(this));
|
||||
postForm.add(otherTrainProps);
|
||||
postForm.add(brakeTimes());
|
||||
postForm.add(blockHistory());
|
||||
|
||||
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
@@ -700,7 +719,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
public void set(Block newBlock) {
|
||||
LOG.debug("{}.set({})",this,newBlock);
|
||||
currentBlock = newBlock;
|
||||
if (isSet(currentBlock)) currentBlock.setTrain(this);
|
||||
if (isSet(currentBlock)) {
|
||||
currentBlock.setTrain(this);
|
||||
lastBlocks.add(newBlock);
|
||||
if (lastBlocks.size()>32) lastBlocks.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
private String setDestination(HashMap<String, String> params) {
|
||||
|
||||
Reference in New Issue
Block a user