DutyContent/DcContent.cs

276 lines
5.8 KiB
C#
Raw Permalink Normal View History

2021-03-17 18:14:48 +01:00
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DutyContent
{
public class DcContent
{
// roulette
public class Roulette
{
public string Name { get; set; }
public static explicit operator Roulette(string name)
{
return new Roulette { Name = name };
}
}
// instance
public class Instance
{
public string Name { get; set; }
public static explicit operator Instance(string name)
{
return new Instance { Name = name };
}
}
// area
public class Area
{
public string Name { get; set; }
public IReadOnlyDictionary<int, Fate> Fates { get; set; }
}
// fate
public class Fate
{
public Area Area { get; set; }
public string Name { get; set; }
public static explicit operator Fate(string name)
{
return new Fate { Name = name };
}
}
// group
public class Group
{
public decimal Version { get; set; }
2021-03-17 18:14:48 +01:00
public string Language { get; set; }
public string DisplayLanguage { get; set; }
2021-03-17 18:14:48 +01:00
public Dictionary<int, Roulette> Roulettes { get; set; }
public Dictionary<int, Instance> Instances { get; set; }
public Dictionary<int, Area> Areas { get; set; }
}
//
public static decimal Version { get; private set; } = 0;
2021-03-17 18:14:48 +01:00
public static string Language { get; private set; }
public static string DisplayLanguage { get; private set; }
2021-03-17 18:14:48 +01:00
public static IReadOnlyDictionary<int, Roulette> Roulettes { get; private set; } = new Dictionary<int, Roulette>();
public static IReadOnlyDictionary<int, Instance> Instances { get; private set; } = new Dictionary<int, Instance>();
public static IReadOnlyDictionary<int, Area> Areas { get; private set; } = new Dictionary<int, Area>();
public static IReadOnlyDictionary<int, Fate> Fates { get; private set; } = new Dictionary<int, Fate>();
public static Dictionary<int, int> Missions { get; private set; } = new Dictionary<int, int>();
//
public static string GetInformation()
{
return Locale.Text(20,
Language,
Version,
Areas.Count,
Roulettes.Count,
Instances.Count,
Fates.Count);
}
2021-03-17 18:14:48 +01:00
//
public static bool Initialize(string json)
{
if (string.IsNullOrWhiteSpace(json))
return false;
bool ret;
try
{
ret = Fill(json);
}
catch
{
ret = false;
}
return ret;
}
// parse json
public static bool Fill(string json)
2021-03-17 18:14:48 +01:00
{
Group data = JsonConvert.DeserializeObject<Group>(json);
Dictionary<int, Fate> fates = new Dictionary<int, Fate>();
var version = data.Version;
var language = data.Language;
if (version > Version || language != Language)
2021-03-17 18:14:48 +01:00
{
foreach (var area in data.Areas)
2021-03-17 18:14:48 +01:00
{
foreach (var fate in area.Value.Fates)
2021-03-17 18:14:48 +01:00
{
try
{
fate.Value.Area = area.Value;
fates.Add(fate.Key, fate.Value);
}
catch (NullReferenceException /*nex*/)
{
Logger.E(7, fate.Key);
return false;
}
catch (Exception ex)
{
Logger.Ex(ex, 8);
return false;
}
2021-03-17 18:14:48 +01:00
}
}
Version = data.Version;
Language = data.Language;
DisplayLanguage = data.DisplayLanguage;
Roulettes = data.Roulettes;
Instances = data.Instances;
Areas = data.Areas;
Fates = fates;
}
2021-03-17 18:14:48 +01:00
return true;
}
//
public static Roulette TryRoulette(int code)
{
return Roulettes.TryGetValue(code, out Roulette roulette) ? roulette : null;
}
//
public static Instance TryInstance(int code)
{
return Instances.TryGetValue(code, out Instance instance) ? instance : null;
}
//
public static Area TryArea(int code)
{
return Areas.TryGetValue(code, out Area area) ? area : null;
}
//
public static Fate TryFate(int code)
{
return Fates.ContainsKey(code) ? Fates[code] : null;
}
//
public static Roulette GetRoulette(int code)
{
return Roulettes.TryGetValue(code, out Roulette roulette) ? roulette :
new Roulette { Name = Locale.Text(9, code) };
2021-03-17 18:14:48 +01:00
}
//
public static Instance GetInstance(int code)
{
return Instances.TryGetValue(code, out Instance instance) ? instance :
new Instance { Name = Locale.Text(10, code) };
2021-03-17 18:14:48 +01:00
}
//
public static Area GetArea(int code)
{
return Areas.TryGetValue(code, out Area area) ? area :
new Area { Name = Locale.Text(11, code) };
2021-03-17 18:14:48 +01:00
}
//
public static Fate GetFate(int code)
{
return Fates.ContainsKey(code) ? Fates[code] :
new Fate { Name = Locale.Text(12, code) };
2021-03-17 18:14:48 +01:00
}
//
public static bool ReadContent(string language = null)
{
if (language == null)
language = DcConfig.Duty.Language;
else if (!DcConfig.Duty.Language.Equals(language))
DcConfig.Duty.Language = language;
2021-06-14 11:45:49 +02:00
string filename = DcConfig.BuildDutyFileName(language);
2021-03-17 18:14:48 +01:00
if (!File.Exists(filename))
{
2021-06-14 11:45:49 +02:00
filename = DcConfig.BuildDutyFileName(language = "English");
2021-03-17 18:14:48 +01:00
if (!File.Exists(filename))
{
Logger.E(14, filename);
2021-03-17 18:14:48 +01:00
return false;
}
if (!DcConfig.Duty.Language.Equals(language))
DcConfig.Duty.Language = language;
}
string json = File.ReadAllText(filename, Encoding.UTF8);
if (Initialize(json))
2021-03-17 18:14:48 +01:00
{
Logger.Write(GetInformation());
return true;
2021-03-17 18:14:48 +01:00
}
else
{
Logger.E(13);
return false;
2021-03-17 18:14:48 +01:00
}
}
public static string CeStatusToString(int s)
{
// 10[1] status 0=end, 1=wait, 2=??, 3=progress
switch (s)
{
case 0: return Locale.Text(10017);
case 1: return Locale.Text(10018);
case 2: return Locale.Text(10019);
case 3: return Locale.Text(10020);
default: return Locale.Text(10021);
2021-03-17 18:14:48 +01:00
}
}
2021-05-26 12:39:25 +02:00
// save the queen type
public enum SaveTheQueenType
{
No,
Bozja,
Delubrum,
Zadnor,
}
//
public static int SaveTheQueenTypeToCeBase(SaveTheQueenType stq)
{
return
stq == SaveTheQueenType.Bozja ? 30000 :
stq == SaveTheQueenType.Zadnor ? 30100 :
30100; // fail safe
}
2021-03-17 18:14:48 +01:00
}
}