From 9ede6f58bcce2b17ca2290d5788f07cb344b3bba Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 13 May 2017 21:37:30 +0300 Subject: [PATCH 01/49] start working on the tests of game center --- .../Core/GameCenterTests.cs | 89 +++++++++++++++++++ Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 88 +++++++++++++++++- Poker.BE/Poker.BE.Domain/Game/League.cs | 8 +- Poker.BE/Poker.BE.Domain/Game/Room.cs | 21 ++++- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + .../Exceptions/NotEnoughPlayersException.cs | 2 +- .../Utility/Exceptions/PokerException.cs | 28 ++++++ 7 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/PokerException.cs diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs new file mode 100644 index 0000000..ac00150 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -0,0 +1,89 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Poker.BE.Domain.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Core.Tests +{ + [TestClass()] + public class GameCenterTests + { + #region Set Up + + public TestContext TestContext { get; set; } + + public GameCenterTests() + { + + } + #endregion + + [TestMethod()] + public void GameCenterTest() + { + // TODO - simple constructor - may not need to test? + throw new NotImplementedException(); + } + + #region Find an Existing Room - Functions Overloading Tests + [TestMethod()] + public void FindRoomsByCriteriaTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void FindRoomsByCriteriaTest1() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void FindRoomsByCriteriaTest2() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void FindRoomsByCriteriaTest3() + { + // TODO + throw new NotImplementedException(); + } + #endregion + + [TestMethod()] + public void EnterRoomTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void CreateNewRoomTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void JoinNextHandTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void StandUpToSpactateTest() + { + // TODO + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index ac527df..d85e652 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -29,17 +29,24 @@ public enum Move #region Fields private IDictionary playersManager; + private IDictionary roomsManager; + private IDictionary leaguesManager; + private ICollection leagues; #endregion #region Properties - public ICollection Rooms { get { return playersManager.Values.Distinct().ToList(); } } + public ICollection Rooms { get { return roomsManager.Keys; } } public ICollection Players { get { return playersManager.Keys; } } + public ICollection Leagues { get { return leagues; } } #endregion #region Constructors public GameCenter() { playersManager = new Dictionary(); + roomsManager = new Dictionary(); + leaguesManager = new Dictionary(); + leagues = new List(); } #endregion @@ -55,9 +62,80 @@ private Player CreatePlayer(Room room) { return room.CreatePlayer(); } + + private void AddLeage(int level) + { + var league = new League(); + leagues.Add(league); + leaguesManager.Add(level, league); + } + + private void BindRoomToLeague(Room room, League league) + { + roomsManager.Add(room, league); + } + + private bool RemovePlayer(Player player) + { + playersManager[player].RemovePlayer(player); + return playersManager.Remove(player); + } + + private bool RemoveRoom(Room room) + { + // Note: aggressive approach - all the players kicked out + /* TODO: try/catch for gentle approach? + * example - if there are players at the room, this removal will fail. + * */ + if (room.Players.Count > 0) + { + foreach (var player in room.Players) + { + RemovePlayer(player); + } + + room.ClearAll(); + } + + // remove the room from the league + roomsManager[room].RemoveRoom(room); + + // remove the room from the room's dictionary. + return roomsManager.Remove(room); + } + + private bool RemoveLeague(League league) + { + if (league.Rooms.Count > 0) + { + foreach (var room in league.Rooms) + { + RemoveRoom(room); + } + + // call clear() of current room, to clean all his resources. + league.Rooms.Clear(); + } + + // remove all occurrences of this league at the league's dictionary + foreach (var key in leaguesManager.Where(pair => pair.Value == league).Select(pair => pair.Key)) + { + leaguesManager.Remove(new KeyValuePair(key, league)); + } + + // remove the league from the league collection (field) + return leagues.Remove(league); + } + + private void BindPlayerToRoom(Player player, Room room) + { + playersManager.Add(player, room); + } #endregion #region Methods + + #region Find an Existing Room /// /// Allow the user to find an existing room according to different criteria and enter the room as a spectator. /// @@ -87,6 +165,7 @@ public ICollection FindRoomsByCriteria(double betSize) // TODO throw new NotImplementedException(); } + #endregion /// /// Allow the user to enter a room from a list as a spectator. @@ -97,7 +176,12 @@ public ICollection FindRoomsByCriteria(double betSize) /// public Player EnterRoom(Room room) { - return CreatePlayer(room); + var player = CreatePlayer(room); + + // for the gameCenter player's dictionary (players manager) + BindPlayerToRoom(player, room); + + return player; } /// diff --git a/Poker.BE/Poker.BE.Domain/Game/League.cs b/Poker.BE/Poker.BE.Domain/Game/League.cs index a778972..eb659d0 100644 --- a/Poker.BE/Poker.BE.Domain/Game/League.cs +++ b/Poker.BE/Poker.BE.Domain/Game/League.cs @@ -8,7 +8,13 @@ namespace Poker.BE.Domain.Game { public class League { - // TODO: complete - set team member to do this + public ICollection Rooms { get; set; } + // TODO: complete - set team member to do this + internal void RemoveRoom(Room room) + { + // TODO + throw new NotImplementedException(); + } } } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 83c8767..83a6672 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -29,7 +29,7 @@ public class Room public ICollection Chairs { get { return chairs; } } public Hand CurrentHand { get; private set; } public GamePreferences Preferences { get; set; } - private ICollection ActivePlayers + public ICollection ActivePlayers { get { @@ -38,7 +38,7 @@ private ICollection ActivePlayers .ToList(); } } - private ICollection PassivePlayers + public ICollection PassivePlayers { get { @@ -48,6 +48,7 @@ private ICollection PassivePlayers } } + public ICollection Players { get { return activeAndPassivePlayers; } } #endregion @@ -70,6 +71,12 @@ private Room() } + internal void RemovePlayer(Player player) + { + // TODO + throw new NotImplementedException(); + } + /// /// UC003 Create a new room /// @@ -80,6 +87,8 @@ public Room(Player creator) : this() activeAndPassivePlayers.Add(creator); } + + /// /// UC003 Create a new room /// @@ -94,7 +103,13 @@ public Room(Player creator, GamePreferences preferences) : this(creator) #endregion #region Methods - internal Player CreatePlayer() + public void ClearAll() + { + //TODO: this function used be gameCenter do delete the room. all players and other resources of room need to be deleted. + throw new NotImplementedException(); + } + + public Player CreatePlayer() { var result = new Player(); activeAndPassivePlayers.Add(result); diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index f13b38f..a5b0de5 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -67,6 +67,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughPlayersException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughPlayersException.cs index 6ce4f96..e1146b2 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughPlayersException.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughPlayersException.cs @@ -4,7 +4,7 @@ namespace Poker.BE.Domain.Utility.Exceptions { [Serializable] - internal class NotEnoughPlayersException : Exception + public class NotEnoughPlayersException : PokerException { public NotEnoughPlayersException() { diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PokerException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PokerException.cs new file mode 100644 index 0000000..af698f9 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PokerException.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + public abstract class PokerException : Exception + { + public PokerException() + { + } + + public PokerException(string message) : base(message) + { + } + + public PokerException(string message, Exception innerException) : base(message, innerException) + { + } + + protected PokerException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} From a5f0916b8ae826b5c40fcd68e2447293085be8ca Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 13 May 2017 21:46:29 +0300 Subject: [PATCH 02/49] start working on the tests of game center --- .../Core/GameCenterTests.cs | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index ac00150..ee39b69 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Poker.BE.Domain.Core; +using Poker.BE.Domain.Game; using System; using System.Collections.Generic; using System.Linq; @@ -14,10 +15,18 @@ public class GameCenterTests #region Set Up public TestContext TestContext { get; set; } + private GameCenter gameCenter; - public GameCenterTests() + [TestInitialize] + public void Before() { - + gameCenter = new GameCenter(); + } + + [TestCleanup] + public void After() + { + gameCenter = null; } #endregion @@ -61,8 +70,18 @@ public void FindRoomsByCriteriaTest3() [TestMethod()] public void EnterRoomTest() { - // TODO - throw new NotImplementedException(); + // TODO idan - continue from here + + //Arrange + var expected = default(Player); + var actual = default(Player); + + Room room = null; + //Act + actual = gameCenter.EnterRoom(room); + + //Assert + Assert.AreEqual(expected, actual); } [TestMethod()] From 421ce8046c2a0bad2d9bbd9e3d7493f3c1f454a6 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 14 May 2017 22:13:48 +0300 Subject: [PATCH 03/49] leagues level, enter a room. create a room --- .../Core/GameCenterTests.cs | 6 +- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 66 +++++++++++++++---- Poker.BE/Poker.BE.Domain/Game/League.cs | 32 ++++++++- Poker.BE/Poker.BE.Domain/Game/Room.cs | 28 +++++--- .../Exceptions/LevelNotFoundException.cs | 25 +++++++ 5 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/LevelNotFoundException.cs diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index ee39b69..0ad8307 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -70,13 +70,11 @@ public void FindRoomsByCriteriaTest3() [TestMethod()] public void EnterRoomTest() { - // TODO idan - continue from here - //Arrange - var expected = default(Player); + var expected = new Player(); var actual = default(Player); + Room room = new Room(new Player()); - Room room = null; //Act actual = gameCenter.EnterRoom(room); diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index d85e652..64e0477 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -1,4 +1,5 @@ using Poker.BE.Domain.Game; +using Poker.BE.Domain.Utility.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -30,7 +31,6 @@ public enum Move #region Fields private IDictionary playersManager; private IDictionary roomsManager; - private IDictionary leaguesManager; private ICollection leagues; #endregion @@ -45,7 +45,6 @@ public GameCenter() { playersManager = new Dictionary(); roomsManager = new Dictionary(); - leaguesManager = new Dictionary(); leagues = new List(); } #endregion @@ -63,16 +62,10 @@ private Player CreatePlayer(Room room) return room.CreatePlayer(); } - private void AddLeage(int level) + private void AddLeage(int minLevel, int maxLevel) { - var league = new League(); + var league = new League() { MaxLevel = maxLevel, MinLevel = minLevel }; leagues.Add(league); - leaguesManager.Add(level, league); - } - - private void BindRoomToLeague(Room room, League league) - { - roomsManager.Add(room, league); } private bool RemovePlayer(Player player) @@ -131,6 +124,42 @@ private void BindPlayerToRoom(Player player, Room room) { playersManager.Add(player, room); } + + private void BindRoomToLeague(Room room, League league) + { + roomsManager.Add(room, league); + } + + private bool SetLeagueLevelRange(int minlevel, int maxLevel, League league) + { + if (!leagues.Contains(league)) return false; + + league.MaxLevel = maxLevel; + league.MinLevel = minlevel; + + return true; + } + + /// + /// look up for leagues that their level rang contains the requested level + /// + /// the user level + /// a collection of leagues + private ICollection GetAllLeaguesAtLevel(int level) + { + var result = new List(); + + foreach(var league in leagues) + { + if(league.MinLevel < level & league.MaxLevel > level) + { + result.Add(league); + } + } + + return result; + } + #endregion #region Methods @@ -193,8 +222,21 @@ public Player EnterRoom(Room room) /// the new created room public Room CreateNewRoom(int level, GameConfig config) { - // TODO - throw new NotImplementedException(); + var creator = new Player(); + + // creating the room and adding the creator as a passive player to it. + var room = new Room(creator, config); + BindPlayerToRoom(creator, room); + + var league = leaguesManager[level] as League; + + if (league == null) + throw new LevelNotFoundException("Requested level: " + level); + + // bind + BindRoomToLeague(room, league); + + return room; } /// diff --git a/Poker.BE/Poker.BE.Domain/Game/League.cs b/Poker.BE/Poker.BE.Domain/Game/League.cs index eb659d0..470bfd6 100644 --- a/Poker.BE/Poker.BE.Domain/Game/League.cs +++ b/Poker.BE/Poker.BE.Domain/Game/League.cs @@ -8,13 +8,41 @@ namespace Poker.BE.Domain.Game { public class League { + // TODO: complete - set team member to do this - Gal? + #region Constants + public const int MAX_LEVEL = 100; + public const int MIN_LEVEL = 1; + #endregion + + #region Fields + #endregion + + #region Properties public ICollection Rooms { get; set; } + public int MaxLevel { get; set; } + public int MinLevel { get; set; } + #endregion + + #region Constructors + public League() + { + Rooms = new List(); + MaxLevel = MAX_LEVEL; + MinLevel = MIN_LEVEL; + } + #endregion + + #region Private Functions - // TODO: complete - set team member to do this - internal void RemoveRoom(Room room) + #endregion + + #region Methods + public void RemoveRoom(Room room) { // TODO throw new NotImplementedException(); } + #endregion + } } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 83a6672..07e2bb6 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -47,9 +47,12 @@ public ICollection PassivePlayers .ToList(); } } - public ICollection Players { get { return activeAndPassivePlayers; } } - + public bool IsSpactatorsAllowd { get; } + public int MaxNumberOfPlayers { get; private set; } + public int MinNumberOfPlayers { get; private set; } + public double MinimumBet { get; private set; } + #endregion #region Constructors @@ -71,12 +74,6 @@ private Room() } - internal void RemovePlayer(Player player) - { - // TODO - throw new NotImplementedException(); - } - /// /// UC003 Create a new room /// @@ -87,8 +84,6 @@ public Room(Player creator) : this() activeAndPassivePlayers.Add(creator); } - - /// /// UC003 Create a new room /// @@ -100,9 +95,22 @@ public Room(Player creator, GamePreferences preferences) : this(creator) Preferences = preferences; } + public Room(Player creator, GameConfig config) : this(creator, config.GamePrefrences) + { + IsSpactatorsAllowd = config.IsSpactatorsAllowd; + MaxNumberOfPlayers = config.MaxNumberOfPlayers; + MinNumberOfPlayers = config.MinNumberOfPlayers; + MinimumBet = config.MinimumBet; + } + #endregion #region Methods + public void RemovePlayer(Player player) + { + // TODO + throw new NotImplementedException(); + } public void ClearAll() { //TODO: this function used be gameCenter do delete the room. all players and other resources of room need to be deleted. diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/LevelNotFoundException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/LevelNotFoundException.cs new file mode 100644 index 0000000..d95400a --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/LevelNotFoundException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class LevelNotFoundException : PokerException + { + public LevelNotFoundException() + { + } + + public LevelNotFoundException(string message) : base(message) + { + } + + public LevelNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + + protected LevelNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file From 004f14bd963806337272fe53dbb071c7b8e715f3 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 14 May 2017 22:43:22 +0300 Subject: [PATCH 04/49] room creation --- .../Poker.BE.Domain.Tests.csproj | 1 + Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 16 ++++++++-------- Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj index 0247587..8ac3c26 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj +++ b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj @@ -45,6 +45,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 64e0477..71370b0 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -110,12 +110,6 @@ private bool RemoveLeague(League league) league.Rooms.Clear(); } - // remove all occurrences of this league at the league's dictionary - foreach (var key in leaguesManager.Where(pair => pair.Value == league).Select(pair => pair.Key)) - { - leaguesManager.Remove(new KeyValuePair(key, league)); - } - // remove the league from the league collection (field) return leagues.Remove(league); } @@ -160,6 +154,12 @@ private ICollection GetAllLeaguesAtLevel(int level) return result; } + private League FindLeagueToFill(ICollection collection) + { + // TODO + throw new NotImplementedException(); + } + #endregion #region Methods @@ -228,12 +228,12 @@ public Room CreateNewRoom(int level, GameConfig config) var room = new Room(creator, config); BindPlayerToRoom(creator, room); - var league = leaguesManager[level] as League; + League league = FindLeagueToFill(GetAllLeaguesAtLevel(level)); if (league == null) throw new LevelNotFoundException("Requested level: " + level); - // bind + league.Rooms.Add(room); BindRoomToLeague(room, league); return room; diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index a5b0de5..46aebd9 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -56,6 +56,7 @@ + From bdcdfc9aae1c7689e4635661cae4189c813ffc32 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 15 May 2017 21:13:53 +0300 Subject: [PATCH 05/49] adding 2 exceptionsworking on gamecenter, game config min/max enforcement --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 30 ++++++++++-- Poker.BE/Poker.BE.Domain/Game/Chair.cs | 4 +- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 49 ++++++++++++++++--- Poker.BE/Poker.BE.Domain/Game/Room.cs | 18 +++++-- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 2 + .../Utility/Exceptions/PlayerModeException.cs | 25 ++++++++++ .../Exceptions/RoomNotFoundException.cs | 25 ++++++++++ 7 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerModeException.cs create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomNotFoundException.cs diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 71370b0..a8019de 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -143,9 +143,9 @@ private ICollection GetAllLeaguesAtLevel(int level) { var result = new List(); - foreach(var league in leagues) + foreach (var league in leagues) { - if(league.MinLevel < level & league.MaxLevel > level) + if (league.MinLevel < level & league.MaxLevel > level) { result.Add(league); } @@ -156,7 +156,7 @@ private ICollection GetAllLeaguesAtLevel(int level) private League FindLeagueToFill(ICollection collection) { - // TODO + // TODO: pick a league to insert the new created room, by the relevant requirements. throw new NotImplementedException(); } @@ -244,13 +244,35 @@ public Room CreateNewRoom(int level, GameConfig config) /// /// UC020: Join Next Hand /// - /// + /// public void JoinNextHand(Player player) { + /* Checking Preconditions */ + + // get the room of the player belongs to + if (!playersManager.TryGetValue(player, out Room room)) + { + throw new RoomNotFoundException("Error: Try joining next hand for a player with room that can't be found"); + } + + // check the user is a spectator + if (player.CurrentState != Player.State.Passive) + { + throw new PlayerModeException("Error: Player is trying to join a hand, but he's not a spectator!"); + } + + + // TODO throw new NotImplementedException(); } + /// + /// Allow the player to be a spectator over the table at the room, after playing (get up from the table) + /// + /// UC013: Stand Up To Spectate + /// + /// public void StandUpToSpactate(Player player) { // TODO diff --git a/Poker.BE/Poker.BE.Domain/Game/Chair.cs b/Poker.BE/Poker.BE.Domain/Game/Chair.cs index 873bf36..8f6517c 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Chair.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Chair.cs @@ -8,9 +8,7 @@ namespace Poker.BE.Domain.Game { public class Chair { - #region Constants - public static readonly int NCHAIRS_IN_ROOM = 10; - #endregion + #region Fields private int index; diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index 30dcd14..0658edd 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -8,26 +8,63 @@ public class GameConfig { #region Properties + /// /// Choose between 3 game modes: “Limit Hold’em”, “No-Limit Hold’em” and “Pot Limit Hold’em”. when ‘Limit Hold’em’ is chosen the user is requested to enter a max bet limit. /// public GamePreferences GamePrefrences { get; set; } + /// /// choose whether spectating a game is allowed or not. /// - public bool IsSpactatorsAllowd { get; set; } + public bool IsSpactatorsAllowed + { + get { return IsSpactatorsAllowed; } + set + { + // when spectator not allowed, enforce it on max number of players + if (!value & MaxNumberOfPlayers > MaxNumberOfActivePlayers) + { + MaxNumberOfPlayers = MaxNumberOfActivePlayers; + } + } + } + /// - /// choose the min amount of players in the table. + /// choose the min amount of players in the room. (active + passive) /// - public int MinNumberOfPlayers { get; set; } + public int MinNumberOfPlayers + { + get { return MinNumberOfPlayers; } + // enforce min >= 2 + set { MinNumberOfPlayers = (value < 2) ? 2 : value; } + } + /// - /// choose the min amount of players in the table. + /// choose the min amount of players in the room. (active + passive) /// - public int MaxNumberOfPlayers { get; set; } + public int MaxNumberOfPlayers + { + get { return MaxNumberOfPlayers; } + set + { + // enforce max number of players when spectators not allowed. + MaxNumberOfPlayers = + (!IsSpactatorsAllowed & value > MaxNumberOfActivePlayers) ? + MaxNumberOfActivePlayers : value; + } + } + + /// + /// choose the max amount of players in the table (active players) + /// + public int MaxNumberOfActivePlayers { get; set; } + /// /// choose the cost of joining the game. /// - public double BuyInCost { get; set;} + public double BuyInCost { get; set; } + /// /// The creator sets the big blind of the room’s table. /// diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 07e2bb6..7b46346 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -16,6 +16,10 @@ namespace Poker.BE.Domain.Game /// public class Room { + #region Constants + public const int NCHAIRS_IN_ROOM = 10; + #endregion + #region Fields private ICollection activeAndPassivePlayers; private Deck deck; @@ -51,6 +55,14 @@ public ICollection PassivePlayers public bool IsSpactatorsAllowd { get; } public int MaxNumberOfPlayers { get; private set; } public int MinNumberOfPlayers { get; private set; } + public int MaxNumberOfActivePlayers + { + get { return MaxNumberOfActivePlayers; } + private set + { + MaxNumberOfActivePlayers = (value > NCHAIRS_IN_ROOM) ? NCHAIRS_IN_ROOM : value; + } + } public double MinimumBet { get; private set; } #endregion @@ -60,9 +72,9 @@ private Room() { activeAndPassivePlayers = new List(); deck = new Deck(); - chairs = new Chair[Chair.NCHAIRS_IN_ROOM]; + chairs = new Chair[NCHAIRS_IN_ROOM]; - for (int i = 0; i < Chair.NCHAIRS_IN_ROOM; i++) + for (int i = 0; i < NCHAIRS_IN_ROOM; i++) { Chairs.ToArray()[i] = new Chair(i); } @@ -97,7 +109,7 @@ public Room(Player creator, GamePreferences preferences) : this(creator) public Room(Player creator, GameConfig config) : this(creator, config.GamePrefrences) { - IsSpactatorsAllowd = config.IsSpactatorsAllowd; + IsSpactatorsAllowd = config.IsSpactatorsAllowed; MaxNumberOfPlayers = config.MaxNumberOfPlayers; MinNumberOfPlayers = config.MinNumberOfPlayers; MinimumBet = config.MinimumBet; diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index 46aebd9..88e73d9 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -68,7 +68,9 @@ + + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerModeException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerModeException.cs new file mode 100644 index 0000000..283d581 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerModeException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class PlayerModeException : PokerException + { + public PlayerModeException() + { + } + + public PlayerModeException(string message) : base(message) + { + } + + public PlayerModeException(string message, Exception innerException) : base(message, innerException) + { + } + + protected PlayerModeException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomNotFoundException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomNotFoundException.cs new file mode 100644 index 0000000..471e8fa --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomNotFoundException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class RoomNotFoundException : PokerException + { + public RoomNotFoundException() + { + } + + public RoomNotFoundException(string message) : base(message) + { + } + + public RoomNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + + protected RoomNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file From ef7fc4664a74a9099b5a827e3b0eccfe79fd53b2 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 15 May 2017 21:54:40 +0300 Subject: [PATCH 06/49] join next hand --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 28 +++++++++++++--- Poker.BE/Poker.BE.Domain/Game/Player.cs | 3 +- Poker.BE/Poker.BE.Domain/Game/Room.cs | 33 ++++++++++++++++++- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + .../Utility/Exceptions/RoomRulesException.cs | 25 ++++++++++++++ 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index a8019de..44e54ac 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -245,26 +245,44 @@ public Room CreateNewRoom(int level, GameConfig config) /// UC020: Join Next Hand /// /// - public void JoinNextHand(Player player) + public void JoinNextHand(Player player, int seatIndex, double buyIn) { /* Checking Preconditions */ // get the room of the player belongs to if (!playersManager.TryGetValue(player, out Room room)) { - throw new RoomNotFoundException("Error: Try joining next hand for a player with room that can't be found"); + throw new RoomNotFoundException("Try joining next hand for a player with room that can't be found"); } // check the user is a spectator if (player.CurrentState != Player.State.Passive) { - throw new PlayerModeException("Error: Player is trying to join a hand, but he's not a spectator!"); + throw new PlayerModeException("Player " + player.Nickname + " is trying to join a hand, but he's not a spectator!"); } + // check if table is full + if (room.IsTableFull) + { + throw new RoomRulesException("The Table is full."); + } + // the chosen seat is not taken + if (!room.TakeChair(player, seatIndex)) + { + throw new RoomRulesException("The seat is already taken, please try again"); + } + + // the user has enough money to buy in + if(buyIn < room.MinimumBet) + { + throw new RoomRulesException("Buy in amount is less then the minimum to join the table. Please insert more money!"); + } + + /* Joining the player to the next hand */ + + room.JoinPlayerToTable(player); - // TODO - throw new NotImplementedException(); } /// diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 17c13ae..dc75dbb 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -16,12 +16,13 @@ public enum State Passive } - public static readonly int NPRIVATE_CARDS = 2; + public const int NPRIVATE_CARDS = 2; #endregion #region Properties public State CurrentState { get; private set; } public Card[] PrivateCards { get; set; } + public string Nickname { get; set; } #endregion #region Constructors diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 7b46346..fe7864f 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -60,10 +60,18 @@ public int MaxNumberOfActivePlayers get { return MaxNumberOfActivePlayers; } private set { + // enforce number of active players < number of chairs. MaxNumberOfActivePlayers = (value > NCHAIRS_IN_ROOM) ? NCHAIRS_IN_ROOM : value; } } public double MinimumBet { get; private set; } + public bool IsTableFull + { + get + { + return ActivePlayers.Count == MaxNumberOfActivePlayers; + } + } #endregion @@ -118,6 +126,18 @@ public Room(Player creator, GameConfig config) : this(creator, config.GamePrefre #endregion #region Methods + /// + /// Converting passive player to active player. + /// precondition: the player must be a passive player at the room. + /// postcondition: the player is active player at the room. + /// + /// a passive player at the room + public void JoinPlayerToTable(Player player) + { + throw new NotImplementedException(); + } + + public void RemovePlayer(Player player) { // TODO @@ -141,7 +161,17 @@ public void StartNewHand() CurrentHand = new Hand(deck, ActivePlayers); } - // TODO: Take a chair, leave a chair - will chairsSemaphore.WaitOne() or Release() + public bool TakeChair(Player player, int index) + { + // TODO + throw new NotImplementedException(); + } + + public bool LeaveChair(Player player) + { + // TODO + throw new NotImplementedException(); + } public void SendMessage() { @@ -159,6 +189,7 @@ private void ReleaseAChair(int index) { chairs[index].Release(); } + #endregion diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index 88e73d9..e222c39 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -71,6 +71,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs new file mode 100644 index 0000000..1dd790b --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class RoomRulesException : Exception + { + public RoomRulesException() + { + } + + public RoomRulesException(string message) : base(message) + { + } + + public RoomRulesException(string message, Exception innerException) : base(message, innerException) + { + } + + protected RoomRulesException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file From 9b4506344c4b6b1ee94c33d208d8f090db7a5a29 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 15 May 2017 23:40:52 +0300 Subject: [PATCH 07/49] wallet: - bank and wallet extend same abstruct class. - todo: adapt bank to this change. - implementing features to game center, room, player. related to 'join next hand' --- Poker.BE/Poker.BE.Domain/Core/Bank.cs | 4 +- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 8 +- Poker.BE/Poker.BE.Domain/Game/Player.cs | 22 +++- Poker.BE/Poker.BE.Domain/Game/Room.cs | 8 +- Poker.BE/Poker.BE.Domain/Game/Wallet.cs | 3 +- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 2 + .../Exceptions/NotEnoughMoneyException.cs | 25 +++++ .../Poker.BE.Domain/Utility/Logger/ILogger.cs | 3 +- .../Poker.BE.Domain/Utility/MoneyStorage.cs | 101 ++++++++++++++++++ 9 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs create mode 100644 Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs diff --git a/Poker.BE/Poker.BE.Domain/Core/Bank.cs b/Poker.BE/Poker.BE.Domain/Core/Bank.cs index 8ae1abd..3b979f1 100644 --- a/Poker.BE/Poker.BE.Domain/Core/Bank.cs +++ b/Poker.BE/Poker.BE.Domain/Core/Bank.cs @@ -6,8 +6,10 @@ namespace Poker.BE.Domain.Core { - public class Bank + public class Bank : Utility.MoneyStorage { + // UNDONE: make this class align with extending MoneyStorage Class. + #region Properties protected double Money { get; set;} diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 44e54ac..f88866b 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -270,19 +270,17 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) // the chosen seat is not taken if (!room.TakeChair(player, seatIndex)) { - throw new RoomRulesException("The seat is already taken, please try again"); + throw new RoomRulesException("The seat is already taken, please try again."); } // the user has enough money to buy in if(buyIn < room.MinimumBet) { - throw new RoomRulesException("Buy in amount is less then the minimum to join the table. Please insert more money!"); + throw new NotEnoughMoneyException("Buy in amount is less then the minimum to join the table."); } /* Joining the player to the next hand */ - - room.JoinPlayerToTable(player); - + room.JoinPlayerToTable(player, buyIn); } /// diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index dc75dbb..78850d2 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -15,12 +15,16 @@ public enum State ActiveFolded, Passive } - public const int NPRIVATE_CARDS = 2; #endregion + #region Fields + private Wallet wallet = default(Wallet); + #endregion + #region Properties public State CurrentState { get; private set; } + public double Wallet { get { return wallet.Value; } private set { } } public Card[] PrivateCards { get; set; } public string Nickname { get; set; } #endregion @@ -30,6 +34,22 @@ public Player() { PrivateCards = new Card[NPRIVATE_CARDS]; CurrentState = State.Passive; + wallet = new Wallet(); + Wallet = 0.0; + } + + public bool JoinToTable(double buyIn) + { + if (CurrentState != State.Passive) + { + return false; + } + + // buy in to wallet + Wallet = buyIn; + + CurrentState = State.ActiveUnfolded; + return true; } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index fe7864f..8e49b46 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -132,11 +132,13 @@ public Room(Player creator, GameConfig config) : this(creator, config.GamePrefre /// postcondition: the player is active player at the room. /// /// a passive player at the room - public void JoinPlayerToTable(Player player) + public bool JoinPlayerToTable(Player player, double buyIn) { - throw new NotImplementedException(); - } + if (!PassivePlayers.Contains(player)) + return false; + return player.JoinToTable(buyIn); + } public void RemovePlayer(Player player) { diff --git a/Poker.BE/Poker.BE.Domain/Game/Wallet.cs b/Poker.BE/Poker.BE.Domain/Game/Wallet.cs index 873f318..f9fe519 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Wallet.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Wallet.cs @@ -6,9 +6,8 @@ namespace Poker.BE.Domain.Game { - public class Wallet + public class Wallet : Utility.MoneyStorage { - // TODO: complete - set team member to do this } } diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index e222c39..c60741c 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -57,6 +57,7 @@ + @@ -74,6 +75,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs new file mode 100644 index 0000000..9522e02 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class NotEnoughMoneyException : Exception + { + public NotEnoughMoneyException() + { + } + + public NotEnoughMoneyException(string message) : base(message) + { + } + + public NotEnoughMoneyException(string message, Exception innerException) : base(message, innerException) + { + } + + protected NotEnoughMoneyException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Utility/Logger/ILogger.cs b/Poker.BE/Poker.BE.Domain/Utility/Logger/ILogger.cs index 63fea37..dfd0b55 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Logger/ILogger.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Logger/ILogger.cs @@ -8,8 +8,7 @@ namespace Poker.BE.Domain.Utility.Logger { public interface ILogger { - /* Note: - * Every function should deliver: + /* Note: Every function should deliver: * time stamp * severity / priority - type & color of message * the message diff --git a/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs b/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs new file mode 100644 index 0000000..e1dc8d1 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Utility +{ + public abstract class MoneyStorage + { + #region Constants + public enum Currency + { + USD, + NIS, + CHIP + } + + #endregion + + #region Fields + /// + /// caching the gate value that we got from the service responsible to retrieve the gate value. + /// so don't need to call this service more than once (for a wallet). to improve performance. + /// + private Dictionary moneyGateCache = default(Dictionary); + + private Currency currency; + private double basicAmount; + #endregion + + #region Properties + /// + /// the evaluated value of the wallet - by currency exchange. + /// + public double Value + { + get + { + if (!moneyGateCache.ContainsKey(currency)) + { + moneyGateCache.Add(currency, RetrieveGate(currency)); + } + + return basicAmount * moneyGateCache[currency]; + } + set + { + if (!moneyGateCache.ContainsKey(currency)) + { + moneyGateCache.Add(currency, RetrieveGate(currency)); + } + basicAmount = value / moneyGateCache[currency]; + } + } + #endregion + + #region Constructors + public MoneyStorage() + { + currency = Currency.USD; + moneyGateCache = new Dictionary(); + basicAmount = 0; + } + + public MoneyStorage(Currency currency): this() + { + this.currency = currency; + } + + public MoneyStorage(Currency currency, double amount): this(currency) + { + basicAmount = amount; + } + #endregion + + #region Private Functions + private double RetrieveGate(Currency currency) + { + var result = default(double); + + switch (currency) + { + // UNDONE: call a service that gets the actual currency gate. + case Currency.USD: + case Currency.NIS: + case Currency.CHIP: + default: + result = 1.0; + break; + } + + return result; + } + #endregion + + #region Methods + + #endregion + } +} From 7114134f856ddbc168baf2b74494cad9599216b1 Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 00:06:19 +0300 Subject: [PATCH 08/49] uc013 stand up from table --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 23 +++++++++++-- Poker.BE/Poker.BE.Domain/Game/Hand.cs | 2 ++ Poker.BE/Poker.BE.Domain/Game/Player.cs | 9 +++++ Poker.BE/Poker.BE.Domain/Game/Room.cs | 33 ++++++++++++++++++- Poker.BE/Poker.BE.Domain/Game/Round.cs | 2 +- Poker.BE/Poker.BE.Domain/Game/Turn.cs | 2 +- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + .../Exceptions/NotEnoughMoneyException.cs | 2 +- .../Exceptions/NotPlayersTurnException.cs | 25 ++++++++++++++ .../Utility/Exceptions/RoomRulesException.cs | 2 +- Poker.BE/Poker.BE.sln | 2 +- 11 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotPlayersTurnException.cs diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index f88866b..cb7e523 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -286,13 +286,30 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) /// /// Allow the player to be a spectator over the table at the room, after playing (get up from the table) /// + /// remaining money from the wallet to transfer back to the user bank /// UC013: Stand Up To Spectate /// /// - public void StandUpToSpactate(Player player) + public double StandUpToSpactate(Player player) { - // TODO - throw new NotImplementedException(); + /* Checking Preconditions */ + + // the player is sitting at the table + if(!playersManager.TryGetValue(player, out Room room)) + { + throw new RoomNotFoundException("Unable to stand up - The player is not at the room"); + } + + // it's the player's turn + if (room.CurrentHand.CurrentRound.CurrentTurn.CurrentPlayer != player) + { + throw new NotPlayersTurnException("Unable to stand up"); + } + + /* Action - Make the player to stand up */ + + + return room.SeparatePlayerFromTable(player); } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Hand.cs b/Poker.BE/Poker.BE.Domain/Game/Hand.cs index d51646e..2543cd0 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Hand.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Hand.cs @@ -38,6 +38,8 @@ public Hand(Deck deck, ICollection players) this.activePlayers = players; this.pot = new Pot(); } + + public Round CurrentRound { get; internal set; } #endregion #region Methods diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 78850d2..5dd38fc 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -51,6 +51,15 @@ public bool JoinToTable(double buyIn) CurrentState = State.ActiveUnfolded; return true; } + /// + /// Make the player to leave the table, and return his remaining wallet money to the user bank + /// + /// the remaining wallet money + public double StandUp() + { + // TODO + throw new NotImplementedException(); + } #endregion } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 8e49b46..13c5570 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -134,17 +134,48 @@ public Room(Player creator, GameConfig config) : this(creator, config.GamePrefre /// a passive player at the room public bool JoinPlayerToTable(Player player, double buyIn) { - if (!PassivePlayers.Contains(player)) + if (player.CurrentState != Player.State.Passive) + { return false; + } return player.JoinToTable(buyIn); } + /// + /// Make the player to stand up from the table and returning the remaining money from his wallet + /// (back to the bank) + /// + /// UC013: Stand Up To Spectate + /// + /// the wallet of the player + public double SeparatePlayerFromTable(Player player) + { + double remainingMoney = 0; + + if (player.CurrentState == Player.State.Passive) + { + throw new PlayerModeException("Unable to stand up: Player already a spectator."); + } + + if (player.CurrentState == Player.State.ActiveUnfolded) + { + throw new PlayerModeException("Unable to stand up: Player needs to fold first."); + } + + remainingMoney = player.StandUp(); + return remainingMoney; + } + public void RemovePlayer(Player player) { // TODO throw new NotImplementedException(); } + + /// + /// Method as a destructor - delete all players and other resources from the room. + /// public void ClearAll() { //TODO: this function used be gameCenter do delete the room. all players and other resources of room need to be deleted. diff --git a/Poker.BE/Poker.BE.Domain/Game/Round.cs b/Poker.BE/Poker.BE.Domain/Game/Round.cs index 254ea46..3ffb3b9 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Round.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Round.cs @@ -9,6 +9,6 @@ namespace Poker.BE.Domain.Game public class Round { // TODO: complete - set team member to do this - + public Turn CurrentTurn { get; internal set; } } } diff --git a/Poker.BE/Poker.BE.Domain/Game/Turn.cs b/Poker.BE/Poker.BE.Domain/Game/Turn.cs index 945fe1b..2b24d86 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Turn.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Turn.cs @@ -9,6 +9,6 @@ namespace Poker.BE.Domain.Game public class Turn { // TODO: complete - set team member to do this - + public Player CurrentPlayer { get; internal set; } } } diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index c60741c..935857b 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -69,6 +69,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs index 9522e02..ce8851c 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotEnoughMoneyException.cs @@ -4,7 +4,7 @@ namespace Poker.BE.Domain.Utility.Exceptions { [Serializable] - public class NotEnoughMoneyException : Exception + public class NotEnoughMoneyException : PokerException { public NotEnoughMoneyException() { diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotPlayersTurnException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotPlayersTurnException.cs new file mode 100644 index 0000000..16d2cc0 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/NotPlayersTurnException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class NotPlayersTurnException : PokerException + { + public NotPlayersTurnException() + { + } + + public NotPlayersTurnException(string message) : base(message) + { + } + + public NotPlayersTurnException(string message, Exception innerException) : base(message, innerException) + { + } + + protected NotPlayersTurnException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs index 1dd790b..f64a414 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/RoomRulesException.cs @@ -4,7 +4,7 @@ namespace Poker.BE.Domain.Utility.Exceptions { [Serializable] - public class RoomRulesException : Exception + public class RoomRulesException : PokerException { public RoomRulesException() { diff --git a/Poker.BE/Poker.BE.sln b/Poker.BE/Poker.BE.sln index f22837d..d9be330 100644 --- a/Poker.BE/Poker.BE.sln +++ b/Poker.BE/Poker.BE.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.7 +VisualStudioVersion = 15.0.26430.6 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poker.BE.Domain", "Poker.BE.Domain\Poker.BE.Domain.csproj", "{FE034180-E60E-416E-8D0F-0D9155AC52CC}" EndProject From 406e5ece016dba14b2344f56680fa09fb3238909 Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 01:34:25 +0300 Subject: [PATCH 09/49] working on user exposing methods of gamecenter. todo: make the gamecenter a singleton --- .../Core/GameCenterTests.cs | 1 + Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 5 +- Poker.BE/Poker.BE.Domain/Core/User.cs | 111 ++++++++++++++++-- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 5 + Poker.BE/Poker.BE.Domain/Game/Player.cs | 24 ++-- Poker.BE/Poker.BE.Domain/Game/Room.cs | 29 +---- 6 files changed, 134 insertions(+), 41 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 0ad8307..1bc16c7 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -68,6 +68,7 @@ public void FindRoomsByCriteriaTest3() #endregion [TestMethod()] + [ExpectedException(typeof(Exception))] public void EnterRoomTest() { //Arrange diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index cb7e523..bd8f797 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -16,6 +16,7 @@ namespace Poker.BE.Domain.Core /// public class GameCenter { + // UNDONE: Make the GameCenter a Singleton! #region Constants public enum Move { @@ -307,9 +308,9 @@ public double StandUpToSpactate(Player player) } /* Action - Make the player to stand up */ - - return room.SeparatePlayerFromTable(player); + room.LeaveChair(player); + return player.StandUp(); } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 382c032..6d18082 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -1,4 +1,7 @@ -using System; +using Poker.BE.Domain.Game; +using Poker.BE.Domain.Utility.Exceptions; +using Poker.BE.Domain.Utility.Logger; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,22 +11,116 @@ namespace Poker.BE.Domain.Core { public class User : AbstractUser { + #region Fields + /* singletons */ + private GameCenter gameCenter; + private ILogger logger = Logger.Instance; + /* ---------- */ + + #endregion + + #region Properties + /// + /// Map for the player (user session) ID -> player at the given session + /// + /// + /// session ID is the ID we give for a screen the user opens. + /// this is a need because the user can play several screen at once. + /// thus, to play with different players at the same time. + /// + /// for now - the user cannot play as several players, at the same room. + /// - this option is blocked. + /// + public IDictionary Players { get; set; } + #endregion + #region Methods - public User (string userName, string password, double sumToDeposit){ + public User(string userName, string password, double sumToDeposit) + { UserName = userName; Password = password; - UserBank = new Bank (sumToDeposit); + UserBank = new Bank(sumToDeposit); IsConnected = true; + Players = new Dictionary(); } - public void Connect(){ + public void Connect() + { IsConnected = true; } - public void Disconnect(){ + public void Disconnect() + { IsConnected = false; } - #endregion - } + #region Game Play Methods + + #region Find an Existing Room + public ICollection FindRoomsByCriteria(int level) + { + // TODO + throw new NotImplementedException(); + } + + public ICollection FindRoomsByCriteria(Player player) + { + // TODO + throw new NotImplementedException(); + } + + public ICollection FindRoomsByCriteria(GamePreferences preferences) + { + // TODO + throw new NotImplementedException(); + } + + public ICollection FindRoomsByCriteria(double betSize) + { + // TODO + throw new NotImplementedException(); + } + #endregion + + public Player EnterRoom(Room room) + { + // check that the user doesn't have already a player in the room + if (Players.Count > 0 && room.Players.Count > 0) + { + var result = from player in room.Players + where Players.Values.Contains(player) + select player; + + if (result.Count() > 0) + { + throw new RoomRulesException("the user already play this room: " + room.Name); + } + } + + return + + } + + public Room CreateNewRoom(int level, GameConfig config) + { + // TODO + throw new NotImplementedException(); + } + + public void JoinNextHand(Player player, int seatIndex, double buyIn) + { + // TODO + throw new NotImplementedException(); + } + + public void StandUpToSpactate(int playerId) + { + // TODO + throw new NotImplementedException(); + } + #endregion + + + #endregion + } } diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index 0658edd..f5cb43a 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -70,6 +70,11 @@ public int MaxNumberOfPlayers /// public double MinimumBet { get; set; } + /// + /// the room name - set by the user. + /// + public string Name { get; set; } + #endregion } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 5dd38fc..6c55df2 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -1,4 +1,5 @@ -using System; +using Poker.BE.Domain.Utility.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -19,12 +20,12 @@ public enum State #endregion #region Fields - private Wallet wallet = default(Wallet); + private Wallet _wallet = default(Wallet); #endregion #region Properties public State CurrentState { get; private set; } - public double Wallet { get { return wallet.Value; } private set { } } + public double Wallet { get { return _wallet.Value; } private set { _wallet.Value = value; } } public Card[] PrivateCards { get; set; } public string Nickname { get; set; } #endregion @@ -34,7 +35,7 @@ public Player() { PrivateCards = new Card[NPRIVATE_CARDS]; CurrentState = State.Passive; - wallet = new Wallet(); + _wallet = new Wallet(); Wallet = 0.0; } @@ -57,10 +58,19 @@ public bool JoinToTable(double buyIn) /// the remaining wallet money public double StandUp() { - // TODO - throw new NotImplementedException(); + if (CurrentState == State.Passive) + { + throw new PlayerModeException("Unable to stand up: Player already a spectator."); + } + + if (CurrentState == State.ActiveUnfolded) + { + throw new PlayerModeException("Unable to stand up: Player needs to fold first."); + } + + return Wallet; } #endregion - } + }// class } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 13c5570..21cae37 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -30,6 +30,7 @@ public class Room // TODO: do we need ID for the Room? if so, what type should it be? 'long?' means nullable long. //public long? ID { get; } + public string Name { get; set; } public ICollection Chairs { get { return chairs; } } public Hand CurrentHand { get; private set; } public GamePreferences Preferences { get; set; } @@ -92,6 +93,8 @@ private Room() // Note: making default preferences to the room (poker game) Preferences = new GamePreferences(); + Name = "Unknown Room"; + } /// @@ -121,6 +124,7 @@ public Room(Player creator, GameConfig config) : this(creator, config.GamePrefre MaxNumberOfPlayers = config.MaxNumberOfPlayers; MinNumberOfPlayers = config.MinNumberOfPlayers; MinimumBet = config.MinimumBet; + Name = config.Name; } #endregion @@ -142,31 +146,6 @@ public bool JoinPlayerToTable(Player player, double buyIn) return player.JoinToTable(buyIn); } - /// - /// Make the player to stand up from the table and returning the remaining money from his wallet - /// (back to the bank) - /// - /// UC013: Stand Up To Spectate - /// - /// the wallet of the player - public double SeparatePlayerFromTable(Player player) - { - double remainingMoney = 0; - - if (player.CurrentState == Player.State.Passive) - { - throw new PlayerModeException("Unable to stand up: Player already a spectator."); - } - - if (player.CurrentState == Player.State.ActiveUnfolded) - { - throw new PlayerModeException("Unable to stand up: Player needs to fold first."); - } - - remainingMoney = player.StandUp(); - return remainingMoney; - } - public void RemovePlayer(Player player) { // TODO From 780b9a50dec9246bf91548a2c0ae0e0461ecab8d Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 02:25:42 +0300 Subject: [PATCH 10/49] game center is now a singleton. logger is thread safe (tests pass :) ) --- .../Core/GameCenterTests.cs | 2 +- .../Utility/Logger/LoggerTests.cs | 2 +- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 19 ++++-- Poker.BE/Poker.BE.Domain/Core/User.cs | 3 +- .../Poker.BE.Domain/Utility/Logger/Logger.cs | 63 ++++++++++++++----- 5 files changed, 68 insertions(+), 21 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 1bc16c7..04fa4c5 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -20,7 +20,7 @@ public class GameCenterTests [TestInitialize] public void Before() { - gameCenter = new GameCenter(); + gameCenter = GameCenter.Instance; } [TestCleanup] diff --git a/Poker.BE/Poker.BE.Domain.Tests/Utility/Logger/LoggerTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Utility/Logger/LoggerTests.cs index 988a706..a95d81d 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Utility/Logger/LoggerTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Utility/Logger/LoggerTests.cs @@ -90,7 +90,7 @@ public void DebugTest() public void ErrorTest() { //Arrange - string message = "test"; + string message = "test_errortest0"; string[] expected = new string[] { LogTestPrefix("Error", "High") + message + LogTestSuffix() }; //Act diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index bd8f797..dee8261 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -14,7 +14,7 @@ namespace Poker.BE.Domain.Core /// /// Idan Izicovich /// - public class GameCenter + public sealed class GameCenter { // UNDONE: Make the GameCenter a Singleton! #region Constants @@ -42,12 +42,23 @@ public enum Move #endregion #region Constructors - public GameCenter() + + #region Singleton + // for singleton implementation at C# + static GameCenter() { } + private static readonly GameCenter _instance = new GameCenter(); + public static GameCenter Instance { get { return _instance; } } + #endregion + + // all constructors needs to be private for being a singleton + private GameCenter() { playersManager = new Dictionary(); roomsManager = new Dictionary(); leagues = new List(); } + + #endregion #region Private Functions @@ -275,7 +286,7 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) } // the user has enough money to buy in - if(buyIn < room.MinimumBet) + if (buyIn < room.MinimumBet) { throw new NotEnoughMoneyException("Buy in amount is less then the minimum to join the table."); } @@ -296,7 +307,7 @@ public double StandUpToSpactate(Player player) /* Checking Preconditions */ // the player is sitting at the table - if(!playersManager.TryGetValue(player, out Room room)) + if (!playersManager.TryGetValue(player, out Room room)) { throw new RoomNotFoundException("Unable to stand up - The player is not at the room"); } diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 6d18082..4f0a437 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -97,7 +97,8 @@ where Players.Values.Contains(player) } } - return + // TODO + throw new NotImplementedException(); } diff --git a/Poker.BE/Poker.BE.Domain/Utility/Logger/Logger.cs b/Poker.BE/Poker.BE.Domain/Utility/Logger/Logger.cs index 5b2d592..4743dd1 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Logger/Logger.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Logger/Logger.cs @@ -16,8 +16,6 @@ namespace Poker.BE.Domain.Utility.Logger public sealed class Logger : ILogger { - //TODO: IMPORTANT - make the logger thread safe! (using the 'lock' keyword?) - #region Constants public static readonly string ENDL = "; "; public static readonly string DELIMITER = ","; // CSV delimiter @@ -75,37 +73,56 @@ private void OverwriteFile(string message) { try { - System.IO.File.WriteAllText(dirPath + filename, message); + lock (this) + { + File.WriteAllText(dirPath + filename, message); + } } catch (IOException e) { //Note: we don't know if we have console to that Console.WriteLine(e.Message + ":\n\n" + e.StackTrace); } + catch (Exception) + { + // NOTE: do nothing at total fail, to avoid crash. + return; + } } private void OverwriteFile(string[] message) { try { - System.IO.File.WriteAllLines(dirPath + filename, message); + lock (this) + { + File.WriteAllLines(dirPath + filename, message); + } } catch (IOException e) { //Note: we don't know if we have console to that Console.WriteLine(e.Message + ":\n\n" + e.StackTrace); } + catch (Exception) + { + // NOTE: do nothing at total fail, to avoid crash. + return; + } } private void AppendToFile(string message) { try { - using (System.IO.StreamWriter file = - new System.IO.StreamWriter(dirPath + filename, true)) + lock (this) { - file.Write(message); - file.Write("\n"); + using (StreamWriter file = + new StreamWriter(dirPath + filename, true)) + { + file.Write(message); + file.Write("\n"); + } } } catch (IOException e) @@ -113,20 +130,28 @@ private void AppendToFile(string message) //Note: we don't know if we have console to that Console.WriteLine(e.Message + ":\n\n" + e.StackTrace); } + catch (Exception) + { + // NOTE: do nothing at total fail, to avoid crash. + return; + } } private void AppendToFile(string[] message) { try { - using (System.IO.StreamWriter file = - new System.IO.StreamWriter(dirPath + filename, true)) + lock (this) { - foreach (string str in message) + using (StreamWriter file = + new StreamWriter(dirPath + filename, true)) { - file.Write(message); - } - file.Write("\n"); + foreach (string str in message) + { + file.Write(message); + } + file.Write("\n"); + } } } catch (IOException e) @@ -134,6 +159,11 @@ private void AppendToFile(string[] message) //Note: we don't know if we have console to that Console.WriteLine(e.Message + ":\n\n" + e.StackTrace); } + catch (Exception) + { + // NOTE: do nothing at total fail, to avoid crash. + return; + } } private string[] ReadFromFile() @@ -148,6 +178,11 @@ private string[] ReadFromFile() Console.WriteLine(e.Message + ":\n\n" + e.StackTrace); return null; } + catch (Exception) + { + // NOTE: do nothing at total fail, to avoid crash. + return null; + } } //made this public for easy testing From 4d402ff3c3f19782c264598a8286438a70ffd390 Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 02:42:19 +0300 Subject: [PATCH 11/49] user.enter room --- Poker.BE/Poker.BE.Domain/Core/User.cs | 29 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 4f0a437..d0aef50 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -13,7 +13,7 @@ public class User : AbstractUser { #region Fields /* singletons */ - private GameCenter gameCenter; + private GameCenter gameCenter = GameCenter.Instance; private ILogger logger = Logger.Instance; /* ---------- */ @@ -21,7 +21,8 @@ public class User : AbstractUser #region Properties /// - /// Map for the player (user session) ID -> player at the given session + /// Map for the player (user session) ID -> player at the given session. + /// using the player.GetHashCode() for generating this ID. /// /// /// session ID is the ID we give for a screen the user opens. @@ -34,7 +35,7 @@ public class User : AbstractUser public IDictionary Players { get; set; } #endregion - #region Methods + #region Constructors public User(string userName, string password, double sumToDeposit) { UserName = userName; @@ -43,6 +44,10 @@ public User(string userName, string password, double sumToDeposit) IsConnected = true; Players = new Dictionary(); } + #endregion + + #region Methods + public void Connect() { @@ -82,7 +87,7 @@ public ICollection FindRoomsByCriteria(double betSize) } #endregion - public Player EnterRoom(Room room) + public int EnterRoom(Room room) { // check that the user doesn't have already a player in the room if (Players.Count > 0 && room.Players.Count > 0) @@ -97,12 +102,18 @@ where Players.Values.Contains(player) } } - // TODO - throw new NotImplementedException(); - + // entering the room + var freshPlayer = gameCenter.EnterRoom(room); + + // logging + logger.Log(string.Format("User {0} has player {1}",UserName, freshPlayer.GetHashCode()), this, "Medium"); + + Players.Add(freshPlayer.GetHashCode(), freshPlayer); + + return freshPlayer.GetHashCode(); } - public Room CreateNewRoom(int level, GameConfig config) + public void CreateNewRoom(int level, GameConfig config) { // TODO throw new NotImplementedException(); @@ -114,7 +125,7 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) throw new NotImplementedException(); } - public void StandUpToSpactate(int playerId) + public void StandUpToSpactate(int sessionID) { // TODO throw new NotImplementedException(); From 5cbaf5764f8550dfdaa4d6997f3b1e5134085a1d Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 03:50:38 +0300 Subject: [PATCH 12/49] player test - pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 78 ++++++++++++++++++- .../Poker.BE.Domain.Tests.csproj | 1 + Poker.BE/Poker.BE.Domain/Core/User.cs | 24 +++--- Poker.BE/Poker.BE.Domain/Game/Player.cs | 4 +- 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index f7ddfa8..1cf3aff 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -17,7 +17,7 @@ public class RoomTests public TestContext TestContext { get { return testContext; } set { testContext = value; } } [TestMethod] - public void RoomTest() + public void RoomTest()// (:Player, :Preferences) { //Arrange Player player = new Player(); @@ -30,5 +30,81 @@ public void RoomTest() TestContext.WriteLine("end of roomTest"); } + [TestMethod()] + public void RoomTest1() // () + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void RoomTest2() // (:Player) + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void RoomTest3() // (:Player, :GameConfig) + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void JoinPlayerToTableTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void RemovePlayerTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void ClearAllTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void CreatePlayerTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void StartNewHandTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void TakeChairTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void LeaveChairTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void SendMessageTest() + { + // TODO + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj index 8ac3c26..1101dbb 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj +++ b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj @@ -49,6 +49,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index d0aef50..a9ac238 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -48,7 +48,7 @@ public User(string userName, string password, double sumToDeposit) #region Methods - + // TODO: for Ariel - what is this 2 methods? public void Connect() { IsConnected = true; @@ -59,8 +59,7 @@ public void Disconnect() IsConnected = false; } - #region Game Play Methods - + #region Find an Existing Room public ICollection FindRoomsByCriteria(int level) { @@ -87,6 +86,14 @@ public ICollection FindRoomsByCriteria(double betSize) } #endregion + /// + /// Allow the user to enter a room from a list as a spectator. + /// + /// to enter + /// + /// Session ID of the new player for this user, for the user to store as a cookie + /// + /// public int EnterRoom(Room room) { // check that the user doesn't have already a player in the room @@ -106,33 +113,32 @@ where Players.Values.Contains(player) var freshPlayer = gameCenter.EnterRoom(room); // logging - logger.Log(string.Format("User {0} has player {1}",UserName, freshPlayer.GetHashCode()), this, "Medium"); + logger.Log(string.Format("User {0} has player {1}", UserName, freshPlayer.GetHashCode()), + this, "Medium"); Players.Add(freshPlayer.GetHashCode(), freshPlayer); return freshPlayer.GetHashCode(); } - public void CreateNewRoom(int level, GameConfig config) + public bool CreateNewRoom(int level, GameConfig config) { // TODO throw new NotImplementedException(); } - public void JoinNextHand(Player player, int seatIndex, double buyIn) + public bool JoinNextHand(int sessionID, int seatIndex, double buyIn) { // TODO throw new NotImplementedException(); } - public void StandUpToSpactate(int sessionID) + public bool StandUpToSpactate(int sessionID) { // TODO throw new NotImplementedException(); } #endregion - - #endregion } } diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 6c55df2..da011bc 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -24,7 +24,7 @@ public enum State #endregion #region Properties - public State CurrentState { get; private set; } + public State CurrentState { get; protected set; } public double Wallet { get { return _wallet.Value; } private set { _wallet.Value = value; } } public Card[] PrivateCards { get; set; } public string Nickname { get; set; } @@ -68,8 +68,10 @@ public double StandUp() throw new PlayerModeException("Unable to stand up: Player needs to fold first."); } + CurrentState = State.Passive; return Wallet; } + #endregion }// class From 6547ec042908d99fe1a4f13c47a9a62cdc1e16ce Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 03:51:02 +0300 Subject: [PATCH 13/49] player test - pass --- .../Poker.BE.Domain.Tests/Game/PlayerTests.cs | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs new file mode 100644 index 0000000..53cdc43 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs @@ -0,0 +1,134 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Poker.BE.Domain.Game; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Game.Tests +{ + [TestClass()] + public class PlayerTests + { + #region Set Up + private Player player = default(Player); + + [TestInitialize] + public void Before() + { + player = new Player(); + } + + [TestCleanup] + public void After() + { + player = null; + } + #endregion + + + [TestMethod()] + public void PlayerTest() + { + // simple constructor + } + + [TestMethod()] + public void JoinToTableTest() + { + //Arrange + var expected1 = true; + var expected2 = Player.State.Passive; + var expected3 = Player.State.ActiveUnfolded; + + //Act + var actual2 = player.CurrentState; + var actual1 = player.JoinToTable(100.0); + var actual3 = player.CurrentState; + + //Assert + Assert.AreEqual(expected1, actual1); + Assert.AreEqual(expected2, actual2); + Assert.AreEqual(expected3, actual3); + } + + private class PlayerStub : Player + { + public void Fold() + { + CurrentState = State.ActiveFolded; + } + } + + [TestMethod()] + [ExpectedException(typeof(Utility.Exceptions.PlayerModeException))] + public void StandUpTest() // already a spectator + { + player = new PlayerStub(); + + //Arrange + var expected1 = 10.3; + var expected0 = Player.State.ActiveFolded; + var expected2 = Player.State.Passive; + + //Act + var actual0 = player.CurrentState; + var actual1 = player.StandUp(); + var actual2 = player.CurrentState; + + //Assert + Assert.AreEqual(expected0, actual0); + Assert.AreEqual(expected1, actual1); + Assert.AreEqual(expected2, actual2); + } + + [TestMethod()] + [ExpectedException(typeof(Utility.Exceptions.PlayerModeException))] + public void StandUpTest1() // need to fold first + { + player = new PlayerStub(); + + //Arrange + var expected1 = 10.3; + var expected0 = Player.State.ActiveFolded; + var expected2 = Player.State.Passive; + + //Act + var actual0 = player.CurrentState; + player.JoinToTable(10.3); + var actual1 = player.StandUp(); + var actual2 = player.CurrentState; + + //Assert + Assert.AreEqual(expected0, actual0); + Assert.AreEqual(expected1, actual1); + Assert.AreEqual(expected2, actual2); + } + + [TestMethod()] + public void StandUpTest2() // good + { + // shadowing + var player = new PlayerStub(); + + //Arrange + var expected1 = 10.3; + var expected0 = Player.State.ActiveFolded; + var expected2 = Player.State.Passive; + + //Act + player.JoinToTable(10.3); + player.Fold(); + + var actual0 = player.CurrentState; + var actual1 = player.StandUp(); + var actual2 = player.CurrentState; + + //Assert + Assert.AreEqual(expected0, actual0); + Assert.AreEqual(expected1, actual1); + Assert.AreEqual(expected2, actual2); + } + } +} \ No newline at end of file From 62ba03581460db3509232992b18760461a6704fe Mon Sep 17 00:00:00 2001 From: idanizi Date: Wed, 17 May 2017 16:45:10 +0300 Subject: [PATCH 14/49] service layer example --- Poker.BE/Poker.BE.API/Poker.BE.API.csproj | 12 ++++++------ .../Poker.BE.Service/IServices/IRoomsService.cs | 11 ++++++++++- .../Modules/Requests/EnterRoomRequest.cs | 10 ++++++++++ .../Modules/Requests/IRequest.cs | 12 ++++++++++++ .../Modules/Requests/JoinNextHandRequest.cs | 6 ++++++ .../Modules/Requests/NewRoomRequest.cs | 10 ++++++++++ .../Requests/StandUpToSpactateRequest.cs | 6 ++++++ .../Modules/Responses/IResponse.cs | 12 ++++++++++++ .../Modules/Results/EnterRoomResult.cs | 10 ++++++++++ .../Poker.BE.Service/Modules/Results/IResult.cs | 12 ++++++++++++ .../Modules/Results/JoinNextHandResult.cs | 6 ++++++ .../Modules/Results/NewRoomResult.cs | 16 ++++++++++++++++ .../Modules/Results/StandUpToSpactateResult.cs | 6 ++++++ .../Poker.BE.Service/Poker.BE.Service.csproj | 17 ++++++++++++++++- 14 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Requests/IRequest.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Requests/JoinNextHandRequest.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Requests/StandUpToSpactateRequest.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Results/JoinNextHandResult.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs create mode 100644 Poker.BE/Poker.BE.Service/Modules/Results/StandUpToSpactateResult.cs diff --git a/Poker.BE/Poker.BE.API/Poker.BE.API.csproj b/Poker.BE/Poker.BE.API/Poker.BE.API.csproj index 376e48f..d3bd6f9 100644 --- a/Poker.BE/Poker.BE.API/Poker.BE.API.csproj +++ b/Poker.BE/Poker.BE.API/Poker.BE.API.csproj @@ -261,18 +261,18 @@ - - - {fe034180-e60e-416e-8d0f-0d9155ac52cc} - Poker.BE.Domain - - + + + {fe55ce81-34e4-49dd-b43d-b62b2490dc34} + Poker.BE.Service + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs index c06c655..c2dbb2a 100644 --- a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs +++ b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs @@ -1,8 +1,17 @@ -namespace Poker.BE.Service.IServices +using Poker.BE.Domain.Game; +using Poker.BE.Service.Modules.Results; +using Poker.BE.Service.Modules.Requests; + + +namespace Poker.BE.Service.IServices { /// Supports UCC03: Rooms Management /// public interface IRoomsService { + NewRoomResult CreateNewRoom(NewRoomRequest request); + EnterRoomResult EnterRoom(EnterRoomRequest request); + JoinNextHandResult JoinNextHand(JoinNextHandRequest request); + StandUpToSpactateResult StandUpToSpactate(StandUpToSpactateRequest request); } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs new file mode 100644 index 0000000..002f248 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs @@ -0,0 +1,10 @@ +namespace Poker.BE.Service.Modules.Requests +{ + public class EnterRoomRequest :IRequest + { + /// + /// requested room hash code + /// + public int room { get; set; } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/IRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/IRequest.cs new file mode 100644 index 0000000..5bccc79 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/IRequest.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Service.Modules.Requests +{ + public interface IRequest + { + } +} diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/JoinNextHandRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/JoinNextHandRequest.cs new file mode 100644 index 0000000..b0b9fec --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/JoinNextHandRequest.cs @@ -0,0 +1,6 @@ +namespace Poker.BE.Service.Modules.Requests +{ + public class JoinNextHandRequest : IRequest + { + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs new file mode 100644 index 0000000..b6ca16b --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs @@ -0,0 +1,10 @@ +using Poker.BE.Domain.Game; + +namespace Poker.BE.Service.Modules.Requests +{ + public class NewRoomRequest : IRequest + { + public int Level { get; set; } + public GameConfig GameConfig { get; set; } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/StandUpToSpactateRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/StandUpToSpactateRequest.cs new file mode 100644 index 0000000..fd383ea --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/StandUpToSpactateRequest.cs @@ -0,0 +1,6 @@ +namespace Poker.BE.Service.Modules.Requests +{ + public class StandUpToSpactateRequest : IRequest + { + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs b/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs new file mode 100644 index 0000000..e4c7b2a --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Service.Modules.Responses +{ + public interface IResponse + { + } +} diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs new file mode 100644 index 0000000..11fd930 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs @@ -0,0 +1,10 @@ +namespace Poker.BE.Service.Modules.Results +{ + public class EnterRoomResult : IResult + { + /// + /// player hash code (session id) + /// + public int player { get; set; } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs new file mode 100644 index 0000000..70767b8 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Service.Modules.Results +{ + public interface IResult + { + } +} diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/JoinNextHandResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/JoinNextHandResult.cs new file mode 100644 index 0000000..9fff223 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Results/JoinNextHandResult.cs @@ -0,0 +1,6 @@ +namespace Poker.BE.Service.Modules.Results +{ + public class JoinNextHandResult : IResult + { + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs new file mode 100644 index 0000000..fed0d87 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Service.Modules.Results +{ + public class NewRoomResult : IResult + { + /// + /// room hash code + /// + public int Room { get; set; } + } +} diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/StandUpToSpactateResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/StandUpToSpactateResult.cs new file mode 100644 index 0000000..eb95357 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/Modules/Results/StandUpToSpactateResult.cs @@ -0,0 +1,6 @@ +namespace Poker.BE.Service.Modules.Results +{ + public class StandUpToSpactateResult : IResult + { + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj index 0fa4d15..913962e 100644 --- a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj +++ b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj @@ -47,6 +47,17 @@ + + + + + + + + + + + @@ -60,8 +71,12 @@ + - + + {fe034180-e60e-416e-8d0f-0d9155ac52cc} + Poker.BE.Domain + \ No newline at end of file From 3d933941bf265324047dbf03f1d87585af992e08 Mon Sep 17 00:00:00 2001 From: idanizi Date: Thu, 18 May 2017 13:55:25 +0300 Subject: [PATCH 15/49] renaming NewRoom request / result to CreateNewRoom... --- Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs | 2 +- .../Requests/{NewRoomRequest.cs => CreateNewRoomRequest.cs} | 2 +- .../Results/{NewRoomResult.cs => CreateNewRoomResult.cs} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename Poker.BE/Poker.BE.Service/Modules/Requests/{NewRoomRequest.cs => CreateNewRoomRequest.cs} (78%) rename Poker.BE/Poker.BE.Service/Modules/Results/{NewRoomResult.cs => CreateNewRoomResult.cs} (86%) diff --git a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs index c2dbb2a..fae3758 100644 --- a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs +++ b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs @@ -9,7 +9,7 @@ namespace Poker.BE.Service.IServices /// public interface IRoomsService { - NewRoomResult CreateNewRoom(NewRoomRequest request); + CreateNewRoomResult CreateNewRoom(CreateNewRoomRequest request); EnterRoomResult EnterRoom(EnterRoomRequest request); JoinNextHandResult JoinNextHand(JoinNextHandRequest request); StandUpToSpactateResult StandUpToSpactate(StandUpToSpactateRequest request); diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/CreateNewRoomRequest.cs similarity index 78% rename from Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs rename to Poker.BE/Poker.BE.Service/Modules/Requests/CreateNewRoomRequest.cs index b6ca16b..7f70fe5 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Requests/NewRoomRequest.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/CreateNewRoomRequest.cs @@ -2,7 +2,7 @@ namespace Poker.BE.Service.Modules.Requests { - public class NewRoomRequest : IRequest + public class CreateNewRoomRequest : IRequest { public int Level { get; set; } public GameConfig GameConfig { get; set; } diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/CreateNewRoomResult.cs similarity index 86% rename from Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs rename to Poker.BE/Poker.BE.Service/Modules/Results/CreateNewRoomResult.cs index fed0d87..1d76c69 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Results/NewRoomResult.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Results/CreateNewRoomResult.cs @@ -6,7 +6,7 @@ namespace Poker.BE.Service.Modules.Results { - public class NewRoomResult : IResult + public class CreateNewRoomResult : IResult { /// /// room hash code From 55087d18e00a90c78e20a118bf9be8dbd97966fe Mon Sep 17 00:00:00 2001 From: idanizi Date: Thu, 18 May 2017 14:25:52 +0300 Subject: [PATCH 16/49] service layer - to add - user not found exception --- Poker.BE/Poker.BE.Domain/Core/User.cs | 27 +++----- .../Modules/Requests/EnterRoomRequest.cs | 7 +- .../Poker.BE.Service/Poker.BE.Service.csproj | 4 +- .../Poker.BE.Service/Services/RoomsService.cs | 67 +++++++++++++++++++ 4 files changed, 84 insertions(+), 21 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index a9ac238..2110cd8 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -20,19 +20,8 @@ public class User : AbstractUser #endregion #region Properties - /// - /// Map for the player (user session) ID -> player at the given session. - /// using the player.GetHashCode() for generating this ID. - /// - /// - /// session ID is the ID we give for a screen the user opens. - /// this is a need because the user can play several screen at once. - /// thus, to play with different players at the same time. - /// - /// for now - the user cannot play as several players, at the same room. - /// - this option is blocked. - /// - public IDictionary Players { get; set; } + + public ICollection Players { get; set; } #endregion #region Constructors @@ -42,7 +31,7 @@ public User(string userName, string password, double sumToDeposit) Password = password; UserBank = new Bank(sumToDeposit); IsConnected = true; - Players = new Dictionary(); + Players = new List(); } #endregion @@ -94,18 +83,20 @@ public ICollection FindRoomsByCriteria(double betSize) /// Session ID of the new player for this user, for the user to store as a cookie /// /// - public int EnterRoom(Room room) + public Player EnterRoom(Room room) { // check that the user doesn't have already a player in the room if (Players.Count > 0 && room.Players.Count > 0) { var result = from player in room.Players - where Players.Values.Contains(player) + where Players.Contains(player) select player; if (result.Count() > 0) { - throw new RoomRulesException("the user already play this room: " + room.Name); + throw new RoomRulesException( + string.Format("the user already play this room: {0} with player nickname: {1}", + room.Name, result.First().Nickname)); } } @@ -118,7 +109,7 @@ where Players.Values.Contains(player) Players.Add(freshPlayer.GetHashCode(), freshPlayer); - return freshPlayer.GetHashCode(); + return freshPlayer; } public bool CreateNewRoom(int level, GameConfig config) diff --git a/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs b/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs index 002f248..d811b63 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Requests/EnterRoomRequest.cs @@ -5,6 +5,11 @@ public class EnterRoomRequest :IRequest /// /// requested room hash code /// - public int room { get; set; } + public int Room { get; set; } + + /// + /// requesting user hash code + /// + public int User { get; set; } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj index 913962e..7b805d6 100644 --- a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj +++ b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj @@ -50,13 +50,13 @@ - + - + diff --git a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs index 901748f..801eb4d 100644 --- a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs +++ b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs @@ -3,10 +3,77 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Poker.BE.Service.Modules.Requests; +using Poker.BE.Service.Modules.Results; +using Poker.BE.Domain.Game; +using Poker.BE.Domain.Utility.Exceptions; +using Poker.BE.Domain.Core; namespace Poker.BE.Service.Services { public class RoomsService : IServices.IRoomsService { + + #region Properties + /// + /// Map for the player (user session) ID -> player at the given session. + /// using the player.GetHashCode() for generating this ID. + /// + /// + /// session ID is the ID we give for a screen the user opens. + /// this is a need because the user can play several screen at once. + /// thus, to play with different players at the same time. + /// + /// for now - the user cannot play as several players, at the same room. + /// - this option is blocked. + /// + public IDictionary Players { get; set; } + public IDictionary Rooms { get; set; } + public IDictionary Users { get; set; } + #endregion + + + public CreateNewRoomResult CreateNewRoom(CreateNewRoomRequest request) + { + // TODO + throw new NotImplementedException(); + } + + public EnterRoomResult EnterRoom(EnterRoomRequest request) + { + var result = default(EnterRoomResult); + + if (!Rooms.TryGetValue(request.Room, out Room room)) + { + throw new RoomNotFoundException(string.Format("Requested room ID {0} not found", request.Room)); + } + + if(!Users.TryGetValue(request.User, out User user)) + { + throw new UserNotFoundException(); + } + + result = new EnterRoomResult() + { + player = request.User + }; + + + // UNDONE - idan - continue from here 18/5 + + return result; + } + + public JoinNextHandResult JoinNextHand(JoinNextHandRequest request) + { + // TODO + throw new NotImplementedException(); + } + + public StandUpToSpactateResult StandUpToSpactate(StandUpToSpactateRequest request) + { + // TODO + throw new NotImplementedException(); + } } } From 20017a4aa971773379aa05877901662489d945c3 Mon Sep 17 00:00:00 2001 From: idanizi Date: Thu, 18 May 2017 23:04:48 +0300 Subject: [PATCH 17/49] working on the service and communication layer - ucc03 room service(service layer), controller(comm layer) --- Poker.BE/Poker.BE.Domain/Core/User.cs | 2 +- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + .../Exceptions/UserNotFoundException.cs | 25 +++++++++++++++++++ .../Modules/Responses/IResponse.cs | 4 ++- .../Modules/Results/IResult.cs | 4 ++- .../Poker.BE.Service/Services/RoomsService.cs | 12 ++++----- 6 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/UserNotFoundException.cs diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 2110cd8..0b89fb7 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -107,7 +107,7 @@ where Players.Contains(player) logger.Log(string.Format("User {0} has player {1}", UserName, freshPlayer.GetHashCode()), this, "Medium"); - Players.Add(freshPlayer.GetHashCode(), freshPlayer); + Players.Add(freshPlayer); return freshPlayer; } diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index 935857b..44352b7 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -74,6 +74,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/UserNotFoundException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/UserNotFoundException.cs new file mode 100644 index 0000000..d3185b8 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/UserNotFoundException.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions +{ + [Serializable] + public class UserNotFoundException : Exception + { + public UserNotFoundException() + { + } + + public UserNotFoundException(string message) : base(message) + { + } + + public UserNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + + protected UserNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs b/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs index e4c7b2a..153ffa7 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Responses/IResponse.cs @@ -6,7 +6,9 @@ namespace Poker.BE.Service.Modules.Responses { - public interface IResponse + public abstract class IResponse { + public string ErrorMessage { get; set; } + public bool? Success { get; set; } } } diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs index 70767b8..88935ac 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs @@ -6,7 +6,9 @@ namespace Poker.BE.Service.Modules.Results { - public interface IResult + public abstract class IResult { + public string ErrorMessage { get; set; } + public bool? Success { get; set; } } } diff --git a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs index 801eb4d..c2ca0b1 100644 --- a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs +++ b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs @@ -11,9 +11,12 @@ namespace Poker.BE.Service.Services { + /// + /// UCC03 Rooms Management + /// + /// public class RoomsService : IServices.IRoomsService { - #region Properties /// /// Map for the player (user session) ID -> player at the given session. @@ -50,17 +53,14 @@ public EnterRoomResult EnterRoom(EnterRoomRequest request) if(!Users.TryGetValue(request.User, out User user)) { - throw new UserNotFoundException(); + throw new UserNotFoundException(string.Format("User ID {0} not found", request.User)); } result = new EnterRoomResult() { - player = request.User + player = user.EnterRoom(room).GetHashCode(), }; - - // UNDONE - idan - continue from here 18/5 - return result; } From a56b784abb63ed012412535c4b43a39e2d21540d Mon Sep 17 00:00:00 2001 From: idanizi Date: Thu, 18 May 2017 23:16:52 +0300 Subject: [PATCH 18/49] clearing some uneeded files at Poker.API, todo: need to clear more. most of them related to not meeded MVC --- .../Controllers/HomeControllerTest.cs | 25 -------- .../Controllers/ValuesControllerTest.cs | 60 ------------------- .../Poker.BE.API.Tests.csproj | 5 +- .../Poker.BE.API/App_Start/WebApiConfig.cs | 4 +- .../Controllers/HomeController.cs | 19 ------ .../Controllers/ValuesController.cs | 52 ---------------- Poker.BE/Poker.BE.API/Global.asax.cs | 4 ++ Poker.BE/Poker.BE.API/Poker.BE.API.csproj | 2 - Poker.BE/Poker.BE.API/Web.config | 5 ++ 9 files changed, 14 insertions(+), 162 deletions(-) delete mode 100644 Poker.BE/Poker.BE.API.Tests/Controllers/HomeControllerTest.cs delete mode 100644 Poker.BE/Poker.BE.API.Tests/Controllers/ValuesControllerTest.cs delete mode 100644 Poker.BE/Poker.BE.API/Controllers/HomeController.cs delete mode 100644 Poker.BE/Poker.BE.API/Controllers/ValuesController.cs diff --git a/Poker.BE/Poker.BE.API.Tests/Controllers/HomeControllerTest.cs b/Poker.BE/Poker.BE.API.Tests/Controllers/HomeControllerTest.cs deleted file mode 100644 index fc98bfa..0000000 --- a/Poker.BE/Poker.BE.API.Tests/Controllers/HomeControllerTest.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Web.Mvc; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Poker.BE.API; -using Poker.BE.API.Controllers; - -namespace Poker.BE.API.Tests.Controllers -{ - [TestClass] - public class HomeControllerTest - { - [TestMethod] - public void Index() - { - // Arrange - HomeController controller = new HomeController(); - - // Act - ViewResult result = controller.Index() as ViewResult; - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual("Home Page", result.ViewBag.Title); - } - } -} diff --git a/Poker.BE/Poker.BE.API.Tests/Controllers/ValuesControllerTest.cs b/Poker.BE/Poker.BE.API.Tests/Controllers/ValuesControllerTest.cs deleted file mode 100644 index a1fea0a..0000000 --- a/Poker.BE/Poker.BE.API.Tests/Controllers/ValuesControllerTest.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Web.Http; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Poker.BE.API; -using Poker.BE.API.Controllers; - -namespace Poker.BE.API.Tests.Controllers -{ - [TestClass] - public class ValuesControllerTest - { - [TestMethod] - public void Get() - { - // Arrange - ValuesController controller = new ValuesController(); - - // Act - IEnumerable result = controller.Get(); - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual(2, result.Count()); - Assert.AreEqual("value1", result.ElementAt(0)); - Assert.AreEqual("value2", result.ElementAt(1)); - } - - [TestMethod] - public void GetById() - { - } - - [TestMethod] - public void Post() - { - // Arrange - ValuesController controller = new ValuesController(); - - // Act - controller.Post("value"); - - // Assert - } - - [TestMethod] - public void Put() - { - } - - [TestMethod] - public void Delete() - { - - } - } -} diff --git a/Poker.BE/Poker.BE.API.Tests/Poker.BE.API.Tests.csproj b/Poker.BE/Poker.BE.API.Tests/Poker.BE.API.Tests.csproj index a197def..084593e 100644 --- a/Poker.BE/Poker.BE.API.Tests/Poker.BE.API.Tests.csproj +++ b/Poker.BE/Poker.BE.API.Tests/Poker.BE.API.Tests.csproj @@ -94,8 +94,6 @@ - - @@ -109,6 +107,9 @@ Poker.BE.API + + + From c6fd335f00fb44c01c9753dfb9687233149e8a22 Mon Sep 17 00:00:00 2001 From: idanizi Date: Fri, 19 May 2017 02:00:05 +0300 Subject: [PATCH 19/49] initial room controller, todo excemption catching --- .../Controllers/RoomController.cs | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Poker.BE/Poker.BE.API/Controllers/RoomController.cs b/Poker.BE/Poker.BE.API/Controllers/RoomController.cs index 03140ab..9c55373 100644 --- a/Poker.BE/Poker.BE.API/Controllers/RoomController.cs +++ b/Poker.BE/Poker.BE.API/Controllers/RoomController.cs @@ -1,4 +1,9 @@ -using System; +using Poker.BE.Service.Modules.Requests; +using Poker.BE.Service.Modules.Results; +using Poker.BE.Service.IServices; +using Poker.BE.Service.Services; + +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -9,5 +14,35 @@ namespace Poker.BE.API.Controllers { public class RoomController : ApiController { + #region Fields + private IRoomsService service; + #endregion + + #region Constructors + public RoomController() + { + service = new RoomsService(); + } + #endregion + + #region Methods + [HttpPost] + public HttpResponseMessage EnterRoom(EnterRoomRequest request) + { + var result = default(EnterRoomResult); + + try + { + result = service.EnterRoom(request); + } + catch (Exception) + { + // TODO + throw new NotImplementedException(); + } + + return Request.CreateResponse(HttpStatusCode.OK, result); + } + #endregion } } From a434becb0e08ec87358ee86616e3eb04210af06e Mon Sep 17 00:00:00 2001 From: idanizi Date: Fri, 19 May 2017 02:53:33 +0300 Subject: [PATCH 20/49] room service and controller --- .../Controllers/RoomController.cs | 8 ++- .../IServices/IPokerService.cs | 9 ++++ .../IServices/IRoomsService.cs | 5 +- .../Modules/Results/EnterRoomResult.cs | 9 +++- .../Modules/Results/IResult.cs | 19 ++++++- .../Poker.BE.Service/Poker.BE.Service.csproj | 1 + .../Poker.BE.Service/Services/RoomsService.cs | 52 +++++++++++++++---- 7 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 Poker.BE/Poker.BE.Service/IServices/IPokerService.cs diff --git a/Poker.BE/Poker.BE.API/Controllers/RoomController.cs b/Poker.BE/Poker.BE.API/Controllers/RoomController.cs index 9c55373..6559dae 100644 --- a/Poker.BE/Poker.BE.API/Controllers/RoomController.cs +++ b/Poker.BE/Poker.BE.API/Controllers/RoomController.cs @@ -1,7 +1,6 @@ using Poker.BE.Service.Modules.Requests; using Poker.BE.Service.Modules.Results; using Poker.BE.Service.IServices; -using Poker.BE.Service.Services; using System; using System.Collections.Generic; @@ -21,7 +20,7 @@ public class RoomController : ApiController #region Constructors public RoomController() { - service = new RoomsService(); + service = new Service.Services.RoomsService(); } #endregion @@ -35,10 +34,9 @@ public HttpResponseMessage EnterRoom(EnterRoomRequest request) { result = service.EnterRoom(request); } - catch (Exception) + catch (Exception e) { - // TODO - throw new NotImplementedException(); + result.ErrorMessage = e.Message; } return Request.CreateResponse(HttpStatusCode.OK, result); diff --git a/Poker.BE/Poker.BE.Service/IServices/IPokerService.cs b/Poker.BE/Poker.BE.Service/IServices/IPokerService.cs new file mode 100644 index 0000000..74b3d76 --- /dev/null +++ b/Poker.BE/Poker.BE.Service/IServices/IPokerService.cs @@ -0,0 +1,9 @@ +using Poker.BE.Domain.Utility.Logger; + +namespace Poker.BE.Service.IServices +{ + public interface IPokerService + { + ILogger Logger { get; } + } +} \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs index fae3758..d3c1616 100644 --- a/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs +++ b/Poker.BE/Poker.BE.Service/IServices/IRoomsService.cs @@ -1,14 +1,15 @@ using Poker.BE.Domain.Game; using Poker.BE.Service.Modules.Results; using Poker.BE.Service.Modules.Requests; - +using Poker.BE.Domain.Utility.Logger; namespace Poker.BE.Service.IServices { /// Supports UCC03: Rooms Management /// - public interface IRoomsService + public interface IRoomsService : IPokerService { + CreateNewRoomResult CreateNewRoom(CreateNewRoomRequest request); EnterRoomResult EnterRoom(EnterRoomRequest request); JoinNextHandResult JoinNextHand(JoinNextHandRequest request); diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs index 11fd930..3dd6457 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Results/EnterRoomResult.cs @@ -5,6 +5,13 @@ public class EnterRoomResult : IResult /// /// player hash code (session id) /// - public int player { get; set; } + public int? Player { get; set; } + + public EnterRoomResult() + { + Player = null; + ErrorMessage = ""; + Success = true; + } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs index 88935ac..2688539 100644 --- a/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs +++ b/Poker.BE/Poker.BE.Service/Modules/Results/IResult.cs @@ -8,7 +8,24 @@ namespace Poker.BE.Service.Modules.Results { public abstract class IResult { - public string ErrorMessage { get; set; } + /// + /// error message log with accumulate set (= is +=) + /// + public string ErrorMessage + { + get { return ErrorMessage; } + set + { + if(!ErrorMessage.Equals("") & ErrorMessage != null) + { + ErrorMessage += "\n" + value; + } + else + { + ErrorMessage = value; + } + } + } public bool? Success { get; set; } } } diff --git a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj index 7b805d6..293cf55 100644 --- a/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj +++ b/Poker.BE/Poker.BE.Service/Poker.BE.Service.csproj @@ -46,6 +46,7 @@ + diff --git a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs index c2ca0b1..8b673f5 100644 --- a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs +++ b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs @@ -8,6 +8,7 @@ using Poker.BE.Domain.Game; using Poker.BE.Domain.Utility.Exceptions; using Poker.BE.Domain.Core; +using Poker.BE.Domain.Utility.Logger; namespace Poker.BE.Service.Services { @@ -33,9 +34,20 @@ public class RoomsService : IServices.IRoomsService public IDictionary Players { get; set; } public IDictionary Rooms { get; set; } public IDictionary Users { get; set; } + public ILogger Logger { get; } #endregion + #region Constructors + public RoomsService() + { + Players = new Dictionary(); + Rooms = new Dictionary(); + Users = new Dictionary(); + Logger = Domain.Utility.Logger.Logger.Instance; + } + #endregion + #region Methods public CreateNewRoomResult CreateNewRoom(CreateNewRoomRequest request) { // TODO @@ -44,22 +56,38 @@ public CreateNewRoomResult CreateNewRoom(CreateNewRoomRequest request) public EnterRoomResult EnterRoom(EnterRoomRequest request) { - var result = default(EnterRoomResult); + var result = new EnterRoomResult(); - if (!Rooms.TryGetValue(request.Room, out Room room)) + try { - throw new RoomNotFoundException(string.Format("Requested room ID {0} not found", request.Room)); - } + if (!Rooms.TryGetValue(request.Room, out Room room)) + { + throw new RoomNotFoundException(string.Format("Requested room ID {0} not found", request.Room)); + } + + if (!Users.TryGetValue(request.User, out User user)) + { + throw new UserNotFoundException(string.Format("User ID {0} not found", request.User)); + } - if(!Users.TryGetValue(request.User, out User user)) + result.Player = user.EnterRoom(room).GetHashCode(); + } + catch (RoomNotFoundException e) { - throw new UserNotFoundException(string.Format("User ID {0} not found", request.User)); + // TODO + throw e; } - - result = new EnterRoomResult() + catch (UserNotFoundException e) + { + // TODO + throw e; + } + catch (PokerException e) { - player = user.EnterRoom(room).GetHashCode(), - }; + result.Success = false; + result.ErrorMessage = e.Message; + Logger.Error(e, "At " + GetType().Name, e.Source); + } return result; } @@ -75,5 +103,7 @@ public StandUpToSpactateResult StandUpToSpactate(StandUpToSpactateRequest reques // TODO throw new NotImplementedException(); } - } + #endregion + + }// class } From a9c149ae1a522ea7fb62315e466be224b77c0583 Mon Sep 17 00:00:00 2001 From: idanizi Date: Fri, 19 May 2017 03:33:14 +0300 Subject: [PATCH 21/49] setting Newtonsoft.Json as the json formatter (and xml formatter removal) --- Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs b/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs index 3ea2624..0210a40 100644 --- a/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs +++ b/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs @@ -11,6 +11,11 @@ public static void Register(HttpConfiguration config) { // Web API configuration and services + //Setting the configuration for Json Formatting + var jsonFormatter = config.Formatters.JsonFormatter; + jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; + config.Formatters.Remove(config.Formatters.XmlFormatter); + // Web API routes config.MapHttpAttributeRoutes(); From 49cd55f23b48a8c01b23e604b73260834408021a Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 03:43:29 +0300 Subject: [PATCH 22/49] testing room --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 64 ++++++++++++++----- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 38 ++++++++++- 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 1cf3aff..08039cf 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -12,43 +12,75 @@ namespace Poker.BE.Domain.Game.Tests public class RoomTests { - private TestContext testContext; + #region Setup + public TestContext TestContext { get; set; } + private Room room; - public TestContext TestContext { get { return testContext; } set { testContext = value; } } + [TestInitialize] + public void Before() + { + room = new Room(); + } + + [TestCleanup] + public void After() + { + room = null; + } + #endregion [TestMethod] public void RoomTest()// (:Player, :Preferences) { //Arrange - Player player = new Player(); - GamePreferences preferences = new GamePreferences(); + var player = new Player(); + var preferences = new GamePreferences(); + var expected = new Room(player, preferences); + //Act - var result = new Room(player, preferences); + var actual = new Room(player, preferences); //Assert - TestContext.WriteLine("end of roomTest"); - } - [TestMethod()] - public void RoomTest1() // () - { - // TODO - throw new NotImplementedException(); } [TestMethod()] public void RoomTest2() // (:Player) { - // TODO - throw new NotImplementedException(); + //Arrange + var player = new Player(); + var expected = player; + + //Act + var actual = new Room(player); + + //Assert + Assert.IsTrue(actual.Players.Count == 1); + Assert.AreEqual(expected, actual.Players.First()); + Assert.IsTrue(actual.PassivePlayers.Contains(expected)); } [TestMethod()] public void RoomTest3() // (:Player, :GameConfig) { - // TODO - throw new NotImplementedException(); + //Arrange + var player = new Player(); + var confing = new GameConfig() { + BuyInCost = 50.2, + GamePrefrences = new GamePreferences(), + IsSpactatorsAllowed = false, + MaxNumberOfActivePlayers = 8, + MaxNumberOfPlayers = 9, + MinimumBet = + }; + var expected = ; + + //Act + var actual = 1; + + //Assert + Assert.AreEqual(expected, actual); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index f5cb43a..860bb83 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -62,13 +62,47 @@ public int MaxNumberOfPlayers /// /// choose the cost of joining the game. + /// always higher than minimum bet /// - public double BuyInCost { get; set; } + public double BuyInCost + { + get { return BuyInCost; } + + // NOTE: adapt the set of buy in cost to the minimum bet + set + { + if (value < MinimumBet) + { + MinimumBet = BuyInCost = value; + } + else + { + BuyInCost = value; + } + } + } /// /// The creator sets the big blind of the room’s table. + /// always lower than buy in cost /// - public double MinimumBet { get; set; } + public double MinimumBet + { + get { return MinimumBet; } + + // Note: adapt the set of minimum bet to the buy in cost + set + { + if (value > BuyInCost) + { + MinimumBet = BuyInCost = value; + } + else + { + MinimumBet = value; + } + } + } /// /// the room name - set by the user. From 3b9f3ef99dad76f8d704695126ae992fa60f5455 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 04:08:03 +0300 Subject: [PATCH 23/49] editing todos, testing room and bug fixing --- .../Poker.BE.API/App_Start/WebApiConfig.cs | 2 +- Poker.BE/Poker.BE.API/Global.asax.cs | 2 - .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 41 +++++++++++++------ Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 9 ++-- .../Poker.BE.Domain/Utility/MoneyStorage.cs | 2 +- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs b/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs index 0210a40..b45a090 100644 --- a/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs +++ b/Poker.BE/Poker.BE.API/App_Start/WebApiConfig.cs @@ -11,7 +11,7 @@ public static void Register(HttpConfiguration config) { // Web API configuration and services - //Setting the configuration for Json Formatting + // Setting the configuration for Json Formatting var jsonFormatter = config.Formatters.JsonFormatter; jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); diff --git a/Poker.BE/Poker.BE.API/Global.asax.cs b/Poker.BE/Poker.BE.API/Global.asax.cs index 0faf1f0..f976ebf 100644 --- a/Poker.BE/Poker.BE.API/Global.asax.cs +++ b/Poker.BE/Poker.BE.API/Global.asax.cs @@ -20,8 +20,6 @@ protected void Application_Start() FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); - - // UNDONE: idan - add JSON filter here } } } diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 08039cf..f822154 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -15,11 +15,13 @@ public class RoomTests #region Setup public TestContext TestContext { get; set; } private Room room; + private Player roomCreator; [TestInitialize] public void Before() { - room = new Room(); + roomCreator = new Player(); + room = new Room(roomCreator); } [TestCleanup] @@ -56,9 +58,12 @@ public void RoomTest2() // (:Player) var actual = new Room(player); //Assert - Assert.IsTrue(actual.Players.Count == 1); - Assert.AreEqual(expected, actual.Players.First()); - Assert.IsTrue(actual.PassivePlayers.Contains(expected)); + Assert.AreEqual(1, actual.Players.Count, "one player in new room"); + Assert.AreEqual(expected, actual.Players.First(), "the creator is first"); + Assert.AreEqual(Player.State.Passive, player.CurrentState, "the creator is passive"); + + //fixme - idan - continue from here - fix passive players collection at Room + Assert.IsTrue(actual.PassivePlayers.Contains(expected), "the creator found in passive players collection at room"); } [TestMethod()] @@ -66,21 +71,31 @@ public void RoomTest3() // (:Player, :GameConfig) { //Arrange var player = new Player(); + const string expName = "test room 3"; + const double expBuyinCost = 50.2; + GamePreferences expGamePreferences = new GamePreferences(); + const bool expIsSpecAllowed = false; + const int expNActive = 8; + const int expMaxNumberPlayers = 9; + const double expMinBet = 8.6; + const int expMinPlayers = 3; var confing = new GameConfig() { - BuyInCost = 50.2, - GamePrefrences = new GamePreferences(), - IsSpactatorsAllowed = false, - MaxNumberOfActivePlayers = 8, - MaxNumberOfPlayers = 9, - MinimumBet = + BuyInCost = expBuyinCost, + GamePrefrences = expGamePreferences, + IsSpactatorsAllowed = expIsSpecAllowed, + MaxNumberOfActivePlayers = expNActive, + MaxNumberOfPlayers = expMaxNumberPlayers, + MinimumBet = expMinBet, + MinNumberOfPlayers = expMinPlayers, + Name = expName, }; - var expected = ; //Act - var actual = 1; + var actual = new Room(player, confing); //Assert - Assert.AreEqual(expected, actual); + Assert.AreEqual(expName, actual.Name); + Assert.AreEqual(expNActive, actual.MinNumberOfPlayers); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index dee8261..2a0113c 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -16,7 +16,6 @@ namespace Poker.BE.Domain.Core /// public sealed class GameCenter { - // UNDONE: Make the GameCenter a Singleton! #region Constants public enum Move { @@ -185,25 +184,25 @@ private League FindLeagueToFill(ICollection collection) /// public ICollection FindRoomsByCriteria(int level) { - // TODO + // TODO: idan throw new NotImplementedException(); } public ICollection FindRoomsByCriteria(Player player) { - // TODO + // TODO: idan throw new NotImplementedException(); } public ICollection FindRoomsByCriteria(GamePreferences preferences) { - // TODO + // TODO: idan throw new NotImplementedException(); } public ICollection FindRoomsByCriteria(double betSize) { - // TODO + // TODO: idan throw new NotImplementedException(); } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs b/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs index e1dc8d1..e08f628 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/MoneyStorage.cs @@ -81,7 +81,7 @@ private double RetrieveGate(Currency currency) switch (currency) { - // UNDONE: call a service that gets the actual currency gate. + // TODO: call a service that gets the actual currency gate. case Currency.USD: case Currency.NIS: case Currency.CHIP: From dd99c5305f8e11cabc017ccaa2bdb0e3db266040 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 13:10:20 +0300 Subject: [PATCH 24/49] fixing game config --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 8 ++- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 18 +++++ Poker.BE/Poker.BE.Domain/Game/Room.cs | 68 ++++++++++++------- 3 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index f822154..afc58f1 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -61,9 +61,13 @@ public void RoomTest2() // (:Player) Assert.AreEqual(1, actual.Players.Count, "one player in new room"); Assert.AreEqual(expected, actual.Players.First(), "the creator is first"); Assert.AreEqual(Player.State.Passive, player.CurrentState, "the creator is passive"); - - //fixme - idan - continue from here - fix passive players collection at Room + Assert.AreEqual(1, actual.PassivePlayers.Count, "one passive player"); + Assert.AreEqual(0, actual.ActivePlayers.Count , "zero active players"); Assert.IsTrue(actual.PassivePlayers.Contains(expected), "the creator found in passive players collection at room"); + Assert.AreEqual(null, actual.CurrentHand); + Assert.AreEqual(true, actual.IsSpactatorsAllowd); + Assert.AreEqual(false , actual.IsTableFull); + Assert.AreEqual( ,actual.) } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index 860bb83..3f30164 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -110,5 +110,23 @@ public double MinimumBet public string Name { get; set; } #endregion + + #region Constructors + /// + /// default configuration + /// + public GameConfig() + { + BuyInCost = 50.0; + GamePrefrences = new GamePreferences(); + IsSpactatorsAllowed = true; + MaxNumberOfActivePlayers = 10; + MaxNumberOfPlayers = 15; + MinNumberOfPlayers = 2; + MinimumBet = 5.0; + Name = "Unknown Room"; + } + #endregion + } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 21cae37..487e95d 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -38,22 +38,32 @@ public ICollection ActivePlayers { get { - return activeAndPassivePlayers.Where( - player => (player.CurrentState == Player.State.ActiveUnfolded | player.CurrentState == Player.State.ActiveFolded)) - .ToList(); + // TODO clean this comment code + //return activeAndPassivePlayers.Where( + // player => (player.CurrentState == Player.State.ActiveUnfolded | player.CurrentState == Player.State.ActiveFolded)) + // .ToList(); + + var result = from player in activeAndPassivePlayers + where player.CurrentState != Player.State.Passive + select player; + + return result.ToList(); + } } public ICollection PassivePlayers { get { - return activeAndPassivePlayers.Where( - player => (player.CurrentState == Player.State.ActiveUnfolded | player.CurrentState == Player.State.ActiveFolded)) - .ToList(); + var result = from player in activeAndPassivePlayers + where player.CurrentState == Player.State.Passive + select player; + + return result.ToList(); } } public ICollection Players { get { return activeAndPassivePlayers; } } - public bool IsSpactatorsAllowd { get; } + public bool IsSpactatorsAllowd { get; private set; } public int MaxNumberOfPlayers { get; private set; } public int MinNumberOfPlayers { get; private set; } public int MaxNumberOfActivePlayers @@ -73,6 +83,7 @@ public bool IsTableFull return ActivePlayers.Count == MaxNumberOfActivePlayers; } } + public double BuyInCost { get; private set; } #endregion @@ -90,10 +101,8 @@ private Room() CurrentHand = null; - // Note: making default preferences to the room (poker game) - Preferences = new GamePreferences(); - - Name = "Unknown Room"; + // Note: default configuration + Configure(new GameConfig()); } @@ -120,11 +129,34 @@ public Room(Player creator, GamePreferences preferences) : this(creator) public Room(Player creator, GameConfig config) : this(creator, config.GamePrefrences) { + Configure(config); + } + + #endregion + + #region Private Functions + + private void Configure(GameConfig config) + { + /*Note: 8 configurations */ IsSpactatorsAllowd = config.IsSpactatorsAllowed; MaxNumberOfPlayers = config.MaxNumberOfPlayers; + MaxNumberOfActivePlayers = config.MaxNumberOfActivePlayers; MinNumberOfPlayers = config.MinNumberOfPlayers; MinimumBet = config.MinimumBet; + BuyInCost = config.BuyInCost; Name = config.Name; + Preferences = config.GamePrefrences; + } + + private void TakeAChair(int index) + { + chairs[index].Take(); + } + + private void ReleaseAChair(int index) + { + chairs[index].Release(); } #endregion @@ -191,19 +223,5 @@ public void SendMessage() } #endregion - #region Private Functions - private void TakeAChair(int index) - { - chairs[index].Take(); - } - - private void ReleaseAChair(int index) - { - chairs[index].Release(); - } - - #endregion - - }//class } From eed172273d0945bafe5d3f60402beb4bdaadebad Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 14:40:54 +0300 Subject: [PATCH 25/49] room(:player) ctor test pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 23 +++++++++---- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 33 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index afc58f1..2bacf0e 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -52,22 +52,31 @@ public void RoomTest2() // (:Player) { //Arrange var player = new Player(); - var expected = player; + var expPlayer = player; + var expConfig = new GameConfig(); //Act var actual = new Room(player); //Assert Assert.AreEqual(1, actual.Players.Count, "one player in new room"); - Assert.AreEqual(expected, actual.Players.First(), "the creator is first"); + Assert.AreEqual(expPlayer, actual.Players.First(), "the creator is first"); Assert.AreEqual(Player.State.Passive, player.CurrentState, "the creator is passive"); Assert.AreEqual(1, actual.PassivePlayers.Count, "one passive player"); Assert.AreEqual(0, actual.ActivePlayers.Count , "zero active players"); - Assert.IsTrue(actual.PassivePlayers.Contains(expected), "the creator found in passive players collection at room"); - Assert.AreEqual(null, actual.CurrentHand); - Assert.AreEqual(true, actual.IsSpactatorsAllowd); - Assert.AreEqual(false , actual.IsTableFull); - Assert.AreEqual( ,actual.) + Assert.IsTrue(actual.PassivePlayers.Contains(expPlayer), "the creator found in passive players collection at room"); + Assert.AreEqual(null, actual.CurrentHand, "current hand null"); + Assert.AreEqual(false , actual.IsTableFull, "table not full"); + + // 8 Configurations of game-config + Assert.AreEqual(expConfig.BuyInCost, actual.BuyInCost, "default buy in"); + Assert.AreEqual(true, actual.IsSpactatorsAllowd, "default spectators allowed"); + Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); + Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); + Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); + Assert.AreEqual(expConfig.MinNumberOfPlayers, actual.MinNumberOfPlayers, "min players default"); + Assert.AreEqual(expConfig.Name, actual.Name, "default name"); + // TODO: idan - add assert for default game preferences. } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index 3f30164..db810b5 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -128,5 +128,38 @@ public GameConfig() } #endregion + #region Methods + // override object.Equals + public override bool Equals(object obj) + { + // + // See the full list of guidelines at + // http://go.microsoft.com/fwlink/?LinkID=85237 + // and also the guidance for operator== at + // http://go.microsoft.com/fwlink/?LinkId=85238 + // + + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + // TODO: write your implementation of Equals() here + var other = obj as GameConfig; + if (other == null) return false; + + return + //this.GamePreferences.Equals(other.GamePreferences) && // undone: override GamePreferences.Equals(:object) + this.BuyInCost.Equals(other.BuyInCost) && + this.IsSpactatorsAllowed == other.IsSpactatorsAllowed && + this.MaxNumberOfActivePlayers == other.MaxNumberOfActivePlayers && + this.MaxNumberOfPlayers == other.MaxNumberOfPlayers && + this.MinimumBet == other.MinimumBet && + this.Name.Equals(other.Name) && + this.MinNumberOfPlayers == other.MinNumberOfPlayers; + } + + #endregion + } } \ No newline at end of file From a18823827912562fd22d76d039c3951cc1b8a0bd Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 15:13:34 +0300 Subject: [PATCH 26/49] room(:player, :preferences) ctor test pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 25 ++++++++++--- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 35 ++++++++++++------- Poker.BE/Poker.BE.Domain/Game/Room.cs | 5 +-- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 2bacf0e..d7674b5 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -35,15 +35,32 @@ public void After() public void RoomTest()// (:Player, :Preferences) { //Arrange - var player = new Player(); + var expPlayer = new Player(); var preferences = new GamePreferences(); - var expected = new Room(player, preferences); - + var expConfig = new GameConfig(); //Act - var actual = new Room(player, preferences); + var actual = new Room(expPlayer, preferences); //Assert + Assert.AreEqual(1, actual.Players.Count, "one player in new room"); + Assert.AreEqual(expPlayer, actual.Players.First(), "the creator is first"); + Assert.AreEqual(Player.State.Passive, expPlayer.CurrentState, "the creator is passive"); + Assert.AreEqual(1, actual.PassivePlayers.Count, "one passive player"); + Assert.AreEqual(0, actual.ActivePlayers.Count, "zero active players"); + Assert.IsTrue(actual.PassivePlayers.Contains(expPlayer), "the creator found in passive players collection at room"); + Assert.AreEqual(null, actual.CurrentHand, "current hand null"); + Assert.AreEqual(false, actual.IsTableFull, "table not full"); + + // 8 Configurations of game-config + Assert.AreEqual(expConfig.BuyInCost, actual.BuyInCost, "default buy in"); + Assert.AreEqual(true, actual.IsSpactatorsAllowd, "default spectators allowed"); + Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); + Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); + Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); + Assert.AreEqual(expConfig.MinNumberOfPlayers, actual.MinNumberOfPlayers, "min players default"); + Assert.AreEqual(expConfig.Name, actual.Name, "default name"); + // TODO: idan - add assert for default game preferences. } diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index db810b5..c579b1a 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -6,6 +6,13 @@ namespace Poker.BE.Domain.Game { public class GameConfig { + #region Fields + private bool _isSpactatorsAllowed; + private int _minNumberOfPlayers; + private int _maxNumberOfPlayers; + private double _buyInCost; + private double _minimumBet; + #endregion #region Properties @@ -19,14 +26,16 @@ public class GameConfig /// public bool IsSpactatorsAllowed { - get { return IsSpactatorsAllowed; } + get { return _isSpactatorsAllowed; } set { // when spectator not allowed, enforce it on max number of players if (!value & MaxNumberOfPlayers > MaxNumberOfActivePlayers) { - MaxNumberOfPlayers = MaxNumberOfActivePlayers; + _maxNumberOfPlayers = MaxNumberOfActivePlayers; } + + _isSpactatorsAllowed = value; } } @@ -35,9 +44,9 @@ public bool IsSpactatorsAllowed /// public int MinNumberOfPlayers { - get { return MinNumberOfPlayers; } + get { return _minNumberOfPlayers; } // enforce min >= 2 - set { MinNumberOfPlayers = (value < 2) ? 2 : value; } + set { _minNumberOfPlayers = (value < 2) ? 2 : value; } } /// @@ -45,11 +54,11 @@ public int MinNumberOfPlayers /// public int MaxNumberOfPlayers { - get { return MaxNumberOfPlayers; } + get { return _maxNumberOfPlayers; } set { // enforce max number of players when spectators not allowed. - MaxNumberOfPlayers = + _maxNumberOfPlayers = (!IsSpactatorsAllowed & value > MaxNumberOfActivePlayers) ? MaxNumberOfActivePlayers : value; } @@ -66,18 +75,18 @@ public int MaxNumberOfPlayers /// public double BuyInCost { - get { return BuyInCost; } + get { return _buyInCost; } // NOTE: adapt the set of buy in cost to the minimum bet set { if (value < MinimumBet) { - MinimumBet = BuyInCost = value; + _minimumBet = _buyInCost = value; } else { - BuyInCost = value; + _buyInCost = value; } } } @@ -88,18 +97,18 @@ public double BuyInCost /// public double MinimumBet { - get { return MinimumBet; } + get { return _minimumBet; } // Note: adapt the set of minimum bet to the buy in cost set { - if (value > BuyInCost) + if (value > _buyInCost) { - MinimumBet = BuyInCost = value; + _minimumBet = _buyInCost = value; } else { - MinimumBet = value; + _minimumBet = value; } } } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 487e95d..c53722c 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -24,6 +24,7 @@ public class Room private ICollection activeAndPassivePlayers; private Deck deck; private Chair[] chairs; + private int _maxNumberOfActivePlayers; #endregion #region Properties @@ -68,11 +69,11 @@ public ICollection PassivePlayers public int MinNumberOfPlayers { get; private set; } public int MaxNumberOfActivePlayers { - get { return MaxNumberOfActivePlayers; } + get { return _maxNumberOfActivePlayers; } private set { // enforce number of active players < number of chairs. - MaxNumberOfActivePlayers = (value > NCHAIRS_IN_ROOM) ? NCHAIRS_IN_ROOM : value; + _maxNumberOfActivePlayers = (value > NCHAIRS_IN_ROOM) ? NCHAIRS_IN_ROOM : value; } } public double MinimumBet { get; private set; } From 049640b6bf74a20a43391cd0df49bc53ab2a6858 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 15:25:28 +0300 Subject: [PATCH 27/49] working on room(:player, :gameconfig) ctor test --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index d7674b5..09115bc 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -100,32 +100,58 @@ public void RoomTest2() // (:Player) public void RoomTest3() // (:Player, :GameConfig) { //Arrange - var player = new Player(); + + var expPlayer = new Player(); const string expName = "test room 3"; - const double expBuyinCost = 50.2; - GamePreferences expGamePreferences = new GamePreferences(); const bool expIsSpecAllowed = false; - const int expNActive = 8; - const int expMaxNumberPlayers = 9; - const double expMinBet = 8.6; + GamePreferences expGamePreferences = new GamePreferences(); const int expMinPlayers = 3; - var confing = new GameConfig() { - BuyInCost = expBuyinCost, + + // changed parameters + const double insertBuyinCost = 50.2; + const int insertNActive = 8; + const int insertMaxNumberPlayers = 9; + const double insertMinBet = 8.6; + + // expected results by the parameters // undone - idan continue from here. + double expBuyinCost = Math.Max(insertBuyinCost, insertMinBet); + double expMinBet = Math.Min(insertMinBet, insertBuyinCost); + int expNActive = expIsSpecAllowed ? insertNActive : insertMaxNumberPlayers; + int expMaxNumberPlayers = expIsSpecAllowed ? insertMaxNumberPlayers : insertNActive; + + var expConfig = new GameConfig() { + BuyInCost = insertBuyinCost, GamePrefrences = expGamePreferences, IsSpactatorsAllowed = expIsSpecAllowed, - MaxNumberOfActivePlayers = expNActive, - MaxNumberOfPlayers = expMaxNumberPlayers, - MinimumBet = expMinBet, + MaxNumberOfActivePlayers = insertNActive, + MaxNumberOfPlayers = insertMaxNumberPlayers, + MinimumBet = insertMinBet, MinNumberOfPlayers = expMinPlayers, Name = expName, }; //Act - var actual = new Room(player, confing); + var actual = new Room(expPlayer, expConfig); //Assert - Assert.AreEqual(expName, actual.Name); - Assert.AreEqual(expNActive, actual.MinNumberOfPlayers); + Assert.AreEqual(1, actual.Players.Count, "one player in new room"); + Assert.AreEqual(expPlayer, actual.Players.First(), "the creator is first"); + Assert.AreEqual(Player.State.Passive, expPlayer.CurrentState, "the creator is passive"); + Assert.AreEqual(1, actual.PassivePlayers.Count, "one passive player"); + Assert.AreEqual(0, actual.ActivePlayers.Count, "zero active players"); + Assert.IsTrue(actual.PassivePlayers.Contains(expPlayer), "the creator found in passive players collection at room"); + Assert.AreEqual(null, actual.CurrentHand, "current hand null"); + Assert.AreEqual(false, actual.IsTableFull, "table not full"); + + // 8 Configurations of game-config + Assert.AreEqual(insertBuyinCost, actual.BuyInCost, "default buy in"); + Assert.AreEqual(expIsSpecAllowed, actual.IsSpactatorsAllowd, "default spectators allowed"); + Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); + Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); + Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); + Assert.AreEqual(expConfig.MinNumberOfPlayers, actual.MinNumberOfPlayers, "min players default"); + Assert.AreEqual(expConfig.Name, actual.Name, "default name"); + // TODO: idan - add assert for default game preferences. } [TestMethod()] From 0ce05e4b235315af0ea2f54208e46ff6022465f9 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 18:12:47 +0300 Subject: [PATCH 28/49] working on room(:player, :gameconfig) ctor test --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 23 ++--- Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 67 ++++++++++---- Poker.BE/Poker.BE.Domain/Game/Room.cs | 90 +++++++++++++------ 3 files changed, 127 insertions(+), 53 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 09115bc..337f1a0 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -54,7 +54,7 @@ public void RoomTest()// (:Player, :Preferences) // 8 Configurations of game-config Assert.AreEqual(expConfig.BuyInCost, actual.BuyInCost, "default buy in"); - Assert.AreEqual(true, actual.IsSpactatorsAllowd, "default spectators allowed"); + Assert.AreEqual(true, actual.IsSpactatorsAllowed, "default spectators allowed"); Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); @@ -87,7 +87,7 @@ public void RoomTest2() // (:Player) // 8 Configurations of game-config Assert.AreEqual(expConfig.BuyInCost, actual.BuyInCost, "default buy in"); - Assert.AreEqual(true, actual.IsSpactatorsAllowd, "default spectators allowed"); + Assert.AreEqual(true, actual.IsSpactatorsAllowed, "default spectators allowed"); Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); @@ -113,15 +113,16 @@ public void RoomTest3() // (:Player, :GameConfig) const int insertMaxNumberPlayers = 9; const double insertMinBet = 8.6; - // expected results by the parameters // undone - idan continue from here. + // expected results by the parameters double expBuyinCost = Math.Max(insertBuyinCost, insertMinBet); double expMinBet = Math.Min(insertMinBet, insertBuyinCost); int expNActive = expIsSpecAllowed ? insertNActive : insertMaxNumberPlayers; int expMaxNumberPlayers = expIsSpecAllowed ? insertMaxNumberPlayers : insertNActive; + var expConfig = new GameConfig() { BuyInCost = insertBuyinCost, - GamePrefrences = expGamePreferences, + Preferences = expGamePreferences, IsSpactatorsAllowed = expIsSpecAllowed, MaxNumberOfActivePlayers = insertNActive, MaxNumberOfPlayers = insertMaxNumberPlayers, @@ -144,13 +145,13 @@ public void RoomTest3() // (:Player, :GameConfig) Assert.AreEqual(false, actual.IsTableFull, "table not full"); // 8 Configurations of game-config - Assert.AreEqual(insertBuyinCost, actual.BuyInCost, "default buy in"); - Assert.AreEqual(expIsSpecAllowed, actual.IsSpactatorsAllowd, "default spectators allowed"); - Assert.AreEqual(expConfig.MaxNumberOfActivePlayers, actual.MaxNumberOfActivePlayers, "max active players default"); - Assert.AreEqual(expConfig.MaxNumberOfPlayers, actual.MaxNumberOfPlayers, "default max players number"); - Assert.AreEqual(expConfig.MinimumBet, actual.MinimumBet, "minimum bet default"); - Assert.AreEqual(expConfig.MinNumberOfPlayers, actual.MinNumberOfPlayers, "min players default"); - Assert.AreEqual(expConfig.Name, actual.Name, "default name"); + Assert.AreEqual(expBuyinCost, actual.BuyInCost, "default buy in"); //fixme - idan + Assert.AreEqual(expIsSpecAllowed, actual.IsSpactatorsAllowed, "default spectators allowed"); + Assert.AreEqual(expNActive, actual.MaxNumberOfActivePlayers, "max active players default"); + Assert.AreEqual(expMaxNumberPlayers, actual.MaxNumberOfPlayers, "default max players number"); + Assert.AreEqual(expMinBet, actual.MinimumBet, "minimum bet default"); + Assert.AreEqual(expMinPlayers, actual.MinNumberOfPlayers, "min players default"); + Assert.AreEqual(expName, actual.Name, "default name"); // TODO: idan - add assert for default game preferences. } diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index c579b1a..df27058 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -6,12 +6,16 @@ namespace Poker.BE.Domain.Game { public class GameConfig { + #region Fields + private int _maxNumberOfActivePlayers; + private int _maxNumberOfPlayers; private bool _isSpactatorsAllowed; private int _minNumberOfPlayers; - private int _maxNumberOfPlayers; private double _buyInCost; private double _minimumBet; + private string _name; + private GamePreferences _preferences; #endregion #region Properties @@ -19,7 +23,11 @@ public class GameConfig /// /// Choose between 3 game modes: “Limit Hold’em”, “No-Limit Hold’em” and “Pot Limit Hold’em”. when ‘Limit Hold’em’ is chosen the user is requested to enter a max bet limit. /// - public GamePreferences GamePrefrences { get; set; } + public GamePreferences Preferences + { + get { return _preferences; } + set { _preferences = value; } + } /// /// choose whether spectating a game is allowed or not. @@ -29,13 +37,11 @@ public bool IsSpactatorsAllowed get { return _isSpactatorsAllowed; } set { - // when spectator not allowed, enforce it on max number of players - if (!value & MaxNumberOfPlayers > MaxNumberOfActivePlayers) - { - _maxNumberOfPlayers = MaxNumberOfActivePlayers; - } - _isSpactatorsAllowed = value; + + // refresh the depended properties + MaxNumberOfPlayers = _maxNumberOfPlayers; + MaxNumberOfActivePlayers = _maxNumberOfActivePlayers; } } @@ -45,8 +51,11 @@ public bool IsSpactatorsAllowed public int MinNumberOfPlayers { get { return _minNumberOfPlayers; } - // enforce min >= 2 - set { _minNumberOfPlayers = (value < 2) ? 2 : value; } + set + { + //enforce min >= 2 + _minNumberOfPlayers = (value < 2) ? 2 : value; + } } /// @@ -57,17 +66,32 @@ public int MaxNumberOfPlayers get { return _maxNumberOfPlayers; } set { - // enforce max number of players when spectators not allowed. - _maxNumberOfPlayers = - (!IsSpactatorsAllowed & value > MaxNumberOfActivePlayers) ? - MaxNumberOfActivePlayers : value; + _maxNumberOfPlayers = (value < _maxNumberOfActivePlayers) ? _maxNumberOfActivePlayers : value; + if (!_isSpactatorsAllowed) + { + _maxNumberOfActivePlayers = _maxNumberOfPlayers; + } } } /// /// choose the max amount of players in the table (active players) /// - public int MaxNumberOfActivePlayers { get; set; } + public int MaxNumberOfActivePlayers + { + get { return _maxNumberOfActivePlayers; } + set + { + // enforce number of active players < number of chairs. + _maxNumberOfActivePlayers = (value > Room.NCHAIRS_IN_ROOM) ? Room.NCHAIRS_IN_ROOM : value; + + // enforce max number of players when spectators not allowed. + _maxNumberOfPlayers = + (!IsSpactatorsAllowed & value > MaxNumberOfActivePlayers) ? + MaxNumberOfActivePlayers : value; + } + + } /// /// choose the cost of joining the game. @@ -116,7 +140,11 @@ public double MinimumBet /// /// the room name - set by the user. /// - public string Name { get; set; } + public string Name + { + get { return _name; } + set { _name = value ?? ""; } + } #endregion @@ -127,7 +155,7 @@ public double MinimumBet public GameConfig() { BuyInCost = 50.0; - GamePrefrences = new GamePreferences(); + Preferences = new GamePreferences(); IsSpactatorsAllowed = true; MaxNumberOfActivePlayers = 10; MaxNumberOfPlayers = 15; @@ -168,6 +196,11 @@ public override bool Equals(object obj) this.MinNumberOfPlayers == other.MinNumberOfPlayers; } + public override int GetHashCode() + { + return base.GetHashCode(); + } + #endregion } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index c53722c..399af7a 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -24,17 +24,16 @@ public class Room private ICollection activeAndPassivePlayers; private Deck deck; private Chair[] chairs; - private int _maxNumberOfActivePlayers; + private GameConfig config; + #endregion #region Properties // TODO: do we need ID for the Room? if so, what type should it be? 'long?' means nullable long. //public long? ID { get; } - public string Name { get; set; } public ICollection Chairs { get { return chairs; } } public Hand CurrentHand { get; private set; } - public GamePreferences Preferences { get; set; } public ICollection ActivePlayers { get @@ -64,19 +63,57 @@ public ICollection PassivePlayers } } public ICollection Players { get { return activeAndPassivePlayers; } } - public bool IsSpactatorsAllowd { get; private set; } - public int MaxNumberOfPlayers { get; private set; } - public int MinNumberOfPlayers { get; private set; } + + #region GameConfig Properties (8) + public GamePreferences Preferences + { + get { return config.Preferences; } + set { config.Preferences = value; } + } + + public string Name + { + get { return config.Name; } + set { config.Name = value; } + } + + public bool IsSpactatorsAllowed + { + get { return config.IsSpactatorsAllowed; } + set { config.IsSpactatorsAllowed = value; } + } + + public int MaxNumberOfPlayers + { + get { return config.MaxNumberOfPlayers; } + set { config.MaxNumberOfPlayers = value; } + } + + public int MinNumberOfPlayers + { + get { return config.MinNumberOfPlayers; } + set { config.MinNumberOfPlayers = value; } + } + public int MaxNumberOfActivePlayers { - get { return _maxNumberOfActivePlayers; } - private set - { - // enforce number of active players < number of chairs. - _maxNumberOfActivePlayers = (value > NCHAIRS_IN_ROOM) ? NCHAIRS_IN_ROOM : value; - } + get { return config.MaxNumberOfActivePlayers; } + set { config.MaxNumberOfActivePlayers = value; } + } + + public double MinimumBet + { + get { return config.MinimumBet; } + set { config.MinimumBet = value; } } - public double MinimumBet { get; private set; } + + public double BuyInCost + { + get { return config.BuyInCost; } + set { config.BuyInCost = value; } + } + #endregion + public bool IsTableFull { get @@ -84,7 +121,7 @@ public bool IsTableFull return ActivePlayers.Count == MaxNumberOfActivePlayers; } } - public double BuyInCost { get; private set; } + #endregion @@ -103,8 +140,7 @@ private Room() CurrentHand = null; // Note: default configuration - Configure(new GameConfig()); - + config = new GameConfig(); } /// @@ -128,7 +164,7 @@ public Room(Player creator, GamePreferences preferences) : this(creator) Preferences = preferences; } - public Room(Player creator, GameConfig config) : this(creator, config.GamePrefrences) + public Room(Player creator, GameConfig config) : this(creator, config.Preferences) { Configure(config); } @@ -139,15 +175,19 @@ public Room(Player creator, GameConfig config) : this(creator, config.GamePrefre private void Configure(GameConfig config) { + /*Note: 8 configurations */ - IsSpactatorsAllowd = config.IsSpactatorsAllowed; - MaxNumberOfPlayers = config.MaxNumberOfPlayers; - MaxNumberOfActivePlayers = config.MaxNumberOfActivePlayers; - MinNumberOfPlayers = config.MinNumberOfPlayers; - MinimumBet = config.MinimumBet; - BuyInCost = config.BuyInCost; - Name = config.Name; - Preferences = config.GamePrefrences; + config = new GameConfig() + { + IsSpactatorsAllowed = config.IsSpactatorsAllowed, + MaxNumberOfPlayers = config.MaxNumberOfPlayers, + MaxNumberOfActivePlayers = config.MaxNumberOfActivePlayers, + MinNumberOfPlayers = config.MinNumberOfPlayers, + MinimumBet = config.MinimumBet, + BuyInCost = config.BuyInCost, + Name = config.Name, + Preferences = config.Preferences, + }; } private void TakeAChair(int index) From bf1f9db3720201a90df004d9afe9f77b328d71a4 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 19:32:50 +0300 Subject: [PATCH 29/49] room(:player, :gameconfig) ctor test pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 29 +++++++++++++------ Poker.BE/Poker.BE.Domain/Game/GameConfig.cs | 12 ++++---- Poker.BE/Poker.BE.Domain/Game/Room.cs | 20 ++----------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 337f1a0..b16ef71 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -100,7 +100,7 @@ public void RoomTest2() // (:Player) public void RoomTest3() // (:Player, :GameConfig) { //Arrange - + #region Arrange var expPlayer = new Player(); const string expName = "test room 3"; const bool expIsSpecAllowed = false; @@ -117,10 +117,11 @@ public void RoomTest3() // (:Player, :GameConfig) double expBuyinCost = Math.Max(insertBuyinCost, insertMinBet); double expMinBet = Math.Min(insertMinBet, insertBuyinCost); int expNActive = expIsSpecAllowed ? insertNActive : insertMaxNumberPlayers; - int expMaxNumberPlayers = expIsSpecAllowed ? insertMaxNumberPlayers : insertNActive; + int expMaxNumberPlayers = insertMaxNumberPlayers; - var expConfig = new GameConfig() { + var expConfig = new GameConfig() + { BuyInCost = insertBuyinCost, Preferences = expGamePreferences, IsSpactatorsAllowed = expIsSpecAllowed, @@ -129,12 +130,14 @@ public void RoomTest3() // (:Player, :GameConfig) MinimumBet = insertMinBet, MinNumberOfPlayers = expMinPlayers, Name = expName, - }; + }; + #endregion //Act var actual = new Room(expPlayer, expConfig); //Assert + #region Default asserts Assert.AreEqual(1, actual.Players.Count, "one player in new room"); Assert.AreEqual(expPlayer, actual.Players.First(), "the creator is first"); Assert.AreEqual(Player.State.Passive, expPlayer.CurrentState, "the creator is passive"); @@ -142,15 +145,23 @@ public void RoomTest3() // (:Player, :GameConfig) Assert.AreEqual(0, actual.ActivePlayers.Count, "zero active players"); Assert.IsTrue(actual.PassivePlayers.Contains(expPlayer), "the creator found in passive players collection at room"); Assert.AreEqual(null, actual.CurrentHand, "current hand null"); - Assert.AreEqual(false, actual.IsTableFull, "table not full"); + Assert.AreEqual(false, actual.IsTableFull, "table not full"); + #endregion // 8 Configurations of game-config - Assert.AreEqual(expBuyinCost, actual.BuyInCost, "default buy in"); //fixme - idan - Assert.AreEqual(expIsSpecAllowed, actual.IsSpactatorsAllowed, "default spectators allowed"); - Assert.AreEqual(expNActive, actual.MaxNumberOfActivePlayers, "max active players default"); - Assert.AreEqual(expMaxNumberPlayers, actual.MaxNumberOfPlayers, "default max players number"); + Assert.AreEqual(expBuyinCost, actual.BuyInCost, "exp buy in"); + Assert.AreEqual(expIsSpecAllowed, actual.IsSpactatorsAllowed, "exp spectators not allowed"); + Assert.AreEqual(expNActive, actual.MaxNumberOfActivePlayers, "exp max active players"); + + Assert.AreEqual(expMaxNumberPlayers, actual.MaxNumberOfPlayers, "max players number"); + Assert.IsTrue(!expIsSpecAllowed && actual.MaxNumberOfActivePlayers == actual.MaxNumberOfPlayers, "number of players without spectators"); + Assert.IsFalse(actual.MaxNumberOfActivePlayers > actual.MaxNumberOfPlayers, "active players equal or less to all players at the room"); + Assert.AreEqual(expMinBet, actual.MinimumBet, "minimum bet default"); Assert.AreEqual(expMinPlayers, actual.MinNumberOfPlayers, "min players default"); + Assert.IsFalse(actual.MinimumBet > actual.BuyInCost, "minBet <= buyIn"); + + Assert.IsNotNull(actual.Name, "name not null"); Assert.AreEqual(expName, actual.Name, "default name"); // TODO: idan - add assert for default game preferences. } diff --git a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs index df27058..7bb65fe 100644 --- a/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs +++ b/Poker.BE/Poker.BE.Domain/Game/GameConfig.cs @@ -104,13 +104,11 @@ public double BuyInCost // NOTE: adapt the set of buy in cost to the minimum bet set { - if (value < MinimumBet) - { - _minimumBet = _buyInCost = value; - } - else + _buyInCost = value; + + if (_buyInCost < MinimumBet) { - _buyInCost = value; + _minimumBet = _buyInCost; } } } @@ -154,7 +152,7 @@ public string Name /// public GameConfig() { - BuyInCost = 50.0; + BuyInCost = 60.0; Preferences = new GamePreferences(); IsSpactatorsAllowed = true; MaxNumberOfActivePlayers = 10; diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 399af7a..a29286f 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -166,30 +166,14 @@ public Room(Player creator, GamePreferences preferences) : this(creator) public Room(Player creator, GameConfig config) : this(creator, config.Preferences) { - Configure(config); + /*Note: 8 configurations */ + this.config = config; } #endregion #region Private Functions - private void Configure(GameConfig config) - { - - /*Note: 8 configurations */ - config = new GameConfig() - { - IsSpactatorsAllowed = config.IsSpactatorsAllowed, - MaxNumberOfPlayers = config.MaxNumberOfPlayers, - MaxNumberOfActivePlayers = config.MaxNumberOfActivePlayers, - MinNumberOfPlayers = config.MinNumberOfPlayers, - MinimumBet = config.MinimumBet, - BuyInCost = config.BuyInCost, - Name = config.Name, - Preferences = config.Preferences, - }; - } - private void TakeAChair(int index) { chairs[index].Take(); From e33952eb4dbbc8529ca899f68db18635325a91cb Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 19:43:28 +0300 Subject: [PATCH 30/49] join player to table - room - pass --- Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs | 11 +++++++++-- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 2 +- Poker.BE/Poker.BE.Domain/Game/Room.cs | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index b16ef71..9ee0f32 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -169,8 +169,15 @@ public void RoomTest3() // (:Player, :GameConfig) [TestMethod()] public void JoinPlayerToTableTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expPlayer = room.PassivePlayers.First(); + + //Act + var actual = room.JoinPlayerToTable(expPlayer); + + //Assert + Assert.IsTrue(actual); + Assert.IsTrue(room.ActivePlayers.Contains(expPlayer)); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 2a0113c..9b43724 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -291,7 +291,7 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) } /* Joining the player to the next hand */ - room.JoinPlayerToTable(player, buyIn); + room.JoinPlayerToTable(player); } /// diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index a29286f..8552429 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -193,14 +193,14 @@ private void ReleaseAChair(int index) /// postcondition: the player is active player at the room. /// /// a passive player at the room - public bool JoinPlayerToTable(Player player, double buyIn) + public bool JoinPlayerToTable(Player player) { if (player.CurrentState != Player.State.Passive) { return false; } - return player.JoinToTable(buyIn); + return player.JoinToTable(BuyInCost); } public void RemovePlayer(Player player) From 06c3e7c88d11b434c1169e9f55500c0a37c017b8 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 20:12:47 +0300 Subject: [PATCH 31/49] create player - room - pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 40 ++++++++++++++++--- Poker.BE/Poker.BE.Domain/Game/Player.cs | 37 +++++++++++++++++ Poker.BE/Poker.BE.Domain/Game/Room.cs | 12 ++++-- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 9ee0f32..d430abe 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -183,22 +183,50 @@ public void JoinPlayerToTableTest() [TestMethod()] public void RemovePlayerTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expected = room.Players.First(); + + //Act + var actual = room; + room.RemovePlayer(expected); + + //Assert + Assert.IsFalse(actual.Players.Contains(expected)); + Assert.IsFalse(actual.PassivePlayers.Contains(expected)); + Assert.IsFalse(actual.ActivePlayers.Contains(expected)); } [TestMethod()] public void ClearAllTest() { - // TODO - throw new NotImplementedException(); + //Arrange + + //Act + room.ClearAll(); + var actual = room; + + //Assert + Assert.AreEqual(0, actual.ActivePlayers.Count); + Assert.AreEqual(0, actual.PassivePlayers.Count); + Assert.AreEqual(0, actual.Players.Count); + //Assert.AreEqual(0, actual.Preferences); // TODO + Assert.AreEqual(false, actual.IsTableFull); + Assert.AreEqual(null, actual.CurrentHand); + Assert.IsNotNull(actual.Name); + } [TestMethod()] public void CreatePlayerTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expected = new Player(); + + //Act + var actual = room.CreatePlayer(); + + //Assert + Assert.AreEqual(expected, actual); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index da011bc..4a171eb 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -37,8 +37,13 @@ public Player() CurrentState = State.Passive; _wallet = new Wallet(); Wallet = 0.0; + Nickname = ""; } + #endregion + + #region Methods + public bool JoinToTable(double buyIn) { if (CurrentState != State.Passive) @@ -52,6 +57,7 @@ public bool JoinToTable(double buyIn) CurrentState = State.ActiveUnfolded; return true; } + /// /// Make the player to leave the table, and return his remaining wallet money to the user bank /// @@ -72,6 +78,37 @@ public double StandUp() return Wallet; } + // override object.Equals + public override bool Equals(object obj) + { + // + // See the full list of guidelines at + // http://go.microsoft.com/fwlink/?LinkID=85237 + // and also the guidance for operator== at + // http://go.microsoft.com/fwlink/?LinkId=85238 + // + + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + var other = obj as Player; + return + other != null + && CurrentState == other.CurrentState + && Nickname.Equals(other.Nickname) + //&& this.PrivateCards.Equals(other.PrivateCards) //TODO override card.equals + && Wallet.Equals(other.Wallet) + ; + } + + // override object.GetHashCode + public override int GetHashCode() + { + return base.GetHashCode(); + } + #endregion }// class diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 8552429..b6eb70b 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -205,17 +205,21 @@ public bool JoinPlayerToTable(Player player) public void RemovePlayer(Player player) { - // TODO - throw new NotImplementedException(); + activeAndPassivePlayers.Remove(player); } /// /// Method as a destructor - delete all players and other resources from the room. /// + /// + /// this function used be gameCenter do delete the room. + /// All players and other resources of room need to be deleted. + /// public void ClearAll() { - //TODO: this function used be gameCenter do delete the room. all players and other resources of room need to be deleted. - throw new NotImplementedException(); + this.activeAndPassivePlayers.Clear(); + this.CurrentHand = null; + this.Name = new GameConfig().Name; } public Player CreatePlayer() From 888637a843f9650f1336771a03c2e2f25a6667e6 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sat, 20 May 2017 22:13:26 +0300 Subject: [PATCH 32/49] take and leave chair - room - pass --- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 53 ++++++++++++------- Poker.BE/Poker.BE.Domain/Game/Chair.cs | 4 ++ Poker.BE/Poker.BE.Domain/Game/Room.cs | 47 +++++++++------- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + .../Exceptions/PlayerNotFoundException.cs | 6 +++ 5 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index d430abe..8f4c628 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -1,5 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Poker.BE.Domain.Game; +using Poker.BE.Domain.Utility.Exceptions; + using System; using System.Collections.Generic; using System.Linq; @@ -229,32 +231,47 @@ public void CreatePlayerTest() Assert.AreEqual(expected, actual); } - [TestMethod()] - public void StartNewHandTest() - { - // TODO - throw new NotImplementedException(); - } - [TestMethod()] public void TakeChairTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expPlayer = new Player(); + + //Act + var actual1 = room.TakeChair(expPlayer, 2); + var actTable = room.TableLocationOfActivePlayers.Count; + var actual2 = room.TakeChair(roomCreator, 9); + + //Assert + Assert.AreEqual(false, actual1); + Assert.AreEqual(0, actTable); + Assert.AreEqual(false, room.Chairs.ElementAt(2).IsBusy); + + Assert.AreEqual(true, actual2); + Assert.AreEqual(1, room.TableLocationOfActivePlayers.Count); + Assert.AreEqual(roomCreator, room.TableLocationOfActivePlayers[room.Chairs.ElementAt(9)]); + Assert.AreEqual(true, room.Chairs.ElementAt(9).IsBusy); } [TestMethod()] public void LeaveChairTest() { - // TODO - throw new NotImplementedException(); - } + //Arrange - [TestMethod()] - public void SendMessageTest() - { - // TODO - throw new NotImplementedException(); + //Act + room.TakeChair(roomCreator, 3); + var actual1 = room.TableLocationOfActivePlayers.Count; + var actChair = room.Chairs.ElementAt(3).IsBusy; + room.LeaveChair(roomCreator); + var actual2 = room.TableLocationOfActivePlayers.Count; + var actChair2 = room.Chairs.ElementAt(3).IsBusy; + + //Assert + Assert.AreEqual(1, actual1); + Assert.AreEqual(0, actual2); + Assert.AreEqual(true, actChair); + Assert.AreEqual(false, actChair2); } - } + + } // class } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Game/Chair.cs b/Poker.BE/Poker.BE.Domain/Game/Chair.cs index 8f6517c..cb476cc 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Chair.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Chair.cs @@ -15,6 +15,10 @@ public class Chair private bool isBusy; #endregion + #region Properties + public bool IsBusy { get { return isBusy; } } + #endregion + #region Constructors public Chair(int index) { diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index b6eb70b..68f3af9 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -63,6 +63,7 @@ public ICollection PassivePlayers } } public ICollection Players { get { return activeAndPassivePlayers; } } + public IDictionary TableLocationOfActivePlayers { get; private set; } #region GameConfig Properties (8) public GamePreferences Preferences @@ -129,15 +130,17 @@ public bool IsTableFull private Room() { activeAndPassivePlayers = new List(); + deck = new Deck(); chairs = new Chair[NCHAIRS_IN_ROOM]; for (int i = 0; i < NCHAIRS_IN_ROOM; i++) { - Chairs.ToArray()[i] = new Chair(i); + chairs[i] = new Chair(i); } CurrentHand = null; + TableLocationOfActivePlayers = new Dictionary(); // Note: default configuration config = new GameConfig(); @@ -172,20 +175,6 @@ public Room(Player creator, GameConfig config) : this(creator, config.Preference #endregion - #region Private Functions - - private void TakeAChair(int index) - { - chairs[index].Take(); - } - - private void ReleaseAChair(int index) - { - chairs[index].Release(); - } - - #endregion - #region Methods /// /// Converting passive player to active player. @@ -236,14 +225,32 @@ public void StartNewHand() public bool TakeChair(Player player, int index) { - // TODO - throw new NotImplementedException(); + var result = PassivePlayers.Where(item => item == player); + if (result.Count() != 1 || !chairs[index].Take()) + { + return false; + } + + // register player location at the table. + TableLocationOfActivePlayers.Add(chairs[index], player); + + return true; } - public bool LeaveChair(Player player) + public void LeaveChair(Player player) { - // TODO - throw new NotImplementedException(); + if (TableLocationOfActivePlayers.Values.Contains(player)) + { + foreach (var chair in TableLocationOfActivePlayers.Keys) + { + if(TableLocationOfActivePlayers[chair] == player) + { + TableLocationOfActivePlayers.Remove(chair); + chair.Release(); + return; + } + } + } } public void SendMessage() diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index 44352b7..70173cd 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -71,6 +71,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs new file mode 100644 index 0000000..04b710b --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs @@ -0,0 +1,6 @@ +namespace Poker.BE.Domain.Utility.Exceptions +{ + public class PlayerNotFoundException + { + } +} \ No newline at end of file From c45618d28f90b8e4f3ebe08ad87fef89b21607d8 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 13:11:22 +0300 Subject: [PATCH 33/49] enter room & create new room - gamecenter - pass --- .../Core/GameCenterTests.cs | 33 ++++++++++++------- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 31 ++++++++++++++--- Poker.BE/Poker.BE.Domain/Game/League.cs | 1 + 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 04fa4c5..3b4aeea 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -26,17 +26,11 @@ public void Before() [TestCleanup] public void After() { + gameCenter.ClearAll(); gameCenter = null; } #endregion - [TestMethod()] - public void GameCenterTest() - { - // TODO - simple constructor - may not need to test? - throw new NotImplementedException(); - } - #region Find an Existing Room - Functions Overloading Tests [TestMethod()] public void FindRoomsByCriteriaTest() @@ -68,7 +62,6 @@ public void FindRoomsByCriteriaTest3() #endregion [TestMethod()] - [ExpectedException(typeof(Exception))] public void EnterRoomTest() { //Arrange @@ -80,14 +73,32 @@ public void EnterRoomTest() actual = gameCenter.EnterRoom(room); //Assert - Assert.AreEqual(expected, actual); + Assert.AreEqual(expected, actual, "returned player"); + Assert.IsNotNull(actual.Nickname, "nickname not null"); + Assert.AreEqual(0, actual.Wallet, "wallet = 0 (before buy in to table)"); } [TestMethod()] public void CreateNewRoomTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expPlayer = new Player(); + var expRoom = new Room(expPlayer); + + GameConfig inConfig = new GameConfig(); + int inLevel = 3; + + //Act + var actual = gameCenter.CreateNewRoom(inLevel, inConfig, out Player actCreator); + + //Assert + Assert.AreEqual(expPlayer, actCreator, "eq creators"); + Assert.AreEqual(1, gameCenter.Rooms.Count, "rooms count"); + Assert.AreEqual(1, gameCenter.Players.Count, "players count"); + Assert.AreEqual(1, gameCenter.Leagues.Count, "leagues count"); + Assert.AreEqual(actual, gameCenter.Rooms.First(), "room is registered"); + Assert.AreEqual(expPlayer, gameCenter.Players.First(), "player is registered"); + Assert.IsTrue(gameCenter.Leagues.First().Rooms.Contains(actual), "league is registered and contain room"); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 9b43724..acd0fce 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -26,6 +26,16 @@ public enum Move Call, AllIn } + + /// + /// clears all game-center resources + /// + public void ClearAll() + { + playersManager.Clear(); + roomsManager.Clear(); + leagues.Clear(); + } #endregion #region Fields @@ -73,10 +83,11 @@ private Player CreatePlayer(Room room) return room.CreatePlayer(); } - private void AddLeage(int minLevel, int maxLevel) + private League AddLeage(int minLevel = League.MIN_LEVEL, int maxLevel = League.MAX_LEVEL) { var league = new League() { MaxLevel = maxLevel, MinLevel = minLevel }; leagues.Add(league); + return league; } private bool RemovePlayer(Player player) @@ -162,13 +173,23 @@ private ICollection GetAllLeaguesAtLevel(int level) } } + if (result.Count == 0) + { + result.Add(AddLeage()); + } + return result; } - private League FindLeagueToFill(ICollection collection) + private League FindLeagueToFill(ICollection leaguesPartialGroup) { // TODO: pick a league to insert the new created room, by the relevant requirements. - throw new NotImplementedException(); + + // HACK: this is a stub return + return + (from league in leaguesPartialGroup + where !league.IsFull + select league).First(); } #endregion @@ -231,9 +252,9 @@ public Player EnterRoom(Room room) /// /// user level /// the new created room - public Room CreateNewRoom(int level, GameConfig config) + public Room CreateNewRoom(int level, GameConfig config, out Player creator) { - var creator = new Player(); + creator = new Player(); // creating the room and adding the creator as a passive player to it. var room = new Room(creator, config); diff --git a/Poker.BE/Poker.BE.Domain/Game/League.cs b/Poker.BE/Poker.BE.Domain/Game/League.cs index 470bfd6..a260592 100644 --- a/Poker.BE/Poker.BE.Domain/Game/League.cs +++ b/Poker.BE/Poker.BE.Domain/Game/League.cs @@ -21,6 +21,7 @@ public class League public ICollection Rooms { get; set; } public int MaxLevel { get; set; } public int MinLevel { get; set; } + public bool IsFull { get; internal set; } #endregion #region Constructors From 6fc5e29450af1bdff42160e35663a9eaeee55786 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 13:28:58 +0300 Subject: [PATCH 34/49] working on join next hand tests --- .../Core/GameCenterTests.cs | 49 +++++++++++++++++-- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 2 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 3b4aeea..3b66ec9 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -13,7 +13,7 @@ namespace Poker.BE.Domain.Core.Tests public class GameCenterTests { #region Set Up - + public TestContext TestContext { get; set; } private GameCenter gameCenter; @@ -102,10 +102,53 @@ public void CreateNewRoomTest() } [TestMethod()] + [ExpectedException(typeof(Utility.Exceptions.NotEnoughMoneyException))] public void JoinNextHandTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + double inBuyin = expRoom.BuyInCost - 20.2; + int inSeatIndex = 2; + + //Act + gameCenter.JoinNextHand(expPlayer, inSeatIndex, inBuyin); + + //Assert + + // if no exception occurs + Assert.Fail(); + } + + [TestMethod] + [ExpectedException(typeof(Utility.Exceptions.PlayerNotFoundException))] + public void JoinNextHandTest1() + { + //Arrange + //var expected = ; + + Player expPlayer = null; + double inBuyin = 0; + int inSeatIndex = 0; + + //Act + gameCenter.JoinNextHand(expPlayer, inSeatIndex, inBuyin); + + //Assert + //Assert.AreEqual(expected, actual); + } + + [TestMethod] + [ExpectedException(typeof(Utility.Exceptions.RoomRulesException))] + public void JoinNextHandTest2() + { + //Arrange + //var expected = ; + + //Act + //var actual = ; + + //Assert + //Assert.AreEqual(expected, actual); } [TestMethod()] diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index acd0fce..c0c40e9 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -306,7 +306,7 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) } // the user has enough money to buy in - if (buyIn < room.MinimumBet) + if (buyIn < room.BuyInCost) { throw new NotEnoughMoneyException("Buy in amount is less then the minimum to join the table."); } From 7475af0aa2565b67678450025135a1cad3e647dc Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 15:25:36 +0300 Subject: [PATCH 35/49] working on find rooms --- .../Core/GameCenterTests.cs | 71 ++++++++++++++++--- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 60 ++++++++++------ Poker.BE/Poker.BE.Domain/Game/Player.cs | 2 +- Poker.BE/Poker.BE.Domain/Game/Room.cs | 21 ++++-- 4 files changed, 115 insertions(+), 39 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 3b66ec9..d2f470e 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -116,17 +116,17 @@ public void JoinNextHandTest() //Assert // if no exception occurs - Assert.Fail(); + Assert.Fail("not enough money"); } [TestMethod] - [ExpectedException(typeof(Utility.Exceptions.PlayerNotFoundException))] + [ExpectedException(typeof(Utility.Exceptions.RoomNotFoundException))] public void JoinNextHandTest1() { //Arrange //var expected = ; - Player expPlayer = null; + Player expPlayer = new Player(); double inBuyin = 0; int inSeatIndex = 0; @@ -134,7 +134,9 @@ public void JoinNextHandTest1() gameCenter.JoinNextHand(expPlayer, inSeatIndex, inBuyin); //Assert - //Assert.AreEqual(expected, actual); + + // if no exception + Assert.Fail("room not found for player"); } [TestMethod] @@ -142,20 +144,71 @@ public void JoinNextHandTest1() public void JoinNextHandTest2() { //Arrange - //var expected = ; + int seatIndex = 200; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + double buyIn = expRoom.BuyInCost + 2.5; //Act - //var actual = ; + gameCenter.JoinNextHand(expPlayer, seatIndex, buyIn); //Assert - //Assert.AreEqual(expected, actual); + Assert.Fail("seat taken / not exists"); + } + + [TestMethod] + public void JoinNextHand_success() + { + //Arrange + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + expPlayer.Nickname = "yosi"; + int seatIndex = 4; + double buyIn = expRoom.BuyInCost; + + //Act + gameCenter.JoinNextHand(expPlayer, seatIndex, buyIn); + + //Assert + Assert.AreEqual(Player.State.ActiveUnfolded, expPlayer.CurrentState); + Assert.AreEqual(buyIn, expPlayer.Wallet); + Assert.AreEqual(expPlayer , expRoom.TableLocationOfActivePlayers[expRoom.Chairs.ElementAt(seatIndex)]); + Assert.AreEqual(1, expRoom.ActivePlayers.Count); } [TestMethod()] + [ExpectedException(typeof(Utility.Exceptions.PlayerModeException))] public void StandUpToSpactateTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player actPlayer); + actPlayer.Nickname = "yossi"; + var expMoney = expRoom.BuyInCost; + gameCenter.JoinNextHand(actPlayer, 2, expMoney); + + //Act + var actMoney = gameCenter.StandUpToSpactate(actPlayer); + + //Assert + Assert.Fail("expected exception: player mode exception"); + } + + [TestMethod] + public void StandUpToSpactateTest_success() + { + //Arrange + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player actPlayer); + actPlayer.Nickname = "yossi"; + var expMoney = expRoom.BuyInCost; + gameCenter.JoinNextHand(actPlayer, 2, expMoney); + + actPlayer.CurrentState = Player.State.ActiveFolded; + + //Act + var actMoney = gameCenter.StandUpToSpactate(actPlayer); + + //Assert + Assert.AreEqual(expMoney, actMoney); + Assert.AreEqual(Player.State.Passive, actPlayer.CurrentState); + Assert.AreEqual(0, expRoom.ActivePlayers.Count); } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index c0c40e9..0e3e011 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -196,37 +196,42 @@ private League FindLeagueToFill(ICollection leaguesPartialGroup) #region Methods - #region Find an Existing Room /// /// Allow the user to find an existing room according to different criteria and enter the room as a spectator. /// /// UC004: Find an Existing Room /// Collection of rooms /// - public ICollection FindRoomsByCriteria(int level) + public ICollection FindRoomsByCriteria(int level = -1, Player player = null, GamePreferences preferences = null, double betSize = -1.0) { - // TODO: idan - throw new NotImplementedException(); - } + var result = new List(); - public ICollection FindRoomsByCriteria(Player player) - { - // TODO: idan - throw new NotImplementedException(); - } + if(level > 0) + { + var collections = (from league in leagues + where league.MinLevel < level & league.MaxLevel > level + select league.Rooms); + var flat = collections.Aggregate(new List(), (acc, x) => acc.Concat(x).ToList()); + result.Concat(flat); + } - public ICollection FindRoomsByCriteria(GamePreferences preferences) - { - // TODO: idan - throw new NotImplementedException(); - } + if(player != null) + { - public ICollection FindRoomsByCriteria(double betSize) - { - // TODO: idan - throw new NotImplementedException(); + } + + if(preferences != null) + { + + } + + if(betSize > 0) + { + + } + + return result; } - #endregion /// /// Allow the user to enter a room from a list as a spectator. @@ -333,9 +338,20 @@ public double StandUpToSpactate(Player player) } // it's the player's turn - if (room.CurrentHand.CurrentRound.CurrentTurn.CurrentPlayer != player) + + try { - throw new NotPlayersTurnException("Unable to stand up"); + if (room.CurrentHand.CurrentRound.CurrentTurn.CurrentPlayer != player) + { + throw new NotPlayersTurnException("Unable to stand up"); + } + } + catch (NullReferenceException) + { + if(player.CurrentState == Player.State.Passive) + { + throw new RoomRulesException("Player is already a spectator"); + } } /* Action - Make the player to stand up */ diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 4a171eb..0b79d1e 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -24,7 +24,7 @@ public enum State #endregion #region Properties - public State CurrentState { get; protected set; } + public State CurrentState { get; set; } public double Wallet { get { return _wallet.Value; } private set { _wallet.Value = value; } } public Card[] PrivateCards { get; set; } public string Nickname { get; set; } diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index 68f3af9..ed785d2 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -225,16 +225,23 @@ public void StartNewHand() public bool TakeChair(Player player, int index) { - var result = PassivePlayers.Where(item => item == player); - if (result.Count() != 1 || !chairs[index].Take()) + try { - return false; - } + var result = PassivePlayers.Where(item => item == player); + if (result.Count() != 1 || !chairs[index].Take()) + { + return false; + } - // register player location at the table. - TableLocationOfActivePlayers.Add(chairs[index], player); + // register player location at the table. + TableLocationOfActivePlayers.Add(chairs[index], player); - return true; + return true; + } + catch (IndexOutOfRangeException) + { + throw new RoomRulesException("chair index does not exists"); + } } public void LeaveChair(Player player) From 717aaac78c8a37fd93f88e8faa544e7bc55eeccd Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 15:50:49 +0300 Subject: [PATCH 36/49] working on find rooms --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 35 ++++++++++++++----- .../Poker.BE.Domain/Poker.BE.Domain.csproj | 1 + 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 0e3e011..04ee9d0 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -206,28 +206,47 @@ public ICollection FindRoomsByCriteria(int level = -1, Player player = nul { var result = new List(); - if(level > 0) + if (level > 0) { var collections = (from league in leagues where league.MinLevel < level & league.MaxLevel > level select league.Rooms); - var flat = collections.Aggregate(new List(), (acc, x) => acc.Concat(x).ToList()); + + var flat = + collections.Aggregate(new List(), (acc, x) => acc.Concat(x).ToList()) + .Distinct(new Utility.AddressComparer()); result.Concat(flat); } - if(player != null) + if (player != null) { + if (!playersManager.TryGetValue(player, out Room room)) + { + throw new RoomNotFoundException("room not found for player: " + player.GetHashCode()); + } + if (result.Count == 0) + { + result.Add(room); + } + else + { + result = (from item in result where item == room select item).ToList(); + if (result.Count != 1) + { + throw new RoomNotFoundException("mismatch of searching room. only 1 occurrence didn't returned"); + } + } } - if(preferences != null) + if (preferences != null) { - + // undone - idan - continue from here } - if(betSize > 0) + if (betSize > 0) { - + // TODO } return result; @@ -348,7 +367,7 @@ public double StandUpToSpactate(Player player) } catch (NullReferenceException) { - if(player.CurrentState == Player.State.Passive) + if (player.CurrentState == Player.State.Passive) { throw new RoomRulesException("Player is already a spectator"); } diff --git a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj index 70173cd..c647121 100644 --- a/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj +++ b/Poker.BE/Poker.BE.Domain/Poker.BE.Domain.csproj @@ -56,6 +56,7 @@ + From eff02ed1f4cac1a62a3690faf4cebc878590a36c Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 15:51:04 +0300 Subject: [PATCH 37/49] working on find rooms --- .../Utility/AddressComparer.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs diff --git a/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs b/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs new file mode 100644 index 0000000..56053b2 --- /dev/null +++ b/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Utility +{ + public class AddressComparer : IEqualityComparer + { + public bool Equals(T x, T y) + { + return x.GetHashCode() == y.GetHashCode(); + } + + public int GetHashCode(T obj) + { + return obj.GetHashCode(); + } + } +} From 017e20686476473e6e3747e4c69537251649cfa3 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 20:06:56 +0300 Subject: [PATCH 38/49] find room by criteria - gamecenter - level, palyer - pass --- .../Core/GameCenterTests.cs | 77 ++++++++++++++++--- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 15 +++- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index d2f470e..458aa1b 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -33,28 +33,87 @@ public void After() #region Find an Existing Room - Functions Overloading Tests [TestMethod()] - public void FindRoomsByCriteriaTest() + public void FindRoomsByCriteriaTest_level() { - // TODO - throw new NotImplementedException(); + //Arrange + + GameConfig config = new GameConfig(); + int level = 4; + var expRoom = gameCenter.CreateNewRoom(level, config, out Player creator); + Exception expE = null; + expRoom.Name = "test room"; + + + TestContext.WriteLine("min level {0} max level {1}", gameCenter.Leagues.Single().MinLevel, gameCenter.Leagues.Single().MaxLevel); + + //Act + var actRoom = gameCenter.FindRoomsByCriteria(45); + try + { + gameCenter.FindRoomsByCriteria(0); + + Assert.Fail("exception missing"); + } + catch (Utility.Exceptions.RoomNotFoundException e) + { + expE = e; + } + + //Assert + Assert.IsNotNull(expE); + TestContext.WriteLine("exception message: {0}", expE.Message); + Assert.AreEqual(1, actRoom.Count); + Assert.AreEqual(expRoom, actRoom.Single()); + } + + [TestMethod] + public void FindRoomsByCriteriaTest_multiple_rooms() + { + //Arrange + GameConfig config = new GameConfig(); + int level = 4; + var expRoom = gameCenter.CreateNewRoom(level, config, out Player creator).Name = "test room 1"; + var expRoom2 = gameCenter.CreateNewRoom(level, config, out Player creator2).Name = "test room 2"; + + //Act + var actual = gameCenter.FindRoomsByCriteria(25); + + //Assert + Assert.AreEqual(2, actual.Count); } [TestMethod()] - public void FindRoomsByCriteriaTest1() + public void FindRoomsByCriteriaTest_player() { - // TODO - throw new NotImplementedException(); + //Arrange + var expRoom = gameCenter.CreateNewRoom(6, new GameConfig(), out Player expPlayer); + + //Act + var actual = gameCenter.FindRoomsByCriteria(-1, expPlayer); + try + { + gameCenter.FindRoomsByCriteria(-1, new Player()); + Assert.Fail("room not found exception"); + } + catch (Utility.Exceptions.RoomNotFoundException e) + { + TestContext.WriteLine("exception message " + e.Message); + } + + //Assert + Assert.AreEqual(1, actual.Count); + Assert.AreEqual(expRoom, actual.Single()); } [TestMethod()] - public void FindRoomsByCriteriaTest2() + public void FindRoomsByCriteriaTest_perf() { // TODO throw new NotImplementedException(); } [TestMethod()] - public void FindRoomsByCriteriaTest3() + public void FindRoomsByCriteriaTest_betsize() { // TODO throw new NotImplementedException(); @@ -170,7 +229,7 @@ public void JoinNextHand_success() //Assert Assert.AreEqual(Player.State.ActiveUnfolded, expPlayer.CurrentState); Assert.AreEqual(buyIn, expPlayer.Wallet); - Assert.AreEqual(expPlayer , expRoom.TableLocationOfActivePlayers[expRoom.Chairs.ElementAt(seatIndex)]); + Assert.AreEqual(expPlayer, expRoom.TableLocationOfActivePlayers[expRoom.Chairs.ElementAt(seatIndex)]); Assert.AreEqual(1, expRoom.ActivePlayers.Count); } diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 04ee9d0..fb5ec50 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -209,13 +209,13 @@ public ICollection FindRoomsByCriteria(int level = -1, Player player = nul if (level > 0) { var collections = (from league in leagues - where league.MinLevel < level & league.MaxLevel > level + where league.MinLevel <= level & league.MaxLevel >= level select league.Rooms); var flat = collections.Aggregate(new List(), (acc, x) => acc.Concat(x).ToList()) .Distinct(new Utility.AddressComparer()); - result.Concat(flat); + result.AddRange(flat); } if (player != null) @@ -249,6 +249,17 @@ where league.MinLevel < level & league.MaxLevel > level // TODO } + if (result.Count == 0) + { + throw new RoomNotFoundException( + "no rooms are find by the criteria: " + + (level > -1 ? "level: " + level : "") + + (player != null ? "player id: " + player.GetHashCode() : "") + + (preferences != null ? "game preferences: " + preferences.GetType().Name : "") + + (betSize > -1.0 ? "bet size: " + betSize : "") + ); + } + return result; } From 12edd8f3efc64d719498704ec266fea048c4bbb3 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 22:39:05 +0300 Subject: [PATCH 39/49] gamecenter test all pass. working on user tests of ucc03 methods - last thig! --- .../Core/GameCenterTests.cs | 32 +++++++++--- .../Poker.BE.Domain.Tests.csproj | 1 + Poker.BE/Poker.BE.Domain/Core/Bank.cs | 37 ++++++++++---- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 8 ++- Poker.BE/Poker.BE.Domain/Core/User.cs | 50 +++++++++---------- 5 files changed, 80 insertions(+), 48 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 458aa1b..1b3b08e 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -105,18 +105,34 @@ public void FindRoomsByCriteriaTest_player() Assert.AreEqual(expRoom, actual.Single()); } - [TestMethod()] - public void FindRoomsByCriteriaTest_perf() - { - // TODO - throw new NotImplementedException(); - } + // TODO: find room by preferences + //[TestMethod()] + //public void FindRoomsByCriteriaTest_perf() + //{ + // //Arrange + + // //Act + + // //Assert + //} [TestMethod()] public void FindRoomsByCriteriaTest_betsize() { - // TODO - throw new NotImplementedException(); + //Arrange + var expPlayers = new Player[3]; + var expRooms = new Room[] { + gameCenter.CreateNewRoom(1, new GameConfig(){ Name = "test room 1", MinimumBet = 20.9}, out expPlayers[0]), + gameCenter.CreateNewRoom(1, new GameConfig(){ Name = "test room 2", MinimumBet = 20.9}, out expPlayers[1]), + gameCenter.CreateNewRoom(1, new GameConfig(){ Name = "test room 3", MinimumBet = 30.2}, out expPlayers[2]), + }; + + //Act + var actual = gameCenter.FindRoomsByCriteria(-1, null, null, 20.9); + + //Assert + Assert.AreEqual(2, actual.Count); + Assert.AreEqual(3, gameCenter.Rooms.Count); } #endregion diff --git a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj index 1101dbb..bf746d7 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj +++ b/Poker.BE/Poker.BE.Domain.Tests/Poker.BE.Domain.Tests.csproj @@ -46,6 +46,7 @@ + diff --git a/Poker.BE/Poker.BE.Domain/Core/Bank.cs b/Poker.BE/Poker.BE.Domain/Core/Bank.cs index 3b979f1..68d03bb 100644 --- a/Poker.BE/Poker.BE.Domain/Core/Bank.cs +++ b/Poker.BE/Poker.BE.Domain/Core/Bank.cs @@ -11,28 +11,43 @@ public class Bank : Utility.MoneyStorage // UNDONE: make this class align with extending MoneyStorage Class. #region Properties - protected double Money { get; set;} + public double Money { get; set; } + #endregion + + #region Constructors + public Bank() + { + Money = 0.0; + } + public Bank(double sumToDeposit) + { + Deposit(sumToDeposit); + } #endregion #region Methods - public Bank (double sumToDeposit){ - Deposit (sumToDeposit); - } - protected bool CanWithdraw (double sum){ - return (sum < Money) ; + + + + protected bool CanWithdraw(double sum) + { + return (sum < Money); } - protected bool Withdraw (double sum){ - if (CanWithdraw (sum)){ + protected bool Withdraw(double sum) + { + if (CanWithdraw(sum)) + { Money = Money - sum; return true; } return false; } - protected void Deposit (double sum){ + protected void Deposit(double sum) + { Money = Money + sum; } @@ -42,7 +57,7 @@ protected void Deposit (double sum){ } - - + + } diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index fb5ec50..6912686 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -241,12 +241,16 @@ public ICollection FindRoomsByCriteria(int level = -1, Player player = nul if (preferences != null) { - // undone - idan - continue from here + // undone - idan - continue from here - after game preferences will be implemented. } if (betSize > 0) { - // TODO + result.AddRange( + from room in Rooms + where room.MinimumBet == betSize + select room + ); } if (result.Count == 0) diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 0b89fb7..943975c 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -20,18 +20,26 @@ public class User : AbstractUser #endregion #region Properties - + public ICollection Players { get; set; } #endregion #region Constructors - public User(string userName, string password, double sumToDeposit) + public User() + { + IsConnected = false; + Password = ""; + Players = new List(); + UserBank = new Bank(); + UserName = GetHashCode().ToString(); + } + + public User(string userName, string password, double sumToDeposit) : this() { UserName = userName; Password = password; UserBank = new Bank(sumToDeposit); IsConnected = true; - Players = new List(); } #endregion @@ -48,32 +56,17 @@ public void Disconnect() IsConnected = false; } - - #region Find an Existing Room - public ICollection FindRoomsByCriteria(int level) - { - // TODO - throw new NotImplementedException(); - } - - public ICollection FindRoomsByCriteria(Player player) - { - // TODO - throw new NotImplementedException(); - } - - public ICollection FindRoomsByCriteria(GamePreferences preferences) - { - // TODO - throw new NotImplementedException(); - } - - public ICollection FindRoomsByCriteria(double betSize) + #region UCC03 Rooms Management Methods + /// + /// Allow the user to find an existing room according to different criteria and enter the room as a spectator. + /// + /// UC004: Find an Existing Room + /// Collection of rooms + /// + public ICollection FindRoomsByCriteria(int level = -1, Player player = null, GamePreferences preferences = null, double betSize = -1.0) { - // TODO - throw new NotImplementedException(); + return gameCenter.FindRoomsByCriteria(level, player, preferences, betSize); } - #endregion /// /// Allow the user to enter a room from a list as a spectator. @@ -131,5 +124,8 @@ public bool StandUpToSpactate(int sessionID) } #endregion + + #endregion + } } From 053b01820deae1e62688cc7ec71f68ad40c2f921 Mon Sep 17 00:00:00 2001 From: idanizi Date: Sun, 21 May 2017 22:39:26 +0300 Subject: [PATCH 40/49] gamecenter test all pass. working on user tests of ucc03 methods - last thig! --- .../Poker.BE.Domain.Tests/Core/UserTests.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs new file mode 100644 index 0000000..d46a0fd --- /dev/null +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs @@ -0,0 +1,80 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Poker.BE.Domain.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Poker.BE.Domain.Core.Tests +{ + [TestClass()] + public class UserTests + { + + #region Setup + private User user; + + public TestContext TestContext { get; set; } + + [TestInitialize] + public void Before() + { + user = new User(); + } + + [TestCleanup] + public void After() + { + user = null; + } + #endregion + + // TODO: Tomer / Ariel: unit testing for user class + //[TestMethod()] + //public void UserTest() + //{ + + //} + + //[TestMethod()] + //public void ConnectTest() + //{ + + //} + + //[TestMethod()] + //public void DisconnectTest() + //{ + + //} + + [TestMethod()] + public void EnterRoomTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void CreateNewRoomTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void JoinNextHandTest() + { + // TODO + throw new NotImplementedException(); + } + + [TestMethod()] + public void StandUpToSpactateTest() + { + // TODO + throw new NotImplementedException(); + } + } +} \ No newline at end of file From b34ea9e1c4a9cebd1d85af46e30750bf309e7e97 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 01:33:56 +0300 Subject: [PATCH 41/49] all tests pass ucc03 --- .../Poker.BE.Domain.Tests/Core/UserTests.cs | 82 ++++++++++++++++--- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 2 +- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 2 +- Poker.BE/Poker.BE.Domain/Core/User.cs | 35 +++++--- Poker.BE/Poker.BE.Domain/Game/Room.cs | 4 +- .../Exceptions/PlayerNotFoundException.cs | 23 +++++- 6 files changed, 120 insertions(+), 28 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs index d46a0fd..2e84856 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs @@ -1,5 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Poker.BE.Domain.Core; +using Poker.BE.Domain.Game; + using System; using System.Collections.Generic; using System.Linq; @@ -50,31 +52,87 @@ public void After() //} [TestMethod()] - public void EnterRoomTest() + public void CreateNewRoomTest() { - // TODO - throw new NotImplementedException(); + //Arrange + + //Act + var actRoom = user.CreateNewRoom(1, new GameConfig(), out Player creator); + + //Assert + Assert.IsTrue(user.Players.Contains(creator, new Utility.AddressComparer())); } [TestMethod()] - public void CreateNewRoomTest() + public void EnterRoomTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var expRoom = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + creator.Nickname = "test player"; + + //Act + try + { + user.EnterRoom(expRoom); + Assert.Fail("expected exception"); + } + catch (Utility.Exceptions.RoomRulesException e) + { + TestContext.WriteLine(e.Message); + } + var user2 = new User(); + var player2 = user2.EnterRoom(expRoom); + + //Assert + Assert.IsTrue(user.Players.Contains(creator, new Utility.AddressComparer())); + Assert.IsTrue(user2.Players.Contains(player2, new Utility.AddressComparer())); } - [TestMethod()] - public void JoinNextHandTest() + [TestMethod] + public void JoinNextHand() { - // TODO - throw new NotImplementedException(); + //Arrange + var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + + //Act + try + { + user.JoinNextHand(new Player(), 0, room.BuyInCost); + Assert.Fail("expected exception"); + } + catch (Utility.Exceptions.PlayerNotFoundException) + { + } + + user.JoinNextHand(creator, 0, room.BuyInCost + 20.2); + + //Assert + Assert.AreEqual(Player.State.ActiveUnfolded, user.Players.Single().CurrentState); } [TestMethod()] public void StandUpToSpactateTest() { - // TODO - throw new NotImplementedException(); + //Arrange + var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + user.JoinNextHand(creator, 0, room.BuyInCost + 20.2); + user.Players.Single().CurrentState = Player.State.ActiveFolded; + + //Act + try + { + user.StandUpToSpactate(new Player()); + Assert.Fail("expected exception"); + } + catch (Utility.Exceptions.PlayerNotFoundException) + { + } + + var act = user.StandUpToSpactate(user.Players.Single()); + + //Assert + Assert.AreEqual(Player.State.Passive, user.Players.Single().CurrentState); + Assert.AreEqual(room.BuyInCost + 20.2, act); } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 8f4c628..e157ed5 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -175,7 +175,7 @@ public void JoinPlayerToTableTest() var expPlayer = room.PassivePlayers.First(); //Act - var actual = room.JoinPlayerToTable(expPlayer); + var actual = room.JoinPlayerToTable(expPlayer, room.BuyInCost + 10.3); //Assert Assert.IsTrue(actual); diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 6912686..112e76c 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -351,7 +351,7 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) } /* Joining the player to the next hand */ - room.JoinPlayerToTable(player); + room.JoinPlayerToTable(player, buyIn); } /// diff --git a/Poker.BE/Poker.BE.Domain/Core/User.cs b/Poker.BE/Poker.BE.Domain/Core/User.cs index 943975c..d86ae06 100644 --- a/Poker.BE/Poker.BE.Domain/Core/User.cs +++ b/Poker.BE/Poker.BE.Domain/Core/User.cs @@ -82,7 +82,7 @@ public Player EnterRoom(Room room) if (Players.Count > 0 && room.Players.Count > 0) { var result = from player in room.Players - where Players.Contains(player) + where Players.Contains(player, new Utility.AddressComparer()) select player; if (result.Count() > 0) @@ -105,22 +105,37 @@ where Players.Contains(player) return freshPlayer; } - public bool CreateNewRoom(int level, GameConfig config) + public Room CreateNewRoom(int level, GameConfig config, out Player creator) { - // TODO - throw new NotImplementedException(); + var result = gameCenter.CreateNewRoom(level, config, out creator); + Players.Add(creator); + + // log info + logger.Info(string.Format("user {0} has created an new room {1}", GetHashCode(), result.GetHashCode()), this); + + return result; } - public bool JoinNextHand(int sessionID, int seatIndex, double buyIn) + public void JoinNextHand(Player player, int seatIndex, double buyIn) { - // TODO - throw new NotImplementedException(); + if(!Players.Contains(player, new Utility.AddressComparer())) + { + throw new PlayerNotFoundException("the user doesn't have this player"); + } + + gameCenter.JoinNextHand(player, seatIndex, buyIn); } - public bool StandUpToSpactate(int sessionID) + public double StandUpToSpactate(Player player) { - // TODO - throw new NotImplementedException(); + if (!Players.Contains(player, new Utility.AddressComparer())) + { + throw new PlayerNotFoundException("the user doesn't have this player"); + } + + UserBank.Money = gameCenter.StandUpToSpactate(player); + + return UserBank.Money; } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Room.cs b/Poker.BE/Poker.BE.Domain/Game/Room.cs index ed785d2..cf6e533 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Room.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Room.cs @@ -182,14 +182,14 @@ public Room(Player creator, GameConfig config) : this(creator, config.Preference /// postcondition: the player is active player at the room. /// /// a passive player at the room - public bool JoinPlayerToTable(Player player) + public bool JoinPlayerToTable(Player player, double buyIn) { if (player.CurrentState != Player.State.Passive) { return false; } - return player.JoinToTable(BuyInCost); + return player.JoinToTable(buyIn); } public void RemovePlayer(Player player) diff --git a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs index 04b710b..a0ea224 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/Exceptions/PlayerNotFoundException.cs @@ -1,6 +1,25 @@ -namespace Poker.BE.Domain.Utility.Exceptions +using System; +using System.Runtime.Serialization; + +namespace Poker.BE.Domain.Utility.Exceptions { - public class PlayerNotFoundException + [Serializable] + public class PlayerNotFoundException : PokerException { + public PlayerNotFoundException() + { + } + + public PlayerNotFoundException(string message) : base(message) + { + } + + public PlayerNotFoundException(string message, Exception innerException) : base(message, innerException) + { + } + + protected PlayerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } } } \ No newline at end of file From e2258b741b3550f75f0bf3c122da5b067839af3a Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 01:53:28 +0300 Subject: [PATCH 42/49] fixing for ci --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index 112e76c..d796fd4 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -222,7 +222,7 @@ public ICollection FindRoomsByCriteria(int level = -1, Player player = nul { if (!playersManager.TryGetValue(player, out Room room)) { - throw new RoomNotFoundException("room not found for player: " + player.GetHashCode()); + throw new RoomNotFoundException("room not found for player: " + player.GetHashCode().ToString()); } if (result.Count == 0) @@ -366,7 +366,8 @@ public double StandUpToSpactate(Player player) /* Checking Preconditions */ // the player is sitting at the table - if (!playersManager.TryGetValue(player, out Room room)) + Room room = null; + if (!playersManager.TryGetValue(player, out room)) { throw new RoomNotFoundException("Unable to stand up - The player is not at the room"); } From e71afa63faebeefb3a0cde016b77377311ae86c7 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 02:07:00 +0300 Subject: [PATCH 43/49] fixing for ci --- Poker.BE/Poker.BE.Domain/Core/GameCenter.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs index d796fd4..50b972d 100644 --- a/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs +++ b/Poker.BE/Poker.BE.Domain/Core/GameCenter.cs @@ -220,7 +220,8 @@ public ICollection FindRoomsByCriteria(int level = -1, Player player = nul if (player != null) { - if (!playersManager.TryGetValue(player, out Room room)) + Room room = null; + if (!playersManager.TryGetValue(player, out room)) { throw new RoomNotFoundException("room not found for player: " + player.GetHashCode().ToString()); } @@ -321,7 +322,8 @@ public void JoinNextHand(Player player, int seatIndex, double buyIn) /* Checking Preconditions */ // get the room of the player belongs to - if (!playersManager.TryGetValue(player, out Room room)) + Room room = null; + if (!playersManager.TryGetValue(player, out room)) { throw new RoomNotFoundException("Try joining next hand for a player with room that can't be found"); } From 18b073225c2741f0d3131fac9d4964091ba11f5f Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 02:17:17 +0300 Subject: [PATCH 44/49] fixing for ci --- .../Core/GameCenterTests.cs | 26 ++++++++++++------- .../Poker.BE.Domain.Tests/Core/UserTests.cs | 12 ++++++--- .../Poker.BE.Service/Services/RoomsService.cs | 6 +++-- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index 1b3b08e..a6cf8ee 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -39,7 +39,8 @@ public void FindRoomsByCriteriaTest_level() GameConfig config = new GameConfig(); int level = 4; - var expRoom = gameCenter.CreateNewRoom(level, config, out Player creator); + Player creator; + var expRoom = gameCenter.CreateNewRoom(level, config, out creator); Exception expE = null; expRoom.Name = "test room"; @@ -72,8 +73,9 @@ public void FindRoomsByCriteriaTest_multiple_rooms() //Arrange GameConfig config = new GameConfig(); int level = 4; - var expRoom = gameCenter.CreateNewRoom(level, config, out Player creator).Name = "test room 1"; - var expRoom2 = gameCenter.CreateNewRoom(level, config, out Player creator2).Name = "test room 2"; + Player creator, creator2; + var expRoom = gameCenter.CreateNewRoom(level, config, out creator).Name = "test room 1"; + var expRoom2 = gameCenter.CreateNewRoom(level, config, out creator2).Name = "test room 2"; //Act var actual = gameCenter.FindRoomsByCriteria(25); @@ -86,7 +88,8 @@ public void FindRoomsByCriteriaTest_multiple_rooms() public void FindRoomsByCriteriaTest_player() { //Arrange - var expRoom = gameCenter.CreateNewRoom(6, new GameConfig(), out Player expPlayer); + Player expPlayer; + var expRoom = gameCenter.CreateNewRoom(6, new GameConfig(), out expPlayer); //Act var actual = gameCenter.FindRoomsByCriteria(-1, expPlayer); @@ -181,7 +184,8 @@ public void CreateNewRoomTest() public void JoinNextHandTest() { //Arrange - var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + Player expPlayer; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out expPlayer); double inBuyin = expRoom.BuyInCost - 20.2; int inSeatIndex = 2; @@ -220,7 +224,8 @@ public void JoinNextHandTest2() { //Arrange int seatIndex = 200; - var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + Player expPlayer; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out expPlayer); double buyIn = expRoom.BuyInCost + 2.5; //Act @@ -234,7 +239,8 @@ public void JoinNextHandTest2() public void JoinNextHand_success() { //Arrange - var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player expPlayer); + Player expPlayer; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out expPlayer); expPlayer.Nickname = "yosi"; int seatIndex = 4; double buyIn = expRoom.BuyInCost; @@ -254,7 +260,8 @@ public void JoinNextHand_success() public void StandUpToSpactateTest() { //Arrange - var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player actPlayer); + Player actPlayer; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out actPlayer); actPlayer.Nickname = "yossi"; var expMoney = expRoom.BuyInCost; gameCenter.JoinNextHand(actPlayer, 2, expMoney); @@ -270,7 +277,8 @@ public void StandUpToSpactateTest() public void StandUpToSpactateTest_success() { //Arrange - var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out Player actPlayer); + Player actPlayer; + var expRoom = gameCenter.CreateNewRoom(1, new GameConfig(), out actPlayer); actPlayer.Nickname = "yossi"; var expMoney = expRoom.BuyInCost; gameCenter.JoinNextHand(actPlayer, 2, expMoney); diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs index 2e84856..6a68af1 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/UserTests.cs @@ -57,7 +57,8 @@ public void CreateNewRoomTest() //Arrange //Act - var actRoom = user.CreateNewRoom(1, new GameConfig(), out Player creator); + Player creator; + var actRoom = user.CreateNewRoom(1, new GameConfig(), out creator); //Assert Assert.IsTrue(user.Players.Contains(creator, new Utility.AddressComparer())); @@ -67,7 +68,8 @@ public void CreateNewRoomTest() public void EnterRoomTest() { //Arrange - var expRoom = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + Player creator; + var expRoom = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out creator); creator.Nickname = "test player"; //Act @@ -92,7 +94,8 @@ public void EnterRoomTest() public void JoinNextHand() { //Arrange - var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + Player creator; + var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out creator); //Act try @@ -114,7 +117,8 @@ public void JoinNextHand() public void StandUpToSpactateTest() { //Arrange - var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out Player creator); + Player creator; + var room = user.CreateNewRoom(1, new GameConfig() { Name = "test room" }, out creator); user.JoinNextHand(creator, 0, room.BuyInCost + 20.2); user.Players.Single().CurrentState = Player.State.ActiveFolded; diff --git a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs index 8b673f5..513e202 100644 --- a/Poker.BE/Poker.BE.Service/Services/RoomsService.cs +++ b/Poker.BE/Poker.BE.Service/Services/RoomsService.cs @@ -60,12 +60,14 @@ public EnterRoomResult EnterRoom(EnterRoomRequest request) try { - if (!Rooms.TryGetValue(request.Room, out Room room)) + Room room; + if (!Rooms.TryGetValue(request.Room, out room)) { throw new RoomNotFoundException(string.Format("Requested room ID {0} not found", request.Room)); } - if (!Users.TryGetValue(request.User, out User user)) + User user; + if (!Users.TryGetValue(request.User, out user)) { throw new UserNotFoundException(string.Format("User ID {0} not found", request.User)); } From be9f0212d051da17c0dad32257af9bd44e719259 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 02:19:36 +0300 Subject: [PATCH 45/49] fixing for ci --- Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index a6cf8ee..bae3756 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -167,7 +167,8 @@ public void CreateNewRoomTest() int inLevel = 3; //Act - var actual = gameCenter.CreateNewRoom(inLevel, inConfig, out Player actCreator); + Player actCreator; + var actual = gameCenter.CreateNewRoom(inLevel, inConfig, out actCreator); //Assert Assert.AreEqual(expPlayer, actCreator, "eq creators"); From 29c6e5d88d9e4c516d5e03bfd330b9a9c4423763 Mon Sep 17 00:00:00 2001 From: idanizi Date: Mon, 22 May 2017 03:11:04 +0300 Subject: [PATCH 46/49] fixing build bugs after merge. todo - paly move test not fail - fixme --- .../Core/GameCenterTests.cs | 4 +- .../Poker.BE.Domain.Tests/Game/PlayerTests.cs | 4 +- .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 4 +- .../Poker.BE.Domain.Tests/Game/TurnTests.cs | 6 +- Poker.BE/Poker.BE.Domain/Game/Hand.cs | 1 - Poker.BE/Poker.BE.Domain/Game/Player.cs | 17 ++--- Poker.BE/Poker.BE.Domain/Game/Round.cs | 72 ++++++++++++------- Poker.BE/Poker.BE.Domain/Game/Wallet.cs | 11 ++- 8 files changed, 72 insertions(+), 47 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index bae3756..f1f9e65 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -153,7 +153,7 @@ public void EnterRoomTest() //Assert Assert.AreEqual(expected, actual, "returned player"); Assert.IsNotNull(actual.Nickname, "nickname not null"); - Assert.AreEqual(0, actual.Wallet, "wallet = 0 (before buy in to table)"); + Assert.AreEqual(0, actual.WalletValue, "wallet = 0 (before buy in to table)"); } [TestMethod()] @@ -251,7 +251,7 @@ public void JoinNextHand_success() //Assert Assert.AreEqual(Player.State.ActiveUnfolded, expPlayer.CurrentState); - Assert.AreEqual(buyIn, expPlayer.Wallet); + Assert.AreEqual(buyIn, expPlayer.WalletValue); Assert.AreEqual(expPlayer, expRoom.TableLocationOfActivePlayers[expRoom.Chairs.ElementAt(seatIndex)]); Assert.AreEqual(1, expRoom.ActivePlayers.Count); } diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs index abc248c..767f43c 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs @@ -143,7 +143,7 @@ public void AddMoneyTest() player.AddMoney(100); //Assert - Assert.AreEqual(player.Wallet.amountOfMoney, 100); + Assert.AreEqual(player.Wallet.AmountOfMoney, 100); } [TestMethod()] @@ -168,7 +168,7 @@ public void SubstractMoneyTest() //Assert Assert.AreEqual(expectedException.Message, "Player doesn't have enough money!"); - Assert.AreEqual(player.Wallet.amountOfMoney, 50); + Assert.AreEqual(player.Wallet.AmountOfMoney, 50); } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 5b34daf..8fc3d44 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -281,7 +281,7 @@ public void StartNewHandTest() //Arrange Player player1 = new Player(); GamePreferences preferences = new GamePreferences(); - GameCenter center = new GameCenter(); + GameCenter center = GameCenter.Instance; Room room = new Room(player1, preferences); Exception expectedExcetpion = null; //Act @@ -318,7 +318,7 @@ public void ChoosePlayMoveTest() //Arrange Player player1 = new Player(); GamePreferences preferences = new GamePreferences(); - GameCenter center = new GameCenter(); + GameCenter center = GameCenter.Instance; Room room = new Room(player1, preferences); Exception expectedExcetpion = null; //Act diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/TurnTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/TurnTests.cs index f2649be..ae2a2e3 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/TurnTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/TurnTests.cs @@ -38,7 +38,7 @@ public void CallTest() player.AddMoney(200); turn.Call(50); - var res2 = player.Wallet.amountOfMoney != 150; + var res2 = player.Wallet.AmountOfMoney != 150; //Assert Assert.AreEqual(expectedException.Message, "Player doesn't have enough money!"); @@ -80,7 +80,7 @@ public void BetTest() player.AddMoney(200); turn.Bet(50); - var res2 = player.Wallet.amountOfMoney != 150; + var res2 = player.Wallet.AmountOfMoney != 150; //Assert Assert.AreEqual(expectedException.Message, "Player doesn't have enough money!"); @@ -107,7 +107,7 @@ public void RaiseTest() player.AddMoney(200); turn.Raise(50); - var res2 = player.Wallet.amountOfMoney != 150; + var res2 = player.Wallet.AmountOfMoney != 150; //Assert Assert.AreEqual(expectedException.Message, "Player doesn't have enough money!"); diff --git a/Poker.BE/Poker.BE.Domain/Game/Hand.cs b/Poker.BE/Poker.BE.Domain/Game/Hand.cs index 716ebd5..d2bd896 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Hand.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Hand.cs @@ -46,7 +46,6 @@ public Hand(Player dealer, Deck deck, ICollection players) } - public Round CurrentRound { get; internal set; } #endregion #region Methods diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index 65471fe..a16cfb5 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -26,7 +26,8 @@ public enum State #region Properties public State CurrentState { get; set; } - public double Wallet { get { return _wallet.Value; } private set { _wallet.Value = value; } } + public Wallet Wallet { get { return _wallet; } } + public double WalletValue { get { return _wallet.Value; } private set { _wallet.Value = value; } } public Card[] PrivateCards { get; set; } public string Nickname { get; set; } #endregion @@ -37,7 +38,7 @@ public Player() PrivateCards = new Card[NPRIVATE_CARDS]; CurrentState = State.Passive; _wallet = new Wallet(); - Wallet = 0.0; + WalletValue = 0.0; Nickname = ""; } @@ -53,7 +54,7 @@ public bool JoinToTable(double buyIn) } // buy in to wallet - Wallet = buyIn; + WalletValue = buyIn; CurrentState = State.ActiveUnfolded; return true; @@ -76,7 +77,7 @@ public double StandUp() } CurrentState = State.Passive; - return Wallet; + return WalletValue; } // override object.Equals @@ -100,7 +101,7 @@ public override bool Equals(object obj) && CurrentState == other.CurrentState && Nickname.Equals(other.Nickname) //&& this.PrivateCards.Equals(other.PrivateCards) //TODO override card.equals - && Wallet.Equals(other.Wallet) + && WalletValue.Equals(other.WalletValue) ; } @@ -113,14 +114,14 @@ public override int GetHashCode() public void AddMoney(int amount) { - this.Wallet.amountOfMoney += amount; + _wallet.AmountOfMoney += amount; } public void SubstractMoney(int amount) { - if (Wallet.amountOfMoney < amount) + if (_wallet.AmountOfMoney < amount) throw new NotEnoughMoneyException("Player doesn't have enough money!"); - this.Wallet.amountOfMoney -= amount; + _wallet.AmountOfMoney -= amount; } #endregion } diff --git a/Poker.BE/Poker.BE.Domain/Game/Round.cs b/Poker.BE/Poker.BE.Domain/Game/Round.cs index e12f65f..a8014bb 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Round.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Round.cs @@ -88,7 +88,7 @@ public void PlayMove(Move playMove, int amountToBetOrCall) partialPotIterator.PlayersClaimPot.Remove(playerToRemove); partialPotIterator = partialPotIterator.PartialPot; } - + //Remove player from round this.currentPlayer = this.ActiveUnfoldedPlayers.ElementAt((ActiveUnfoldedPlayers.ToList().IndexOf(this.CurrentPlayer) - 1) % ActiveUnfoldedPlayers.Count); ActiveUnfoldedPlayers.Remove(playerToRemove); @@ -100,13 +100,13 @@ public void PlayMove(Move playMove, int amountToBetOrCall) //Check preconditions if (LastRaise > 0 || TotalRaise > 0) throw new GameRulesException("Can't bet if someone had bet before... use raise move"); - if (TotalRaise + amountToBetOrCall == CurrentPlayer.Wallet.amountOfMoney + LiveBets[CurrentPlayer]) + if (TotalRaise + amountToBetOrCall == CurrentPlayer.Wallet.AmountOfMoney + LiveBets[CurrentPlayer]) throw new WrongIOException("Can't use bet move for all of your money... use all-in move"); RaisePreconditions(amountToBetOrCall); //Make bet Raise(amountToBetOrCall); - + break; } case Move.raise: @@ -114,7 +114,7 @@ public void PlayMove(Move playMove, int amountToBetOrCall) //Check preconditions if (LastRaise == 0) throw new GameRulesException("Can't raise if no one had bet before... use bet move"); - if (TotalRaise + amountToBetOrCall == CurrentPlayer.Wallet.amountOfMoney + LiveBets[CurrentPlayer]) + if (TotalRaise + amountToBetOrCall == CurrentPlayer.Wallet.AmountOfMoney + LiveBets[CurrentPlayer]) throw new WrongIOException("Can't use raise move for all of your money... use all-in move"); RaisePreconditions(amountToBetOrCall); @@ -129,24 +129,24 @@ public void PlayMove(Move playMove, int amountToBetOrCall) int highestOtherAllIn = 0; foreach (Player player in ActiveUnfoldedPlayers) //find highest all-in of the other players at the table { - if (player != this.CurrentPlayer && player.Wallet.amountOfMoney + LiveBets[player] > highestOtherAllIn) - highestOtherAllIn = player.Wallet.amountOfMoney; + if (player != this.CurrentPlayer && player.Wallet.AmountOfMoney + LiveBets[player] > highestOtherAllIn) + highestOtherAllIn = player.Wallet.AmountOfMoney; } - if (this.CurrentPlayer.Wallet.amountOfMoney > highestOtherAllIn) + if (this.CurrentPlayer.Wallet.AmountOfMoney > highestOtherAllIn) throw new GameRulesException("all-in is bigger than the highest other player's all-in... use bet\raise move"); - if (CurrentPlayer.Wallet.amountOfMoney == 0) + if (CurrentPlayer.Wallet.AmountOfMoney == 0) throw new WrongIOException("You're already all-in!!"); //Make all-in - if (this.CurrentPlayer.Wallet.amountOfMoney + LiveBets[currentPlayer] <= TotalRaise) - Call(this.CurrentPlayer.Wallet.amountOfMoney); + if (this.CurrentPlayer.Wallet.AmountOfMoney + LiveBets[currentPlayer] <= TotalRaise) + Call(this.CurrentPlayer.Wallet.AmountOfMoney); else { Call(0); if (LiveBets[currentPlayer] != totalRaise) throw new WrongIOException("Didn't call right!"); - RaisePreconditions(this.CurrentPlayer.Wallet.amountOfMoney + LiveBets[CurrentPlayer] - TotalRaise); - Raise(this.CurrentPlayer.Wallet.amountOfMoney + LiveBets[CurrentPlayer] - TotalRaise); + RaisePreconditions(this.CurrentPlayer.Wallet.AmountOfMoney + LiveBets[CurrentPlayer] - TotalRaise); + Raise(this.CurrentPlayer.Wallet.AmountOfMoney + LiveBets[CurrentPlayer] - TotalRaise); } CurrentTurn.AllIn(); @@ -198,7 +198,7 @@ private void Call(int amountToBetOrCall) amountToBetOrCall -= amountToAdd; lastPlayerCurrentBet = playerCurrentBet; playerCurrentBet = 0; - + } else { @@ -212,24 +212,34 @@ private void Call(int amountToBetOrCall) lastPartialPot = partialPotIterator; partialPotIterator = partialPotIterator.PartialPot; } - + if (amountToBetOrCall + playerCurrentBet > lastPartialPot.AmountToClaim) - throw new WrongIOException("Not enough partial pots were created! Somthing isn't right!"); - - if (CurrentPlayer.Wallet.amountOfMoney == 0) //if call all-in move + throw new WrongIOException("Not enough partial pots were created! Something isn't right!"); + + if (CurrentPlayer.Wallet.AmountOfMoney == 0) //if call all-in move { //Add new partial pot in the middle - Pot newPartialPot = new Pot(lastPartialPot.BasePot); - newPartialPot.PartialPot = lastPartialPot; + Pot newPartialPot = new Pot(lastPartialPot.BasePot) + { + PartialPot = lastPartialPot + }; lastPartialPot.BasePot = newPartialPot; + if (newPartialPot.BasePot != null) + { newPartialPot.BasePot.PartialPot = newPartialPot; + } else - this.currentPot = newPartialPot; - + { + currentPot = newPartialPot; + } + //Update partial pot's fields foreach (Player player in lastPartialPot.PlayersClaimPot) + { newPartialPot.PlayersClaimPot.Add(player); + } + lastPartialPot.PlayersClaimPot.Remove(CurrentPlayer); newPartialPot.AmountToClaim = amountToAdd + lastPlayerCurrentBet; @@ -237,22 +247,32 @@ private void Call(int amountToBetOrCall) newPartialPot.Value = lastPartialPot.Value - (lastPartialPot.AmountToClaim * lastPartialPot.PlayersClaimPot.Count); lastPartialPot.Value = lastPartialPot.AmountToClaim * lastPartialPot.PlayersClaimPot.Count; - + } } private void RaisePreconditions(int amountToRaise) { if (LastRaise > amountToRaise) + { throw new GameRulesException("Can't raise less than last raise"); + } + int highestAllIn = 0; - foreach (Player player in ActiveUnfoldedPlayers) //find highest all-in at the table + + //Note: find highest all-in at the table + foreach (Player player in ActiveUnfoldedPlayers) { - if (player.Wallet.amountOfMoney + LiveBets[player] > highestAllIn) - highestAllIn = player.Wallet.amountOfMoney + LiveBets[player]; + if (player.Wallet.AmountOfMoney + LiveBets[player] > highestAllIn) + { + highestAllIn = player.Wallet.AmountOfMoney + LiveBets[player]; + } } + if (TotalRaise + amountToRaise > highestAllIn) + { throw new GameRulesException("Raise is bigger than the highest player's all-in"); + } } private void Raise(int amountToRaise) @@ -272,7 +292,7 @@ private void Raise(int amountToRaise) lastPartialPot.PlayersClaimPot = new List(); lastPartialPot.PlayersClaimPot.Add(CurrentPlayer); - if (currentPlayer.Wallet.amountOfMoney == 0) + if (currentPlayer.Wallet.AmountOfMoney == 0) lastPartialPot.PartialPot = new Pot(lastPartialPot); } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Wallet.cs b/Poker.BE/Poker.BE.Domain/Game/Wallet.cs index 6800a7e..c5d89e1 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Wallet.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Wallet.cs @@ -9,13 +9,18 @@ namespace Poker.BE.Domain.Game public class Wallet : Utility.MoneyStorage { #region Properties - public int amountOfMoney { get; set; } + public int AmountOfMoney { get; set; } #endregion - #region Contructors + #region Constructors + public Wallet() : base() { } + public Wallet(Currency currency) : base(currency) { } + public Wallet(Currency currency, double amount) : base(currency, amount) { } + + // fixme - make wallet unified by ucc03/6 public Wallet(int amount) { - this.amountOfMoney = amount; + AmountOfMoney = amount; } #endregion From 218bb3ae8a1b913ade964bc0eec2fd766725c356 Mon Sep 17 00:00:00 2001 From: idanizi Date: Tue, 23 May 2017 02:36:58 +0300 Subject: [PATCH 47/49] all tests pass --- Poker.BE/Poker.BE.Domain.Tests/Game/RoundTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoundTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoundTests.cs index 8e7ae69..7ad099e 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoundTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoundTests.cs @@ -22,9 +22,9 @@ public void RoundTest() public void PlayMoveTest() { //Arrange - var player1 = new Player(); - var player2 = new Player(); - var player3 = new Player(); + var player1 = new Player() { Nickname = "test player 1" }; + var player2 = new Player() { Nickname = "test player 2" }; + var player3 = new Player() { Nickname = "test player 3" }; player1.AddMoney(500); player2.AddMoney(350); player3.AddMoney(600); @@ -50,7 +50,7 @@ public void PlayMoveTest() { round.PlayMove(Round.Move.raise, 20); } - catch(GameRulesException e) + catch (GameRulesException e) { expectedException1 = e; } @@ -67,7 +67,7 @@ public void PlayMoveTest() round.PlayMove(Round.Move.allin, 0); var res5 = round.CurrentPlayer == player3 && round.CurrentPot.PlayersClaimPot.Contains(player1) && round.CurrentPot.PlayersClaimPot.Contains(player2) && !round.CurrentPot.PartialPot.PlayersClaimPot.Contains(player2) && round.CurrentPot.PartialPot.PlayersClaimPot.Contains(player1) - && round.LiveBets[player2] == 350 && round.CurrentPot.AmountToClaim == 350 && round.CurrentPot.Value == 820 + && round.LiveBets[player2] == 350 && round.CurrentPot.AmountToClaim == 350 && round.CurrentPot.Value == 820 && round.CurrentPot.PartialPot.Value == 150 && round.CurrentPot.PartialPot.AmountToClaim == 150; try From 5193a2d93c3a8af4830197de539db95d850acd15 Mon Sep 17 00:00:00 2001 From: idanizi Date: Tue, 23 May 2017 17:03:16 +0300 Subject: [PATCH 48/49] fix for PR comments --- .../Poker.BE.Domain.Tests/Game/PlayerTests.cs | 17 +++++++++++++++++ .../Poker.BE.Domain.Tests/Game/RoomTests.cs | 8 ++++---- Poker.BE/Poker.BE.Domain/Core/Bank.cs | 1 + Poker.BE/Poker.BE.Domain/Game/Chair.cs | 14 +++++++------- Poker.BE/Poker.BE.Domain/Game/Player.cs | 7 +++++++ .../Poker.BE.Domain/Utility/AddressComparer.cs | 4 ++++ 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs index 767f43c..05aeee0 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/PlayerTests.cs @@ -170,5 +170,22 @@ public void SubstractMoneyTest() Assert.AreEqual(expectedException.Message, "Player doesn't have enough money!"); Assert.AreEqual(player.Wallet.AmountOfMoney, 50); } + + [TestMethod] + public void Equals() + { + //Arrange + var other = new Player(); + + //Act + var afalse = player.Equals(other); + player.Nickname = "test"; + other.Nickname = "test"; + var atrue = player.Equals(other); + + //Assert + Assert.IsTrue(atrue); + Assert.IsFalse(afalse); + } } } \ No newline at end of file diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index 8fc3d44..ceba393 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -246,12 +246,12 @@ public void TakeChairTest() //Assert Assert.AreEqual(false, actual1); Assert.AreEqual(0, actTable); - Assert.AreEqual(false, room.Chairs.ElementAt(2).IsBusy); + Assert.AreEqual(false, room.Chairs.ElementAt(2).IsTaken); Assert.AreEqual(true, actual2); Assert.AreEqual(1, room.TableLocationOfActivePlayers.Count); Assert.AreEqual(roomCreator, room.TableLocationOfActivePlayers[room.Chairs.ElementAt(9)]); - Assert.AreEqual(true, room.Chairs.ElementAt(9).IsBusy); + Assert.AreEqual(true, room.Chairs.ElementAt(9).IsTaken); } [TestMethod()] @@ -262,10 +262,10 @@ public void LeaveChairTest() //Act room.TakeChair(roomCreator, 3); var actual1 = room.TableLocationOfActivePlayers.Count; - var actChair = room.Chairs.ElementAt(3).IsBusy; + var actChair = room.Chairs.ElementAt(3).IsTaken; room.LeaveChair(roomCreator); var actual2 = room.TableLocationOfActivePlayers.Count; - var actChair2 = room.Chairs.ElementAt(3).IsBusy; + var actChair2 = room.Chairs.ElementAt(3).IsTaken; //Assert Assert.AreEqual(1, actual1); diff --git a/Poker.BE/Poker.BE.Domain/Core/Bank.cs b/Poker.BE/Poker.BE.Domain/Core/Bank.cs index 68d03bb..ee9a251 100644 --- a/Poker.BE/Poker.BE.Domain/Core/Bank.cs +++ b/Poker.BE/Poker.BE.Domain/Core/Bank.cs @@ -22,6 +22,7 @@ public Bank() public Bank(double sumToDeposit) { + // TODO: Ariel / Tomer - test sumToDeposit > 0 Deposit(sumToDeposit); } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Chair.cs b/Poker.BE/Poker.BE.Domain/Game/Chair.cs index cb476cc..1934f27 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Chair.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Chair.cs @@ -12,18 +12,18 @@ public class Chair #region Fields private int index; - private bool isBusy; + private bool isTaken; #endregion #region Properties - public bool IsBusy { get { return isBusy; } } + public bool IsTaken { get { return isTaken; } } #endregion #region Constructors public Chair(int index) { this.index = index; - lock (this) { isBusy = false; } + lock (this) { isTaken = false; } } #endregion @@ -31,18 +31,18 @@ public Chair(int index) #region Methods public bool Take() { - if (isBusy) return false; + if (isTaken) return false; lock (this) { - if (isBusy) return false; - isBusy = true; + if (isTaken) return false; + isTaken = true; } return true; } public void Release() { - lock (this) { isBusy = false; } + lock (this) { isTaken = false; } } #endregion diff --git a/Poker.BE/Poker.BE.Domain/Game/Player.cs b/Poker.BE/Poker.BE.Domain/Game/Player.cs index a16cfb5..fc50d62 100644 --- a/Poker.BE/Poker.BE.Domain/Game/Player.cs +++ b/Poker.BE/Poker.BE.Domain/Game/Player.cs @@ -96,6 +96,13 @@ public override bool Equals(object obj) } var other = obj as Player; + + // Note: when nickname == "" -> shallow compare. + if (Nickname.Equals("") && other.Nickname.Equals("")) + { + return this == other; + } + return other != null && CurrentState == other.CurrentState diff --git a/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs b/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs index 56053b2..66d69cf 100644 --- a/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs +++ b/Poker.BE/Poker.BE.Domain/Utility/AddressComparer.cs @@ -6,6 +6,10 @@ namespace Poker.BE.Domain.Utility { + /// + /// Comparator for shallow compare + /// + /// public class AddressComparer : IEqualityComparer { public bool Equals(T x, T y) From fe633eb602c43ce2f056c7be7b91092a03937a8a Mon Sep 17 00:00:00 2001 From: idanizi Date: Tue, 23 May 2017 17:35:52 +0300 Subject: [PATCH 49/49] fix for PR comments - all test pass --- Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs | 8 +++++--- Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs index f1f9e65..c8897b9 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Core/GameCenterTests.cs @@ -143,12 +143,13 @@ public void FindRoomsByCriteriaTest_betsize() public void EnterRoomTest() { //Arrange - var expected = new Player(); + var expected = new Player() { Nickname = "test player" }; var actual = default(Player); - Room room = new Room(new Player()); + Room room = new Room(new Player() { Nickname = "test player" }); //Act actual = gameCenter.EnterRoom(room); + actual.Nickname = "test player"; //Assert Assert.AreEqual(expected, actual, "returned player"); @@ -160,7 +161,7 @@ public void EnterRoomTest() public void CreateNewRoomTest() { //Arrange - var expPlayer = new Player(); + var expPlayer = new Player() { Nickname = "test player" }; var expRoom = new Room(expPlayer); GameConfig inConfig = new GameConfig(); @@ -169,6 +170,7 @@ public void CreateNewRoomTest() //Act Player actCreator; var actual = gameCenter.CreateNewRoom(inLevel, inConfig, out actCreator); + actCreator.Nickname = "test player"; //Assert Assert.AreEqual(expPlayer, actCreator, "eq creators"); diff --git a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs index ceba393..2c7d0df 100644 --- a/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs +++ b/Poker.BE/Poker.BE.Domain.Tests/Game/RoomTests.cs @@ -223,10 +223,11 @@ public void ClearAllTest() public void CreatePlayerTest() { //Arrange - var expected = new Player(); + var expected = new Player() { Nickname = "test player" }; //Act var actual = room.CreatePlayer(); + actual.Nickname = "test player"; //Assert Assert.AreEqual(expected, actual);