ForgeCore/Charts/ChartMemory.cs
2020-09-04 13:24:43 -07:00

163 lines
4.2 KiB
C#

using Newtonsoft.Json;
using OpenMetaverse;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
namespace Bot.Charts
{
public sealed class ChartMemory
{
private static readonly object lck = new object();
private static ChartMemory a;
static ChartMemory() { }
public static ChartMemory Instance
{
get
{
if (a != null) return a;
else
{
lock (lck)
{
if (a == null) a = LoadFromFile();
return a;
}
}
}
}
private static readonly object FileHandler = new object();
private static ChartMemory LoadFromFile()
{
lock (FileHandler)
{
if (File.Exists("Charts.json")) File.Move("Charts.json", Path.Combine("BotData", "Charts.json"));
// Read json file
if (!File.Exists("BotData/Charts.json")) return new ChartMemory();
return JsonConvert.DeserializeObject<ChartMemory>(File.ReadAllText("BotData/Charts.json"));
}
}
public static void Reload()
{
lock (lck)
{
a = LoadFromFile();
}
}
public static void SaveToFile()
{
lock (FileHandler)
{
File.WriteAllText("BotData/Charts.json", JsonConvert.SerializeObject(Instance, Formatting.Indented));
}
}
public List<Chart> Charts { get; set; } = new List<Chart>();
public static bool HasChart(string ChartName)
{
if (Instance.Charts.Where(x => x.ChartName == ChartName).Count() > 0) return true;
else return false;
}
public static bool HasChartID(string ID)
{
if (Instance.Charts.Where(x => x.ChartID.ToString() == ID).Count() > 0) return true;
else return false;
}
public static Chart GetNamedChart(string chartName)
{
return Instance.Charts.Where(x => x.ChartName == chartName).First();
}
public static Chart GetChartByID(string ID)
{
return Instance.Charts.Where(x => x.ChartID.ToString() == ID).First();
}
}
/// <summary>
/// A chart itself
/// </summary>
public class Chart
{
public string ChartName;
public ChartColumn ColumnData;
public List<ChartRow> RowData;
public UUID ChartID;
public string ChartDescription = "";
public Chart(string ChartNm)
{
ChartName = ChartNm;
ColumnData = new ChartColumn();
RowData = new List<ChartRow>();
ChartID = UUID.Random();
ChartDescription = "A chart generated by the BotCore5 system";
}
}
/// <summary>
/// Field data for column
/// </summary>
public class ChartColumnField
{
public string Label;
public string ColorCode;
}
/// <summary>
/// The column itself
/// </summary>
public class ChartColumn
{
public List<ChartColumnField> Fields;
public int Bit2Pos(int bit)
{
return Convert.ToInt32(Math.Round(Math.Log10(Convert.ToDouble(bit))));
}
public int Field2Bit(string FieldLabel)
{
if (Fields.Select(x => x.Label == FieldLabel).Count() > 0)
{
return Convert.ToInt32(Math.Pow(2,Fields.IndexOf(Fields.Where(x => x.Label == FieldLabel).First())));
}
else
{
return 0;
}
}
public int Pos2Bit(int pos)
{
return Convert.ToInt32(Math.Pow(2, Convert.ToDouble(pos)));
}
public string ColorForBit(int BitMask)
{
int posInList = Bit2Pos(BitMask);
return Fields[posInList].ColorCode;
}
}
/// <summary>
/// A row occupying all columns
/// </summary>
public class ChartRow
{
public string Label;
public int Mask;
}
}