ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

commit 1c9fde6bfd33a1d20e46689e44c5685655c0c611
parent 1644fda532cd6fddce69acc33fe525ea3a0fe5af
Author: Milan Pässler <milan@petabyte.dev>
Date: Fri, 11 Sep 2020 15:14:18 +0200

add train type information

Thanks to derf for providing the train type information for Germany
(https://lib.finalrewind.org/dbdb/)
3 files changed, 37 insertions(+), 0 deletions(-)
M
client/js/canvas.js
|
16
++++++++++++++++
M
src/backend/hafas/parse/line.nim
|
20
++++++++++++++++++++
M
src/types.nim
|
1
+
diff --git a/client/js/canvas.js b/client/js/canvas.js
@@ -11,6 +11,13 @@ const formatTime = (date) => {
 };
 
 const textFor = (leg) => leg.line && leg.line.name || "";
+const typeTextFor = (leg) => {
+	let res = "(" + leg.line.trainType + ")";
+	while (res.length < 12) {
+		res = " " + res + " ";
+	}
+	return res;
+};
 const colorFor = (leg, type) => {
 	let product = leg.line && leg.line.product || "walk";
 	return colors[type][product] || colors[type].default;

@@ -107,6 +114,7 @@ const updateTextCache = () => {
 	for (let journey of canvasState.journeys) {
 		for (let leg of journey.legs) {
 			addTextToCache(textFor(leg), colorFor(leg, "text"));
+			if (leg.line.trainType) addTextToCache(typeTextFor(leg), "#555");
 
 			let times = [];
 			if (journey.legs.indexOf(leg) == 0) times.push(leg.departure);

@@ -215,6 +223,14 @@ const renderJourneys = () => {
 				ctx.drawImage(preRenderedText, dpr * (x + 5), Math.floor(dpr * (y + duration / 2) - preRenderedText.height / 2.3));
 				ctx.scale(dpr, dpr);
 			}
+			if (leg.line.trainType) {
+				let preRenderedTypeText = textCache[typeTextFor(leg)];
+				if ((preRenderedTypeText.height / dpr + preRenderedText.height / dpr) < duration - 5) {
+					ctx.scale(1 / dpr, 1 / dpr);
+					ctx.drawImage(preRenderedTypeText, dpr * (x + 5), Math.floor(dpr * (y + duration / 2 + preRenderedText.height / 2 + 10) - preRenderedTypeText.height / 2.3));
+					ctx.scale(dpr, dpr);
+				}
+			}
 
 			if (leg.cancelled) {
 				ctx.strokeStyle = '#cc4444ff';
diff --git a/src/backend/hafas/parse/line.nim b/src/backend/hafas/parse/line.nim
@@ -3,6 +3,22 @@ import ../util
 import ./products
 import json
 import options
+import tables
+import httpClient
+import asyncdispatch
+
+
+var trainTypes = initTable[string, string]()
+
+proc fetchTrainTypes() {.async.} =
+  var client = newAsyncHttpClient()
+  let resp = await client.getContent("https://lib.finalrewind.org/dbdb/ice_type.json")
+  let data = parseJson(resp)
+  for key, info in pairs(data):
+    trainTypes[key] = info{"type"}.getStr
+
+asyncCheck fetchTrainTypes()
+
 
 proc parseLine*(common: CommonData, i: int): Option[Line] =
   let l = common.lines[i]

@@ -26,6 +42,10 @@ proc parseLine*(common: CommonData, i: int): Option[Line] =
 
   # DB
 
+  if res.productName == "IC" or res.productName == "ICE" or res.productName == "EE" or res.productName == "ECE":
+    if trainTypes.contains(res.tripNum):
+      res.trainType = some(trainTypes[res.tripNum])
+
   if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
     res.name = line.nameS.get
 
diff --git a/src/types.nim b/src/types.nim
@@ -88,6 +88,7 @@ type
     fullProductName*:   string
     operator*:          Option[Operator]
     additionalName*:    Option[string]
+    trainType*:         Option[string]
 
   Operator* = object
     id*:   string