Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions public/Assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,17 @@
"insufficientDataTooltip": "Not enough transaction history to make an accurate estimate. Please wait for more transactions to complete.",
"label": "Unpending Robux tomorrow"
},
"serverControls": {
"restart": "Restart servers for updates",
"restartTitle": "Restart servers for updates?",
"restartMessage": "Servers running an old version will close and players will be moved to the latest version.",
"restartDone": "Servers on old versions are restarting.",
"shutdown": "Shut down all servers",
"shutdownTitle": "Shut down all servers?",
"shutdownMessage": "Every server of this experience will close, including servers on the latest version.",
"shutdownDone": "All servers are shutting down.",
"failed": "Couldn't update the servers."
},
"totalEarned": {
"calculateEarnings": "Calculate Earnings",
"calculateEarned": "Calculate Earned",
Expand Down
27 changes: 27 additions & 0 deletions src/content/core/settings/settingConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3752,6 +3752,33 @@ export const SETTINGS_CONFIG = {
},
},
},
serverControlsEnabled: {
label: 'Server Controls',
description: [
'Adds server actions to the menu on experience pages you manage.',
],
type: 'checkbox',
default: true,
contributors: ['4975603156'],
childSettings: {
serverControlsRestartEnabled: {
label: 'Restart servers for updates',
description: [
'Closes servers running an old version and moves players to the latest version.',
],
type: 'checkbox',
default: true,
},
serverControlsShutdownEnabled: {
label: 'Shut down all servers',
description: [
'Closes every server of the experience, including servers on the latest version.',
],
type: 'checkbox',
default: true,
},
},
},
viewIdEnabled: {
label: 'View Bundled IDs',
description:
Expand Down
141 changes: 141 additions & 0 deletions src/content/features/games/serverControls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { observeElement } from '../../core/observer.js';
import { callRobloxApi, callRobloxApiJson } from '../../core/api.js';
import { settings } from '../../core/settings/getSettings.js';
import { ts } from '../../core/locale/i18n.js';
import { showConfirmationPrompt } from '../../core/ui/confirmationPrompt.js';
import { showSystemAlert } from '../../core/ui/roblox/alert.js';

const ITEM_CLASS = 'rovalra-server-control';

let observer = null;
let listening = false;
let state = { universeId: null };

const ACTIONS = {
restart: {
endpoint: '/matchmaking-api/v1/game-instances/launch-update',
body: () => ({
universeId: Number(state.universeId),
closeOldVersionsOnly: true,
}),
confirmType: 'primary',
},
shutdown: {
endpoint: '/matchmaking-api/v1/game-instances/launch-update',
body: () => ({
universeId: Number(state.universeId),
closeOldVersionsOnly: false,
}),
confirmType: 'alert',
},
};

async function canManage(universeId) {
const data = await callRobloxApiJson({
subdomain: 'develop',
endpoint: `/v1/universes/multiget/permissions?ids=${universeId}`,
noCache: true,
});
return data?.data?.[0]?.canManage === true;
}

async function runAction(key) {
const action = ACTIONS[key];
try {
const response = await callRobloxApi({
subdomain: 'apis',
endpoint: action.endpoint,
method: 'POST',
body: action.body(),
noCache: true,
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
showSystemAlert(ts(`serverControls.${key}Done`), 'success');
} catch (error) {
console.error(`RoValra: Failed to ${key} servers`, error);
showSystemAlert(ts('serverControls.failed'), 'warning');
}
}

function onClick(event) {
const key = event.target.closest?.(`.${ITEM_CLASS} button`)?.dataset
.rovalraServerAction;
if (!key || !state.universeId) return;

showConfirmationPrompt({
title: ts(`serverControls.${key}Title`),
message: ts(`serverControls.${key}Message`),
confirmText: ts(`serverControls.${key}`),
confirmType: ACTIONS[key].confirmType,
onConfirm: () => runAction(key),
});
}

async function addMenuItems(menu) {
const universeId = document.querySelector('#game-detail-meta-data')?.dataset
.universeId;
if (
!/^\d+$/.test(universeId || '') ||
menu.querySelector(`.${ITEM_CLASS}`)
) {
return;
}

try {
const [restartEnabled, shutdownEnabled] = await Promise.all([
settings.serverControlsRestartEnabled,
settings.serverControlsShutdownEnabled,
]);
const keys = Object.keys(ACTIONS).filter((key) =>
key === 'restart'
? restartEnabled !== false
: shutdownEnabled !== false,
);
if (!keys.length) return;

if (!(await canManage(universeId))) return;
if (menu.querySelector(`.${ITEM_CLASS}`)) return;

state = { universeId };
const items = keys.map((key) => {
const item = document.createElement('li');
item.className = ITEM_CLASS;
const button = document.createElement('button');
button.type = 'button';
button.setAttribute('role', 'button');
button.dataset.rovalraServerAction = key;
button.textContent = ts(`serverControls.${key}`);
item.append(button);
return item;
});

const anchor = menu.querySelector('#rbx-configure-game');
if (anchor) anchor.after(...items);
else menu.append(...items);

if (!listening) {
listening = true;
document.addEventListener('click', onClick);
}
} catch (error) {
console.error('RoValra: Failed to add server controls', error);
}
}

export async function init() {
observer?.disconnect();
observer = null;
state = { universeId: null };

if (
!window.location.pathname.startsWith('/games/') ||
!(await settings.serverControlsEnabled)
) {
return;
}

observer = observeElement(
'#game-context-menu ul.dropdown-menu',
addMenuItems,
);
}
2 changes: 2 additions & 0 deletions src/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ import { init as initHeatmap } from './features/games/tab/updateHistory.js';
import { init as initTotalSpentGames } from './features/games/tab/totalSpentGames.js';
import { init as initEvents } from './features/games/about/events.js';
import { init as initUnderReviewPill } from './features/games/underReviewPill.js';
import { init as initServerControls } from './features/games/serverControls.js';
// transactions
import { init as initTotalSpent } from './features/transactions/totalspent.js';
import { init as initSpending } from './features/transactions/spending.js';
Expand Down Expand Up @@ -431,6 +432,7 @@ const featureRoutes = [
initAutoFriendsAllowed,
initCatalogExplorer,
initUnderReviewPill,
initServerControls,
initGameStatTimestamps,
],
},
Expand Down
Loading