cloud-game/web/js/room.js
Sergey Stepanov 7ee98c1b03 Add keyboard and mouse support
Keyboard and mouse controls will now work if you use the kbMouseSupport parameter in the config for Libretro cores. Be aware that capturing mouse and keyboard controls properly is only possible in fullscreen mode.

Note: In the case of DOSBox, a virtual filesystem handler is not yet implemented, thus each game state will be shared between all rooms (DOS game instances) of CloudRetro.
2024-08-02 11:04:44 +03:00

81 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.id = data.roomId
room.save(data.roomId);
}, 1);
/**
* Game room module.
*/
export const room = {
get id() {
return id
},
set id(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.id)}`,
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);
}
}