sembokulove
@sembokulove (Missing place)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

列車走行位置プログラムにおいて、特別ダイヤにおいても、特別ダイヤ期間中が平日の場合は平日、土休日であれば土休日のダイヤを返したいです。

解決したいこと

列車走行位置プログラムを作っています。
全日ダイヤと特別ダイヤの実装方法については成功しましたが、
特定日の運転の際に、平日ダイヤや土休日ダイヤで設定したダイヤが消えてしまいます。
具体的には特定日が平日のダイヤの場合には平日ダイヤを、
土休日ダイヤの場合には土休日ダイヤを返したいのですが、どのようにしたらいいのでしょうか。
解決方法をご提示ください。

if (value === "10月12日・11月3/24日" && diaDay === DiaType.SPECIAL) return true;で、10月12日・11月3・24日に運転する特別ダイヤを設定すると、
if (value === "全日") return true;で、全日(毎日)運転としたダイヤは動く一方で、
if (value === "平日" && diaDay === DiaType.WEEKDAY) return true;→平日運転、
if (value === "土・休日" && diaDay === DiaType.HOLIDAY) return true;→土休日運転は消えてしまいます。

在線 質問 20240603 Mon.png

該当するソースコード

function getStationTimetable(currentStaNum)
{
	stationTimetableArrayKey = new Array();
	stationTimetableArrayKey[currentStaNum] = new Array();
	stationTimetableArrayKey[currentStaNum]["下り"] = new Array();
	stationTimetableArrayKey[currentStaNum]["上り"] = new Array();
	var i=0;
	var trainCount = new Array;
	trainCount["下り"] = 0;
	trainCount["上り"] = 0;
	for(i=0 ; i<trainBasicData.length ; i++)
	{		
		arrayKey = timeTableArrayKey[i];
		if(!judgeTrainRunningDay(timeTable[arrayKey]["運転日"]))
			continue;
		if(timeTable[arrayKey]["駅時刻"][currentStaNum][""] >= 0  )
		{
			var direction = timeTable[arrayKey]["方向"];
			stationTimetableArrayKey[currentStaNum][direction][trainCount[direction]] = arrayKey;
			trainCount[direction]++;
			//alert(arrayKey + " は " + stationList[currentStaNum] + "駅で " + timeTable[arrayKey]["駅時刻"][stationList[currentStaNum]]["着"] + "〜" + timeTable[arrayKey]["駅時刻"][stationList[currentStaNum]]["発"]);
			//stationTimetableArrayKey[19][trainCount]
			//trainCount++;
		}
	}
	var i, j, k;
	for(i = 0 ; i<directionNameAry.length ; i++)
	{
		var currentDirection = directionNameAry[i];
		var buff = "";
		for(j=stationTimetableArrayKey[currentStaNum][currentDirection].length-1 ; j>=0 ; j--)
		{
			for(k=0 ; k<j ; k++)
			{
				var arrayKeyA = stationTimetableArrayKey[currentStaNum][currentDirection][k];
				var arrayKeyB = stationTimetableArrayKey[currentStaNum][currentDirection][k+1];
				
				//alert(arrayKeyA);
				//alert(arrayKeyB);
				
				if(timeTable[arrayKeyA]["駅時刻"][currentStaNum][""] > timeTable[arrayKeyB]["駅時刻"][currentStaNum][""])
				{
					stationTimetableArrayKey[currentStaNum][currentDirection][k] = arrayKeyB;
					stationTimetableArrayKey[currentStaNum][currentDirection][k+1] = arrayKeyA;
				}
			}
		}
	}
	for(i = 0 ; i<directionNameAry.length ; i++)
	{
		var currentDirection = directionNameAry[i];
		var output = "";
		output += "";
		output += "";
		//output += "<table border='0'>";
		output += "<div>";
		output += "";
		output += "";
		output += "";
		nextTrainHeight = "";
		divTop = 0;
		for(j=0 ; j < stationTimetableArrayKey[currentStaNum][currentDirection].length ; j++)
		{
			var currentArrayKey = stationTimetableArrayKey[currentStaNum][currentDirection][j];
			//alert(currentArrayKey);
			//document.getElementById("staTimetableTimeDiv").innerHTML += i+"本目 "+currentArrayKey+"<br />";
			//output += "<tr style='white-space:nowrap; height:25px; '>";
			var bgColor = getTypeColor(timeTable[currentArrayKey]["種別"], 0);
			var color = getTypeColor(timeTable[currentArrayKey]["種別"], 1);
			output += "<div style='position:absolute; top:"+divTop+"px; left:0px; width:60px; text-align:center;'><span style='color:"+color+"; background-color:"+bgColor+"'>" + timeTable[currentArrayKey]["列車番号"] + "</span></div>";
			output += "<div style='position:absolute; top:"+divTop+"px; left:40px; width:120px; text-align:center;'><span style='color:"+color+"; background-color:"+bgColor+"'>" + timeTable[currentArrayKey]["種別"] + "</span></div>";
			if(timeTable[currentArrayKey]["駅時刻"][currentStaNum][""] != timeTable[currentArrayKey]["駅時刻"][currentStaNum][""])
			{
				var timeOutput = "";
				var secondSum = timeTable[currentArrayKey]["駅時刻"][currentStaNum][""];
				timeOutput += digitAdjuster(secondToHour(secondSum), "&nbsp;");
				timeOutput += ":";
				timeOutput += digitAdjuster(secondToMinute(secondSum), "0");
				//timeOutput += "<span style='font-size:70%;'> ";
				//timeOutput += digitAdjuster(secondToSecond(secondSum), "0");
				//timeOutput += "</span>";
				timeOutput += "";
				output += "<div style='position:absolute; top:"+divTop+"px; left:150px; width:70px; text-align:right;'>" + timeOutput + " <span style='font-size:80%; '>着</span></div>";
				var timeOutput = "";
				var secondSum = timeTable[currentArrayKey]["駅時刻"][currentStaNum][""];
				timeOutput += digitAdjuster(secondToHour(secondSum), "&nbsp;");
				timeOutput += ":";
				timeOutput += digitAdjuster(secondToMinute(secondSum), "0");
				//timeOutput += "<span style='font-size:70%;'> ";
				//timeOutput += digitAdjuster(secondToSecond(secondSum), "0");
				//timeOutput += "</span>";
				timeOutput += "";
				output += "<div style='position:absolute; top:"+divTop+"px; left:230px; width:70px; text-align:right;'>" + timeOutput + " <span style='font-size:80%; '>発 </span></div>";
			}
			else
			{
				var timeOutput = "";
				var secondSum = timeTable[currentArrayKey]["駅時刻"][currentStaNum][""];
				timeOutput += digitAdjuster(secondToHour(secondSum), "&nbsp;");
				timeOutput += ":";
				timeOutput += digitAdjuster(secondToMinute(secondSum), "0");
				//timeOutput += "<span style='font-size:70%;'> ";
				//timeOutput += digitAdjuster(secondToSecond(secondSum), "0");
				//timeOutput += "</span>";
				timeOutput += "";
				//output += "<td colspan='2' style='text-align:center;'>" + timeOutput + " 通過</td>";
				output += "<div style='position:absolute; top:"+divTop+"px; left:230px; width:70px; text-align:center; color:#aaa; '>" + timeOutput + " <span style='font-size:80%; '>通過</span></div>";
			}
			var secondSum = timeTable[currentArrayKey]["駅時刻"][currentStaNum][""];
			if(setSecondSum < secondSum && nextTrainHeight == "")
				nextTrainHeight = divTop - 20;
			if(timeTable[currentArrayKey]["行先"] != stationList[currentStaNum])
			{
				output += "<div style='position:absolute; top:"+divTop+"px; left:310px; width:150px; '>" + timeTable[currentArrayKey]["行先"] + "<span style='font-size:80%;'> 行</span></div>";
			}
			else
			{
				output += "<div style='position:absolute; top:"+divTop+"px; left:310px; width:80px; '>当駅止まり</div>";
			}
			
			if(timeTable[currentArrayKey]["駅時刻"][currentStaNum]["のりば"] >= 0)
				output += "<div style='position:absolute; top:"+divTop+"px; left:390px; width:80px; text-align:right;'>" + timeTable[currentArrayKey]["駅時刻"][currentStaNum]["のりば"] + "<span style='font-size:80%;'> 番線</span></div>";
			else
				output += "<td></td>";
			output += "</tr>\n";
			
			divTop+= 20;
		}
		//output += "</table>";
		output += "</div>";
		var idName = "staTimetableTime"+i+"Div";
		if(document.getElementById(idName) != null)
			document.getElementById(idName).innerHTML = output;
		if(document.getElementById(idName) != null)
			document.getElementById(idName).scrollTop = nextTrainHeight;
		var idName = "staTimetableTimeTitle"+i+"Div";
		if(document.getElementById(idName) != null)
			document.getElementById(idName).innerHTML = stationList[currentStaNum]+""+currentDirection+" 時刻表";
		output += "";
		output += "";
		output += "";
	}
}
const DayOfWeek = {
    SUNDAY: 0,
    MONDAY: 1,
    TUESDAY: 2,
    WEDNESDAY: 3,
    THURSDAY: 4,
    FRIDAY: 5,
    SATURDAY: 6
};
const DiaType = {
    WEEKDAY: 0,
    HOLIDAY: 1,
    SPECIAL: 2,
    SPECIAL2: 3,
};
let displayDiaType = DiaType.WEEKDAY;
let trainDiaType = DiaType.WEEKDAY;
function isSpecialDay(month, date) {
    if ((month === 10 && date === 12) || (month === 11 && date === 3) || (month === 11 && date === 24)) {
        return DiaType.SPECIAL;
    } else if ((month === 10 && date === 9) || (month === 10 && date === 31) || (month === 11 && date === 21)) {
        return DiaType.SPECIAL2;
    }
    return null;
}
function getDiaTypeByDayOfWeek(dayOfWeek) {
    if (dayOfWeek === DayOfWeek.SUNDAY || dayOfWeek === DayOfWeek.SATURDAY) {
        return DiaType.HOLIDAY;
    }
    return DiaType.WEEKDAY;
}
function judgeDiaDay() {
    let setYear = nowYear; // 年
    let setMonth = nowMonth; // 月
    let setDate = nowDate; // 日
    let setDay = nowDay; // 曜日

    let specialDiaType = isSpecialDay(setMonth, setDate);
    if (specialDiaType !== null) {
        displayDiaType = specialDiaType;
        trainDiaType = specialDiaType;
    } else {
        let dayDiaType = getDiaTypeByDayOfWeek(setDay);
        displayDiaType = dayDiaType;
        trainDiaType = dayDiaType;
    }
    updateDisplayDiaDay();
    return trainDiaType === DiaType.WEEKDAY;
}
function updateDisplayDiaDay() {
    const todayDiaDaySpan = document.getElementById("todayDiaDaySpan");
    if (todayDiaDaySpan) {
        todayDiaDaySpan.innerHTML = getDiaTypeString(displayDiaType);
        todayDiaDaySpan.style.backgroundColor = getDiaTypeColor(displayDiaType);
    }
}
function getDiaTypeString(diaType) {
    switch (diaType) {
        case DiaType.WEEKDAY:
            return " 平日ダイヤ ";
        case DiaType.HOLIDAY:
            return "土・休日ダイヤ";
        case DiaType.SPECIAL:
            return " 10月9/12/31日・11月3/21/24日ダイヤ ";
        case DiaType.SPECIAL2:
            return " 10月12日・11月3/24日ダイヤ ";
        default:
            return "";
    }
}
function judgeTrainRunningDay(value) {
    if (value === "全日") return true;
    if ((value === "平日" || value === "毎日運転") && (trainDiaType === DiaType.WEEKDAY || trainDiaType === DiaType.SPECIAL || trainDiaType === DiaType.SPECIAL2)) return true;
    if ((value === "土休日" || value === "毎日運転") && (trainDiaType === DiaType.HOLIDAY || trainDiaType === DiaType.SPECIAL || trainDiaType === DiaType.SPECIAL2)) return true;
    if (value === "土曜・休日運休" && trainDiaType === DiaType.WEEKDAY) return true;
    if (value === "土曜・休日運転" && (trainDiaType === DiaType.HOLIDAY || trainDiaType === DiaType.SPECIAL || trainDiaType === DiaType.SPECIAL2)) return true;
    if (value === "10月9/12/31日・11月3/21/24日" && trainDiaType === DiaType.SPECIAL) return true;
    if (value === "10月12日・11月3/24日" && trainDiaType === DiaType.SPECIAL2) return true;
    return false;
}
function changeDisplayDiaType() {
    switch (displayDiaType) {
        case DiaType.WEEKDAY:
            displayDiaType = DiaType.HOLIDAY;
            break;
        case DiaType.HOLIDAY:
            displayDiaType = DiaType.SPECIAL;
            break;
        case DiaType.SPECIAL:
            displayDiaType = DiaType.SPECIAL2;
            break;
        case DiaType.SPECIAL2:
            displayDiaType = DiaType.WEEKDAY;
            break;
        default:
            break;
    }
    trainDiaType = displayDiaType;
    updateDisplayDiaDay();
    getTimeTable();
    setNewTime(setSecondSum);
    drawTrain();
    resetTimetableSpace();
}
function getTrainTimetableInfo(key) {
    alert(key);
}

自分で試したこと

チャットgptで調べましたが、利用制限オーバーとなりました。
現時点で分かっているのは、

// 各列車が本日運転する列車なのかそうでないのかを判定する
// 引数はその列車の運転日情報 例:平日 全日 土・休日
function judgeTrainRunningDay(value) {
    if (value === "全日") return true;

    if (value === "平日" && diaDay === DiaType.WEEKDAY) return true;
    if (value === "土曜・休日運休" && diaDay === DiaType.WEEKDAY) return true;
    if (value === "毎日運転" && diaDay === DiaType.WEEKDAY) return true;

    if (value === "休日" && diaDay === DiaType.HOLIDAY) return true;
    if (value === "土休日" && diaDay === DiaType.HOLIDAY) return true;
    if (value === "土・休日" && diaDay === DiaType.HOLIDAY) return true;
    if (value === "土曜・休日運転" && diaDay === DiaType.HOLIDAY) return true;
    if (value === "毎日運転" && diaDay === DiaType.HOLIDAY) return true;

    if (value === "10月9/12/31日・11月3/21/24日" && ((diaDay === DiaType.SPECIAL)||(diaDay === DiaType.SPECIAL2))) return true;
    if (value === "10月12日・11月3/24日" && diaDay === DiaType.SPECIAL) return true;

    return false;
}

により、全日運転はすべての日に表示。逆に平日であれば、平日と入力されたものをここで返して、表示する。土休日も然りということと、

// 平日ダイヤと土・休日ダイヤと特殊ダイヤの入れ替え
function changeDiaType() {
    switch (diaDay) {
        case DiaType.WEEKDAY:
            diaDay = DiaType.HOLIDAY;
            break;
        case DiaType.HOLIDAY:
            diaDay = DiaType.SPECIAL;
            break;
        case DiaType.SPECIAL:
            diaDay = DiaType.SPECIAL2;
            break;
        case DiaType.SPECIAL2:
            diaDay = DiaType.WEEKDAY;
            break;
        default:
            break;
    }

で、平日の場合と土休日の場合とその他特別ダイヤなどの日をそれぞれ表示のきりかえができることです。
どうかご助言をご提示お願いします。
尚その際、javascript文は省略せずにお願い申し上げます。
また、参考にしているのは、こちらのHPになります。
http://r113.web.fc2.com/p/viewer/jrw-hokuriku-20150314/index.html

0

2Answer

変数diaDayが2つの意味を持っているのが原因のような気がしています。
2つの意味というのは「画面表示のダイヤ」と「列車のダイヤ」です。
表示が特定日の場合は「特定日ダイヤと平日 or 休日ダイヤの2つを表示する」ということなので、当初1:1で対応していたものが特別日ダイヤの登場で対応しなくなった、というのが背景にありそうですね。
こうした違いを混同して扱っていると、プログラムもごちゃごちゃして変更が難しくなります。

コードが断片的で、特にjudgeTrainRunningDayをどのように使っているのかわからないので具体的なコードを示すのは難しいです。
ただ「本日運転する列車なのかそうでないのか」というのですから、日付で判定すればいいのではないかと思います。

2Like

Comments

  1. This comment has been deleted for violation of our Terms of Service.
  2. 何か誤解があるようですが、このサービスにおいて質問への回答は任意ですし、報酬もありません。「◯日以内に回答する」のような義務もありません。
    自分の時間を使って支援しているわけですから、回答するしないは個人の判断です。

    また、期待通りにいかないからといって、マナーやガイドラインを逸脱するような行為が許容されるわけではありません。
    あなた自身の振る舞いはあなたの責任ですので、そのような警告を受ける理由がありません。
    どのサービスを利用するかは個人の自由です。

  3. @sembokulove

    Questioner

    僕は諦めるのが嫌な人間です。
    諦めるのは最低な人間がすることです。以上。

具体的には特定日が平日のダイヤの場合には平日ダイヤを、
土休日ダイヤの場合には土休日ダイヤを返したい

平日/土休日 の情報と 特定日 を別々に管理すれば良いでしょう。

現在は diaDay としてこれらを混ぜて管理しているので「特定日」の場合、曜日の概念が消えてしまっています。

0Like

Comments

  1. @sembokulove

    Questioner

    もしかして、回答例を書くのが難しい感じですか。

  2. 横から失礼します。
    「回答例を書くのが難しい」というより、「このデータの持ち方ではおそらく希望している出力を実現できない」だと思います。
    diaDayに入る値はWEEKDAY,HOLIDAY,SPECIAL,SPECIAL2のうちの どれか一つ なので、たとえばSPECIALの場合はWEEKDAYHOLIDAYのダイヤは対象外となります。

    特定日が平日のダイヤの場合には(特別ダイヤに加えて)平日ダイヤを

    といったことを実現するのなら
    定期ダイヤ(diaDay): 平日,土日
    特別ダイヤ(diaSpecial): なし,特別1,特別2,...
    のように二種類の変数を用意する必要があり、コードの書き方で対処できるものではなく設計から見直さないといけないでしょう。

Your answer might help someone💌