コード例 #1
0
        /// <summary>
        /// AddPlayer
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <AddPlayerResponse> AddPlayer(AddPlayerRequest request, ServerCallContext context)
        {
            // Log request.
            CreateLogEntry(request, gRPCMessageType.Request, nameof(AddPlayer));

            var currentPlayerId = Players.Count;

            // Add new player into Players collection.
            // Normally this would be a database call instead.
            // The database would then set the PlayerId.
            // Implementation would usually involve a Repository that uses a mapper to map the Player data type to database table.

            Players.Add(new Player
            {
                Id          = currentPlayerId + 1,
                FirstName   = request.Player.FirstName,
                LastName    = request.Player.LastName,
                DateOfBirth = request.Player.DateOfBirth
            });

            // Notify players subscribers that collection has been updated.
            PlayersUpdated?.Invoke(this, new EventArgs());

            var response = new AddPlayerResponse
            {
                Success = true
            };

            // Log response.
            CreateLogEntry(response, gRPCMessageType.Response, nameof(AddPlayer));

            // Return response.
            return(response);
        }
コード例 #2
0
        private async Task LoadPlayers()
        {
            var players = await _storageService.LoadPlayersAsync();

            if (players != null && players.Any())
            {
                _players = players;
            }
            else
            {
                _players.Add(new Player());
            }
            PlayersUpdated?.Invoke(this, null);
        }
コード例 #3
0
        private void AddPlayer(Player player)
        {
            var oldPlayer = _players.FirstOrDefault(p => p.Equals(player));

            if (oldPlayer != null)
            {
                _players.Remove(oldPlayer);
            }
            _players.Insert(0, player);
            if (_players.Count > 4)
            {
                _players.RemoveAt(_players.Count - 1);
            }
            _storageService.SavePlayersAsync(_players);
            PlayerToBeSetIndex = _players.IndexOf(player);
            PlayersUpdated?.Invoke(this, null);
        }
コード例 #4
0
        private void FinalCompute(List <FinalPoker> pokers)
        {
            this.IsFinished = true;
            this.FinalPokers.AddRange(PokerService.Instance.Summary(pokers));
            var result = this.FinalPokers.GroupBy(it => it.Result);

            var totalMoney = this.PoolMoney;

            foreach (var item in result)
            {
                if (totalMoney == 0)
                {
                    break;
                }
                var count = item.Count();
                foreach (var fp in item)
                {
                    var winMoney = GetChipMoney(totalMoney / count);
                    var player   = Players.FirstOrDefault(it => it.Name == fp.PlayerName);
                    player.IsWinner = true;
                    if (player.IsAllIn)
                    {
                        if (player.BetMoney <= winMoney)
                        {
                            totalMoney -= player.Money;
                            AllInPlayerWinMoney(player);
                        }
                        else
                        {
                            PlayerWinMoney(player, winMoney);
                            totalMoney -= winMoney;
                        }
                    }
                    else
                    {
                        PlayerWinMoney(player, winMoney);
                        totalMoney -= winMoney;
                    }
                }
            }

            PlayersUpdated?.Invoke(this.Players[_currentIndex]);
        }
コード例 #5
0
        private void GoToNextRound()
        {
            if (CurrentRound == Round.First)
            {
                OpenThreeCards();
            }
            else if (CurrentRound == Round.Second)
            {
                OpenFourthCards();
            }
            else if (CurrentRound == Round.Third)
            {
                OpenFifthCards();
            }
            else
            {
                List <FinalPoker> pokers = new List <FinalPoker>();
                var currentPlayers       = Players.Where(it => it.Status == GamePlayerStatus.Play);
                foreach (var player in currentPlayers)
                {
                    player.IsCheck = false;
                    pokers.Add(new FinalPoker(player.Poker1, player.Poker2, RiverPokers, player.Name));
                }

                FinalCompute(pokers);
                GameStatus = GameStatus.Finished;
                return;
            }

            CurrentRound = (Round)(CurrentRound + 1);

            foreach (var item in Players)
            {
                item.IsCheck = false;
            }

            _currentIndex = GetFirstIndex(_smallIndex);
            this.Players[_currentIndex].IsActive = true;
            PlayersUpdated?.Invoke(this.Players[_currentIndex]);
        }
コード例 #6
0
        /// <summary>
        /// Called when [get players].
        /// </summary>
        /// <param name="packet">The packet.</param>
        private void OnGetPlayers(Packet packet)
        {
            try
            {
                List <Player> players = new List <Player>();
                string        message = packet.DataAsString();
                if (message.Trim() == "No Players Connected")
                {
                    CurrentPlayerCountUpdated?.Invoke(this, new PlayerCountEventArgs()
                    {
                        PlayerCount = players.Count
                    });
                    PlayersUpdated?.Invoke(this, new PlayersEventArgs()
                    {
                        Players = players
                    });
                    ConsoleLogUpdated?.Invoke(this, new ConsoleLogEventArgs()
                    {
                        Message   = "Server Response: No Players Connected",
                        Timestamp = packet.Timestamp
                    });
                    return;
                }
                string   str        = packet.DataAsString();
                string[] lines      = str.Split('\n');
                string[] cleanLines = lines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                foreach (string line in cleanLines)
                {
                    string   lineData     = line.Replace("...", "");
                    string[] split1       = lineData.Split(new char[] { '.' }, 2);
                    string[] split2       = split1[1].Split(new char[] { ',' }, 2);
                    string   playerNumber = split1[0].Trim();
                    string   name         = split2[0].Trim();
                    string   steamId      = split2[1].Trim();

                    players.Add(new Player
                    {
                        PlayerNumber = int.Parse(playerNumber),
                        Name         = name,
                        SteamID      = ulong.Parse(steamId)
                    });
                }

                CurrentPlayerCountUpdated?.Invoke(this, new PlayerCountEventArgs()
                {
                    PlayerCount = players.Count
                });
                PlayersUpdated?.Invoke(this, new PlayersEventArgs()
                {
                    Players = players
                });
                ConsoleLogUpdated?.Invoke(this, new ConsoleLogEventArgs()
                {
                    Message   = "Server Response: Player List Updated",
                    Timestamp = packet.Timestamp
                });
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Exception in OnGetPlayers: {exception}", ex.Message);
            }
        }