-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEventNode.cs
More file actions
120 lines (102 loc) · 3.9 KB
/
GameEventNode.cs
File metadata and controls
120 lines (102 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Text;
using CodeName.EventEngine.GameEvents;
using CodeName.Serialization;
using CodeName.Serialization.Validation;
using Newtonsoft.Json;
namespace CodeName.EventEngine
{
[ValidateSerializeByValue]
public class GameEventNode<TGameState>
{
[JsonConstructor]
private GameEventNode() {}
public GameEventNode(GameEvent<TGameState> gameEvent, IEnumerable<int> path, ISerializer serializer, EventId eventId = default)
{
Id = eventId.IsValid() ? eventId : EventId.Generate();
OriginalEvent = serializer.Clone(gameEvent);
Event = serializer.Clone(gameEvent);
Path = new List<int>(path);
}
/// <summary>
/// Unique ID of this event.
/// </summary>
[JsonProperty] public EventId Id { get; private set; }
/// <summary>
/// The original event as created by the event raiser.
/// <para/>
/// Modifying this event is not recommended.
/// </summary>
[JsonProperty(TypeNameHandling = TypeNameHandling.Auto)] [SerializeByValue]
public GameEvent<TGameState> OriginalEvent { get; private set; }
/// <summary>
/// The event that will be applied after the OnEventConfirmed phase.
/// <para/>
/// Modifying this event is allowed.
/// </summary>
[JsonProperty(TypeNameHandling = TypeNameHandling.Auto)] [SerializeByValue]
public GameEvent<TGameState> Event { get; private set; }
/// <summary>
/// The path of this <see cref="GameEventNode{TState}"/> in the event tree.
/// </summary>
[JsonProperty]
public List<int> Path { get; private set; }
/// <summary>
/// The events raised by this event being applied or in response to this event.
/// <para/>
/// In other words, child events are events caused by this event.
/// </summary>
[JsonProperty] [SerializeByValue]
public List<GameEventNode<TGameState>> Children { get; private set; } = new();
/// <summary>
/// A locked event can no longer be prevented.
/// </summary>
[JsonProperty]
public bool IsLocked { get; private set; }
/// <summary>
/// Expected game state after the event is applied. Usually used for debugging purposes.
/// </summary>
[JsonIgnore]
public TGameState ExpectedState { get; set; }
/// <summary>
/// Prevent an event from being applied.
/// <para/>
/// An event can only be prevented during the OnEventRaised phase.
/// Additionally, an event can only be prevented once.
/// </summary>
public void Prevent()
{
if (IsLocked)
{
throw new InvalidOperationException("Event has been locked. Events can only be prevented during the OnEventRaised event.");
}
if (Event is PreventedEvent<TGameState>)
{
throw new InvalidOperationException("Event has already been prevented. Events can only be prevented once.");
}
Event = new PreventedEvent<TGameState>(Event);
}
/// <summary>
/// Called by <see cref="ISimulation{TGameState}"/> after the OnEventRaised event.
/// </summary>
public void Lock()
{
IsLocked = true;
}
public override string ToString()
{
return ToString(0);
}
private string ToString(int level)
{
var indent = new string(' ', level * 4);
var childEventTextBuilder = new StringBuilder();
foreach (var node in Children)
{
childEventTextBuilder.Append(node.ToString(level + 1));
}
return $"{indent}{Event}\n{childEventTextBuilder}";
}
}
}