feat: handle line.txt and train arrival/departure events
This commit is contained in:
+60
-1
@@ -838,6 +838,10 @@ function resolveManagedPath(inputPath) {
|
||||
return { logicalPath: 'list.txt', fullPath: listFilePath };
|
||||
}
|
||||
|
||||
if (normalized === 'Audio/general/line.txt' || normalized === 'uploads/audio/general/line.txt') {
|
||||
return { logicalPath: 'Audio/general/line.txt', fullPath: path.join(audioGeneralDir, 'line.txt') };
|
||||
}
|
||||
|
||||
const prefixes = [
|
||||
{ prefix: 'Audio/music/', baseDir: audioMusicDir, logicalPrefix: 'Audio/music/' },
|
||||
{ prefix: 'Audio/voice/', baseDir: audioVoiceDir, logicalPrefix: 'Audio/voice/' },
|
||||
@@ -923,7 +927,7 @@ function getManagedFiles() {
|
||||
files.push({
|
||||
name: file,
|
||||
path: `Audio/general/${file}`,
|
||||
type: 'beep',
|
||||
type: file === 'line.txt' ? 'config' : 'beep',
|
||||
size: stats.size,
|
||||
mtime: stats.mtimeMs
|
||||
});
|
||||
@@ -1587,7 +1591,11 @@ function handleMessage(ws, data) {
|
||||
handleStationRegister(ws, data);
|
||||
break;
|
||||
case 'train_arrival':
|
||||
handleTrainArrival(ws, data);
|
||||
break;
|
||||
case 'train_departure':
|
||||
handleTrainDeparture(ws, data);
|
||||
break;
|
||||
case 'play_sequence':
|
||||
break;
|
||||
case 'request_station_time':
|
||||
@@ -1752,6 +1760,57 @@ function findStationByFlexibleName(targetName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function handleTrainArrival(ws, data) {
|
||||
const trainId = data.trainId;
|
||||
const stationName = getCorrectStationName(data.stationName);
|
||||
|
||||
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
|
||||
const trainData = {
|
||||
trainId,
|
||||
stationName,
|
||||
status: 'arrived',
|
||||
lastUpdated: Date.now()
|
||||
};
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
activeTrains[existingIndex] = trainData;
|
||||
} else {
|
||||
activeTrains.push(trainData);
|
||||
}
|
||||
|
||||
log('INFO', `Train ${trainId} arrived at ${stationName}`);
|
||||
saveData();
|
||||
broadcastUpdate();
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: 'train_arrival_ack',
|
||||
success: true,
|
||||
message: 'Train arrival processed successfully',
|
||||
trainId
|
||||
}));
|
||||
}
|
||||
|
||||
function handleTrainDeparture(ws, data) {
|
||||
const trainId = data.trainId;
|
||||
const stationName = getCorrectStationName(data.stationName);
|
||||
|
||||
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
|
||||
if (existingIndex >= 0) {
|
||||
activeTrains.splice(existingIndex, 1);
|
||||
}
|
||||
|
||||
log('INFO', `Train ${trainId} departed from ${stationName}`);
|
||||
saveData();
|
||||
broadcastUpdate();
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: 'train_departure_ack',
|
||||
success: true,
|
||||
message: 'Train departure processed successfully',
|
||||
trainId
|
||||
}));
|
||||
}
|
||||
|
||||
function handleStationTimeRequest(ws, data) {
|
||||
// Simple logic: default to 60s or lookup schedule
|
||||
const stationTime = 60;
|
||||
|
||||
Reference in New Issue
Block a user