Skip to content

Commit

Permalink
extract IRandomGen interface from RandomGen
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 14, 2024
1 parent c899991 commit c1eea8a
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/api/ApiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ private static bool EmptyJson(string requestBody)
public static GameSession NewGameSession(GameState? initialGameState = null)
{
List<GameSessionTurn>? turnList = initialGameState != null ? [new GameSessionTurn(startState: initialGameState)] : null;
var gameSession = new GameSession(new RandomGen(new Random()), turnList);
var gameSession = new GameSession(new RandomGen(), turnList);
return gameSession;
}

public static GameSession NewGameSessionFromTurn(GameSessionTurn? initialTurn = null)
{
List<GameSessionTurn>? turnList = initialTurn != null ? [initialTurn] : null;
var gameSession = new GameSession(new RandomGen(new Random()), turnList);
var gameSession = new GameSession(new RandomGen(), turnList);
return gameSession;
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/WebApplicationRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Register(WebApplication app)

private static string HelloCoinFlip()
{
var randomGen = new RandomGen(new Random());
var randomGen = new RandomGen();
return $"Hello World! Coin flip: {randomGen.FlipCoin()}";
}
}
2 changes: 1 addition & 1 deletion src/game-cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private static void Main(string[] args)
{
var config = new Configuration(new FileSystem());
using var log = new Log(config);
var randomGen = new RandomGen(new Random());
var randomGen = new RandomGen();

var controller = new GameSessionController(config, log, new GameSession(randomGen));

Expand Down
4 changes: 2 additions & 2 deletions src/game-lib-tests/AIPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AIPlayerTests
{
private Configuration _config = null!;
private ILog _log = null!;
private readonly RandomGen _randomGen = new RandomGen(new Random());
private readonly IRandomGen _randomGen = new RandomGen();

[SetUp]
public void Setup()
Expand Down Expand Up @@ -43,7 +43,7 @@ public void ExampleGameSessionForApi()
{
var config = new Configuration(new SimulatedFileSystem());
var log = new Log(config);
var randomGen = new RandomGen(new Random());
var randomGen = new RandomGen();
var intellect = AIPlayer.Intellect.Basic;
var controller = new GameSessionController(config, log, new GameSession(randomGen));
var aiPlayer = new AIPlayer(log, intellect);
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib-tests/FactionFixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace UfoGameLib.Tests;

public static class FactionFixtures
{
public static Factions SingleFaction(RandomGen randomGen) => new(
public static Factions SingleFaction(IRandomGen randomGen) => new(
[
// Note: need to ensure here that IDs are consecutive, and from zero.
Faction.Init(randomGen, id: 0, "Black Lotus cult", power: 200, powerIncrease: 5),
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib-tests/GameSessionControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GameSessionControllerTests
{
private Configuration _config = null!;
private ILog _log = null!;
private readonly RandomGen _randomGen = new RandomGen(new Random());
private readonly IRandomGen _randomGen = new RandomGen();

[SetUp]
public void Setup()
Expand Down
5 changes: 2 additions & 3 deletions src/game-lib-tests/GameSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class GameSessionTests
// Using null! as these fields will be initialized in Setup() method.
private Configuration _config = null!;
private ILog _log = null!;
private RandomGen _randomGen = null!;
private IRandomGen _randomGen = null!;
private Factions _factions = null!;

[SetUp]
public void Setup()
{
_config = new Configuration(new FileSystem());
_log = new Log(_config);
_randomGen = new RandomGen(new Random());
_randomGen = new RandomGen();
_factions = FactionFixtures.SingleFaction(_randomGen);
}

Expand Down Expand Up @@ -88,7 +88,6 @@ public void BasicHappyPathGameSessionWorks()
//
// This way for testing I can say "the countdown is always 3"
//
// 2. Allow to pass factions when initing game session
MissionSite site = controller.CurrentGameStatePlayerView.MissionSites.First();
turnController.LaunchMission(site, agentCount: 3);
controller.AdvanceTime();
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib/Controller/CheatingGameSessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace UfoGameLib.Controller;
/// </summary>
public class CheatingGameSessionController : GameSessionController
{
public CheatingGameSessionController(Configuration config, ILog log, RandomGen randomGen, GameSession gameSession)
public CheatingGameSessionController(Configuration config, ILog log, IRandomGen randomGen, GameSession gameSession)
: base(
config,
log,
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/Controller/GameTurnController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace UfoGameLib.Controller;
public class GameTurnController
{
private readonly ILog _log;
public RandomGen RandomGen { get; }
public IRandomGen RandomGen { get; }
private readonly EventIdGen _eventIdGen;
private readonly AgentIdGen _agentIdGen;
private readonly MissionIdGen _missionIdGen;
Expand All @@ -19,7 +19,7 @@ public class GameTurnController

public GameTurnController(
ILog log,
RandomGen randomGen,
IRandomGen randomGen,
EventIdGen eventIdGen,
AgentIdGen agentIdGen,
MissionIdGen missionIdGen,
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/Controller/TimeAdvancementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace UfoGameLib.Controller;
public class TimeAdvancementController
{
private readonly ILog _log;
private readonly RandomGen _randomGen;
private readonly IRandomGen _randomGen;
private readonly List<WorldEvent> _worldEvents;
private readonly EventIdGen _eventIdGen;
private readonly MissionSiteIdGen _missionSiteIdGen;

public TimeAdvancementController(
ILog log,
RandomGen randomGen,
IRandomGen randomGen,
EventIdGen eventIdGen,
MissionSiteIdGen missionSiteIdGen)
{
Expand Down
13 changes: 13 additions & 0 deletions src/game-lib/Lib/IRandomGen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace UfoGameLib.Lib;

public interface IRandomGen
{
int Roll1To(int n);
int Roll0To(int n);
int Roll(int min, int max);
int Roll(Range range);
T Pick<T>(List<T> items);
List<T> Pick<T>(List<T> items, int count);
TValue Pick<TKey, TValue>(IDictionary<TKey, TValue> dict);
bool FlipCoin();
}
7 changes: 4 additions & 3 deletions src/game-lib/Lib/RandomGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ namespace UfoGameLib.Lib;

// Note: currently upon save/load the random gets re-randomized.
// To fix that, the Save file should include a seed used to recreate upon load.
public class RandomGen

public class RandomGen : IRandomGen
{
private readonly Random _random;

public RandomGen(Random random)
public RandomGen(Random? random = null)
{
_random = random;
_random = random ?? new Random();
}

public int Roll1To(int n)
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib/Model/AgentSurvivalRoll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AgentSurvivalRoll

public const int MaxRecoversIn = 30;

public AgentSurvivalRoll(ILog log, RandomGen randomGen, Agent agent, Mission mission)
public AgentSurvivalRoll(ILog log, IRandomGen randomGen, Agent agent, Mission mission)
{
SurvivalChance = ComputeSurvivalChance(agent, mission.Site.Difficulty);
Roll = randomGen.Roll(RollRange);
Expand Down
6 changes: 3 additions & 3 deletions src/game-lib/Model/Faction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Faction(
}

public static Faction Init(
RandomGen randomGen,
IRandomGen randomGen,
int id,
string name,
int power,
Expand All @@ -57,7 +57,7 @@ public Faction DeepClone()

public List<MissionSite> CreateMissionSites(
ILog log,
RandomGen randomGen,
IRandomGen randomGen,
MissionSiteIdGen missionSiteIdGen,
GameState state)
{
Expand Down Expand Up @@ -98,6 +98,6 @@ public List<MissionSite> CreateMissionSites(
return sites;
}

private static int RandomizeMissionSiteCountdown(RandomGen randomGen)
private static int RandomizeMissionSiteCountdown(IRandomGen randomGen)
=> randomGen.Roll(Ruleset.FactionMissionSiteCountdown);
}
2 changes: 1 addition & 1 deletion src/game-lib/Model/Factions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Factions DeepClone()

public List<MissionSite> CreateMissionSites(
ILog log,
RandomGen randomGen,
IRandomGen randomGen,
MissionSiteIdGen missionSiteIdGen,
GameState state)
=> this.SelectMany(faction => faction.CreateMissionSites(log, randomGen, missionSiteIdGen, state)).ToList();
Expand Down
6 changes: 3 additions & 3 deletions src/game-lib/Model/Ruleset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class Ruleset

// For more example factions data, see:
// https://github.com/konrad-jamrozik/game/blob/eccb44a1d5f074e95b07aebca2c6bc5bbfdfdda8/src/ufo-game/Model/Data/FactionsData.cs#L34
public static Factions InitialFactions(RandomGen randomGen) => new(
public static Factions InitialFactions(IRandomGen randomGen) => new(
[
// Note: need to ensure here that IDs are consecutive, and from zero.
Faction.Init(randomGen, id: 0, "Black Lotus cult", power: 200, powerIncrease: 5),
Expand All @@ -36,7 +36,7 @@ public static class Ruleset

public static (bool survived, int? recoversIn) RollForAgentSurvival(
ILog log,
RandomGen randomGen,
IRandomGen randomGen,
Agent agent,
Mission mission)
{
Expand Down Expand Up @@ -73,7 +73,7 @@ private static int SkillFromMissions(Agent agent)
// based on faction data and possibly other factors. And faction will have power correlating with turn.
public static (int difficulty, int difficultyFromTurn, int roll) RollMissionSiteDifficulty(
int currentTurn,
RandomGen randomGen)
IRandomGen randomGen)
{
// Note that currently the only ways of increasing agents survivability of difficulty is:
// - by surviving missions
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/State/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace UfoGameLib.State;
/// </summary>
public class GameSession
{
public readonly RandomGen RandomGen;
public readonly IRandomGen RandomGen;

public readonly List<GameSessionTurn> Turns;

Expand All @@ -34,7 +34,7 @@ public class GameSession

public readonly MissionSiteIdGen MissionSiteIdGen;

public GameSession(RandomGen randomGen, List<GameSessionTurn>? turns = null, Factions? factions = null)
public GameSession(IRandomGen randomGen, List<GameSessionTurn>? turns = null, Factions? factions = null)
{
RandomGen = randomGen;
Turns = turns ?? [new GameSessionTurn(startState: GameState.NewInitialGameState(randomGen, factions))];
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/State/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public GameState(
Factions = factions;
}

public static GameState NewInitialGameState(RandomGen? randomGen = null, Factions? factions = null)
public static GameState NewInitialGameState(IRandomGen? randomGen = null, Factions? factions = null)
{
randomGen ??= new RandomGen(new Random());
randomGen ??= new RandomGen();
return new GameState(
updateCount: 0,
new Timeline(currentTurn: Timeline.InitialTurn),
Expand Down

0 comments on commit c1eea8a

Please sign in to comment.