94 lines
4.2 KiB
C#
94 lines
4.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using Decentralized.Domain;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace Decentralized
|
||
{
|
||
/// <summary>上一晚的挖矿报告(终端"报告"页和晨间日志)。</summary>
|
||
public sealed class NightReport
|
||
{
|
||
public int Day;
|
||
public int Rigs, PoweredRigs;
|
||
public double Hash, NetworkHash, Mined, Price, Kj;
|
||
public List<string> Burnt = new List<string>();
|
||
}
|
||
|
||
/// <summary>一局的 mod 状态。整体序列化成 JSON 存进游戏的 mod 存档数据;显卡的健康度等存在显卡物品的标签上。</summary>
|
||
public sealed class SaveData
|
||
{
|
||
public int Version = 1;
|
||
public int Day;
|
||
public Rng Rng = new Rng((ulong)DateTime.UtcNow.Ticks);
|
||
public Market Market;
|
||
public Stablecoins Stable = new Stablecoins();
|
||
public Network Network = new Network();
|
||
public Wallet Hot = new Wallet { Address = "hot" };
|
||
public Exchange Exchange = new Exchange();
|
||
public Mixer Mixer = new Mixer();
|
||
public BlackMarket Black = new BlackMarket();
|
||
public List<MarketEvent> ActiveEvents = new List<MarketEvent>();
|
||
public NightReport LastNight;
|
||
public bool OpeningNewsShown;
|
||
/// <summary>各个收款方的地址(平台、混币器、Ponero 兑换处等),第一次用到时随机生成并存档。付款前要手输。</summary>
|
||
public Dictionary<string, string> Addresses = new Dictionary<string, string>();
|
||
|
||
public string Address(string key)
|
||
{
|
||
if (!Addresses.TryGetValue(key, out var a) || !ChainAddress.Valid(a)) { a = ChainAddress.Make(Rng.NextUInt()); Addresses[key] = a; }
|
||
return a;
|
||
}
|
||
/// <summary>链上的区块高度:每签一笔交易往上走几块。</summary>
|
||
public long Block = 1204331;
|
||
/// <summary>当天用币付款的成交额(信用点),夜间清零。</summary>
|
||
public double CoinSalesToday;
|
||
}
|
||
|
||
/// <summary>当前这一局的状态:读档 / 新游戏时载入,睡觉结算后写回。</summary>
|
||
public static class Session
|
||
{
|
||
private const string Key = "state";
|
||
public static SaveData Data;
|
||
|
||
/// <summary>今晚有电的矿架上的显卡(矿架在夜间早段扣电时登记,结算时用)。</summary>
|
||
public static readonly List<KeyValuePair<GameItem, ModuleBonus>> TonightCards = new List<KeyValuePair<GameItem, ModuleBonus>>();
|
||
public static int TonightRigs, TonightPoweredRigs;
|
||
public static double TonightKj;
|
||
|
||
public static void Load()
|
||
{
|
||
Data = null;
|
||
try
|
||
{
|
||
string json = ModHelper.GetModData(ModInfo.Id, Key);
|
||
if (!string.IsNullOrEmpty(json)) Data = JsonConvert.DeserializeObject<SaveData>(json);
|
||
}
|
||
catch (Exception ex) { Log.Error($"读取存档数据失败,重新开始:{ex.Message}"); }
|
||
if (Data == null) Data = new SaveData();
|
||
if (Data.Market == null) Data.Market = Market.CreateDefault(StoreStation.GetDayCounter());
|
||
// 旧存档里的商家地址没有校验位:换成合法地址
|
||
foreach (var s in Data.Black.Sellers) if (!ChainAddress.Valid(s.Name)) s.Name = ChainAddress.Make(Data.Rng.NextUInt());
|
||
Log.Info($"状态已载入(第 {Data.Day} 天,PSC {Data.Market.Price:0.00})");
|
||
}
|
||
|
||
public static void Save()
|
||
{
|
||
if (Data == null) return;
|
||
try { ModHelper.SetModData(ModInfo.Id, Key, JsonConvert.SerializeObject(Data)); }
|
||
catch (Exception ex) { Log.Error($"写入存档数据失败:{ex.Message}"); }
|
||
}
|
||
|
||
/// <summary>链上资产的信用点价格。</summary>
|
||
public static double Price(string asset)
|
||
{
|
||
if (asset == Assets.PSC) return Data.Market.Price;
|
||
var spec = Assets.Get(asset);
|
||
if (asset == Assets.Ponero) return BlackMarket.PoneroPrice(Data.Market.Price);
|
||
if (spec != null && spec.Stable) return Data.Stable.Price(asset);
|
||
return 0;
|
||
}
|
||
|
||
public static double Rent() => PlayerStore.Instance != null ? PlayerStore.Instance.rentValue : 200;
|
||
}
|
||
}
|