2
\$\begingroup\$

We have a little tournament in our company between coworkers and I decided to write an application for managing games, results and rankings of players and teams.

The base logic for application is quite simple:

  • We have players
  • We have teams, which are formed from players
  • We have game/s
  • There are matches in a game, which could be between two teams, or two players.
  • Each match have a result that specifies the winner and the loser
  • The result contains rounds won, rounds lost, points (or score) taken and points lost for that single participant (team or player) in the given match and game.
  • since we have several games, each game needs its own set of rules for the app to decide who wins and who loses, and of course, how to calculate the points taken and lost for that participant.

    [Fact]
    public void HaveA_Winner()
    {
        Player playerA = new Player(Guid.NewGuid()) { Name = "A" };
        Player playerB = new Player(Guid.NewGuid()) { Name = "B" };
        Game game = new Game(Guid.NewGuid(), "Mortal Kombat");
        DateTime tomorrow = DateTime.Now.AddDays(1);
        PlayerMatch theMatch = new PlayerMatch(Guid.NewGuid(), game.Id, playerA, playerB, game, tomorrow);

        var mockA = new Mock<IGameResultRepository>();
        mockA.Setup(x => x.GameScores(playerA.Id, game.Id)).Returns(ResultScoreMockPlayerA(playerA, game.Id, theMatch.Id));
        var mockB = new Mock<IGameResultRepository>();
        mockB.Setup(x => x.GameScores(playerA.Id, game.Id)).Returns(ResultScoreMockPlayerB(playerB, game.Id, theMatch.Id));

        IMatchResultSpecification<Player> mkSpecs = new MortalKombatPlayerMatchResultSpecification(
            playerA.Specs(mockA.Object, game.Id, 3),
            playerB.Specs(mockB.Object, game.Id, 2));

        var result = theMatch.Result(mkSpecs);

        result.Winner.Participant.Should().Be(playerA);
        result.Loser.Participant.Should().Be(playerB);
    }

    private List<PlayerResultScore> ResultScoreMockPlayerA(Player player, Guid gameId, Guid matchId)
    {
        var score1 = new PlayerResultScore(player, matchId, gameId, 3, 2);
        var clc = new MortalKombatPointCalculator(3, 3, 0, 0);
        score1.SetPointsTaken(clc);
        score1.SetPointsLost(clc);

        var score2 = new PlayerResultScore(player, matchId, gameId, 1, 3);
        clc = new MortalKombatPointCalculator(3, 1, 0, 5);
        score2.SetPointsTaken(clc);
        score2.SetPointsLost(clc);

        var score3 = new PlayerResultScore(player, matchId, gameId, 3, 1);
        clc = new MortalKombatPointCalculator(3, 3, 6.2, 6);
        score3.SetPointsTaken(clc);
        score3.SetPointsLost(clc);

        List<PlayerResultScore> scores = new List<PlayerResultScore> {score1, score2, score3};
        return scores;
    }
    private List<PlayerResultScore> ResultScoreMockPlayerB(Player player, Guid gameId, Guid matchId)
    {
        var score1 = new PlayerResultScore(player, matchId, gameId, 2, 3);
        var clc = new MortalKombatPointCalculator(3, 2, 0, 0);
        score1.SetPointsTaken(clc);
        score1.SetPointsLost(clc);

        var score2 = new PlayerResultScore(player, matchId, gameId, 3, 1);
        clc = new MortalKombatPointCalculator(3, 3, 5, 0);
        score2.SetPointsTaken(clc);
        score2.SetPointsLost(clc);

        var score3 = new PlayerResultScore(player, matchId, gameId, 1, 3);
        clc = new MortalKombatPointCalculator(3, 1, 6, 6.2);
        score2.SetPointsTaken(clc);
        score2.SetPointsLost(clc);

        List<PlayerResultScore> scores = new List<PlayerResultScore> {score1, score2, score3};
        return scores;
    }

Direct link to this test

The core logic is in this GitHub repository from which you can explore the details.

The problem

For now, I just implemented a logic for the "Mortal Kombat" game, with it' rules of scoring and all, as you can see, it already turned into an abomination.

Everything works fine and the test passes, but I know the design could be way better than this. I want to know the weak spots of this design and I want to refactor this to good quality code.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.