Skip to content

Commit

Permalink
fix(web): fixed restarter settings save for empty schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Sep 14, 2022
1 parent bb47c18 commit 61e5610
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
10 changes: 5 additions & 5 deletions core/components/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export default class Scheduler {

//Check validity
if (Array.isArray(this.config.restarterSchedule) && this.config.restarterSchedule.length) {
const parsed = parseSchedule(this.config.restarterSchedule);
const nextSettingRestart = getNextScheduled(parsed);
const { valid } = parseSchedule(this.config.restarterSchedule);
const nextSettingRestart = getNextScheduled(valid);
if (nextSettingRestart.minuteFloorTs < minuteFloorTs) {
throw new Error(`You already have one restart scheduled before that at ${nextSettingRestart.string}.`);
}
Expand All @@ -146,8 +146,8 @@ export default class Scheduler {
if (this.nextTempSchedule) {
nextRestart = this.nextTempSchedule;
} else if (Array.isArray(this.config.restarterSchedule) && this.config.restarterSchedule.length) {
const parsed = parseSchedule(this.config.restarterSchedule);
nextRestart = getNextScheduled(parsed);
const { valid } = parseSchedule(this.config.restarterSchedule);
nextRestart = getNextScheduled(valid);
} else {
//nothing scheduled
this.calculatedNextRestartMinuteFloorTs = false;
Expand All @@ -160,7 +160,7 @@ export default class Scheduler {
if (verbose) log(`Skipping next scheduled restart`);
return;
}

//Calculating dist
const thisMinuteTs = new Date().setSeconds(0, 0);
const nextDistMs = nextRestart.minuteFloorTs - thisMinuteTs;
Expand Down
23 changes: 14 additions & 9 deletions core/extras/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@ export const txAdminASCII = () => {

/**
* Extracts hours and minutes from an string containing times
* @param {Array} schedule
* @param {Array} scheduleTimes
* @param {Boolean} filter default true
* @return {Object} {valid, invalid}
*/
export const parseSchedule = (schedule, filter = true) => {
const out = [];
for (const time of schedule) {
if (!time.length) return;
export const parseSchedule = (scheduleTimes) => {
const valid = [];
const invalid = [];
for (const timeInput of scheduleTimes) {
if (typeof timeInput !== 'string') continue;
const timeTrim = timeInput.trim();
if (!timeTrim.length) continue;

const hmRegex = /^$|^([01]?[0-9]|2[0-3]):([0-5][0-9])$/gm; //need to set it insde the loop
const m = hmRegex.exec(time.trim());
const m = hmRegex.exec(timeTrim);
if (m === null) {
if (!filter) out.push(time);
invalid.push(timeTrim);
} else {
out.push({
valid.push({
string: m[1].padStart(2, '0') + ':' + m[2].padStart(2, '0'),
hours: parseInt(m[1]),
minutes: parseInt(m[2]),
});
}
}
return out;
return {valid, invalid};
}


Expand Down
13 changes: 2 additions & 11 deletions core/webroutes/settings/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,7 @@ async function handleMonitor(ctx) {
};

//Validating restart times
const scheduleTimes = parseSchedule(cfg.restarterSchedule, false);
const invalidRestartTimes = [];
const validRestartTimes = [];
scheduleTimes.forEach((time) => {
if (typeof time === 'string') {
invalidRestartTimes.push(`"${time}"`);
} else {
validRestartTimes.push(time.string);
}
});
const { valid: validRestartTimes, invalid: invalidRestartTimes } = parseSchedule(cfg.restarterSchedule, false);
if (invalidRestartTimes.length) {
let message = '<strong>The following entries were not recognized as valid 24h times:</strong><br>';
message += invalidRestartTimes.join('<br>\n');
Expand All @@ -273,7 +264,7 @@ async function handleMonitor(ctx) {

//Preparing & saving config
const newConfig = globals.configVault.getScopedStructure('monitor');
newConfig.restarterSchedule = validRestartTimes;
newConfig.restarterSchedule = validRestartTimes.map(t => t.string);
newConfig.disableChatWarnings = cfg.disableChatWarnings;
newConfig.resourceStartingTolerance = cfg.resourceStartingTolerance;
const saveStatus = globals.configVault.saveProfile('monitor', newConfig);
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
author 'Tabarra'
description 'Remotely Manage & Monitor your GTA5 FiveM Server'
repository 'https://github.com/tabarra/txAdmin'
version '4.18.0'
version '4.18.1-dev'
ui_label 'txAdmin'

rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aware my resources *will* become incompatible once RedM ships.'
Expand Down

0 comments on commit 61e5610

Please sign in to comment.