refactor: Simplify key generation by using generic names for keys in KeySelection

This commit is contained in:
Z. Cliffe Schreuders
2025-10-28 14:26:52 +00:00
parent 1228bc15fe
commit 0ded5cae05
3 changed files with 16 additions and 43 deletions

View File

@@ -674,9 +674,6 @@ function findObjectsAtPosition(worldX, worldY) {
// Normalize keyPins from 0-100 scale to 25-65 scale in entire scenario
function normalizeScenarioKeyPins(scenario) {
if (!scenario || !scenario.rooms) {
return;
}
// Helper function to convert a single keyPins value
function convertKeyPin(value) {

View File

@@ -65,14 +65,18 @@ export class KeySelection {
if (validKeys.length === 0) {
// No valid keys in inventory, generate random ones
const key1 = this.parent.generateRandomKey(this.parent.pinCount);
const key2 = this.parent.generateRandomKey(this.parent.pinCount);
const key3 = this.parent.generateRandomKey(this.parent.pinCount);
const key1 = this.generateRandomKey(this.parent.pinCount);
const key2 = this.generateRandomKey(this.parent.pinCount);
const key3 = this.generateRandomKey(this.parent.pinCount);
// Make the first key correct
key1.cuts = this.parent.keyData.cuts;
key1.id = correctKeyId || 'correct_key';
key1.name = 'Correct Key';
key1.name = `Key ${Math.floor(Math.random() * 10000)}`;
// Give other keys generic names too
key2.name = `Key ${Math.floor(Math.random() * 10000)}`;
key3.name = `Key ${Math.floor(Math.random() * 10000)}`;
// Randomize the order
const keys = [key1, key2, key3];
@@ -92,18 +96,18 @@ export class KeySelection {
// Create keys for challenge mode (like locksmith-forge.html)
// Generates 3 keys with one guaranteed correct key
const key1 = this.parent.generateRandomKey(this.parent.pinCount);
const key2 = this.parent.generateRandomKey(this.parent.pinCount);
const key3 = this.parent.generateRandomKey(this.parent.pinCount);
const key1 = this.generateRandomKey(this.parent.pinCount);
const key2 = this.generateRandomKey(this.parent.pinCount);
const key3 = this.generateRandomKey(this.parent.pinCount);
// Make the first key correct by copying the actual key cuts
key1.cuts = this.parent.keyData.cuts;
key1.id = correctKeyId;
key1.name = 'Correct Key';
key1.name = `Key ${Math.floor(Math.random() * 10000)}`;
// Give other keys descriptive names
key2.name = 'Wrong Key 1';
key3.name = 'Wrong Key 2';
// Give other keys generic names too
key2.name = `Key ${Math.floor(Math.random() * 10000)}`;
key3.name = `Key ${Math.floor(Math.random() * 10000)}`;
// Randomize the order of keys
const keys = [key1, key2, key3];

View File

@@ -608,35 +608,7 @@
// If this is a key mode level, automatically show key selection
if (config.keyMode) {
setTimeout(() => {
// Override the createKeysForChallenge method to use generic names
const originalCreateKeysForChallenge = this.currentGame.createKeysForChallenge.bind(this.currentGame);
this.currentGame.createKeysForChallenge = (correctKeyId = 'challenge_key') => {
// Create keys for challenge mode (like locksmith-forge.html)
// Generates 3 keys with one guaranteed correct key
const key1 = this.currentGame.generateRandomKey(this.currentGame.pinCount);
const key2 = this.currentGame.generateRandomKey(this.currentGame.pinCount);
const key3 = this.currentGame.generateRandomKey(this.currentGame.pinCount);
// Make the first key correct by copying the actual key cuts
key1.cuts = this.currentGame.keyData.cuts;
key1.id = correctKeyId;
key1.name = `Key ${Math.floor(Math.random() * 1000)}`; // Generic name
// Give other keys generic names too
key2.name = `Key ${Math.floor(Math.random() * 1000)}`;
key3.name = `Key ${Math.floor(Math.random() * 1000)}`;
// Randomize the order of keys
const keys = [key1, key2, key3];
this.currentGame.shuffleArray(keys);
// Find the new index of the correct key after shuffling
const correctKeyIndex = keys.findIndex(key => key.id === correctKeyId);
return this.currentGame.createKeySelectionUI(keys, correctKeyId);
};
// Start key selection challenge
this.currentGame.startWithKeySelection();
}, 500); // Small delay to ensure game is fully initialized
}