public void Hit(Player player) { var HitCallbackSubj = new ReplaySubject <Player>(); var HitCallBackObservable = HitCallbackSubj .SelectMany(sender => _players.Select(pl => new { Sender = sender, Reciever = pl })); HitCallBackObservable.Where(pl => pl.Sender.Name == pl.Reciever.Name) .Select(pl => pl.Reciever) .Subscribe <Player>(pl => pl.Callback.PlayerRecievedCards(pl, pl.Hand.Cards)); HitCallBackObservable.Where(pl => pl.Sender.Name != pl.Reciever.Name) .Select(pl => new { Reciever = pl.Reciever.Callback , Sender = pl.Sender , Cards = pl.Sender.Hand.toUnknown() }) .Subscribe(message => message.Reciever.PlayerRecievedCards(message.Sender, message.Cards)); var currentPlayer = _players .Where(pl => pl.Name == player.Name && pl.Bet > 0 && pl.Hand.GetSumOfHand() < 21).Last(); currentPlayer.Hand.Cards.Add(_deck.Draw()); if (currentPlayer.Hand.GetSumOfHand() > 21) { _players.ToObservable().Select(pl => pl.Callback) .Subscribe(ServAux.Broadcast <IBlackJackServiceCallback> (callback => callback.PlayerStatus(currentPlayer, "Busted!"))); } HitCallbackSubj.OnNext(currentPlayer); }
public void OnNext(Player value) { var isPlayerExists = _players.AsParallel() .Any(currPlayers => currPlayers.Name.Equals (value.Name, StringComparison.OrdinalIgnoreCase)); Observable.Return(value) .Synchronize() .TakeWhile(_ => !isPlayerExists) .Subscribe(pl => _players.Add(pl)); _players.ToObservable() .TakeWhile(_ => !isPlayerExists) .Where(pl => !pl.Name.Equals(value.Name, StringComparison.OrdinalIgnoreCase)) .Select(pl => pl.Callback) .Subscribe(ServAux.Broadcast <IBlackJackServiceCallback>(cl => cl.PlayerJoin(value))); }
private void FinalizeGame() { if (_dealer.Hand.GetSumOfHand() < 17) { while (_dealer.Hand.GetSumOfHand() < 17) { _dealer.Hand.Cards.Add(_deck.Draw()); } } var prize = _players.Select(player => player.Bet) .Sum(); var players = _players.ToObservable().StartWith(_dealer); var winner = players.Where(player => player.Hand.GetSumOfHand() < 22) .MaxBy(player => player.Hand.GetSumOfHand()) .SelectMany(winners => winners.ToObservable()); winner.Where(plr => plr.Name == _dealer.Name) .Subscribe(wner => { _players.ToObservable().Select(pl => pl.Callback) .Subscribe(ServAux.Broadcast <IBlackJackServiceCallback> (callback => callback.PlayerStatus(wner, "Wins!Casino takes the price\n\n\n"))); }); winner.Any(wnr => wnr.Name == _dealer.Name) .Where(result => !result) .SelectMany(result => winner) .Take(1) .Subscribe(wner => { _players.ToObservable().Select(pl => pl.Callback) .Subscribe(ServAux.Broadcast <IBlackJackServiceCallback> (callback => callback.PlayerStatus(wner, "Wins!\n\n\n"))); }); Console.WriteLine("Finilizing"); BlackJackService.FinalizeSession(ID); }