public void StartGame() { if (Players.Count < 2 || Players.Count > 10) { throw new Exception("Invalid team size"); } ResetGamePlay(); StackCard.GenerateDeck(); DistributeCardToPlayers(); History.AddCard(StackCard.PopRandomCard()); Status = GameStatus.Running; CurrentPlayer = Players.First(); try { var e = new Event(EventType.StartGame, CurrentPlayer, this); var serObj = SerializeHandler.SerializeObj(e); foreach (var player in Players) { player.Context.WriteAndFlushAsync(serObj + "\r\n"); } } catch (Exception exception) { Console.WriteLine(exception); throw; } }
public void NotifyYourTurnToCurrentPlayer() { /* Notify current player that is now his turn */ var e = new Event(EventType.YourTurn, CurrentPlayer, this); var serObj = SerializeHandler.SerializeObj(e); CurrentPlayer.Context.WriteAndFlushAsync(serObj + "\r\n"); }
public bool SendError(string msg, Table table = null) { var e = new Event(EventType.Error, this, table) { ErrorMsg = msg }; var serObj = SerializeHandler.SerializeObj(e); Context.WriteAndFlushAsync(serObj + "\r\n"); return(true); }
public void SendObjectToOtherPlayers <T>(T obj, Player currentPlayer) { var serObj = SerializeHandler.SerializeObj(obj); foreach (var player in Players) { if (currentPlayer != player) { player.Context.WriteAndFlushAsync(serObj + "\r\n"); } } }
public void NotifyHasPlayerHasPlayed(Player player) { var e = new Event(EventType.PlayerHasPlayed, player, this); var serObj = SerializeHandler.SerializeObj(e); // Cannot use SendObjectToOtherPlayers() because use CurrentPlayer // but it's changed foreach (var uplayer in Players) { if (uplayer != player) { uplayer.Context.WriteAndFlushAsync(serObj); } } }
public void SetGameEnd(Player winner) { if (winner != null && !winner.HasUno) { var card = StackCard.PopRandomCard(); winner.Hand.AddCard(card); card = StackCard.PopRandomCard(); winner.Hand.AddCard(card); NotifyTurnToAllPlayers(); return; } Winner = winner; foreach (var player in Players) { try { var eventEnd = new Event(EventType.EndGame, winner, this); var serObj = SerializeHandler.SerializeObj(eventEnd); player.Context.WriteAndFlushAsync(serObj + "\r\n"); } catch (Exception e) { Console.WriteLine(e); throw; } } if (winner != null) { Console.WriteLine("[OK] End of the game the winner is : {0}!\n$> ", winner.Id); } else { Console.WriteLine("[OK] End of the game, there is no winner\n$> "); } Status = GameStatus.End; }