partey_workadventure/maps/tests/CoWebsite/script.js

109 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2021-10-12 10:38:49 +02:00
WA.onInit().then(() => {
WA.chat.onChatMessage((message) => {
if (!message.startsWith('/')) {
return;
}
const splitedMessage = message.trim().split(' ');
const command = splitedMessage.shift().substr(1);
executeCommand(command, splitedMessage);
});
});
2022-01-25 20:43:27 +01:00
function wokaSendMessage(message) {
WA.chat.sendChatMessage(message, 'Woka');
2021-10-12 10:38:49 +02:00
}
function unknownCommand() {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Unknown command');
2021-10-12 10:38:49 +02:00
}
function executeCommand(command, args) {
switch(command) {
case 'cowebsite':
coWebsiteCommand(args);
break;
default:
unknownCommand();
}
}
function coWebsiteCommand(args) {
if (args.length < 1) {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Too few arguments');
2021-10-12 10:38:49 +02:00
return;
}
const subCommand = args.shift();
switch(subCommand) {
case 'open':
openCoWebsite(args);
break;
case 'close':
closeCoWebsite(args);
break;
default:
unknownCommand();
}
}
async function openCoWebsite(args) {
if (args.length < 1) {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Too few arguments');
2021-10-12 10:38:49 +02:00
return;
}
try {
const url = new URL(args[0]);
} catch (exception) {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Parameter is not a valid URL !');
2021-10-12 10:38:49 +02:00
return;
}
2021-10-25 18:59:52 +02:00
await WA.nav.openCoWebSite(args[0]).then(() => {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Co-website has been opened !');
2021-10-12 10:38:49 +02:00
}).catch((error) => {
2022-01-25 20:43:27 +01:00
wokaSendMessage(`Something wrong happen during co-website opening: ${error.message}`);
2021-10-12 10:38:49 +02:00
});
}
async function closeCoWebsite(args) {
if (args.length < 1) {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Too few arguments');
2021-10-12 10:38:49 +02:00
return;
}
2021-10-25 18:59:52 +02:00
const coWebsites = await WA.nav.getCoWebSites();
2021-10-12 10:38:49 +02:00
// All
if (args[0] === "all" || args[0] === "*") {
2021-10-25 18:59:52 +02:00
coWebsites.forEach(coWebsite => {
coWebsite.close();
2021-10-12 10:38:49 +02:00
});
2022-01-25 20:43:27 +01:00
wokaSendMessage('All co-websites has been closed !');
2021-10-12 10:38:49 +02:00
return;
}
const position = parseInt(args[0]);
// By ID or Position
const coWebsite =
isNaN(position) ?
coWebsites.find((coWebsite) => coWebsite.id === args[0]) :
coWebsites.find((coWebsite) => coWebsite.position === position);
if (!coWebsite) {
2022-01-25 20:43:27 +01:00
wokaSendMessage('Unknown co-website');
2021-10-12 10:38:49 +02:00
return;
}
2021-10-25 18:59:52 +02:00
await coWebsite.close().then(() => {
2022-01-25 20:43:27 +01:00
wokaSendMessage('This co-websites has been closed !');
2021-10-12 10:38:49 +02:00
}).catch((error) => {
2022-01-25 20:43:27 +01:00
wokaSendMessage(`Something wrong happen during co-website closing: ${error.message}`);
2021-10-12 10:38:49 +02:00
});
}