1
0
mirror of synced 2024-11-28 17:31:00 +01:00
OpenTaiko/TJAPlayer3/Common/SaveFile.cs

215 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace TJAPlayer3
{
internal class SaveFile
{
public void tSaveFile(string filename)
{
path = @"Saves/" + filename + @".json";
name = filename;
if (!File.Exists(path))
{
this.data.Name = filename;
tSaveFile();
}
tLoadFile();
}
#region [Medals]
public void tEarnCoins(int amount)
{
data.Medals += amount;
tSaveFile();
}
// Return false if the current amount of coins is to low
public bool tSpendCoins(int amount)
{
if (data.Medals < amount)
return false;
data.Medals -= amount;
tSaveFile();
return true;
}
#endregion
#region [Dan titles]
public bool tUpdateDanTitle(string title, bool isGold, int clearStatus)
{
bool changed = false;
bool iG = isGold;
int cs = clearStatus;
if (this.data.DanTitles == null)
this.data.DanTitles = new Dictionary<string, CDanTitle>();
if (this.data.DanTitles.ContainsKey(title))
{
if (this.data.DanTitles[title].clearStatus > cs)
cs = this.data.DanTitles[title].clearStatus;
if (this.data.DanTitles[title].isGold)
iG = true;
}
// Automatically set the dan to nameplate if new
// Add a function within the NamePlate.cs file to update the title texture
if (!this.data.DanTitles.ContainsKey(title) || cs != clearStatus || iG != isGold)
{
changed = true;
/*
TJAPlayer3.NamePlateConfig.data.Dan[player] = title;
TJAPlayer3.NamePlateConfig.data.DanGold[player] = iG;
TJAPlayer3.NamePlateConfig.data.DanType[player] = cs;
*/
}
CDanTitle danTitle = new CDanTitle(iG, cs);
this.data.DanTitles[title] = danTitle;
tSaveFile();
return changed;
}
#endregion
#region [Auxilliary classes]
public class CDanTitle
{
public CDanTitle(bool iG, int cs)
{
isGold = iG;
clearStatus = cs;
}
[JsonProperty("isGold")]
public bool isGold;
[JsonProperty("clearStatus")]
public int clearStatus;
}
public class CNamePlateTitle
{
public CNamePlateTitle(int type)
{
iType = type;
}
[JsonProperty("iType")]
public int iType;
}
#endregion
#region [Heya]
public void tReindexCharacter(string[] characterNamesList)
{
string character = this.data.CharacterName;
if (characterNamesList.Contains(character))
this.data.Character = characterNamesList.ToList().IndexOf(character);
}
public void tUpdateCharacterName(string newChara)
{
this.data.CharacterName = newChara;
}
public void tApplyHeyaChanges()
{
this.tSaveFile();
}
#endregion
public class Data
{
[JsonProperty("name")]
public string Name = "プレイヤー1";
[JsonProperty("title")]
public string Title = "初心者";
[JsonProperty("dan")]
public string Dan = "新人";
[JsonProperty("danGold")]
public bool DanGold = false;
[JsonProperty("danType")]
public int DanType = 0;
[JsonProperty("titleType")]
public int TitleType = 0;
[JsonProperty("puchiChara")]
public string PuchiChara = "0";
[JsonProperty("medals")]
public int Medals = 0;
[JsonProperty("character")]
public int Character = 0;
[JsonProperty("characterName")]
public string CharacterName = "0";
[JsonProperty("danTitles")]
public Dictionary<string, CDanTitle> DanTitles = new Dictionary<string, CDanTitle>();
[JsonProperty("namePlateTitles")]
public Dictionary<string, CNamePlateTitle> NamePlateTitles = new Dictionary<string, CNamePlateTitle>();
[JsonProperty("unlockedCharacters")]
public List<string> UnlockedCharacters = new List<string>();
[JsonProperty("unlockedPuchicharas")]
public List<string> UnlockedPuchicharas = new List<string>();
}
public Data data = new Data();
public string path = "Save.json";
public string name = "Save";
#region [private]
private void tSaveFile()
{
ConfigManager.SaveConfig(data, path);
}
private void tLoadFile()
{
data = ConfigManager.GetConfig<Data>(path);
}
#endregion
}
}