Skip to content

Commit

Permalink
Merge pull request NCUAppTeam#70 from pudding0803/main
Browse files Browse the repository at this point in the history
change the data structure returning from route() of Bus.js
  • Loading branch information
pudding0803 authored May 11, 2023
2 parents 9662e18 + 7a3cca6 commit 31e7bc4
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions controller/Bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ async function afun9025(param) {

async function route(param) {
if (param.buses.toString() === ['9025A'].toString()) return await afun9025(param);
if (![['132'], ['133'], ['132', '133'], ['133', '132'], ['172'], ['173'], ['172', '173'], ['173', '172'], ['5035']].some(valid => valid.join() === param.buses.join())) {
param.buses.sort();
if (![['132'], ['133'], ['132', '133'], ['172'], ['173'], ['172', '173'], ['5035']].some(valid => valid.join() === param.buses.join())) {
throw new Error('Invalid Buses: ' + param.buses.join(' '));
}

const APIBASE = 'https://tdx.transportdata.tw/api/basic/v2/Bus/EstimatedTimeOfArrival/City/Taoyuan';
const data = {
$select: 'StopStatus,EstimateTime,StopName,NextBusTime',
$select: 'StopUID,StopStatus,EstimateTime,StopName,NextBusTime',
$filter: `Direction eq ${param.dir}`,
$orderby: 'StopSequence',
$format: 'JSON',
Expand All @@ -179,8 +180,6 @@ async function route(param) {
return url.format(finalUrl);
};

console.log(param)

let responses;
while (true) {
try {
Expand All @@ -194,33 +193,41 @@ async function route(param) {
await new Promise(resolve => setTimeout(resolve, 500));
}
}

const output = responses[0].data.map((data) => ({
stop: data.StopName.Zh_tw,
bus: param.buses[0],
data: data,
}));

responses.forEach((response, busIndex) => {
response.data.forEach((data, dataIndex) => {
if (data.NextBusTime) {
const currentStop = {
stop: data.StopName.Zh_tw,
bus: param.buses[busIndex],
data: data,
};
if (data.NextBusTime < output[dataIndex].data.NextBusTime) {
output[dataIndex] = currentStop;
}

const output = [];
if (param.buses.length == 1) { // 如果只有選擇一個公車
responses[0].data.forEach(stop => {
output.push({
stop: stop.StopName.Zh_tw,
bus: param.buses[0],
...getBusTime(stop),
});
});
} else { // 如果有兩個公車
// 各自的站牌,最後各自加入空字串以計算最後一次的 extra
const names = responses.map(response => response.data.map(stop => stop.StopName.Zh_tw)).map(stops => [...stops, '']);
const commonNames = names[0].filter(name => names[1].includes(name)); // 共通的站牌
const prev = [-1, -1]; // 紀錄上一個共通站排是各自的第幾站,用來計算 skip,因為第一站就是 0,所以初始值設 -1
commonNames.forEach(name => { // 遍歷所有共通站牌
const data = responses.map(response => response.data.find(stop => stop.StopName.Zh_tw === name));
if (name !== '') { // 如果代表一般的站牌,而非用來計算最後一次 extra 的空字串
// 因為剛好 132 和 172 第一班都比較早,所以判斷如果 133 或 173 有車才比較下一班抵達的時間
const earlier = Number(data[1].NextBusTime !== undefined && Date.parse(data[1].NextBusTime) < Date.parse(data[0].NextBusTime));
output.push({
stop: data[earlier].StopName.Zh_tw,
bus: param.buses[earlier],
...getBusTime(data[earlier]),
});
}
// 找到此共通站排是各自的第幾站
const curr = [names[0].findIndex(element => element === name), names[1].findIndex(element => element === name)];
// 計算距離此共通站牌,兩班公車各自獨立的站牌數
const extra = param.buses.reduce((acc, bus, index) => ({ ...acc, [bus]: curr[index] - prev[index] - 1 }), {});
if (Object.values(extra).some(value => value !== 0)) output.push({ 'extra': extra }); // 如果有公車有非共通站牌
prev.splice(0, curr.length, ...curr); // prev = curr 的寫法
});
});

return output.map(element => ({
stop: element.stop,
bus: element.bus,
...getBusTime(element.data),
}));
}
return output;
}

export default route;

0 comments on commit 31e7bc4

Please sign in to comment.