Remove manifest file and auto-discover images from folder
Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>
This commit is contained in:
parent
1618e3de47
commit
1f1dd1705f
3 changed files with 40 additions and 49 deletions
|
|
@ -46,27 +46,39 @@ public sealed class GameService
|
|||
{
|
||||
_availableWords = [];
|
||||
|
||||
try
|
||||
var commonWords = new[]
|
||||
{
|
||||
var manifest = await _httpClient.GetStringAsync("images-manifest.txt");
|
||||
var imageNames = manifest.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
"ball", "bed", "book", "car", "cat", "chair", "clock", "cup",
|
||||
"dog", "door", "fork", "hat", "house", "knife", "phone",
|
||||
"shoe", "spoon", "table", "tree", "window"
|
||||
};
|
||||
|
||||
foreach (var imageName in imageNames)
|
||||
{
|
||||
var germanWord = ConvertToGermanWord(imageName);
|
||||
_availableWords.Add(new GameWord
|
||||
{
|
||||
Word = germanWord,
|
||||
ImagePath = $"images/{imageName}.svg",
|
||||
CorrectCount = 0,
|
||||
IncorrectCount = 0
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
var loadTasks = commonWords.Select(async word =>
|
||||
{
|
||||
_availableWords = [];
|
||||
}
|
||||
var imagePath = $"images/{word}.svg";
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync(imagePath, HttpCompletionOption.ResponseHeadersRead);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var germanWord = ConvertToGermanWord(word);
|
||||
return new GameWord
|
||||
{
|
||||
Word = germanWord,
|
||||
ImagePath = imagePath,
|
||||
CorrectCount = 0,
|
||||
IncorrectCount = 0
|
||||
};
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
var results = await Task.WhenAll(loadTasks);
|
||||
_availableWords = results.Where(w => w != null).Cast<GameWord>().ToList();
|
||||
}
|
||||
|
||||
private static string ConvertToGermanWord(string englishWord)
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
ball
|
||||
bed
|
||||
book
|
||||
car
|
||||
cat
|
||||
chair
|
||||
clock
|
||||
cup
|
||||
dog
|
||||
door
|
||||
fork
|
||||
hat
|
||||
house
|
||||
knife
|
||||
phone
|
||||
shoe
|
||||
spoon
|
||||
table
|
||||
tree
|
||||
window
|
||||
23
README.md
23
README.md
|
|
@ -133,13 +133,18 @@ MemoryGame/
|
|||
|
||||
## Adding New Words/Images
|
||||
|
||||
The game dynamically loads images from the `wwwroot/images/` folder. To add new words and images:
|
||||
The game automatically discovers images from the `wwwroot/images/` folder. To add new words and images:
|
||||
|
||||
1. **Add your image**: Place an SVG image in `wwwroot/images/` with the English filename (e.g., `apple.svg`, `orange.svg`)
|
||||
2. **Update the manifest**: Add the filename (without extension) to `wwwroot/images-manifest.txt`:
|
||||
```
|
||||
apple
|
||||
orange
|
||||
2. **Add to discovery list**: Add the filename (without extension) to the `commonWords` array in `Services/GameService.cs`:
|
||||
```csharp
|
||||
var commonWords = new[]
|
||||
{
|
||||
"ball", "bed", "book", "car", "cat", "chair", "clock", "cup",
|
||||
"dog", "door", "fork", "hat", "house", "knife", "phone",
|
||||
"shoe", "spoon", "table", "tree", "window",
|
||||
"apple", "orange" // Add new words here
|
||||
};
|
||||
```
|
||||
3. **Add German translation** (optional): If you want a custom German translation, update the `ConvertToGermanWord` method in `Services/GameService.cs`:
|
||||
```csharp
|
||||
|
|
@ -149,13 +154,7 @@ The game dynamically loads images from the `wwwroot/images/` folder. To add new
|
|||
|
||||
If no translation is provided, the filename will be capitalized and used as-is.
|
||||
|
||||
4. **Regenerate manifest** (alternative): You can automatically regenerate the manifest by running:
|
||||
```bash
|
||||
cd MemoryGame/wwwroot/images
|
||||
ls *.svg | sed 's/\.svg$//' > ../images-manifest.txt
|
||||
```
|
||||
|
||||
The game will automatically load and use the new images in the next session!
|
||||
The game will automatically discover and load images that exist in the folder. No manifest file needed!
|
||||
|
||||
## Browser Compatibility
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue