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 = [];
|
_availableWords = [];
|
||||||
|
|
||||||
try
|
var commonWords = new[]
|
||||||
{
|
{
|
||||||
var manifest = await _httpClient.GetStringAsync("images-manifest.txt");
|
"ball", "bed", "book", "car", "cat", "chair", "clock", "cup",
|
||||||
var imageNames = manifest.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
"dog", "door", "fork", "hat", "house", "knife", "phone",
|
||||||
|
"shoe", "spoon", "table", "tree", "window"
|
||||||
foreach (var imageName in imageNames)
|
};
|
||||||
|
|
||||||
|
var loadTasks = commonWords.Select(async word =>
|
||||||
|
{
|
||||||
|
var imagePath = $"images/{word}.svg";
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var germanWord = ConvertToGermanWord(imageName);
|
var response = await _httpClient.GetAsync(imagePath, HttpCompletionOption.ResponseHeadersRead);
|
||||||
_availableWords.Add(new GameWord
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
Word = germanWord,
|
var germanWord = ConvertToGermanWord(word);
|
||||||
ImagePath = $"images/{imageName}.svg",
|
return new GameWord
|
||||||
CorrectCount = 0,
|
{
|
||||||
IncorrectCount = 0
|
Word = germanWord,
|
||||||
});
|
ImagePath = imagePath,
|
||||||
|
CorrectCount = 0,
|
||||||
|
IncorrectCount = 0
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
catch
|
||||||
catch
|
{
|
||||||
{
|
}
|
||||||
_availableWords = [];
|
return null;
|
||||||
}
|
});
|
||||||
|
|
||||||
|
var results = await Task.WhenAll(loadTasks);
|
||||||
|
_availableWords = results.Where(w => w != null).Cast<GameWord>().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ConvertToGermanWord(string englishWord)
|
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
|
## 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`)
|
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`:
|
2. **Add to discovery list**: Add the filename (without extension) to the `commonWords` array in `Services/GameService.cs`:
|
||||||
```
|
```csharp
|
||||||
apple
|
var commonWords = new[]
|
||||||
orange
|
{
|
||||||
|
"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`:
|
3. **Add German translation** (optional): If you want a custom German translation, update the `ConvertToGermanWord` method in `Services/GameService.cs`:
|
||||||
```csharp
|
```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.
|
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:
|
The game will automatically discover and load images that exist in the folder. No manifest file needed!
|
||||||
```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!
|
|
||||||
|
|
||||||
## Browser Compatibility
|
## Browser Compatibility
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue