|
| 1 | +#nullable enable |
| 2 | +using System.Linq; |
| 3 | +using Content.IntegrationTests.Fixtures; |
| 4 | +using Content.Server.GameTicking; |
| 5 | +using Content.Server.GameTicking.Rules; |
| 6 | +using Content.Shared.GameTicking.Rules; |
| 7 | +using Robust.Shared.GameObjects; |
| 8 | +using Robust.Shared.Prototypes; |
| 9 | + |
| 10 | +namespace Content.IntegrationTests.Tests._Starlight.GameRules; |
| 11 | + |
| 12 | +[TestFixture] |
| 13 | +[TestOf(typeof(DynamicRuleSystem))] |
| 14 | +public sealed class DynamicRuleTest : GameTest |
| 15 | +{ |
| 16 | + private const string SequentialBudgetRule = "TestDynamicSequentialBudget"; |
| 17 | + private const string MutualExclusionRule = "TestDynamicMutualExclusion"; |
| 18 | + private const string CooldownRule = "TestDynamicCooldown"; |
| 19 | + private const string CooldownChildRule = "TestDynamicCooldownChild"; |
| 20 | + |
| 21 | + [TestPrototypes] |
| 22 | + private const string Prototypes = @" |
| 23 | +- type: entity |
| 24 | + id: TestDynamicSequentialBudget |
| 25 | + parent: BaseGameRule |
| 26 | + components: |
| 27 | + - type: GameRule |
| 28 | + minPlayers: 0 |
| 29 | + - type: DynamicRule |
| 30 | + startingBudgetMin: 325 |
| 31 | + startingBudgetMax: 326 |
| 32 | + budgetPerSecond: 0 |
| 33 | + variantBudgetPerSecond: 0 |
| 34 | + minRuleInterval: 86400 |
| 35 | + maxRuleInterval: 86401 |
| 36 | + table: !type:AllSelector |
| 37 | + children: |
| 38 | + - id: TestDynamicBudgetFirst |
| 39 | + conditions: |
| 40 | + - !type:HasBudgetCondition |
| 41 | + - id: TestDynamicBudgetSecond |
| 42 | + conditions: |
| 43 | + - !type:HasBudgetCondition |
| 44 | +
|
| 45 | +- type: entity |
| 46 | + id: TestDynamicBudgetFirst |
| 47 | + parent: BaseGameRule |
| 48 | + components: |
| 49 | + - type: DynamicRuleCost |
| 50 | + cost: 125 |
| 51 | +
|
| 52 | +- type: entity |
| 53 | + id: TestDynamicBudgetSecond |
| 54 | + parent: BaseGameRule |
| 55 | + components: |
| 56 | + - type: DynamicRuleCost |
| 57 | + cost: 300 |
| 58 | +
|
| 59 | +- type: entity |
| 60 | + id: TestDynamicMutualExclusion |
| 61 | + parent: BaseGameRule |
| 62 | + components: |
| 63 | + - type: GameRule |
| 64 | + minPlayers: 0 |
| 65 | + - type: DynamicRule |
| 66 | + startingBudgetMin: 100 |
| 67 | + startingBudgetMax: 101 |
| 68 | + budgetPerSecond: 0 |
| 69 | + variantBudgetPerSecond: 0 |
| 70 | + minRuleInterval: 86400 |
| 71 | + maxRuleInterval: 86401 |
| 72 | + table: !type:AllSelector |
| 73 | + children: |
| 74 | + - id: TestDynamicExclusiveFirst |
| 75 | + conditions: |
| 76 | + - !type:HasBudgetCondition |
| 77 | + - id: TestDynamicExclusiveSecond |
| 78 | + conditions: |
| 79 | + - !type:HasBudgetCondition |
| 80 | + - !type:MutuallyExclusiveRuleCondition |
| 81 | + rules: |
| 82 | + - TestDynamicExclusiveFirst |
| 83 | + - id: TestDynamicExclusiveFirst |
| 84 | + conditions: |
| 85 | + - !type:HasBudgetCondition |
| 86 | + - !type:MaxRuleOccurenceCondition |
| 87 | +
|
| 88 | +- type: entity |
| 89 | + id: TestDynamicExclusiveFirst |
| 90 | + parent: BaseGameRule |
| 91 | + components: |
| 92 | + - type: GameRule |
| 93 | + delay: |
| 94 | + min: 60 |
| 95 | + max: 60 |
| 96 | + - type: DynamicRuleCost |
| 97 | + cost: 10 |
| 98 | +
|
| 99 | +- type: entity |
| 100 | + id: TestDynamicExclusiveSecond |
| 101 | + parent: BaseGameRule |
| 102 | + components: |
| 103 | + - type: DynamicRuleCost |
| 104 | + cost: 10 |
| 105 | +
|
| 106 | +- type: entity |
| 107 | + id: TestDynamicCooldown |
| 108 | + parent: BaseGameRule |
| 109 | + components: |
| 110 | + - type: GameRule |
| 111 | + minPlayers: 0 |
| 112 | + - type: DynamicRule |
| 113 | + startingBudgetMin: 100 |
| 114 | + startingBudgetMax: 101 |
| 115 | + budgetPerSecond: 0 |
| 116 | + variantBudgetPerSecond: 0 |
| 117 | + minRuleInterval: 86400 |
| 118 | + maxRuleInterval: 86401 |
| 119 | + table: !type:AllSelector |
| 120 | + children: |
| 121 | + - id: TestDynamicCooldownChild |
| 122 | + conditions: |
| 123 | + - !type:HasBudgetCondition |
| 124 | +
|
| 125 | +- type: entity |
| 126 | + id: TestDynamicCooldownChild |
| 127 | + parent: BaseGameRule |
| 128 | + components: |
| 129 | + - type: DynamicRuleCost |
| 130 | + cost: 10 |
| 131 | + cooldown: 1 |
| 132 | +"; |
| 133 | + |
| 134 | + public override PoolSettings PoolSettings => new() |
| 135 | + { |
| 136 | + Dirty = true, |
| 137 | + DummyTicker = false, |
| 138 | + Connected = true, |
| 139 | + InLobby = true, |
| 140 | + }; |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Tests that the budget is updated between table children, so that a child rule can be selected and then the next child rule can be selected in the same table roll. |
| 144 | + /// </summary> |
| 145 | + [Test] |
| 146 | + public async Task DynamicBudgetUpdateTest() |
| 147 | + { |
| 148 | + var server = Pair.Server; |
| 149 | + |
| 150 | + await server.WaitAssertion(() => |
| 151 | + { |
| 152 | + var ticker = server.System<GameTicker>(); |
| 153 | + var uid = ticker.AddGameRule(SequentialBudgetRule); |
| 154 | + var component = server.EntMan.GetComponent<DynamicRuleComponent>(uid); |
| 155 | + |
| 156 | + Assert.Multiple(() => |
| 157 | + { |
| 158 | + Assert.That(component.Budget, Is.EqualTo(200)); |
| 159 | + Assert.That(component.Rules, Has.Count.EqualTo(1)); |
| 160 | + Assert.That(GetPrototypeId(server.EntMan, component.Rules.Single()), |
| 161 | + Is.EqualTo("TestDynamicBudgetFirst")); |
| 162 | + }); |
| 163 | + |
| 164 | + ticker.EndGameRule(uid); |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Tests that mutually exclusive rules are rejected within the same table roll, so that only one of the mutually exclusive rules is selected. |
| 170 | + /// </summary> |
| 171 | + /// <returns></returns> |
| 172 | + [Test] |
| 173 | + public async Task DynamicMutuallyExclusiveRulesRejectionTest() |
| 174 | + { |
| 175 | + var server = Pair.Server; |
| 176 | + |
| 177 | + await server.WaitAssertion(() => |
| 178 | + { |
| 179 | + var ticker = server.System<GameTicker>(); |
| 180 | + var uid = ticker.AddGameRule(MutualExclusionRule); |
| 181 | + var component = server.EntMan.GetComponent<DynamicRuleComponent>(uid); |
| 182 | + |
| 183 | + Assert.Multiple(() => |
| 184 | + { |
| 185 | + Assert.That(component.Budget, Is.EqualTo(90)); |
| 186 | + Assert.That(component.Rules, Has.Count.EqualTo(1)); |
| 187 | + Assert.That(GetPrototypeId(server.EntMan, component.Rules.Single()), |
| 188 | + Is.EqualTo("TestDynamicExclusiveFirst")); |
| 189 | + }); |
| 190 | + |
| 191 | + ticker.EndGameRule(uid); |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + /// <summary> |
| 196 | + /// Tests that a rule with a cooldown is not selected in the next Dynamic round after being selected in the previous Dynamic round. |
| 197 | + /// </summary> |
| 198 | + [Test] |
| 199 | + public async Task DynamicRuleCooldownTest() |
| 200 | + { |
| 201 | + var server = Pair.Server; |
| 202 | + var ticker = server.System<GameTicker>(); |
| 203 | + var dynamic = server.System<DynamicRuleSystem>(); |
| 204 | + |
| 205 | + await server.WaitAssertion(() => |
| 206 | + { |
| 207 | + var uid = ticker.AddGameRule(CooldownRule); |
| 208 | + var component = server.EntMan.GetComponent<DynamicRuleComponent>(uid); |
| 209 | + |
| 210 | + Assert.Multiple(() => |
| 211 | + { |
| 212 | + Assert.That(component.Rules, Has.Count.EqualTo(1)); |
| 213 | + Assert.That(GetPrototypeId(server.EntMan, component.Rules.Single()), |
| 214 | + Is.EqualTo(CooldownChildRule)); |
| 215 | + }); |
| 216 | + |
| 217 | + // A cooldown applies to future Dynamic rounds, not later rolls in the current round. |
| 218 | + Assert.That(dynamic.ExecuteNow(uid).Count(), Is.EqualTo(1)); |
| 219 | + Assert.That(component.Rules, Has.Count.EqualTo(2)); |
| 220 | + }); |
| 221 | + |
| 222 | + await server.WaitPost(() => ticker.RestartRound()); |
| 223 | + await Pair.RunUntilSynced(); |
| 224 | + |
| 225 | + await server.WaitAssertion(() => |
| 226 | + { |
| 227 | + var uid = ticker.AddGameRule(CooldownRule); |
| 228 | + var component = server.EntMan.GetComponent<DynamicRuleComponent>(uid); |
| 229 | + |
| 230 | + Assert.Multiple(() => |
| 231 | + { |
| 232 | + Assert.That(component.Rules, Is.Empty); |
| 233 | + Assert.That(component.Budget, Is.EqualTo(100)); |
| 234 | + }); |
| 235 | + |
| 236 | + // The snapshot remains in force for every roll during this Dynamic round. |
| 237 | + Assert.That(dynamic.ExecuteNow(uid), Is.Empty); |
| 238 | + }); |
| 239 | + |
| 240 | + await server.WaitPost(() => ticker.RestartRound()); |
| 241 | + await Pair.RunUntilSynced(); |
| 242 | + |
| 243 | + await server.WaitAssertion(() => |
| 244 | + { |
| 245 | + var uid = ticker.AddGameRule(CooldownRule); |
| 246 | + var component = server.EntMan.GetComponent<DynamicRuleComponent>(uid); |
| 247 | + |
| 248 | + Assert.That(component.Rules, Has.Count.EqualTo(1)); |
| 249 | + Assert.That(GetPrototypeId(server.EntMan, component.Rules.Single()), |
| 250 | + Is.EqualTo(CooldownChildRule)); |
| 251 | + }); |
| 252 | + } |
| 253 | + |
| 254 | + private static string? GetPrototypeId(IEntityManager entityManager, EntityUid uid) => entityManager.GetComponent<MetaDataComponent>(uid).EntityPrototype?.ID; |
| 255 | +} |
0 commit comments