77 lines
1.6 KiB
C#
77 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Dakota.Logic
|
|
{
|
|
public class DakotaNumberManager
|
|
{
|
|
private readonly string _Filepath;
|
|
private readonly static object _LockObj = new object();
|
|
private Dictionary<string, Tuple<int, int>> _Numbers;
|
|
|
|
public DakotaNumberManager(string filePath)
|
|
{
|
|
_Filepath = filePath;
|
|
_Numbers = LoadNumbers();
|
|
}
|
|
|
|
private Dictionary<string, Tuple<int, int>> LoadNumbers()
|
|
{
|
|
if (!File.Exists(_Filepath))
|
|
{
|
|
return new Dictionary<string, Tuple<int, int>>
|
|
{
|
|
|
|
};
|
|
}
|
|
|
|
var json = File.ReadAllText(_Filepath);
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<string, Tuple<int, int>>>(json) ?? new Dictionary<string, Tuple<int, int>>
|
|
{
|
|
|
|
};
|
|
}
|
|
|
|
private void SafeNumbers()
|
|
{
|
|
var json = JsonSerializer.Serialize(_Numbers, new JsonSerializerOptions()
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
File.WriteAllText(_Filepath, json);
|
|
}
|
|
|
|
public Tuple<int, int> GetNextNumber(string datenannahmestelle, bool newDatenaustauschreferenz)
|
|
{
|
|
lock (_LockObj)
|
|
{
|
|
if (_Numbers.ContainsKey(datenannahmestelle))
|
|
{
|
|
var (datenaustauschreferenz, transfernummer) = _Numbers[datenannahmestelle];
|
|
|
|
if (newDatenaustauschreferenz)
|
|
datenaustauschreferenz++;
|
|
|
|
transfernummer++;
|
|
|
|
_Numbers[datenannahmestelle] = new Tuple<int, int>(datenaustauschreferenz, transfernummer);
|
|
}
|
|
else
|
|
{
|
|
_Numbers.Add(datenannahmestelle, new Tuple<int, int>(1, 0));
|
|
}
|
|
|
|
SafeNumbers();
|
|
|
|
return _Numbers[datenannahmestelle];
|
|
}
|
|
}
|
|
}
|
|
}
|