Merge pull request #96 from progtime-net/data_save

Data save
This commit is contained in:
SergoDobro 2024-08-20 16:46:54 +03:00 committed by GitHub
commit f4a9140bd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers.ItemManager namespace ZoFo.GameCore.GameManagers.ItemManager
@ -15,7 +17,7 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
{ {
LoadPlayerData(); LoadPlayerData();
} }
public Dictionary<string, int> items; public Dictionary<string, int> items = new Dictionary<string, int>();
/// <summary> /// <summary>
/// Принимает тэг и крафтит этот объект /// Принимает тэг и крафтит этот объект
/// </summary> /// </summary>
@ -50,11 +52,44 @@ namespace ZoFo.GameCore.GameManagers.ItemManager
public void LoadPlayerData() public void LoadPlayerData()
{ {
//TODO if (File.Exists("Items.txt"))
items = new Dictionary<string, int>(); {
items.Add("wood", 5); string data;
items.Add("steel", 110); using (StreamReader reader = new StreamReader("Items.txt"))
items.Add("peeble", 6); {
data = reader.ReadToEnd();
}
List<PlayerItemsData> itemsDatas = JsonSerializer.Deserialize<List<PlayerItemsData>>(data);
foreach (var item in itemsDatas)
{
items.Add(item.Name, item.Count);
}
}
}
public void SavePlayerData()
{
var options = new JsonSerializerOptions { WriteIndented = true };
List<PlayerItemsData> playerItemsDatas = new List<PlayerItemsData>();
foreach (var item in items)
{
playerItemsDatas.Add(new PlayerItemsData { Name = item.Key, Count = item.Value });
}
string data = JsonSerializer.Serialize<List<PlayerItemsData>>(playerItemsDatas);
using (StreamWriter outputFile = new StreamWriter("Items.txt", new FileStreamOptions() { Mode = FileMode.Create, Access = FileAccess.Write } ))
{
outputFile.WriteLine(data);
}
} }
} }
class PlayerItemsData
{
public string Name { get; set; }
public int Count { get; set; }
}
} }