From 8fd25af7a165fa73573e3386f393cb5320c4bca0 Mon Sep 17 00:00:00 2001 From: Nathan Byrd Date: Fri, 8 Sep 2023 14:45:36 +0000 Subject: [PATCH] Changes to use acs out of theme --- art/themes/luciano_blocktronics/theme.hjson | 2 + core/login_server_module.js | 2 +- core/nua.js | 2 +- core/theme.js | 65 ++++++++++++++++----- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/art/themes/luciano_blocktronics/theme.hjson b/art/themes/luciano_blocktronics/theme.hjson index fde3659c..f792d839 100644 --- a/art/themes/luciano_blocktronics/theme.hjson +++ b/art/themes/luciano_blocktronics/theme.hjson @@ -4,6 +4,8 @@ author: Luciano Ayres group: blocktronics enabled: true + // this theme requires at least 80 columns + acs: TW80 // // Also check out Luciano's ANSIGARDEN: diff --git a/core/login_server_module.js b/core/login_server_module.js index 9f8b1be6..4defd228 100644 --- a/core/login_server_module.js +++ b/core/login_server_module.js @@ -33,7 +33,7 @@ module.exports = class LoginServerModule extends ServerModule { const preLoginTheme = _.get(Config(), 'theme.preLogin'); const selectedTheme = theme.findMatching(client, preLoginTheme); - if ('*' === selectedTheme) { + if (_.isNil(selectedTheme) || '*' === selectedTheme) { client.user.properties[UserProps.ThemeId] = theme.getRandomTheme() || ''; } else { client.user.properties[UserProps.ThemeId] = selectedTheme; diff --git a/core/nua.js b/core/nua.js index 4e11ed69..e64d17d2 100644 --- a/core/nua.js +++ b/core/nua.js @@ -120,7 +120,7 @@ exports.getModule = class NewUserAppModule extends MenuModule { const defaultTheme = _.get(config, 'theme.default'); const selectedTheme = theme.findMatching(self.client, defaultTheme); - if ('*' === selectedTheme) { + if (_.isNil(selectedTheme) || '*' === selectedTheme) { newUser.properties[UserProps.ThemeId] = theme.getRandomTheme(); } else { newUser.properties[UserProps.ThemeId] = selectedTheme; diff --git a/core/theme.js b/core/theme.js index 9a30c467..4fc9c331 100644 --- a/core/theme.js +++ b/core/theme.js @@ -27,6 +27,7 @@ exports.getThemeArt = getThemeArt; exports.getAvailableThemes = getAvailableThemes; exports.getRandomTheme = getRandomTheme; exports.setClientTheme = setClientTheme; +exports.themeAcsMatches = themeAcsMatches; exports.findMatching = findMatching; exports.selectDefaultTheme = selectDefaultTheme; exports.displayPreparedArt = displayPreparedArt; @@ -50,9 +51,9 @@ exports.ThemeManager = class ThemeManager { themeManagerInstance .getAvailableThemes() .forEach((themeConfig, themeId) => { - const { name, author, group } = themeConfig.get().info; + const { name, author, group, acs } = themeConfig.get().info; Log.info( - { themeId, themeName: name, author, group }, + { themeId, themeName: name, author: author, group: group, acs: acs }, 'Theme loaded' ); }); @@ -420,7 +421,7 @@ function getRandomTheme() { } function selectDefaultTheme(client) { const selectedTheme = theme.findMatching(client, Config().theme.default); - if ('*' === selectedTheme) { + if (_.isNil(selectedTheme) || '*' === selectedTheme) { return theme.getRandomTheme() || ''; } else { return selectedTheme; @@ -428,6 +429,23 @@ function selectDefaultTheme(client) { } +function themeAcsMatches(client, themeName) { + const themeConfig = getAvailableThemes().get(themeName); + if (!_.isNil(themeConfig)) { + if (_.has(themeConfig, 'info.acs')) { + return client.acs.matches(themeConfig.info.acs); + + } + else { + return true; // No ACS means anything can use it + } + } + else { + return false; + } + +} + function findMatching(client, themeSection) { if (!(_.isArray(themeSection))) { Log.debug({ theme: themeSection }, 'Setting the theme from a simple string'); @@ -435,16 +453,11 @@ function findMatching(client, themeSection) { } Log.debug('Finding a matching theme from ACS settings'); - const matchingTheme = client.acs.getConditionalValue(themeSection, 'name'); - if (_.isNil(matchingTheme)) { - Log.warn('No matching theme in configuration found.'); - // Default to random if nothing found - return '*'; - } - else { - Log.debug({ theme: matchingTheme }, 'Found matching theme'); - return matchingTheme; - } + let matchingTheme = themeSection.find((themeName) => { + return themeAcsMatches(client, themeName); + }); + + return matchingTheme; } function setClientTheme(client, themeId) { @@ -456,10 +469,18 @@ function setClientTheme(client, themeId) { const defaultTheme = selectDefaultTheme(client); if (availThemes.has(themeId)) { msg = 'Set client theme'; - setThemeId = themeId; + setThemeId = themeAcsMatches(client, themeId); + if (_.isNil(setThemeId)) { + Log.warn('No theme matching acs found, setting to first theme.'); + setThemeId = availThemes.keys().next().value; + } } else if (availThemes.has(defaultTheme)) { msg = 'Failed setting theme by supplied ID; Using default'; - setThemeId = config.theme.default; + setThemeId = findMatching(client, config.theme.default); + if (_.isNil(setThemeId)) { + Log.warn('No theme matching acs found, setting to first theme.'); + setThemeId = availThemes.keys().next().value; + } } else { msg = 'Failed setting theme by system default ID; Using the first one we can find'; @@ -545,9 +566,21 @@ function getThemeArt(options, cb) { }); }, function fromDefaultTheme(artInfo, callback) { - if (artInfo || config.theme.default === options.themeId) { + if (artInfo) { return callback(null, artInfo); } + + if (!_.isArray(config.theme.default)) { + if (config.theme.default === options.themeId) { + return callback(null, artInfo); + } + } + else { + if (config.theme.default.includes(options.themeId)) { + return callback(null, artInfo); + + } + } const defaultTheme = selectDefaultTheme(options.client); options.basePath = paths.join(config.paths.themes, defaultTheme); art.getArt(options.name, options, (err, artInfo) => {