public sealed override void HandleAccept(Controller controller) {
			IModel model = controller.Model;
			IGame game = model.CurrentGameBox.CurrentGame;
			if(newSelection.IsEmpty) {
				model.CurrentSelection = null;
			} else {
				IStack stack = game.GetStackById(newSelection.StackId);
				if(model.AnimationManager.IsBeingAnimated(stack))
					model.AnimationManager.EndAllAnimations();

				ISelection selection = stack.Select().RemoveAllPieces();
				foreach(int pieceId in newSelection.PieceIds) {
					IPiece piece = game.GetPieceById(pieceId);
					selection = selection.AddPiece(piece);
				}
				model.CurrentSelection = selection;
			}
		}
Exemple #2
0
        public sealed override void Handle(Controller controller)
        {
            IModel model = controller.Model;

            string gameBoxName;

            byte[] gameBoxHash;
            using (MemoryStream stream = new MemoryStream(gameData, false)) {
                using (InflaterInputStream compressionStream = new InflaterInputStream(stream)) {
                    model.RetrieveGameBoxInfoFromGameData(compressionStream, out gameBoxName, out gameBoxHash);
                }
            }

            IGameBoxReference gameBoxReference = model.GameLibrary.FindGameBox(gameBoxHash);

            if (gameBoxReference == null)
            {
                model.ClearTransientState();
                model.IsHosting = true;
                model.RemoveAllPlayers();
                controller.Model.NetworkClient.Disconnect();
                controller.View.Prompter.AddTextToHistory(0xFFFF0000, Resources.UnableToConnectGameBoxNotFound, gameBoxName);
                model.AudioManager.PlayAudioFile("Disconnect.wma");
                return;
            }
            else
            {
                model.ClearTransientState();
                model.RemoveAllPlayers();
                model.StateChangeSequenceNumber = stateChangeSequenceNumber;
                try {
                    model.OpenGameBox(gameBoxReference);
                    using (MemoryStream stream = new MemoryStream(gameData, false)) {
                        using (InflaterInputStream compressionStream = new InflaterInputStream(stream)) {
                            model.CurrentGameBox.OpenGame(compressionStream);
                        }
                    }

                    if (!selection.IsEmpty)
                    {
                        IGame      game         = model.CurrentGameBox.CurrentGame;
                        IStack     stack        = game.GetStackById(selection.StackId);
                        ISelection newSelection = stack.Select().RemoveAllPieces();
                        foreach (int pieceId in selection.PieceIds)
                        {
                            IPiece piece = game.GetPieceById(pieceId);
                            newSelection = newSelection.AddPiece(piece);
                        }
                        model.CurrentSelection = newSelection;
                    }

                    foreach (ConnectionAcceptedMessage.PlayerInfo player in currentPlayers)
                    {
                        model.AddPlayer(player.Id, player.FirstName, player.LastName, player.Guid, player.Color,
                                        Point.Truncate(controller.View.ConvertModelToScreenCoordinates(player.CursorPosition)),
                                        player.IsCursorVisible);
                        // is this player already connected with another instance?
                        if (player.Guid == model.ThisPlayer.Guid)
                        {
                            model.ThisPlayer.Guid = Guid.Empty;                                 // this is to prevent the sharing of a hand
                        }
                    }

                    DisplayProperties properties = controller.View.DisplayProperties;
                    if (widescreen != (properties.GameAspectRatio == AspectRatioType.SixteenToTen))
                    {
                        properties.GameAspectRatio        = (widescreen ? AspectRatioType.SixteenToTen : AspectRatioType.FourToThree);
                        controller.View.DisplayProperties = properties;
                    }

                    controller.View.ResetGraphicsElements();

                    controller.NetworkClient.Send(new PlayerHasJoinedMessage(
                                                      model.ThisPlayer.FirstName, model.ThisPlayer.LastName, model.ThisPlayer.Guid, controller.View.ConvertScreenToModelCoordinates(controller.MainForm.PointToClient(Cursor.Position)), model.ThisPlayer.IsCursorVisible));

                    if (System.Environment.OSVersion.Version.Major >= 6)                        // running Vista?
                    {
                        controller.DialogState.Dialog = new MessageDialog(SystemIcons.Warning, Resources.VistaWarningNoVoice);
                        controller.State = controller.DialogState;
                        controller.View.ShowDialog(controller.DialogState.Dialog);
                    }
                } catch (Exception e) {
                    model.OpenGameBox(model.GameLibrary.DefaultGameBox);
                    model.CurrentGameBox.OpenBuiltInScenario(model.CurrentGameBox.StartupScenarioFileName);

                    model.IsHosting = true;
                    model.RemoveAllPlayers();
                    controller.Model.NetworkClient.Disconnect();
                    controller.View.Prompter.AddTextToHistory(0xFFFF0000, Resources.UnableToConnectGameBoxNotFound, gameBoxName);
                    model.AudioManager.PlayAudioFile("Disconnect.wma");

                    string message = string.Format(Resources.CommandError, e.GetType().Name, e.Message);
                    controller.View.Prompter.AddTextToHistory(0xFFFF0000, message);
                    controller.DialogState.Dialog = new MessageDialog(SystemIcons.Error, message);
                    controller.State = controller.DialogState;
                    controller.View.ShowDialog(controller.DialogState.Dialog);

                    controller.View.ResetGraphicsElements();
                }
            }
        }