Skip to content

Commit

Permalink
add saved maps autocorrection on app init
Browse files Browse the repository at this point in the history
  • Loading branch information
prevzzy committed Nov 5, 2023
1 parent 2818b2f commit 516aa83
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ out/

# Other
src/renderer/js/game/offsets.js
combos/
combos/
highscores.json
2 changes: 1 addition & 1 deletion src/main/browserWindows/windowsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const mainWindowConfig = {
height: 768,
minWidth: 600,
minHeight: 500,
frame: false,
frame: process.env.APP_MODE === 'DEBUG',
webPreferences: {
nodeIntegration: true,
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY
Expand Down
1 change: 0 additions & 1 deletion src/renderer/js/combo/comboSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async function saveNewCombo(mapScriptName, comboData, hasPassedBailedComboCondit
mapName,
)
}


LastComboUI.setLastComboPageInfo(true, 'Saving last combo...', 2, false)
if (fullDataFileName) {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/js/debug/debugHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export function isAppInDebugMode() {
return process.env.APP_MODE === 'DEBUG'
}

export function log(message) {
export function log(...message) {
if (isAppInDebugMode()) {
console.log(message)
console.log(...message)
}
}

Expand Down
39 changes: 30 additions & 9 deletions src/renderer/js/files/fileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const path = require('path')
import rimraf from 'rimraf'
import { ALL_MAPS } from '../utils/constants'
import { maps } from '../utils/maps'
import { correctHighscoresFile } from './highscoresFileValidation'
import { log } from '../debug/debugHelpers'
import * as OverlayUI from '../ui/uiOverlay'
import * as SavedCombosService from '../combo/savedCombosService'

Expand All @@ -15,8 +17,10 @@ function setSavingPaths(paths) {
appFolderPath
} = paths

highscoresJsonPath = path.join(appDataPath, 'highscores.json')
savedCombosFolderPath = path.join(appDataPath, 'combos')
const folderPathToUse = process.env.APP_MODE === 'DEBUG' ? appFolderPath : appDataPath;

highscoresJsonPath = path.join(folderPathToUse, 'highscores.json')
savedCombosFolderPath = path.join(folderPathToUse, 'combos')
}

function readHighscoresJson() {
Expand All @@ -33,9 +37,22 @@ function readHighscoresJson() {
if (error) {
reject(error)
}

let parsedData = JSON.parse(data);
try {
const correctedData = correctHighscoresFile(parsedData)
if (correctedData) {
log('highscore file needed correcting - overriding')
parsedData = correctedData;
saveHighscoresJson(correctedData);
}
} catch {
// It's hard to predict all scenarios that can result in throwing an error here, but having an uncorrected highscores file isn't the end of the world anyway. There is no reason to stop the entire application from running, so just catch the error and move on.
console.error('an error occured when correcting highscores file')
}

try {
SavedCombosService.setSavedCombos(JSON.parse(data));
SavedCombosService.setSavedCombos(parsedData);
resolve()
} catch {
reject(error)
Expand All @@ -60,6 +77,15 @@ function saveHighscoresJson(newSavedCombos) {
})
}

export function createMapObject(mapCategory, mapScriptName) {
return {
name: maps[mapCategory][mapScriptName],
combosTracked: 0,
scores: [],
timeSpent: 0,
}
}

function createNewHighscoresJson() {
return new Promise((resolve, reject) => {
let mapCategoriesJson = {}
Expand All @@ -72,12 +98,7 @@ function createNewHighscoresJson() {
...mapCategoriesJson,
[mapCategory]: {
...mapCategoriesJson[mapCategory],
[mapScriptName]: {
name: maps[mapCategory][mapScriptName],
combosTracked: 0,
scores: [],
timeSpent: 0,
}
[mapScriptName]: createMapObject(mapCategory, mapScriptName),
}
}
})
Expand Down
156 changes: 156 additions & 0 deletions src/renderer/js/files/highscoresFileValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { log } from '../debug/debugHelpers';
import { maps } from '../utils/maps';
import { createMapObject } from './fileService';

const CUSTOM_LEVELS_CATEGORY = 'CUSTOM LEVELS'

function findTargetMapCategoryForMap(mapScriptName) {
return Object.keys(maps).find(mapCategory => maps[mapCategory] && maps[mapCategory][mapScriptName])
}

function isMapPlacedInCorrectCategory(mapCategoryName, mapScriptName) {
if (mapCategoryName === CUSTOM_LEVELS_CATEGORY) {
return !Object.keys(maps).find(_mapCategoryName =>
Object.keys(maps[_mapCategoryName]).find(_mapScriptName =>
mapScriptName === _mapScriptName
)
)
}

return maps[mapCategoryName] && maps[mapCategoryName][mapScriptName]
}

function isMapNameCorrect(mapName, mapCategoryName, mapScriptName) {
if (mapCategoryName === CUSTOM_LEVELS_CATEGORY) {
return true
}

return maps[mapCategoryName] && mapName === maps[mapCategoryName][mapScriptName]
}

function sortMapCategoryInDefaultOrder(fileData, mapCategoryName) {
if (!maps[mapCategoryName]) {
return
}

const defaultMapOrder = Object.keys(maps[mapCategoryName])

const sortedMapData = {}
for (const mapScriptName of defaultMapOrder) {
sortedMapData[mapScriptName] = fileData.mapCategories[mapCategoryName][mapScriptName]
}

fileData.mapCategories[mapCategoryName] = sortedMapData
}

function moveMapToCorrectCategory(
fileData,
mapScriptName,
oldMapCategory,
targetMapCategory
) {
const mapData = fileData.mapCategories[oldMapCategory][mapScriptName]
fileData.mapCategories[targetMapCategory][mapScriptName] = mapData

delete fileData.mapCategories[oldMapCategory][mapScriptName];
}

function setCorrectMapName(mapData, allMapsData, correctMapName) {
const oldMapName = mapData.name
mapData.name = correctMapName

log('incorrect map name:', oldMapName, ', should be:', correctMapName)

mapData.scores.forEach(score => score.mapName = correctMapName)
allMapsData.scores.forEach(score => {
if (score.mapName === oldMapName) {
score.mapName = correctMapName
}
})
}

function handleMovingMapToDifferentMapCategory(fileData, fileMapCategory, mapScriptName) {
const targetMapCategory = findTargetMapCategoryForMap(
mapScriptName,
fileMapCategory === CUSTOM_LEVELS_CATEGORY
)
log('incorrect category for map:', mapScriptName, 'in category', fileMapCategory, ', should go to:', targetMapCategory)

if (targetMapCategory) {
moveMapToCorrectCategory(
fileData,
mapScriptName,
fileMapCategory,
targetMapCategory,
)
}

return targetMapCategory
}

function correctMapData(fileData, mapCategories, mapCategory, mapScriptName) {
let isDataCorrected = false;
let targetMapCategory = mapCategory;
const mapData = mapCategories[mapCategory][mapScriptName]

if (!isMapPlacedInCorrectCategory(mapCategory, mapScriptName)) {
targetMapCategory = handleMovingMapToDifferentMapCategory(fileData, mapCategory, mapScriptName)
isDataCorrected = true
}

if (!isMapNameCorrect(mapData.name, targetMapCategory, mapScriptName)) {
setCorrectMapName(mapData, fileData.allMaps, maps[targetMapCategory][mapScriptName])
isDataCorrected = true
}

return isDataCorrected
}

function verifyCategoryContents(
fileData,
mapCategory,
fileMapScriptsForCategory,
allMapScriptsForCategory
) {
let isDataCorrected = false
allMapScriptsForCategory.forEach(mapScriptName => {
if (!fileMapScriptsForCategory.includes(mapScriptName)) {
log(`missing map in ${mapCategory}:`, mapScriptName)

fileData.mapCategories[mapCategory] = {
...fileData.mapCategories[mapCategory],
[mapScriptName]: createMapObject(mapCategory, mapScriptName)
}
isDataCorrected = true
}
})

return isDataCorrected
}

export function correctHighscoresFile(fileData) {
let isDataCorrected = false
const { mapCategories } = fileData
Object.keys(mapCategories).forEach(mapCategory => {
let fileMapScripts = Object.keys(mapCategories[mapCategory])

if (mapCategory !== CUSTOM_LEVELS_CATEGORY) {
isDataCorrected = verifyCategoryContents(
fileData,
mapCategory,
Object.keys(mapCategories[mapCategory]),
Object.keys(maps[mapCategory])
)
}

fileMapScripts.forEach(mapScriptName => {
isDataCorrected = correctMapData(fileData, mapCategories, mapCategory, mapScriptName)
})

sortMapCategoryInDefaultOrder(fileData, mapCategory)
})

if (isDataCorrected) {
return fileData
}
}

0 comments on commit 516aa83

Please sign in to comment.