working on new storage format
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
/bin/
|
/bin/
|
||||||
/target/
|
/target/
|
||||||
|
/default.*
|
||||||
|
|||||||
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.3</version>
|
<version>0.3.4</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>
|
||||||
|
|||||||
@@ -25,7 +25,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import de.keawe.tools.translations.Translation;
|
import de.keawe.tools.translations.Translation;
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
|
import de.srsoftware.web4rail.moving.Car;
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
|
import de.srsoftware.web4rail.tags.Div;
|
||||||
import de.srsoftware.web4rail.tiles.Block;
|
import de.srsoftware.web4rail.tiles.Block;
|
||||||
import de.srsoftware.web4rail.tiles.BlockH;
|
import de.srsoftware.web4rail.tiles.BlockH;
|
||||||
import de.srsoftware.web4rail.tiles.BlockV;
|
import de.srsoftware.web4rail.tiles.BlockV;
|
||||||
@@ -112,7 +114,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_TRAIN = "train";
|
private static final String ACTION_TRAIN = "train";
|
||||||
|
|
||||||
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
|
public 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>();
|
||||||
private HashMap<String, Route> routes = new HashMap<String, Route>();
|
private HashMap<String, Route> routes = new HashMap<String, Route>();
|
||||||
|
|
||||||
@@ -123,8 +125,8 @@ public class Plan {
|
|||||||
private Tag actionMenu() throws IOException {
|
private Tag actionMenu() throws IOException {
|
||||||
Tag tileMenu = new Tag("div").clazz("actions").content(t("Actions"));
|
Tag tileMenu = new Tag("div").clazz("actions").content(t("Actions"));
|
||||||
StringBuffer tiles = new StringBuffer();
|
StringBuffer tiles = new StringBuffer();
|
||||||
tiles.append(new Tag("div").id("save").content(t("Save plan")));
|
tiles.append(new Div("save").content(t("Save plan")));
|
||||||
tiles.append(new Tag("div").id("analyze").content(t("Analyze plan")));
|
tiles.append(new Div("analyze").content(t("Analyze plan")));
|
||||||
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
|
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,11 +208,8 @@ public class Plan {
|
|||||||
for (Entry<Connector, State> entry: connectors.entrySet()) {
|
for (Entry<Connector, State> entry: connectors.entrySet()) {
|
||||||
route = routes.remove(0);
|
route = routes.remove(0);
|
||||||
connector = entry.getKey();
|
connector = entry.getKey();
|
||||||
State state = entry.getValue();
|
route.setLast(entry.getValue());
|
||||||
route.setLast(state);
|
if (connectors.size()>1) LOG.debug("RESUMING from {}",tile);
|
||||||
if (connectors.size()>1) {
|
|
||||||
LOG.debug("RESUMING from {}",tile);
|
|
||||||
}
|
|
||||||
results.addAll(follow(route,connector));
|
results.addAll(follow(route,connector));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +228,7 @@ public class Plan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Tag heartbeat() {
|
private Tag heartbeat() {
|
||||||
return new Tag("div").id("heartbeat").content("");
|
return new Div("heartbeat").content("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void heatbeat() {
|
public void heatbeat() {
|
||||||
@@ -323,16 +322,16 @@ public class Plan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Tag messages() {
|
private Tag messages() {
|
||||||
return new Tag("div").id("messages").content("");
|
return new Div("messages").content("");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tag moveMenu() {
|
private Tag moveMenu() {
|
||||||
Tag tileMenu = new Tag("div").clazz("move").title(t("Move tiles")).content(t("↹"));
|
Tag tileMenu = new Tag("div").clazz("move").title(t("Move tiles")).content(t("↹"));
|
||||||
StringBuffer tiles = new StringBuffer();
|
StringBuffer tiles = new StringBuffer();
|
||||||
tiles.append(new Tag("div").id("west").title(t("Move west")).content("↤"));
|
tiles.append(new Div("west").title(t("Move west")).content("↤"));
|
||||||
tiles.append(new Tag("div").id("east").title(t("Move east")).content("↦"));
|
tiles.append(new Div("east").title(t("Move east")).content("↦"));
|
||||||
tiles.append(new Tag("div").id("north").title(t("Move north")).content("↥"));
|
tiles.append(new Div("north").title(t("Move north")).content("↥"));
|
||||||
tiles.append(new Tag("div").id("south").title(t("Move south")).content("↧"));
|
tiles.append(new Div("south").title(t("Move south")).content("↧"));
|
||||||
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
|
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,6 +390,10 @@ public class Plan {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void place(Tile tile) throws IOException {
|
||||||
|
stream("place "+tile.tag(null));
|
||||||
|
}
|
||||||
|
|
||||||
public Object process(HashMap<String, String> params) {
|
public Object process(HashMap<String, String> params) {
|
||||||
try {
|
try {
|
||||||
String action = params.get(ACTION);
|
String action = params.get(ACTION);
|
||||||
@@ -420,6 +423,7 @@ public class Plan {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
String msg = e.getMessage();
|
String msg = e.getMessage();
|
||||||
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
|
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
|
||||||
|
LOG.debug(msg,e);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -455,24 +459,12 @@ public class Plan {
|
|||||||
|
|
||||||
private String saveTo(String name) throws IOException {
|
private String saveTo(String name) throws IOException {
|
||||||
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
|
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
|
||||||
File file = new File(name+".plan");
|
Car.saveAll(name+".cars");
|
||||||
|
Tile.saveAll(this.tiles,name+".tiles");
|
||||||
|
Train.saveAll(name+".trains"); // refers to cars, blocks
|
||||||
|
|
||||||
|
File file = new File(name+".routes");
|
||||||
BufferedWriter br = new BufferedWriter(new FileWriter(file));
|
BufferedWriter br = new BufferedWriter(new FileWriter(file));
|
||||||
for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) {
|
|
||||||
int x = column.getKey();
|
|
||||||
for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
|
|
||||||
int y = row.getKey();
|
|
||||||
Tile tile = row.getValue().position(x, y);
|
|
||||||
if (tile != null && !(tile instanceof Shadow)) {
|
|
||||||
br.append(x+":"+y+":"+tile.getClass().getSimpleName());
|
|
||||||
JSONObject config = tile.config();
|
|
||||||
if (!config.isEmpty()) br.append(":"+config);
|
|
||||||
br.append("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
br.close();
|
|
||||||
file = new File(name+".routes");
|
|
||||||
br = new BufferedWriter(new FileWriter(file));
|
|
||||||
for (Route route: routes.values()) {
|
for (Route route: routes.values()) {
|
||||||
br.append(route.id()+"="+route.json()+"\n");
|
br.append(route.id()+"="+route.json()+"\n");
|
||||||
}
|
}
|
||||||
@@ -486,7 +478,7 @@ public class Plan {
|
|||||||
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
|
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
|
||||||
for (int i=1; i<tile.height(); i++) set(x,y+i,new Shadow(tile));
|
for (int i=1; i<tile.height(); i++) set(x,y+i,new Shadow(tile));
|
||||||
set_intern(x,y,tile);
|
set_intern(x,y,tile);
|
||||||
stream("place "+tile.tag(null));
|
place(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void set_intern(int x, int y, Tile tile) {
|
private void set_intern(int x, int y, Tile tile) {
|
||||||
|
|||||||
@@ -50,10 +50,11 @@ public class Route {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Route wurde von Zug betreten
|
* Route wurde von Zug betreten
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void activate() {
|
public void activate() throws IOException {
|
||||||
LOG.debug("{} aktiviert.",this);
|
LOG.debug("{} aktiviert.",this);
|
||||||
for (Tile tile : path) tile.occupy(this);
|
for (Tile tile : path) tile.train(train);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile add(Tile tile, Direction direrction) {
|
public Tile add(Tile tile, Direction direrction) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package de.srsoftware.web4rail.actions;
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import de.srsoftware.web4rail.Route;
|
import de.srsoftware.web4rail.Route;
|
||||||
|
|
||||||
public class ActivateRoute extends Action {
|
public class ActivateRoute extends Action {
|
||||||
@@ -11,7 +13,7 @@ public class ActivateRoute extends Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire() {
|
public void fire() throws IOException {
|
||||||
route.activate();
|
route.activate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,54 @@
|
|||||||
package de.srsoftware.web4rail.moving;
|
package de.srsoftware.web4rail.moving;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class Car {
|
public class Car {
|
||||||
|
private static final String ID = "id";
|
||||||
|
private static final String NAME = "name";
|
||||||
|
private static final String LENGTH = "length";
|
||||||
|
private static HashMap<String,Car> cars = new HashMap<String, Car>();
|
||||||
public int length;
|
public int length;
|
||||||
private String name;
|
private String name;
|
||||||
|
private String id;
|
||||||
|
|
||||||
public Car(String name) {
|
public Car(String name) {
|
||||||
this.name = name;
|
this(name,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Car(String name, String id) {
|
||||||
|
this.name = name;
|
||||||
|
this.id = id == null ? ""+new Date().getTime() : id;
|
||||||
|
cars.put(this.id, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String id() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject json() {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put(ID,id);
|
||||||
|
json.put(NAME, name);
|
||||||
|
json.put(LENGTH, length);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
String name(){
|
String name(){
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void saveAll(String filename) throws IOException {
|
||||||
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
|
for (Entry<String, Car> entry: cars.entrySet()) {
|
||||||
|
file.write(entry.getValue().json()+"\n");
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,25 @@
|
|||||||
package de.srsoftware.web4rail.moving;
|
package de.srsoftware.web4rail.moving;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class Locomotive extends Car {
|
public class Locomotive extends Car {
|
||||||
|
|
||||||
|
private static final String REVERSE = "reverse";
|
||||||
|
private static final String LOCOMOTIVE = "locomotive";
|
||||||
|
private boolean reverse = false;
|
||||||
|
|
||||||
public Locomotive(String name) {
|
public Locomotive(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject json() {
|
||||||
|
JSONObject json = super.json();
|
||||||
|
JSONObject loco = new JSONObject();
|
||||||
|
loco.put(REVERSE, reverse);
|
||||||
|
json.put(LOCOMOTIVE, loco);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSpeed(int v) {
|
public void setSpeed(int v) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package de.srsoftware.web4rail.moving;
|
package de.srsoftware.web4rail.moving;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -23,7 +28,35 @@ import de.srsoftware.web4rail.tiles.Signal;
|
|||||||
import de.srsoftware.web4rail.tiles.Tile;
|
import de.srsoftware.web4rail.tiles.Tile;
|
||||||
|
|
||||||
public class Train {
|
public class Train {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
||||||
|
|
||||||
|
private static final HashMap<Long, Train> trains = new HashMap<>();
|
||||||
|
|
||||||
|
public static final String ID = "id";
|
||||||
|
public long id;
|
||||||
|
|
||||||
|
private static final String NAME = "name";
|
||||||
|
private String name = null;
|
||||||
|
|
||||||
|
private static final String BLOCK = "block";
|
||||||
|
private Block block = null;
|
||||||
|
|
||||||
|
private static final String ROUTE = "route";
|
||||||
|
public Route route;
|
||||||
|
|
||||||
|
private static final String DIRECTION = "direction";
|
||||||
|
private Direction direction;
|
||||||
|
|
||||||
|
private static final String PUSH_PULL = "pushPull";
|
||||||
|
private boolean pushPull = false;
|
||||||
|
|
||||||
|
private static final String CARS = "cars";
|
||||||
|
private Vector<Car> cars = new Vector<Car>();
|
||||||
|
|
||||||
|
private static final String LOCOS = "locomotives";
|
||||||
|
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
||||||
|
|
||||||
|
|
||||||
private class Autopilot extends Thread{
|
private class Autopilot extends Thread{
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -47,35 +80,42 @@ 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>();
|
|
||||||
public static final String ID = "id";
|
|
||||||
public static final String MODE_START = "start";
|
public static final String MODE_START = "start";
|
||||||
public static final String MODE_SHOW = "show";
|
public static final String MODE_SHOW = "show";
|
||||||
private static final String MODE_UPDATE = "update";
|
private static final String MODE_UPDATE = "update";
|
||||||
private static final String MODE_AUTO = "auto";
|
private static final String MODE_AUTO = "auto";
|
||||||
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
|
||||||
private Vector<Car> cars = new Vector<Car>();
|
|
||||||
private String name = null;
|
|
||||||
private Block block = null;
|
|
||||||
public Route route;
|
|
||||||
public int speed = 0;
|
public int speed = 0;
|
||||||
private Direction direction;
|
|
||||||
private boolean pushPull = false;
|
|
||||||
public int id;
|
|
||||||
private Autopilot autopilot = null;
|
private Autopilot autopilot = null;
|
||||||
|
|
||||||
public Train(Locomotive loco) {
|
public Train(Locomotive loco) {
|
||||||
id = ++count;
|
id = new Date().getTime();
|
||||||
add(loco);
|
add(loco);
|
||||||
trains.put(id, this);
|
trains.put(id, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JSONObject json() {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put(ID, id);
|
||||||
|
json.put(NAME,name);
|
||||||
|
if (block != null) json.put(BLOCK, block.id());
|
||||||
|
if (route != null) json.put(ROUTE, route.id());
|
||||||
|
if (direction != null) json.put(DIRECTION, direction);
|
||||||
|
json.put(PUSH_PULL, pushPull);
|
||||||
|
Vector<String> locoIds = new Vector<String>();
|
||||||
|
for (Locomotive loco : locos) locoIds.add(loco.id());
|
||||||
|
json.put(LOCOS, locoIds);
|
||||||
|
Vector<String> carIds = new Vector<String>();
|
||||||
|
for (Car car : cars) carIds.add(car.id());
|
||||||
|
json.put(CARS,carIds);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Object action(HashMap<String, String> params) throws IOException {
|
public static Object action(HashMap<String, String> params) throws IOException {
|
||||||
if (!params.containsKey(Train.ID)) return t("No train id passed!");
|
if (!params.containsKey(Train.ID)) return t("No train id passed!");
|
||||||
int id = Integer.parseInt(params.get(Train.ID));
|
long id = Long.parseLong(params.get(Train.ID));
|
||||||
Train train = trains.get(id);
|
Train train = trains.get(id);
|
||||||
if (train == null) return(t("No train with id {}!",id));
|
if (train == null) return(t("No train with id {}!",id));
|
||||||
if (!params.containsKey("mode")) return t("No mode set for train action!");
|
if (!params.containsKey("mode")) return t("No mode set for train action!");
|
||||||
@@ -118,7 +158,7 @@ public class Train {
|
|||||||
direction = dir;
|
direction = dir;
|
||||||
return this;
|
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;
|
||||||
@@ -145,7 +185,7 @@ public class Train {
|
|||||||
|
|
||||||
Form form = new Form();
|
Form form = new Form();
|
||||||
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "train").addTo(form);
|
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "train").addTo(form);
|
||||||
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", id).addTo(form);
|
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", ""+id).addTo(form);
|
||||||
new Tag("input").attr("type", "hidden").attr("name","mode").attr("value", MODE_UPDATE).addTo(form);
|
new Tag("input").attr("type", "hidden").attr("name","mode").attr("value", MODE_UPDATE).addTo(form);
|
||||||
|
|
||||||
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
||||||
@@ -167,6 +207,15 @@ public class Train {
|
|||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void saveAll(String filename) throws IOException {
|
||||||
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
|
for (Entry<Long, Train> entry:trains.entrySet()) {
|
||||||
|
Train train = entry.getValue();
|
||||||
|
file.write(train.json()+"\n");
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
public void setSpeed(int v) {
|
public void setSpeed(int v) {
|
||||||
LOG.debug("Setting speed to {} kmh.",v);
|
LOG.debug("Setting speed to {} kmh.",v);
|
||||||
for (Locomotive loco : locos) loco.setSpeed(v);
|
for (Locomotive loco : locos) loco.setSpeed(v);
|
||||||
|
|||||||
10
src/main/java/de/srsoftware/web4rail/tags/Div.java
Normal file
10
src/main/java/de/srsoftware/web4rail/tags/Div.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package de.srsoftware.web4rail.tags;
|
||||||
|
|
||||||
|
public class Div extends TagWithId {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 749501876155859607L;
|
||||||
|
|
||||||
|
public Div(String id) {
|
||||||
|
super("div", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main/java/de/srsoftware/web4rail/tags/TagWithId.java
Normal file
13
src/main/java/de/srsoftware/web4rail/tags/TagWithId.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package de.srsoftware.web4rail.tags;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Tag;
|
||||||
|
|
||||||
|
public class TagWithId extends Tag {
|
||||||
|
|
||||||
|
public TagWithId(String type, String id) {
|
||||||
|
super(type);
|
||||||
|
id(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 6349230653857414636L;
|
||||||
|
}
|
||||||
@@ -79,22 +79,8 @@ public abstract class Block extends StretchableTile{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void train(Train train) throws IOException {
|
public void train(Train train) throws IOException {
|
||||||
this.train = train;
|
|
||||||
if (train != null) train.block(this);
|
if (train != null) train.block(this);
|
||||||
plan.stream("place "+tag(null));
|
super.train(train);
|
||||||
}
|
|
||||||
|
|
||||||
public Train train() {
|
|
||||||
return train;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unlock() {
|
|
||||||
route = null;
|
|
||||||
classes.remove("locked");
|
|
||||||
if (train != null) {
|
|
||||||
classes.remove("occupied");
|
|
||||||
plan.stream("dropclass tile-"+x+"-"+y+" locked");
|
|
||||||
} else plan.stream("dropclass tile-"+x+"-"+y+" locked occupied");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package de.srsoftware.web4rail.tiles;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Plan.Direction;
|
import de.srsoftware.web4rail.Plan.Direction;
|
||||||
@@ -13,7 +14,13 @@ public abstract class Signal extends Tile{
|
|||||||
|
|
||||||
public Signal() {
|
public Signal() {
|
||||||
super();
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Vector<String> classes() {
|
||||||
|
Vector<String> classes = super.classes();
|
||||||
classes.add("signal");
|
classes.add("signal");
|
||||||
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract boolean isAffectedFrom(Direction dir);
|
public abstract boolean isAffectedFrom(Direction dir);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package de.srsoftware.web4rail.tiles;
|
package de.srsoftware.web4rail.tiles;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -23,24 +25,54 @@ import de.srsoftware.web4rail.Plan;
|
|||||||
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.moving.Train;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Radio;
|
import de.srsoftware.web4rail.tags.Radio;
|
||||||
|
|
||||||
public abstract class Tile {
|
public abstract class Tile {
|
||||||
|
|
||||||
|
private static final String ID = "id";
|
||||||
|
private static final String TYPE = "type";
|
||||||
|
private static final String LOCKED = "locked";
|
||||||
|
|
||||||
|
private static final String POS = "pos";
|
||||||
|
private static final String X = "x";
|
||||||
|
private static final Object Y = "y";
|
||||||
public int x = -1,y = -1;
|
public int x = -1,y = -1;
|
||||||
protected HashSet<String> classes = new HashSet<>();
|
|
||||||
|
private static final String ROUTE = "route";
|
||||||
|
protected Route route;
|
||||||
|
|
||||||
|
private static final String OCCUPIED = "occupied";
|
||||||
|
private Train train;
|
||||||
|
|
||||||
|
private static final String ONEW_WAY = "one_way";
|
||||||
|
protected Direction oneWay = null;
|
||||||
|
|
||||||
protected HashSet<Shadow> shadows = new HashSet<>();
|
protected HashSet<Shadow> shadows = new HashSet<>();
|
||||||
private HashSet<Route> routes = new HashSet<>();
|
private HashSet<Route> routes = new HashSet<>();
|
||||||
protected Plan plan;
|
protected Plan plan;
|
||||||
protected Route route;
|
|
||||||
protected Direction oneWay = null;
|
|
||||||
|
|
||||||
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
|
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
|
||||||
|
|
||||||
public Tile() {
|
protected Vector<String> classes(){
|
||||||
|
Vector<String> classes = new Vector<String>();
|
||||||
classes.add("tile");
|
classes.add("tile");
|
||||||
classes.add(getClass().getSimpleName());
|
classes.add(getClass().getSimpleName());
|
||||||
|
if (route != null) classes.add(LOCKED);
|
||||||
|
if (train != null) classes.add(OCCUPIED);
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject json() {
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put(ID, id());
|
||||||
|
json.put(TYPE, getClass().getSimpleName());
|
||||||
|
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
|
||||||
|
json.put(POS, pos);
|
||||||
|
if (route != null) json.put(ROUTE, route.id());
|
||||||
|
if (oneWay != null) json.put(ONEW_WAY, oneWay);
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Route route) {
|
public void add(Route route) {
|
||||||
@@ -74,23 +106,19 @@ public abstract class Tile {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String id() {
|
||||||
|
return "tile-"+x+"-"+y;
|
||||||
|
}
|
||||||
|
|
||||||
public int len() {
|
public int len() {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lock(Route route) {
|
public void lock(Route route) throws IOException {
|
||||||
this.route = route;
|
this.route = route;
|
||||||
classes.add("locked");
|
plan.place(this);
|
||||||
plan.stream("addclass tile-"+x+"-"+y+" locked");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void occupy(Route route) {
|
|
||||||
this.route = route;
|
|
||||||
classes.add("occupied");
|
|
||||||
plan.stream("addclass tile-"+x+"-"+y+" occupied");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void plan(Plan plan) {
|
public void plan(Plan plan) {
|
||||||
this.plan = plan;
|
this.plan = plan;
|
||||||
}
|
}
|
||||||
@@ -168,9 +196,29 @@ public abstract class Tile {
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Route route() {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
public HashSet<Route> routes() {
|
public HashSet<Route> routes() {
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void saveAll(HashMap<Integer, HashMap<Integer, Tile>> tiles ,String filename) throws IOException {
|
||||||
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
|
for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) {
|
||||||
|
for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
|
||||||
|
Tile tile = row.getValue();
|
||||||
|
if (tile == null || tile instanceof Shadow) continue;
|
||||||
|
file.append(tile.json()+"\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String t(String txt, Object...fills) {
|
||||||
|
return Translation.get(Application.class, txt, fills);
|
||||||
|
}
|
||||||
|
|
||||||
public Tag tag(Map<String,Object> replacements) throws IOException {
|
public Tag tag(Map<String,Object> replacements) throws IOException {
|
||||||
int width = 100*len();
|
int width = 100*len();
|
||||||
@@ -181,7 +229,7 @@ public abstract class Tile {
|
|||||||
String style = "";
|
String style = "";
|
||||||
Tag svg = new Tag("svg")
|
Tag svg = new Tag("svg")
|
||||||
.id((x!=-1 && y!=-1)?("tile-"+x+"-"+y):(getClass().getSimpleName()))
|
.id((x!=-1 && y!=-1)?("tile-"+x+"-"+y):(getClass().getSimpleName()))
|
||||||
.clazz(classes)
|
.clazz(classes())
|
||||||
.size(100,100)
|
.size(100,100)
|
||||||
.attr("name", getClass().getSimpleName())
|
.attr("name", getClass().getSimpleName())
|
||||||
.attr("viewbox", "0 0 "+width+" "+height);
|
.attr("viewbox", "0 0 "+width+" "+height);
|
||||||
@@ -214,6 +262,8 @@ public abstract class Tile {
|
|||||||
break;
|
break;
|
||||||
case WEST:
|
case WEST:
|
||||||
new Tag("polygon").clazz("oneway").attr("points", "0,50 25,35 25,65").addTo(svg);
|
new Tag("polygon").clazz("oneway").attr("points", "0,50 25,35 25,65").addTo(svg);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -226,22 +276,25 @@ public abstract class Tile {
|
|||||||
|
|
||||||
return svg;
|
return svg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected static String t(String txt, Object...fills) {
|
|
||||||
return Translation.get(Application.class, txt, fills);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
|
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Train train() {
|
||||||
|
return train;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void train(Train train) throws IOException {
|
||||||
|
this.train = train;
|
||||||
|
plan.place(this);
|
||||||
|
}
|
||||||
|
|
||||||
public void unlock() {
|
public void unlock() throws IOException {
|
||||||
route = null;
|
route = null;
|
||||||
classes.remove("locked");
|
train = null;
|
||||||
classes.remove("occupied");
|
plan.place(this);
|
||||||
plan.stream("dropclass tile-"+x+"-"+y+" locked occupied");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile update(HashMap<String, String> params) {
|
public Tile update(HashMap<String, String> params) {
|
||||||
@@ -256,8 +309,4 @@ public abstract class Tile {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Route route() {
|
|
||||||
return route;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user