Skip to content

Commit

Permalink
removed the game repository, game factory, and game interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
lastunicorn committed Jan 17, 2024
1 parent 3d71993 commit 2e70463
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
using DustInTheWind.Dot.AdventureGame.ActionResults;
using DustInTheWind.Dot.AdventureGame.GameModel;
using DustInTheWind.Dot.AdventureGame.ObjectModel;
using DustInTheWind.Dot.Domain.DataAccess;
using DustInTheWind.Dot.Domain.GameModel;

namespace DustInTheWind.Dot.AdventureGame.ActionResultHandlers
{
public class AcquireObjectsResultHandler : ResultHandlerBase<AcquireObjectsResult>
{
private readonly GameRepository gameRepository;
private readonly Game game;

public AcquireObjectsResultHandler(GameRepository gameRepository)
public AcquireObjectsResultHandler(Game game)
{
this.gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
this.game = game ?? throw new ArgumentNullException(nameof(game));
}

public override void Handle(AcquireObjectsResult acquireObjectsResult)
Expand All @@ -26,9 +26,7 @@ public override void Handle(AcquireObjectsResult acquireObjectsResult)
IEnumerable<IObject> objects = acquireObjectsResult.Objects
.Where(x => x != null);

Game game = gameRepository.Get() as Game;

if (game == null)
if (!game.IsLoaded)
{
string objectsNames = string.Join(", ", objects.Select(x => x.Name));
throw new Exception("The objects cannot he acquired. There is no game in progress. Objects: " + objectsNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@
using DustInTheWind.Dot.AdventureGame.ActionModel;
using DustInTheWind.Dot.AdventureGame.ActionResults;
using DustInTheWind.Dot.AdventureGame.GameModel;
using DustInTheWind.Dot.Domain.DataAccess;

namespace DustInTheWind.Dot.AdventureGame.ActionResultHandlers
{
public class ChangeLocationResultHandler : ResultHandlerBase<ChangeLocationResult>
{
private readonly GameRepository gameRepository;
private readonly Game game;

public ChangeLocationResultHandler(GameRepository gameRepository)
public ChangeLocationResultHandler(Game game)
{
this.gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
this.game = game ?? throw new ArgumentNullException(nameof(game));
}

public override void Handle(ChangeLocationResult changeLocationResult)
{
Game game = gameRepository.Get() as Game;

if (game == null)
if (!game.IsLoaded)
throw new Exception("Cannot change location. There is no game in progress.");

game.ChangeLocation(changeLocationResult.DestinationId);
Expand Down
47 changes: 35 additions & 12 deletions sources/Dot.AdventureGame/GameModel/Game.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DustInTheWind.Dot.AdventureGame.ActionModel;
using DustInTheWind.Dot.AdventureGame.Actions;
Expand All @@ -11,21 +8,21 @@

namespace DustInTheWind.Dot.AdventureGame.GameModel
{
public abstract class Game : IGame
public abstract class Game
{
private readonly GameTimer gameTimer = new GameTimer();
private readonly GameTimer gameTimer = new();

private ILocation lastLocation;
private GameState state = GameState.Closed;
private bool isNew = true;
private bool isFinished;

private readonly LocationEngine locationEngine = new LocationEngine();
private readonly AddOnCollection addOns = new AddOnCollection();
private readonly LocationEngine locationEngine = new();
private readonly AddOnCollection addOns = new();

public Inventory Inventory { get; } = new Inventory();
public Inventory Inventory { get; } = new();

public ActionSet Actions { get; } = new ActionSet();
public ActionSet Actions { get; } = new();

public GameState State
{
Expand All @@ -40,6 +37,8 @@ private set
}
}

public bool IsLoaded { get; private set; }

public TimeSpan TotalPlayTime => gameTimer.TotalPlayTime;

public ILocation CurrentLocation => locationEngine.CurrentLocation;
Expand All @@ -57,7 +56,29 @@ protected Game()
locationEngine.CurrentLocationChanged += HandleCurrentLocationChanged;
}

public abstract void InitializeNew();
public void InitializeNew()
{
Clear();

DoInitializeNew();
IsLoaded = true;
}

private void Clear()
{
State = default;
isNew = true;
isFinished = false;
gameTimer.Clear();

locationEngine.Clear();
addOns.Clear();
Inventory.Clear();

IsLoaded = false;
}

protected abstract void DoInitializeNew();

private void HandleCurrentLocationChanged(object sender, EventArgs e)
{
Expand Down Expand Up @@ -88,7 +109,7 @@ public T GetLocation<T>()

public void Open()
{
if (State == GameState.Paused || State == GameState.Closed)
if (State is GameState.Paused or GameState.Closed)
{
if (!isFinished)
gameTimer.Start();
Expand All @@ -103,7 +124,7 @@ public void Open()

State = GameState.Open;

GameOpenEventArgs eventArgs = new GameOpenEventArgs(isNew);
GameOpenEventArgs eventArgs = new(isNew);
OnOpened(eventArgs);

isNew = false;
Expand Down Expand Up @@ -280,6 +301,8 @@ public virtual void Import(ExportData storageData)
ExportNode inventoryExportNode = (ExportNode)storageData["inventory"];
Inventory.Clear();
Inventory.Import(inventoryExportNode);

IsLoaded = true;
}

protected virtual void OnStateChanged()
Expand Down
83 changes: 51 additions & 32 deletions sources/Dot.AdventureGame/GameModel/GameTimer.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
using System;
// Dot
// Copyright (C) 2020-2024 Dust in the Wind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace DustInTheWind.Dot.AdventureGame.GameModel
namespace DustInTheWind.Dot.AdventureGame.GameModel;

public class GameTimer
{
public class GameTimer
{
private DateTime? startTime;
private TimeSpan totalPlayTime;
private DateTime? startTime;
private TimeSpan totalPlayTime;

public TimeSpan TotalPlayTime
{
get => startTime.HasValue
? totalPlayTime + (DateTime.UtcNow - startTime.Value)
: totalPlayTime;
set => totalPlayTime = value;
}
public TimeSpan TotalPlayTime
{
get => startTime.HasValue
? totalPlayTime + (DateTime.UtcNow - startTime.Value)
: totalPlayTime;
set => totalPlayTime = value;
}

public GameTimer()
{
}
public GameTimer()
{
}

public GameTimer(TimeSpan initialValue)
{
totalPlayTime = initialValue;
}
public GameTimer(TimeSpan initialValue)
{
totalPlayTime = initialValue;
}

public void Start()
{
if (startTime.HasValue)
return;
public void Start()
{
if (startTime.HasValue)
return;

startTime = DateTime.UtcNow;
}
startTime = DateTime.UtcNow;
}

public void Stop()
public void Stop()
{
if (startTime.HasValue)
{
if (startTime.HasValue)
{
totalPlayTime += DateTime.UtcNow - startTime.Value;
startTime = null;
}
totalPlayTime += DateTime.UtcNow - startTime.Value;
startTime = null;
}
}

public void Clear()
{
startTime = null;
totalPlayTime = default;
}
}
21 changes: 6 additions & 15 deletions sources/Dot.Application/UseCases/LoadGame/LoadGameUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using DustInTheWind.ConsoleTools.Modularization;
using DustInTheWind.Dot.AdventureGame.GameModel;
using DustInTheWind.Dot.Application.UseCases.SaveGame;
using DustInTheWind.Dot.Domain.DataAccess;
using DustInTheWind.Dot.Domain.GameModel;
using DustInTheWind.Dot.Ports.GameSavesAccess;
using DustInTheWind.Dot.Ports.PresentationAccess;
Expand All @@ -26,19 +26,17 @@ namespace DustInTheWind.Dot.Application.UseCases.LoadGame;
public class LoadGameUseCase
{
private readonly ILoadGameView view;
private readonly GameRepository gameRepository;
private readonly IGameFactory gameFactory;
private readonly Game game;
private readonly IUseCaseFactory useCaseFactory;
private readonly IGameSlotRepository gameSlotRepository;
private readonly IGameSettings gameSettings;
private readonly ModuleEngine moduleEngine;

public LoadGameUseCase(ILoadGameView loadGameView, GameRepository gameRepository, IGameFactory gameFactory,
public LoadGameUseCase(ILoadGameView loadGameView, Game game,
IUseCaseFactory useCaseFactory, IGameSlotRepository gameSlotRepository, IGameSettings gameSettings, ModuleEngine moduleEngine)
{
view = loadGameView ?? throw new ArgumentNullException(nameof(loadGameView));
this.gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
this.gameFactory = gameFactory ?? throw new ArgumentNullException(nameof(gameFactory));
this.game = game ?? throw new ArgumentNullException(nameof(game));
this.useCaseFactory = useCaseFactory ?? throw new ArgumentNullException(nameof(useCaseFactory));
this.gameSlotRepository = gameSlotRepository ?? throw new ArgumentNullException(nameof(gameSlotRepository));
this.gameSettings = gameSettings ?? throw new ArgumentNullException(nameof(gameSettings));
Expand All @@ -47,9 +45,7 @@ public LoadGameUseCase(ILoadGameView loadGameView, GameRepository gameRepository

public void Execute()
{
IGame game = gameRepository.Get();

if (game != null)
if (game.IsLoaded)
{
if (game.IsChanged)
SavePreviousGame();
Expand All @@ -59,12 +55,7 @@ public void Execute()

GameSlot gameSlot = ChooseGameSlot();

IGame newGame = gameFactory.Create();
newGame.Import(gameSlot.Data.ToExportData());
game = newGame;

gameRepository.Add(game);

game.Import(gameSlot.Data.ToExportData());
game.Open();

gameSettings.LastSavedGame = gameSlot.Id;
Expand Down
20 changes: 7 additions & 13 deletions sources/Dot.Application/UseCases/NewGame/CreateNewGameUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,27 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using DustInTheWind.ConsoleTools.Modularization;
using DustInTheWind.Dot.Domain.DataAccess;
using DustInTheWind.Dot.Domain.GameModel;
using DustInTheWind.Dot.AdventureGame.GameModel;

namespace DustInTheWind.Dot.Application.UseCases.NewGame;

public class CreateNewGameUseCase
{
private readonly GameRepository gameRepository;
private readonly IGameFactory gameFactory;
private readonly Game game;
private readonly ModuleEngine moduleEngine;

public CreateNewGameUseCase(GameRepository gameRepository, IGameFactory gameFactory, ModuleEngine moduleEngine)
public CreateNewGameUseCase(Game game, ModuleEngine moduleEngine)
{
this.gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
this.gameFactory = gameFactory ?? throw new ArgumentNullException(nameof(gameFactory));
this.game = game ?? throw new ArgumentNullException(nameof(game));
this.moduleEngine = moduleEngine ?? throw new ArgumentNullException(nameof(moduleEngine));
}

public void Execute()
{
IGame game = gameRepository.Get();
game?.Close();
if (game.IsLoaded)
game.Close();

IGame newGame = gameFactory.Create();
newGame.InitializeNew();
game = newGame;
gameRepository.Add(game);
game.InitializeNew();

moduleEngine.RequestToChangeModule("game");
}
Expand Down
12 changes: 5 additions & 7 deletions sources/Dot.Application/UseCases/SaveGame/SaveGameUseCase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DustInTheWind.Dot.AdventureGame.GameModel;
using DustInTheWind.Dot.Application.UseCases.LoadGame;
using DustInTheWind.Dot.Domain.DataAccess;
using DustInTheWind.Dot.Domain.GameModel;
using DustInTheWind.Dot.Ports.GameSavesAccess;
using DustInTheWind.Dot.Ports.PresentationAccess;
Expand All @@ -12,23 +12,21 @@ namespace DustInTheWind.Dot.Application.UseCases.SaveGame;
public class SaveGameUseCase
{
private readonly ISaveGameView saveGameView;
private readonly GameRepository gameRepository;
private readonly Game game;
private readonly IGameSlotRepository gameSlotRepository;
private readonly IGameSettings gameSettings;

public SaveGameUseCase(ISaveGameView saveGameView, GameRepository gameRepository, IGameSlotRepository gameSlotRepository, IGameSettings gameSettings)
public SaveGameUseCase(ISaveGameView saveGameView, Game game, IGameSlotRepository gameSlotRepository, IGameSettings gameSettings)
{
this.saveGameView = saveGameView ?? throw new ArgumentNullException(nameof(saveGameView));
this.gameRepository = gameRepository ?? throw new ArgumentNullException(nameof(gameRepository));
this.game = game ?? throw new ArgumentNullException(nameof(game));
this.gameSlotRepository = gameSlotRepository ?? throw new ArgumentNullException(nameof(gameSlotRepository));
this.gameSettings = gameSettings ?? throw new ArgumentNullException(nameof(gameSettings));
}

public void Execute()
{
IGame game = gameRepository.Get();

if (game == null)
if (!game.IsLoaded)
throw new Exception("There is no game to be saved.");

List<GameSlot> gameSlots = gameSlotRepository.GetAll().ToList();
Expand Down
Loading

0 comments on commit 2e70463

Please sign in to comment.