Exemplo n.º 1
0
 public ConnectToPrivateGame(ConnectPrivateGameViewModel context)
 {
     InitializeComponent();
     DataContext = context;
     if (context.CloseAction == null)
     {
         context.CloseAction = new Action(this.Close);
     }
 }
        public void Notice(IEvent triggeredEvent)
        {
            var uiEvent = triggeredEvent as UiEvent;

            if (uiEvent != null)
            {
                HandleUiEvent(uiEvent);
            }

            var gameEnd = triggeredEvent as EndGameEvent;

            if (gameEnd != null)
            {
                Console.WriteLine("**************** Received EndGameEvent ****************");

                EndGame(gameEnd.Info);
                ExitGame();

                EnterSubstate(OnlineSubstate.OnlineBoard);
            }

            // After the user create a game in the host menu
            var createGame = triggeredEvent as CreateGameRequestEvent;

            if (createGame != null)
            {
                var config = createGame.Config as OnlineGameConfig;
                Debug.Assert(config != null, "Received a CreateGameRequestEvent with no valid OnlineGameConfig");

                var model = config.GetGameModel();
                // Create game on server
                var created = GameAccess.Instance.CreateGame(model);
                JoinWaitingRoom(created.HashId, true, model.Password);
            }

            var spectateGame = triggeredEvent as SpectateGameEvent;

            if (spectateGame != null)
            {
                GameAccess.Instance.Spectate(spectateGame.HashId);
                // Todo: Add on Spectator list on server
                JoinSession(spectateGame.HashId);
            }

            // When the player want to join a hosted game
            var joinGame = triggeredEvent as JoinOnlineGameRequestEvent;

            if (joinGame != null)
            {
                if (JoinGameHelper.IsReconnection(joinGame.HashId))
                {
                    ReconnectToGame(joinGame.HashId);
                }
                else if (JoinGameHelper.CanJoin(joinGame.HashId, true))
                {
                    try
                    {
                        // Show popup asking the password
                        if (joinGame.IsPrivate && joinGame.Password == null)
                        {
                            var context = new ConnectPrivateGameViewModel(joinGame.HashId, joinGame.Name);
                            var popup   = new ConnectToPrivateGame(context);
                            popup.ShowDialog();
                        }
                        else
                        {
                            GameAccess.Instance.JoinGame(joinGame.HashId, joinGame.Password);
                            JoinWaitingRoom(joinGame.HashId);
                        }
                    }
                    catch (Exception e)
                    {
                        // Cannot join game
                        if (joinGame.IsPrivate)
                        {
                            MessageHelper.ShowError("Vous ne pouvez pas rejoindre ce bal", "Avez vous donné le bon mots de passes?", e);
                        }
                        else
                        {
                            MessageHelper.ShowError("Vous ne pouvez pas rejoindre ce bal", "Et je ne suis pas sur de comprendre pourquoi (s'il vous plait regarder votre journal de princesse)", e);
                        }
                    }
                }
            }

            var matchMaking = triggeredEvent as HostJoiningCreatedGameEvent;

            if (matchMaking != null)
            {
                JoinSession(matchMaking.HashId);
            }


            // Handle the event only if it came from the server
            var onlineSession = triggeredEvent as AbstractSessionEvent;

            if (onlineSession != null && !onlineSession.SendToServer)
            {
                HandleOnlineSessionEvent(onlineSession);
            }

            var reconnectToGame = triggeredEvent as ReconnectToGameEvent;

            if (reconnectToGame != null)
            {
                ReconnectToGame(reconnectToGame.Game.HashId);
            }
        }