Program.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // See https://aka.ms/new-console-template for more information
  2. using Domain;
  3. using Infrastructure;
  4. using System.ComponentModel;
  5. using System.Text.Json;
  6. using System.Text.Json.Serialization;
  7. Console.WriteLine("Hello, World!");
  8. Player perso = new Player();
  9. var fileName = $"C:/newCharacter.json";
  10. const string roll = "roll";
  11. const string createperso = "createperso";
  12. while (true)
  13. {
  14. string? methodToExec = Console.ReadLine();
  15. if (methodToExec != null)
  16. {
  17. switch (methodToExec.ToLower())
  18. {
  19. case roll:
  20. Console.WriteLine(DiceService.RollDices(1, 100));
  21. break;
  22. case createperso:
  23. string jsonPerso = "";
  24. object createPerso = await CreatePerso(perso, fileName, jsonPerso);
  25. Console.Write(createPerso);
  26. break;
  27. }
  28. }
  29. }
  30. static int RollInitialStat()
  31. {
  32. var rollValue = DiceService.RollDices(1, 10);
  33. rollValue += DiceService.RollDices(1, 10);
  34. rollValue += 30;
  35. return rollValue;
  36. }
  37. static async Task<string> CreatePerso(Player perso, string fileName, string jsonPerso)
  38. {
  39. perso = InitRandomStat();
  40. jsonPerso = JsonSerializer.Serialize<Player>(perso);
  41. //if (File.Exists(fileName))
  42. //{
  43. // if (IsFileLocked(fileName))
  44. // {
  45. // Thread.Sleep(1000);
  46. // }
  47. // File.Delete(fileName);
  48. //}
  49. //if (IsFileLocked(fileName))
  50. //{
  51. // Thread.Sleep(1000);
  52. //}
  53. //if (!File.Exists(fileName))
  54. // File.Create(fileName);
  55. //await File.WriteAllTextAsync(fileName, jsonPerso);
  56. return jsonPerso;
  57. }
  58. static Player InitRandomStat()
  59. {
  60. return new Player
  61. {
  62. AG = RollInitialStat(),
  63. CC = RollInitialStat(),
  64. CT = RollInitialStat(),
  65. E = RollInitialStat(),
  66. F = RollInitialStat(),
  67. FM = RollInitialStat(),
  68. INT = RollInitialStat(),
  69. PER = RollInitialStat(),
  70. SOC = RollInitialStat()
  71. };
  72. }
  73. static bool IsFileLocked(string filePath)
  74. {
  75. try
  76. {
  77. using (File.Open(filePath, FileMode.Open))
  78. {
  79. return true;
  80. }
  81. }
  82. catch (IOException e)
  83. {
  84. Console.WriteLine("Exception occured while deleting the file, Exception is {e}", e);
  85. }
  86. return false;
  87. }