Added min/max to numeric config values.

This commit is contained in:
Pachira 2024-04-21 18:58:18 +00:00
parent 36e14399d8
commit a3421af127
2 changed files with 39 additions and 19 deletions

View file

@ -99,7 +99,7 @@ window.Umi = { UI: {} };
settings.define('autoEmbedV1').default(false).create();
settings.define('soundEnable').default(true).critical().create();
settings.define('soundPack').default('').create();
settings.define('soundVolume').default(80).create();
settings.define('soundVolume').default(80).min(0).max(100).create();
settings.define('soundEnableJoin').default(true).create();
settings.define('soundEnableLeave').default(true).create();
settings.define('soundEnableError').default(true).create();
@ -121,7 +121,7 @@ window.Umi = { UI: {} };
settings.define('motivationalVideos').default(false).create();
settings.define('osuKeys').default(false).create();
settings.define('osuKeysV2').type(['no', 'yes', 'rng']).default('no').create();
settings.define('explosionRadius').default(20).create();
settings.define('explosionRadius').default(20).min(0).create();
settings.define('dumpPackets').default(FUTAMI_DEBUG).create();
settings.define('dumpEvents').default(FUTAMI_DEBUG).create();
settings.define('marqueeAllNames').default(false).create();

View file

@ -71,6 +71,11 @@ const MamiSettings = function(storageOrPrefix, eventTarget) {
if(setting.type === 'number') {
value = parseFloat(value);
resolved = true;
if(setting.min !== undefined && value < setting.min)
value = setting.min;
else if(setting.max !== undefined && value > setting.max)
value = setting.max;
} else if(setting.type === 'boolean') {
value = !!value;
resolved = true;
@ -122,11 +127,16 @@ const MamiSettings = function(storageOrPrefix, eventTarget) {
checkDefined();
let created = false;
let type = undefined;
let fallback = null;
let immutable = false;
let critical = false;
let virtual = false;
const setting = {
name: name,
type: undefined,
fallback: null,
immutable: false,
critical: false,
min: undefined,
max: undefined,
};
const checkCreated = () => {
if(created)
@ -140,26 +150,42 @@ const MamiSettings = function(storageOrPrefix, eventTarget) {
checkCreated();
type = value;
setting.type = value;
return pub;
},
default: value => {
checkCreated();
fallback = value === undefined ? null : value;
setting.fallback = value === undefined ? null : value;
if(type === undefined)
type = typeof fallback;
if(setting.type === undefined)
setting.type = typeof setting.fallback;
return pub;
},
immutable: value => {
checkCreated();
immutable = value === undefined || value === true;
setting.immutable = value === undefined || value === true;
return pub;
},
critical: value => {
checkCreated();
critical = value === undefined || value === true;
setting.critical = value === undefined || value === true;
return pub;
},
min: value => {
checkCreated();
if(typeof value !== 'number')
throw 'value must be a number';
setting.min = value;
return pub;
},
max: value => {
checkCreated();
if(typeof value !== 'number')
throw 'value must be a number';
setting.max = value;
return pub;
},
virtual: value => {
@ -171,13 +197,7 @@ const MamiSettings = function(storageOrPrefix, eventTarget) {
checkCreated();
checkDefined();
settings.set(name, Object.freeze({
name: name,
type: type,
fallback: fallback,
immutable: immutable,
critical: critical,
}));
settings.set(name, Object.freeze(setting));
if(virtual)
storage.virtualise(name);