| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- // See https://aka.ms/new-console-template for more information
- using Domain;
- using Infrastructure;
- using System.ComponentModel;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- Console.WriteLine("Hello, World!");
- Player perso = new Player();
- var fileName = $"C:/newCharacter.json";
- while (true)
- {
- string? methodToExec = Console.ReadLine();
- if (methodToExec != null)
- {
- switch (methodToExec.ToLower())
- {
- case "roll":
- Console.WriteLine(RollInitialStat());
- break;
- case "createperso":
- string jsonPerso = "";
- object createPerso = await CreatePerso(perso, fileName, jsonPerso);
- Console.Write(createPerso);
- break;
- }
- }
- }
- static int RollInitialStat()
- {
- var rollValue = DiceService.RollDices(1, 20);
- rollValue += 30;
- return rollValue;
- }
- static async Task<string> CreatePerso(Player perso, string fileName, string jsonPerso)
- {
- perso = InitRandomStat();
- jsonPerso = JsonSerializer.Serialize<Player>(perso);
- //if (File.Exists(fileName))
- //{
- // if (IsFileLocked(fileName))
- // {
- // Thread.Sleep(1000);
- // }
- // File.Delete(fileName);
- //}
- //if (IsFileLocked(fileName))
- //{
- // Thread.Sleep(1000);
- //}
- //if (!File.Exists(fileName))
- // File.Create(fileName);
- //await File.WriteAllTextAsync(fileName, jsonPerso);
- return jsonPerso;
- }
- static Player InitRandomStat()
- {
- return new Player
- {
- AG = RollInitialStat(),
- CC = RollInitialStat(),
- CT = RollInitialStat(),
- E = RollInitialStat(),
- F = RollInitialStat(),
- FM = RollInitialStat(),
- INT = RollInitialStat(),
- MF = RollInitialStat(),
- PER = RollInitialStat(),
- SOC = RollInitialStat()
- };
- }
- static bool IsFileLocked(string filePath)
- {
- try
- {
- using (File.Open(filePath, FileMode.Open))
- {
- return true;
- }
- }
- catch (IOException e)
- {
- Console.WriteLine("Exception occured while deleting the file, Exception is {e}", e);
- }
- return false;
- }
|