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

View File

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