86 lines
2.7 KiB
JavaScript
86 lines
2.7 KiB
JavaScript
const admin = require('firebase-admin');
|
|
|
|
module.exports = async (req, res) => {
|
|
const { player, roomId, uid, showChanged, userName } = JSON.parse(req.body);
|
|
// const statusString =
|
|
// player.status === 'PLAYING'
|
|
// ? 'started playing from'
|
|
// : player.status === 'PAUSED'
|
|
// ? 'paused at'
|
|
// : player.status === 'NOT_PLAYING'
|
|
// ? 'stopped at'
|
|
// : 'is at';
|
|
// const timeString =
|
|
// player.time > 3600
|
|
// ? new Date(player.time * 1000).toISOString().substr(11, 8)
|
|
// : new Date(player.time * 1000).toISOString().substr(14, 5);
|
|
if (!roomId) return res.status(204).send({ message: 'Party name not sent.' });
|
|
const [roomExists, roomData] = await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.get()
|
|
.then((docSnap) => [docSnap.exists, docSnap.data()]);
|
|
if (!roomExists) return res.status(404).send({ message: 'Party not found.' });
|
|
const userExists = await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.collection('users')
|
|
.doc(uid)
|
|
.get()
|
|
.then((userSnap) => userSnap.exists);
|
|
if (userExists) {
|
|
if (showChanged === true && player.showName) {
|
|
if (player.showName !== roomData.player.showName) {
|
|
await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.collection('messages')
|
|
.add({
|
|
uid: '<system>',
|
|
name: '',
|
|
text: `${userName} changed \n${player.showName ? 'to ' + player.showName : ''}`,
|
|
time: new Date(),
|
|
});
|
|
}
|
|
}
|
|
if (roomData.status !== player.status) {
|
|
await admin
|
|
.firestore()
|
|
.collection('rooms')
|
|
.doc(roomId)
|
|
.update({
|
|
player,
|
|
})
|
|
.catch((err) => {
|
|
console.error(`Failed to update player : \n\t Room: ${roomId} \n\t ${uid}`, err);
|
|
res.status(500).send({ message: "Uh oh party could't be synced", err });
|
|
});
|
|
// await admin
|
|
// .firestore()
|
|
// .collection('rooms')
|
|
// .doc(roomId)
|
|
// .collection('messages')
|
|
// .add({
|
|
// uid: '<system>',
|
|
// name: '',
|
|
// text: `${userName} ${statusString} ${timeString}`,
|
|
// time: new Date(),
|
|
// })
|
|
// .catch((err) => {
|
|
// console.error(
|
|
// `Failed to add system update player message : \n\t Room: ${roomId} \n\t ${uid}`,
|
|
// err
|
|
// );
|
|
// });
|
|
return res.status(200).send({ message: 'Synced party player' });
|
|
} else {
|
|
return res.status(208).send({ message: 'Player already up to date' });
|
|
}
|
|
} else {
|
|
return res.status(401).send({ message: 'Not in room to sync' });
|
|
}
|
|
};
|