diff --git a/public/Assets/locales/en.json b/public/Assets/locales/en.json index cd5801a0f..5fbd984ee 100644 --- a/public/Assets/locales/en.json +++ b/public/Assets/locales/en.json @@ -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", diff --git a/src/content/core/settings/settingConfig.js b/src/content/core/settings/settingConfig.js index 4ef06a8ce..a90391c42 100644 --- a/src/content/core/settings/settingConfig.js +++ b/src/content/core/settings/settingConfig.js @@ -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: diff --git a/src/content/features/games/serverControls.js b/src/content/features/games/serverControls.js new file mode 100644 index 000000000..1b0cb2692 --- /dev/null +++ b/src/content/features/games/serverControls.js @@ -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, + ); +} diff --git a/src/content/index.js b/src/content/index.js index 41dbd93a1..8e8906c11 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -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'; @@ -431,6 +432,7 @@ const featureRoutes = [ initAutoFriendsAllowed, initCatalogExplorer, initUnderReviewPill, + initServerControls, initGameStatTimestamps, ], },