Add user database with stats

This commit is contained in:
Masahiko AMANO 2022-03-03 17:52:34 +03:00
parent d74b7a9413
commit d565970903
2 changed files with 78 additions and 9 deletions

44
Database.cs Normal file
View File

@ -0,0 +1,44 @@
using System.Text.Json;
namespace MineSweeper
{
internal class Database
{
private readonly string path;
private class PlayerData
{
public string name { get; set; }
public int wins { get; set; } = 0;
public int loses { get; set; } = 0;
}
private record Root(List<PlayerData> players);
private Root root;
public Database()
{
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MineSweeper by H1K0");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(path, "db.json");
if (!File.Exists(path))
File.WriteAllText(path, "{\"players\":[]}");
root = JsonSerializer.Deserialize<Root>(File.ReadAllText(path));
}
public int PlayersCount() { return root.players.Count; }
public int FindPlayer(string name) { return root.players.FindIndex(p => p.name == name); }
public void AddPlayer(string name)
{
PlayerData newplayer = new();
newplayer.name = name;
root.players.Add(newplayer);
}
public int[] Stats(int playerId) { return new int[] { root.players[playerId].wins, root.players[playerId].loses }; }
public void AddGame(int playerId, bool win)
{
if (win)
root.players[playerId].wins++;
else
root.players[playerId].loses++;
}
public void Update() { File.WriteAllText(path, JsonSerializer.Serialize(root)); }
}
}

43
Main.cs
View File

@ -5,17 +5,40 @@
public static void Main(string[] args)
{
Game game;
Database database = new();
int PlayerId;
string PlayerName = "";
Console.WriteLine("(C) Masahiko AMANO a.k.a. H1K0, 2022\n\n" +
"Hey! Let's play the MineSweeper game!\nPress any button for help or Enter to log in.");
while (Console.ReadKey(true).Key != ConsoleKey.Enter)
Console.WriteLine("\nThe game follows the classic rules.\n" +
"Target cell coordinates are represented this way: A1, F5, I8. Case insensitive.\n" +
"Type coordinates of the cell to open it or use prefixes: \"M\" to mark or \"U\" to unmark the cell.\n" +
"For example: \"B9\", \"M D7\".\n" +
"Press any button to see help again or just press Enter to start the game.");
while (true)
{
Console.WriteLine("(C) Masahiko AMANO a.k.a. H1K0, 2022\n\n" +
"Hey! Let's play the MineSweeper game!\nType anything for help or just press Enter to start the game.");
while (Console.ReadLine().Length != 0)
Console.WriteLine("\nThe game follows the classic rules.\n" +
"Target cell coordinates are represented this way: A1, F5, I8. Case insensitive.\n" +
"Type coordinates of the cell to open it or use prefixes: \"M\" to mark or \"U\" to unmark the cell.\n" +
"For example: \"B9\", \"M D7\".\n" +
"Type anything to see help again or just press Enter to start the game.\n");
Console.Write("Okay, let's go!\nEnter field width, height and number of mines separated with a space: ");
Console.Write("\nYour name: ");
PlayerName = Console.ReadLine();
PlayerId = database.FindPlayer(PlayerName);
if (PlayerId == -1 || PlayerName == "")
{
Console.WriteLine("Could not find a player with the name \"" + PlayerName + "\". Press Enter to add new or any other button to re-enter.");
if (Console.ReadKey(true).Key == ConsoleKey.Enter)
{
database.AddPlayer(PlayerName);
PlayerId = database.PlayersCount() - 1;
break;
}
}
else
break;
}
Console.WriteLine("You logged in as \"" + PlayerName + "\".\nYour stats:");
Console.WriteLine($"Wins: {database.Stats(PlayerId)[0]}\nLoses: {database.Stats(PlayerId)[1]}");
while (true)
{
Console.Write("\nOkay, let's play!\nEnter field width, height and number of mines separated with a space: ");
while (true)
{
try
@ -35,6 +58,8 @@
Console.WriteLine("You win! Congratulations!");
else
Console.WriteLine("You lose!");
database.AddGame(PlayerId, result);
database.Update();
Console.WriteLine("Press Enter to play again or Escape to exit.");
ConsoleKey key;
bool exit = false;