Example #1
0
        /// <summary>
        /// Contains methods that are called when game action messages are received.
        /// </summary>
        /// <summary>
        /// This method is invoked whenever someone (either local or from the server) performs a
        /// peel action.
        /// </summary>
        /// <param name="actionSender">The person who performed the dump action</param>
        /// <param name="IsSenderLocal">If the person performing the peel action is local (not from server) or not</param>
        public async void PeelActionReceived(string actionSender, bool IsSenderLocal = true)
        {
            var serverProxy = ServerProxy.GetInstance();
            var roomManager = RoomManager.GetInstance();
            var viewModel   = GameBoardViewModel.GetInstance();


            //
            // If there are enough tiles left, then do a peel.  Otherwise, if there are not enough
            // tiles, then actionSender has won the game.
            //

            if (_tileManager.GetPileCount() >= (roomManager.RoomMembers.Count + _localPlayers.Count - 1))
            {
                var tiles = _tileManager.PerformPeelAction(roomManager.RoomMembers.Count + _localPlayers.Count - 1);

                if (null == tiles)
                {
                    return;
                }


                //
                // Update the hands of all of the players with one of the returned tiles from the peel.
                // If it's the human player's hand, update the UI too with the returned tile.
                //

                foreach (IPlayer player in _localPlayers)
                {
                    if (player.Alias == _humanPlayer.Alias)
                    {
                        OnPeelOccurred(tiles[0].TileContents, actionSender);
                    }

                    // ToDo: This line can possibly throw an out of bounds exception.
                    player.PeelActionReceived(tiles[0]);
                    tiles.RemoveAt(0);
                }


                //
                // Update UI.
                //

                await App.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    //
                    // Subtract total online players + bots.  Subtract one b/c the human player is
                    // double counted.
                    //

                    viewModel.TilePileCount -=
                        (roomManager.RoomMembers.Count + _localPlayers.Count - 1);
                });


                //
                // If the person performing the peel is local, then send a message to the server informing
                // of the peel.  Otherwise, if it came from the server, then don't send a message
                // to the server.
                //

                if ((serverProxy.messageSender != null) && IsSenderLocal)
                {
                    // ToDo: This will send the incorrect message to the server if a bot has peeled and not the client.
                    await serverProxy.messageSender.SendMessage(PacketType.c_Peel);
                }


                //
                // Log the Peel event
                //

                _gameDataLogger.LogMove(actionSender, viewModel.GameTime, Storage.MoveType.Peel);
            }
            else
            {
                if ((serverProxy.messageSender != null) && IsSenderLocal)
                {
                    await serverProxy.messageSender.SendMessage(PacketType.c_Victory);
                }

                EndGame(actionSender);
            }
        }