Skip to content

Commit

Permalink
Merge pull request #6 from Funny-ppt/dev
Browse files Browse the repository at this point in the history
阶段性更新:后端数据切换到整形、性能优化、bug修复
  • Loading branch information
Funny-ppt authored Mar 29, 2024
2 parents d407cb5 + 5317ec3 commit 04d94d1
Show file tree
Hide file tree
Showing 189 changed files with 1,699 additions and 914 deletions.
106 changes: 49 additions & 57 deletions InfrastSim/AggregateValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@

namespace InfrastSim;

public class AggregateValue : IJsonSerializable {
double _baseValue;
double _value = new();
readonly Dictionary<string, double> _additionValues = new();
readonly HashSet<string> _disables = new();
public class AggregateValue(int baseValue = 0, int min = int.MinValue, int max = int.MaxValue, bool displayAsPercentage = false) : IJsonSerializable {
int _baseValue = baseValue;
int _value = 0;
readonly Dictionary<string, int> _additionValues = [];
readonly HashSet<string> _disables = [];
bool _upToDate = false;

public AggregateValue(double baseValue = 0.0, double min = double.MinValue, double max = double.MaxValue) {
_baseValue = baseValue;
MinValue = min;
MaxValue = max;
}

public bool UpToDate {
get => _upToDate;
}
public double CalculatedValue {
public int CalculatedValue {
get {
if (_upToDate) return _value;
_upToDate = true;
Expand All @@ -28,88 +19,89 @@ public double CalculatedValue {
return _value = Math.Clamp(_baseValue + additions, MinValue, MaxValue);
}
}
public double BaseValue => _baseValue;
public double MinValue { get; set; }
public double MaxValue { get; set; }
public int BaseValue => _baseValue;
public int MinValue { get; set; } = min;
public int MaxValue { get; set; } = max;

public IEnumerable<KeyValuePair<string, double>> EnumerateValues() {
public IEnumerable<KeyValuePair<string, int>> EnumerateValues() {
return _additionValues
.ExceptBy(_disables, p => p.Key);
}

public double GetValue(string name)
=> _additionValues.GetValueOrDefault(name);
public double SetValue(string name, double value) {
public int GetValue(string name) => _additionValues.GetValueOrDefault(name);
public int SetValue(string name, int value) {
var oldValue = GetValue(name);
if (Util.Equals(value, oldValue)) {
if (value == oldValue)
return oldValue;
}
if (!_disables.Contains(name)) _upToDate = false;
if (!_disables.Contains(name))
_upToDate = false;
return _additionValues[name] = value;
}
public double AddValue(string name, double value) {
public int AddValue(string name, int value) {
return SetValue(name, value + GetValue(name));
}
public double SetIfGreater(string name, double value) {
public int SetIfGreater(string name, int value) {
return SetValue(name, Math.Max(value, GetValue(name)));
}
public double SetIfLesser(string name, double value) {
public int SetIfLesser(string name, int value) {
return SetValue(name, Math.Min(value, GetValue(name)));
}
public double SetValue(NamedValue namedValue) {
return SetValue(namedValue.Name, namedValue.Value);
}
public double AddValue(NamedValue namedValue) {
return AddValue(namedValue.Name, namedValue.Value);
}
public double SetIfGreater(NamedValue namedValue) {
return SetIfGreater(namedValue.Name, namedValue.Value);
}
public double SetIfLesser(NamedValue namedValue) {
return SetIfLesser(namedValue.Name, namedValue.Value);
}
public int SetValue(int value) => SetValue("common", value);
public int AddValue(int value) => AddValue("common", value);
public int SetIfGreater(int value) => SetIfGreater("common", value);
public int SetIfLesser(int value) => SetIfLesser("common", value);

public void Disable(string name) {
if (!_disables.Contains(name)) {
if (!Util.Equals(GetValue(name), 0)) _upToDate = false;
_disables.Add(name);
if (_disables.Add(name)) {
if (GetValue(name) != 0) _upToDate = false;
}
}
public void Enable(string name) {
if (_disables.Contains(name)) {
if (!Util.Equals(GetValue(name), 0)) _upToDate = false;
_disables.Remove(name);
if (_disables.Remove(name)) {
if (GetValue(name) != 0) _upToDate = false;
}
}

public void Remove(string name) {
if (!Util.Equals(GetValue(name), 0)) {
if (GetValue(name) != 0) {
_upToDate = false;
}
_additionValues.Remove(name);
}
public void Clear() {
_disables.Clear();
foreach (var key in _additionValues.Keys) {
_additionValues[key] = 0;
}
// BENCHMARK REQUIRED: use Clear() or manually set value to 0?
_additionValues.Clear();
//foreach (var key in _additionValues.Keys) {
// _additionValues[key] = 0;
//}
_upToDate = true;
_value = _baseValue;
}

public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
writer.WriteStartObject();
writer.WriteNumber("value", CalculatedValue);
writer.WriteNumber("base-value", BaseValue);
writer.WriteNumber("min-value", MinValue);
writer.WriteNumber("max-value", MaxValue);
if (displayAsPercentage) {
writer.WriteNumber("value", CalculatedValue / 100.0);
writer.WriteNumber("base-value", BaseValue / 100.0);
writer.WriteNumber("min-value", MinValue / 100.0);
writer.WriteNumber("max-value", MaxValue / 100.0);
} else {
writer.WriteNumber("value", CalculatedValue);
writer.WriteNumber("base-value", BaseValue);
writer.WriteNumber("min-value", MinValue);
writer.WriteNumber("max-value", MaxValue);
}

writer.WritePropertyName("details");
writer.WriteStartArray();
foreach (var kvp in _additionValues) {
writer.WriteStartObject();
writer.WriteString("tag", kvp.Key);
writer.WriteNumber("value", kvp.Value);
if (displayAsPercentage) {
writer.WriteNumber("value", kvp.Value / 100.0);
} else {
writer.WriteNumber("value", kvp.Value);
}
writer.WriteBoolean("disabled", _disables.Contains(kvp.Key));
writer.WriteEndObject();
}
Expand All @@ -118,7 +110,7 @@ public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
writer.WriteEndObject();
}

public static implicit operator double(AggregateValue aggregateValue) {
public static implicit operator int(AggregateValue aggregateValue) {
return aggregateValue.CalculatedValue;
}
}
20 changes: 8 additions & 12 deletions InfrastSim/Algorithms/Combination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Combination(IList<T> values, int n) {
T[] arr;
int cur;
int p;
public int N { get; }
public int M { get; }
public int N { get; } // 组合长度
public int M { get; } // 组合可选项数

public IEnumerable<T>? Enumerate() {
while (cur > 0 && M - p < N - cur) {
Expand All @@ -38,27 +38,23 @@ public Combination(IList<T> values, int n) {
return arr;
}

public IEnumerator<IEnumerable<T>> GetEnumerator() {
public IEnumerator<IEnumerable<T>?> GetEnumerator() {
return new Enumerator(this);
}

public IEnumerable<IEnumerable<T>> ToEnumerable() {
IEnumerable<T>? cur = null;
IEnumerable<T>? cur;
while ((cur = Enumerate()) != null) {
yield return cur;
}
}

class Enumerator : IEnumerator<IEnumerable<T>> {
Combination<T> _combination;
class Enumerator(Combination<T> combination) : IEnumerator<IEnumerable<T>?> {
Combination<T> _combination = combination;
IEnumerable<T>? _cur = null;

public Enumerator(Combination<T> combination) {
_combination = combination;
}

public IEnumerable<T> Current => _cur;
object IEnumerator.Current => _cur;
public IEnumerable<T>? Current => _cur;
object? IEnumerator.Current => _cur;

public bool MoveNext() {
_cur = _combination.Enumerate();
Expand Down
2 changes: 1 addition & 1 deletion InfrastSim/EventDriven/EventSourceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public void NotifyUpdate() {
}

public void AddTrigger(EventTrigger trigger) {

}
}
3 changes: 1 addition & 2 deletions InfrastSim/EventDriven/IEventSource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace InfrastSim.EventDriven;

internal interface IEventSource
{
internal interface IEventSource {
DateTime TriggerTime { get; }
event Action<IEventSource>? TriggerTimeChanged;

Expand Down
8 changes: 3 additions & 5 deletions InfrastSim/EventDriven/OperatorBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace InfrastSim.EventDriven;

internal abstract class OperatorBase : IEventSource
{
internal abstract class OperatorBase : IEventSource {
public double Mood { get; private set; }
public AggregateValue MoodConsumeRate { get; private set; } = new(1.0);
public AggregateValue MoodConsumeRate { get; private set; } = new(100);

public DateTime TriggerTime => throw new NotImplementedException();

Expand All @@ -13,8 +12,7 @@ public void NotifyTimeElapsed(TimeElapsedInfo info) {
throw new NotImplementedException();
}

public void NotifyUpdate()
{
public void NotifyUpdate() {
throw new NotImplementedException();
}
}
8 changes: 1 addition & 7 deletions InfrastSim/FacilityType.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InfrastSim;
namespace InfrastSim;
public enum FacilityType {
ControlCenter, // 控制中枢
Manufacturing, // 制造站
Expand Down
3 changes: 1 addition & 2 deletions InfrastSim/GoldOrderPendingArgs.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace InfrastSim;
public class GoldOrderPendingArgs {
public GoldOrderPendingArgs(AggregateValue priority2Gold, AggregateValue priority3Gold, AggregateValue priority4Gold)
{
public GoldOrderPendingArgs(AggregateValue priority2Gold, AggregateValue priority3Gold, AggregateValue priority4Gold) {
Priority2Gold = priority2Gold ?? throw new ArgumentNullException(nameof(priority2Gold));
Priority3Gold = priority3Gold ?? throw new ArgumentNullException(nameof(priority3Gold));
Priority4Gold = priority4Gold ?? throw new ArgumentNullException(nameof(priority4Gold));
Expand Down
3 changes: 1 addition & 2 deletions InfrastSim/ISimulator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace InfrastSim;

public interface ISimulator
{
public interface ISimulator {
DateTime Now { get; }
}
4 changes: 0 additions & 4 deletions InfrastSim/InfrastSim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<PublishTrimmed>true</PublishTrimmed>
<EnforceExtendedAnaylzerRules>true</EnforceExtendedAnaylzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0-rc.2.23479.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InfrastSimSourceGenerator\InfrastSimSourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\RandomEx\RandomEx.csproj" />
Expand Down
6 changes: 6 additions & 0 deletions InfrastSim/Localization/AliasAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace InfrastSim.Localization;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class AliasAttribute(Language language, string alias) : Attribute {
public Language Language { get; set; } = language;
public string Alias { get; set; } = alias ?? throw new ArgumentNullException(nameof(alias));
}
7 changes: 7 additions & 0 deletions InfrastSim/Localization/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InfrastSim.Localization;
public enum Language {
EN,
English = EN,
CN,
Chinese = CN,
}
15 changes: 5 additions & 10 deletions InfrastSim/NamedValue.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
namespace InfrastSim;
public struct NamedValue {
public string Name;
public double Value;
public struct NamedValue(string name, int value) {
public string Name = name ?? throw new ArgumentNullException(nameof(name));
public int Value = value;

public NamedValue(string name, double value) {
Name = name ?? throw new ArgumentNullException(nameof(name));
Value = value;
}

public static implicit operator NamedValue(double value) {
public static implicit operator NamedValue(int value) {
return new("common", value);
}
public static implicit operator double(NamedValue value) {
public static implicit operator int(NamedValue value) {
return value.Value;
}
}
8 changes: 4 additions & 4 deletions InfrastSim/Order.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using InfrastSim.TimeDriven;
using System.Reflection.Emit;
using System.Text.Json;

namespace InfrastSim;

public record Order(int RequiredLevel, TimeSpan ProduceTime, Material Consumes, Material Earns) : IJsonSerializable {
public readonly static Order[] Gold = {
public int ProduceTicks { get; init; } = ProduceTime.ToSimuTicks();

public readonly static Order[] Gold = [
new(1, TimeSpan.FromMinutes(144), new Material("赤金", 2), new Material("龙门币", 1000)),
new(2, TimeSpan.FromMinutes(210), new Material("赤金", 3), new Material("龙门币", 1500)),
new(3, TimeSpan.FromMinutes(276), new Material("赤金", 4), new Material("龙门币", 2000)),
};
];
public readonly static Order OriginStone = new(3, TimeSpan.FromMinutes(120), new Material("源石碎片", 2), new Material("合成玉", 20));

public void ToJson(Utf8JsonWriter writer, bool detailed = false) {
Expand Down
2 changes: 1 addition & 1 deletion InfrastSim/OrderPendingArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace InfrastSim;
namespace InfrastSim;
public class OrderPendingArgs {
public OrderPendingArgs(Order order) {
CurrentOrder = OriginOrder = order ?? throw new ArgumentNullException(nameof(order));
Expand Down
25 changes: 12 additions & 13 deletions InfrastSim/Product.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace InfrastSim;
public record Product(string Name, int Volume, int RequiredLevel, TimeSpan ProduceTime, Material[]? Consumes = null) {
public readonly static Product[] CombatRecords = {
public record Product(
string Name, int Volume, int RequiredLevel, TimeSpan ProduceTime, Material[]? Consumes = null) {
public int ProduceTicks { get; init; } = ProduceTime.ToSimuTicks();

public readonly static Product[] CombatRecords = [
new("基础作战记录", 2, 1, TimeSpan.FromMinutes(45)),
new("初级作战记录", 3, 2, TimeSpan.FromMinutes(80)),
new("中级作战记录", 5, 3, TimeSpan.FromMinutes(180)),
};
];
public readonly static Product Gold = new("赤金", 2, 1, TimeSpan.FromMinutes(72));
public readonly static Product[] StoneFragment = {
new("源石碎片", 3, 3, TimeSpan.FromMinutes(60),
new[]{ new Material("龙门币", 1600),
new Material("固源岩", 2),}),
new("源石碎片", 3, 3, TimeSpan.FromMinutes(60),
new[]{ new Material("龙门币", 1000),
new Material("装置", 1),}),
};
public readonly static Product[] StoneFragment = [
new("源石碎片", 3, 3, TimeSpan.FromMinutes(60), [new("龙门币", 1600), new("固源岩", 2)]),
new("源石碎片", 3, 3, TimeSpan.FromMinutes(60), [new("龙门币", 1000), new("装置", 1)]
),
];

public readonly static Product[] AllProducts =
CombatRecords.Append(Gold).Concat(StoneFragment).ToArray();
public readonly static Product[] AllProducts = [.. CombatRecords, Gold, .. StoneFragment];
}
Loading

0 comments on commit 04d94d1

Please sign in to comment.