diff --git a/lib/logic/ImageViewerState.js b/lib/logic/ImageViewerState.js index e1037ba..837c529 100644 --- a/lib/logic/ImageViewerState.js +++ b/lib/logic/ImageViewerState.js @@ -14,14 +14,8 @@ class ImageViewerState extends Schema { this.task = null; } - setTask(task, onFinish) { + setTask(task) { this.task = task; - currentCallback = onFinish; - setTimeout(this.onTaskFinished, task.duration); - } - - onTaskFinished() { - currentCallback(); } } diff --git a/lib/rooms/ImageViewerRoom.js b/lib/rooms/ImageViewerRoom.js index 8e2e01b..67bf409 100644 --- a/lib/rooms/ImageViewerRoom.js +++ b/lib/rooms/ImageViewerRoom.js @@ -2,7 +2,8 @@ const MAX_GAZE_POINT_AGE = 4000, // Set max update intervall to ~ 30FPS - GAZE_POINT_UPDATE_DELAY = 1000 / 32; + GAZE_POINT_UPDATE_DELAY = 1000 / 32, + DEFAULT_TASK_DURATION = 30000; const colyseus = require("colyseus"), Logger = require("../utils/Logger.js"), @@ -16,9 +17,11 @@ var tasks = Task.getTasks(), availableTasks, currentTask, currentTaskIndex, + currentTaskStartTime, gazePoints, gazeSubscribers, - lastGazePointUpdate = 0; + lastGazePointUpdate = 0, + context; class ImageViewerRoom extends colyseus.Room { @@ -30,6 +33,7 @@ class ImageViewerRoom extends colyseus.Room { this.setState(new ImageViewerState()); this.setSimulationInterval(this.update.bind(this), ServerConfiguration.getUpdateInterval()); this.setTask(); + context = this; } createTasks() { @@ -50,7 +54,8 @@ class ImageViewerRoom extends colyseus.Room { if (message.type === "gaze") { if (client.color === undefined) { client.color = Colors.getNextColor(); - Logger.log(`Assign client color: ${client.color}`, "ImageViewer Room"); + Logger.log(`Assign client color: ${client.color}`, + "ImageViewer Room"); } message.data.color = client.color; let point = GazePoint.fromClientData(message.data); @@ -104,6 +109,7 @@ class ImageViewerRoom extends colyseus.Room { } } lastGazePointUpdate = now; + this.setTask(); } updateGazePoints(now) { @@ -118,16 +124,24 @@ class ImageViewerRoom extends colyseus.Room { } setTask() { - Logger.log("Sending current task to state", "ImageViewrRoom"); - // Cycle through available tasks - currentTaskIndex += 1; - if (currentTaskIndex >= availableTasks.length) { - currentTaskIndex = 0; + if (currentTask === undefined) { + currentTask = availableTasks[currentTaskIndex]; + Logger.log("Sending current task to state", "ImageViewrRoom"); + this.state.setTask(currentTask, this.onTaskFinished.bind(this)); + currentTaskStartTime = Date.now(); + return; + } + if ((Date.now() - currentTaskStartTime) >= DEFAULT_TASK_DURATION) { + Logger.log("Switching task", "ImageViewrRoom"); + currentTaskIndex += 1; + if (currentTaskIndex >= availableTasks.length) { + currentTaskIndex = 0; + } + currentTask = availableTasks[currentTaskIndex]; + Logger.log("Sending current task to state", "ImageViewrRoom"); + this.state.setTask(currentTask, this.onTaskFinished.bind(this)); + currentTaskStartTime = Date.now(); } - currentTask = availableTasks[currentTaskIndex]; - // Send task to state to auto update clients - // Set listener for task change event (is based on task's duration property) - this.state.setTask(currentTask, this.onTaskFinished.bind(this)); } }