More logical syncronisation

This commit is contained in:
2021-12-31 00:54:48 +03:00
parent 0187518712
commit 26ca76ff0f
2 changed files with 10 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ router.post('/room', async (ctx) => {
createdAt: new Date(), createdAt: new Date(),
movie: torrent.name, movie: torrent.name,
position: 0, position: 0,
syncedAt: new Date(),
}; };
const doc = new RoomModel(room); const doc = new RoomModel(room);
@@ -58,15 +59,20 @@ router.get('/room', async (ctx) => {
}); });
router.post('/position', async (ctx) => { router.post('/position', async (ctx) => {
await RoomModel.updateOne({ id: ctx.request.body.id }, { position: Number(ctx.request.body.position) }); await RoomModel.updateOne({ id: ctx.request.body.id }, {
position: Number(ctx.request.body.position),
syncedAt: new Date(),
});
ctx.body = 'success'; ctx.body = 'success';
}); });
router.get('/position', async (ctx) => { router.get('/position', async (ctx) => {
const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec(); const room = await RoomModel.findOne({ id: ctx.request.query.id }).exec();
if (room) { if (room) {
const elapsedTime = new Date().getTime() - new Date(room.syncedAt).getTime();
ctx.body = { ctx.body = {
position: room.position, position: room.position + elapsedTime / 1000,
syncedAt: room.syncedAt,
}; };
} else { } else {
({ status: ctx.status, body: ctx.body } = errorResponse('room-00', 'Room not found')); ({ status: ctx.status, body: ctx.body } = errorResponse('room-00', 'Room not found'));

View File

@@ -9,6 +9,7 @@ export interface Room {
downloaded?: boolean; downloaded?: boolean;
downloadedAt?: Date; downloadedAt?: Date;
position: number; position: number;
syncedAt: Date;
} }
export const roomSchema = new Schema<Room>({ export const roomSchema = new Schema<Room>({
@@ -20,4 +21,5 @@ export const roomSchema = new Schema<Room>({
downloaded: { type: Boolean, required: false }, downloaded: { type: Boolean, required: false },
downloadedAt: { type: Date, required: false }, downloadedAt: { type: Date, required: false },
position: { type: Number, required: true }, position: { type: Number, required: true },
syncedAt: { type: Date, required: true },
}); });