/**
* Created by trian on 14/09/2016.
*/
(function() {
let _obj = new solapps.dataModule({
key : 'diktyo',
label : 'Σιδηροδρομικό δίκτυο',
type : 'data',
orderBy : null, // the column name by which we expect to order rows. Leave null if not applicable
updateEvery : 5*86400, //secs
requires : [], // other modules
db : {
needed : false,
createStatements : {}
},
listFormatter : function(obj, lang) {
let key = lang.toLowerCase() === 'en' ? 'LABEL_EN' : 'LABEL_GR';
return '
'+obj[key]+'
';
},
optionFormatter : function(obj, lang) {
let key = lang.toLowerCase() === 'en' ? 'LABEL_EN' : 'LABEL_GR';
return '';
}
});
_obj.setUpdater(function(force) {
$.ajax({
data: {c: 'dbsync', op: 'getData', item:'diktyo'},
success: function (reply) {
if (!reply) return;
if (!reply.status && reply.message != null) {alert(reply.message);return;}
let out = {};
for (let e in reply.data.nodes) {
let row = reply.data.nodes[e];
out[ row.STAT ] = {stat : row.STAT, country : row.COUNTRY, label : { en : row.LABEL_EN, gr : row.LABEL_EL, el : row.LABEL_EL }, lat : row.LAT, lon : row.LON, vlepei : {}, is_forced_intersection : row.FORCED_KOMVOS*1 === 1 };
}
for (let e in reply.data.links) {
let o = reply.data.links[e];
if (typeof(out[ o.NODE1 ]) === 'undefined' || typeof(out[ o.NODE2 ]) === 'undefined') {
console.log('Skipping link '+o.NODE1+'<->'+o.NODE2+'. Missing station.');
continue;
}
let link = {km : o.DIST_KM, kmp: o.DIST_KM_PHYSICAL === null ? o.DIST_KM : o.DIST_KM_PHYSICAL, line : o.LINE, rqU:o.DIST_KM_PHYSICAL === null};
out[o.NODE1].vlepei[ o.NODE2 ] = link;
out[o.NODE2].vlepei[ o.NODE1 ] = link;
}
let komvoi = [];
for (let stat in out) {
let n = out[stat];
if (n.is_forced_intersection || solapps.util.objectSize(n.vlepei) > 2)
komvoi.push(stat);
}
_obj.DATA = {
STATIONS : out,
KOMVOI : komvoi,
NODES_IN_USE : reply.data.nodes_in_use,
OASA : reply.data.oasa,
};
_obj.dataUpdated();
}
});
});
_obj.updateIsRequired = function () {
if (!_obj.DATA || !_obj.DATA.STATIONS || solapps.util.objectSize(_obj.DATA.STATIONS) === 0)
return true;
let req = false;
for (let st in _obj.DATA.STATIONS) {
for (let vl in _obj.DATA.STATIONS[st].vlepei) {
let edge = _obj.DATA.STATIONS[st].vlepei[vl];
req = req || edge.km == null || edge.kmp === undefined || edge.rqU;
}
}
return req;
};
_obj.stationIsInUse = function(stat) {
if (typeof(_obj.DATA.NODES_IN_USE) === 'undefined') {
_obj.requestUpdate(true);
return false; // this will essentially force the user to refresh the page
}
return _obj.DATA.NODES_IN_USE.includes(stat);
}
_obj.getAlphabeticalListOfUsedStations = function(lang='el') {
lang = lang.toLowerCase();
if (lang !== 'en')
lang = 'el';
let out = [];
for (let e in _obj.DATA.NODES_IN_USE)
out.push(_obj.DATA.STATIONS[ _obj.DATA.NODES_IN_USE[e] ]);
out.sort(function(a, b) {
if (a.lang[lang] > b.lang[lang])
return 1;
else if (a < b)
return -1;
else return 0;
});
return out;
};
_obj.getIntersections = function() {
return _obj.DATA.KOMVOI;
};
_obj.findRoute = function(apo, ews, timol_km=true) {
let route = _obj.algs.shortest_path(apo, ews, timol_km);
return route;
};
_obj.findAllRoutes = function(apo, ews, timol_km=true) {
return _obj.algs.find_all_paths(apo, ews, timol_km);
};
_obj.algs = {
shortest_path : function(apo, ews, timol_km=true, source=_obj.DATA.STATIONS) {
if (_obj.getStation(apo) == null || _obj.getStation(ews) == null) return null;
let paths = _obj.algs.find_all_paths(apo, ews, timol_km, source);
if (!paths) return;
paths.sort(function(a, b) {if (a.dist > b.dist) return 1;else if (a.dist < b.dist) return -1; else return 0;});
return paths[0];
},
find_all_paths : function(apo, ews, timol_km, source=_obj.DATA.STATIONS) {
let path = [apo];
if (apo === ews)
return path;
let raw = _obj.algs.__fap(apo, ews, [], source);
let paths = [];
for (let r in raw)
paths.push(_obj.algs._make_path_object(raw[r], timol_km, source));
return paths;
},
__fap : function(apo, ews, path, source) {
if (typeof(path) === 'undefined') path = [];
let npath = [];
for (let e in path)
npath.push(path[e]);
npath.push(apo);
if (apo === ews)
return [npath];
let paths = [];
for (let e in source[apo].vlepei) {
if (npath.indexOf(e) !== -1) continue;
let newpath = _obj.algs.__fap(e, ews, npath, source);
for (let n in newpath)
paths.push(newpath[n]);
}
return paths;
},
_make_path_object : function(path, timol_km=true, source=_obj.DATA.STATIONS) {
let obj = {path:path, dist:0, grammes:[], komboi:[path[0]]};
let groups = {};
for (let e in obj.path) {
//console.log('\r\nCheck station: ['+obj.path[e]+'], vlepei-length: ['+solapps.util.objectSize(source[obj.path[e]].vlepei)+']\r\n');
if (solapps.util.objectSize(source[obj.path[e]].vlepei) > 2 && !solapps.util.array_contains(obj.komboi, obj.path[e])) {
//console.log('\r\nAdd station (1) : ['+obj.path[e]+'] to nodes list\r\n');
obj.komboi.push(obj.path[e]);
}
if ((obj.path[e] === 'ΠΚΙΑ') && !solapps.util.array_contains(obj.komboi, obj.path[e])) { // pgeorgas: station PKIA 'vlepei' olny 2 stations, but it is a node !!
obj.komboi.push(obj.path[e]);
}
if (e*1+1 === obj.path.length) break;
let vector = source[obj.path[e]].vlepei[obj.path[e*1+1]];
if (typeof(groups[vector.line]) === 'undefined')
groups[vector.line] = 0;
groups[vector.line] += timol_km ? vector.km : vector.kmp;
obj.dist += timol_km ? vector.km : vector.kmp;
}
if (!solapps.util.array_contains(obj.komboi, path[ path.length-1 ])) {
//console.log('\r\nAdd station (2): ['+path[ path.length-1 ]+'] to nodes list\r\n');
obj.komboi.push(path[ path.length-1 ]);
}
for (let e in groups) {
groups[e] = solapps.util.round(groups[e], 2);
obj.grammes.push(e);
}
obj.line_km = groups;
obj.dist = solapps.util.round(obj.dist, 2);
obj.hash = path[0]+'_'+path[path.length-1]+'-'+path.length+'hops-'+obj.dist+'km-'+obj.grammes.join('_');
return obj;
}
};
_obj.shortest_path = _obj.algs.shortest_path;
_obj.getStation = function(sta) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return null;}
if (!sta || typeof(_obj.DATA.STATIONS[sta]) === 'undefined') return null;
return _obj.DATA.STATIONS[sta];
};
_obj.exists = function(station) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return false;}
return station && typeof(_obj.DATA.STATIONS[station]) !== 'undefined';
};
_obj.onoma = function(station, lang) {
if (!station || !_obj.DATA || !_obj.DATA.STATIONS || typeof(_obj.DATA.STATIONS[station]) === 'undefined') {
_obj.requestUpdate(true);
return `{${station}}`
};
if (typeof(lang) === 'undefined' || ['en', 'gr'].indexOf(lang) === -1) lang = 'gr';
// console.log(_obj.DATA.STATIONS[station]);
if (typeof(_obj.DATA.STATIONS[station].label) !== 'undefined')
return _obj.DATA.STATIONS[station].label[lang];
else
return _obj.DATA.STATIONS[station].lang[lang];
};
_obj.country = function(station) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return 'GRC';}
if (!station || typeof(_obj.DATA.STATIONS[station]) === 'undefined') return null;
return _obj.DATA.STATIONS[station].country;
};
_obj.lat = function(station) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return null;}
if (!station || typeof(_obj.DATA.STATIONS[station]) === 'undefined') return null;
return _obj.DATA.STATIONS[station].lat;
};
_obj.lon = function(station) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return null;}
if (!station || typeof(_obj.DATA.STATIONS[station]) === 'undefined') return null;
return _obj.DATA.STATIONS[station].lon;
};
_obj.coords = function(station) {
if (_obj.DATA == null) {_obj.requestUpdate(true); return {lat:null, lon:null};}
if (!station || typeof(_obj.DATA.STATIONS[station]) === 'undefined') return {lat:null, lon:null};
return {
lat: _obj.DATA.STATIONS[station].lat,
lon: _obj.DATA.STATIONS[station].lon,
};
};
_obj.OASA = {
loaded : function () {
return _obj.DATA != null && typeof(_obj.DATA.OASA) !== 'undefined';
},
includes : function (station) {
if (_obj.DATA.OASA === undefined) {
_obj.requestUpdate(true);
return true; // it's safer and it will only cause a minor confusion at the frontend
}
return _obj.DATA.OASA.includes(station);
},
stations : function () {
if (typeof(_obj.DATA.OASA) === 'undefined')
_obj.requestUpdate(true);
return _obj.DATA.OASA || [];
}
};
return _obj;
})();