2021-09-18 22:36:59 +01:00
|
|
|
|
using Common.Models.Output;
|
|
|
|
|
using Common.Models.Output.Difficulty;
|
2021-08-14 09:44:45 +01:00
|
|
|
|
using Newtonsoft.Json;
|
2022-07-04 21:52:42 +01:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.Collections;
|
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>();
|
2022-01-07 10:32:59 +00:00
|
|
|
|
var botType = botToUpdate.botType.ToString();
|
|
|
|
|
var pathsWithBotType = difficultyFilePaths.Where(x => x.Contains($"_{botType}", StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
foreach (var path in pathsWithBotType)
|
2021-08-12 16:52:06 +01:00
|
|
|
|
{
|
2021-08-14 09:44:45 +01:00
|
|
|
|
var json = File.ReadAllText(path);
|
2022-01-07 10:32:59 +00:00
|
|
|
|
var serialisedDifficultySettings = JsonConvert.DeserializeObject<DifficultySettings>(json);
|
|
|
|
|
|
|
|
|
|
serialisedDifficultySettings = ApplyCustomDifficultyValues(botType, serialisedDifficultySettings);
|
2021-08-12 16:52:06 +01:00
|
|
|
|
|
2021-08-14 09:44:45 +01:00
|
|
|
|
var difficultyOfFile = GetFileDifficultyFromPath(path);
|
2022-01-07 10:32:59 +00:00
|
|
|
|
difficultySettingsJsons.Add(difficultyOfFile, serialisedDifficultySettings);
|
2021-08-14 09:44:45 +01:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 21:52:42 +01:00
|
|
|
|
var warnKey = "WARN_BOT_TYPES";
|
2022-07-04 22:23:16 +01:00
|
|
|
|
if (settings.Value.Mind.ContainsKey(warnKey))
|
2022-07-04 21:52:42 +01:00
|
|
|
|
{
|
|
|
|
|
var deserialisedArray = getDeserializedStringArray(settings, warnKey);
|
|
|
|
|
if (deserialisedArray.Length> 0)
|
|
|
|
|
{
|
|
|
|
|
settings.Value.Mind[warnKey] = deserialisedArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var enemyKey = "ENEMY_BOT_TYPES";
|
|
|
|
|
if (settings.Value.Mind.ContainsKey(enemyKey))
|
|
|
|
|
{
|
|
|
|
|
var deserialisedArray = getDeserializedStringArray(settings, enemyKey);
|
|
|
|
|
if (deserialisedArray.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
settings.Value.Mind[enemyKey] = deserialisedArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var friendlyKey = "FRIENDLY_BOT_TYPES";
|
|
|
|
|
if (settings.Value.Mind.ContainsKey(friendlyKey))
|
|
|
|
|
{
|
|
|
|
|
var deserialisedArray = getDeserializedStringArray(settings, friendlyKey);
|
|
|
|
|
if (deserialisedArray.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
settings.Value.Mind[friendlyKey] = deserialisedArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 22:23:16 +01:00
|
|
|
|
var revengeKey = "REVENGE_BOT_TYPES";
|
|
|
|
|
if (settings.Value.Mind.ContainsKey(revengeKey))
|
|
|
|
|
{
|
|
|
|
|
var deserialisedArray = getDeserializedStringArray(settings, revengeKey);
|
|
|
|
|
if (deserialisedArray.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
settings.Value.Mind[revengeKey] = deserialisedArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 21:52:42 +01:00
|
|
|
|
private static string[] getDeserializedStringArray(KeyValuePair<string, DifficultySettings> settings, string friendlyKey)
|
|
|
|
|
{
|
|
|
|
|
var serialisedArray = JsonConvert.SerializeObject(settings.Value.Mind[friendlyKey]);
|
|
|
|
|
return JsonConvert.DeserializeObject<string[]>(serialisedArray);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-07 10:32:59 +00:00
|
|
|
|
private static DifficultySettings ApplyCustomDifficultyValues(string botType, DifficultySettings difficultySettings)
|
|
|
|
|
{
|
|
|
|
|
switch (botType)
|
|
|
|
|
{
|
|
|
|
|
// make all bosses fight PMCs
|
|
|
|
|
case "bosskilla":
|
|
|
|
|
case "bossgluhar":
|
|
|
|
|
case "bosstagilla":
|
|
|
|
|
case "bossbully":
|
|
|
|
|
case "bosskojaniy":
|
|
|
|
|
AddHostileToPMCSettings(difficultySettings);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return difficultySettings;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 10:34:10 +00:00
|
|
|
|
private static void AddHostileToPMCSettings(DifficultySettings difficultySettings)
|
2022-01-07 10:32:59 +00:00
|
|
|
|
{
|
|
|
|
|
var defaultEnemyUsecKey = "DEFAULT_ENEMY_USEC";
|
2022-01-07 10:34:10 +00:00
|
|
|
|
if (difficultySettings.Mind.ContainsKey(defaultEnemyUsecKey))
|
2022-01-07 10:32:59 +00:00
|
|
|
|
{
|
2022-01-07 10:34:10 +00:00
|
|
|
|
difficultySettings.Mind[defaultEnemyUsecKey] = true;
|
2022-01-07 10:32:59 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-07 10:34:10 +00:00
|
|
|
|
difficultySettings.Mind.Add(defaultEnemyUsecKey, true);
|
2022-01-07 10:32:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defaultEnemyBearKey = "DEFAULT_ENEMY_BEAR";
|
2022-01-07 10:34:10 +00:00
|
|
|
|
if (difficultySettings.Mind.ContainsKey(defaultEnemyUsecKey))
|
2022-01-07 10:32:59 +00:00
|
|
|
|
{
|
2022-01-07 10:34:10 +00:00
|
|
|
|
difficultySettings.Mind[defaultEnemyBearKey] = true;
|
2022-01-07 10:32:59 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-07 10:34:10 +00:00
|
|
|
|
difficultySettings.Mind.Add(defaultEnemyBearKey, true);
|
2022-01-07 10:32:59 +00: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
|
|
|
|
}
|
|
|
|
|
}
|