2021-08-12 16:52:06 +01:00
|
|
|
|
using Generator.Models.Output;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
using Generator.Models.Output.Difficulty;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
|
|
|
|
namespace Generator.Helpers
|
|
|
|
|
{
|
|
|
|
|
public static class DifficultyHelper
|
|
|
|
|
{
|
2021-08-14 09:44:45 +01:00
|
|
|
|
private static readonly string[] _difficulties = new[] { "easy", "normal", "hard", "impossible" };
|
|
|
|
|
|
2021-09-05 12:27:17 +01:00
|
|
|
|
public static void AddDifficultySettings(Bot botToUpdate, List<string> difficultyFilePaths)
|
2021-08-12 16:52:06 +01:00
|
|
|
|
{
|
2021-08-14 09:44:45 +01:00
|
|
|
|
// Read bot setting files from assets folder that match this bots type
|
|
|
|
|
// Save into dictionary with difficulty as key
|
|
|
|
|
var difficultySettingsJsons = new Dictionary<string, DifficultySettings>();
|
2021-09-05 12:27:17 +01:00
|
|
|
|
foreach (var path in difficultyFilePaths.Where(x=>x.Contains($"_{botToUpdate.botType}", System.StringComparison.InvariantCultureIgnoreCase)))
|
2021-08-12 16:52:06 +01:00
|
|
|
|
{
|
2021-08-14 09:44:45 +01:00
|
|
|
|
var json = File.ReadAllText(path);
|
|
|
|
|
var serialisedObject = JsonConvert.DeserializeObject<DifficultySettings>(json);
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
2021-08-14 09:44:45 +01:00
|
|
|
|
var difficultyOfFile = GetFileDifficultyFromPath(path);
|
|
|
|
|
difficultySettingsJsons.Add(difficultyOfFile, serialisedObject);
|
|
|
|
|
}
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
2021-08-14 09:44:45 +01:00
|
|
|
|
// Find each difficulty in dictionary and save into bot
|
|
|
|
|
foreach (var difficulty in _difficulties)
|
2021-08-12 16:52:06 +01:00
|
|
|
|
{
|
2021-08-14 09:44:45 +01:00
|
|
|
|
var settings = difficultySettingsJsons.FirstOrDefault(x => x.Key.Contains(difficulty));
|
|
|
|
|
|
2021-08-17 15:41:35 +01:00
|
|
|
|
// No difficulty settings found, find any settings file and use that
|
|
|
|
|
// This is required for many bot types that only have 'normal' difficulty settings
|
|
|
|
|
if (settings.Key == null)
|
|
|
|
|
{
|
|
|
|
|
settings = difficultySettingsJsons.FirstOrDefault(x => x.Key != null);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 12:27:17 +01:00
|
|
|
|
SaveSettingsIntoBotFile(botToUpdate, difficulty, settings.Value);
|
2021-08-14 09:44:45 +01:00
|
|
|
|
}
|
2021-08-12 16:52:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 09:44:45 +01:00
|
|
|
|
private static string GetFileDifficultyFromPath(string path)
|
|
|
|
|
{
|
|
|
|
|
// Split path into parts and find the last part (filename)
|
|
|
|
|
// Split filename and take the first part (difficulty, easy/normal etc)
|
|
|
|
|
var splitPath = path.Split("\\");
|
2021-09-01 14:41:21 +01:00
|
|
|
|
return splitPath.Last().Split("_")[0];
|
2021-08-14 09:44:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 12:27:17 +01:00
|
|
|
|
private static void SaveSettingsIntoBotFile(Bot botToUpdate, string difficulty, DifficultySettings settings)
|
2021-08-14 09:44:45 +01:00
|
|
|
|
{
|
|
|
|
|
switch (difficulty)
|
|
|
|
|
{
|
|
|
|
|
case "easy":
|
2021-09-05 12:27:17 +01:00
|
|
|
|
botToUpdate.difficulty.easy = settings;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
break;
|
|
|
|
|
case "normal":
|
2021-09-05 12:27:17 +01:00
|
|
|
|
botToUpdate.difficulty.normal = settings;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
break;
|
|
|
|
|
case "hard":
|
2021-09-05 12:27:17 +01:00
|
|
|
|
botToUpdate.difficulty.hard = settings;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
break;
|
|
|
|
|
case "impossible":
|
2021-09-05 12:27:17 +01:00
|
|
|
|
botToUpdate.difficulty.impossible = settings;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-12 16:52:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|