cloud-game/web/js/room.js
Sergey Stepanov 2bc64a3be8 Migrate from IIFE to modern ES modules
These modules should be supported by all contemporary browsers, and this transition should resolve most issues related to the explicit import order of the .js files.
2024-03-17 22:09:43 +03:00

79 lines
1.8 KiB
JavaScript

import {
sub,
GAME_ROOM_AVAILABLE
} from 'event';
let id = '';
// UI
const roomLabel = document.getElementById('room-txt');
// !to rewrite
const parseURLForRoom = () => {
let queryDict = {};
let regex = /^\/?([A-Za-z]*)\/?/g;
const zone = regex.exec(location.pathname)[1];
let room = null;
// get room from URL
location.search.substr(1)
.split('&')
.forEach((item) => {
queryDict[item.split('=')[0]] = item.split('=')[1]
});
if (typeof queryDict.id === 'string') {
room = decodeURIComponent(queryDict.id);
}
return [room, zone];
};
sub(GAME_ROOM_AVAILABLE, data => {
room.setId(data.roomId);
room.save(data.roomId);
}, 1);
/**
* Game room module.
*/
export const room = {
getId: () => id,
setId: (id_) => {
id = id_;
roomLabel.value = id;
},
reset: () => {
id = '';
roomLabel.value = id;
},
save: (roomIndex) => {
localStorage.setItem('roomID', roomIndex);
},
load: () => localStorage.getItem('roomID'),
getLink: () => window.location.href.split('?')[0] + `?id=${encodeURIComponent(room.getId())}`,
loadMaybe: () => {
// localStorage first
//roomID = loadRoomID();
let zone = '';
// Shared URL second
const [parsedId, czone] = parseURLForRoom();
if (parsedId !== null) {
id = parsedId;
}
if (czone !== null) {
zone = czone;
}
return [id, zone];
},
copyToClipboard: () => {
const el = document.createElement('textarea');
el.value = room.getLink();
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
}