public void RegisterController(ServerStartSavedGameController controller)
 {
     this.controller = controller;
 }
Example #2
0
        public void StartServerGame(GameStartMode startMode)
        {
            PlayerInfo thisPlayer = CreateLocalPlayerInfo();
              // ---------------------------------------------------------------
              // choose game parameters or pick a saved game and start to listen
              // ---------------------------------------------------------------
              ServerStartNewGameModel newGameData = null;
              ServerStartSavedGameModel savedGameData = null;
              ServerListener listener = new ServerListener(thisPlayer, servicesProvider, SettingsManager.Settings.ListenPort);
              try
              {
            bool proceed = false;
            switch(startMode)
            {
              case GameStartMode.NewGame:
            {
              var initView = viewFactory.CreateServerStartNewGameView();
              var initModel = new ServerStartNewGameModel();
              var initController = new ServerStartNewGameController(initModel, initView, listener, servicesProvider);
              initController.DeckRoomRequest += new Func<DeckItem>(initController_DeckRoomRequest);
              if(initView.ShowModal() == ViewResult.Ok)
              {
                proceed = true;
                initModel.Deck.MainCards.Shuffle();
                newGameData = initModel;
              }
            }
            break;
              case GameStartMode.SavedGame:
            {
              var initView = viewFactory.CreateServerStartSavedGameView();
              var initModel = new ServerStartSavedGameModel();
              var initController = new ServerStartSavedGameController(initModel, initView, listener, servicesProvider);
              if(initView.ShowModal() == ViewResult.Ok)
              {
                proceed = true;
                savedGameData = initModel;
              }
            }
            break;
            }
            if(proceed)
            {
              // ---------------------------------------------------------------------------------
              // open room, wait other players and chat (obtain clients channels and players list)
              // ---------------------------------------------------------------------------------
              var serverStarterModel = new ServerStarterModel(thisPlayer);
              var serverStarterView = viewFactory.CreateServerStarterView();

              proceed = false;
              using(NetCommandHandler<IGameStarterModel> commandHandler = new NetCommandHandler<IGameStarterModel>(serverStarterModel, thisPlayer.NickName))
              {
            var serverStarterController = new ServerStarterController(commandHandler, serverStarterModel, listener,
              serverStarterView, thisPlayer, servicesProvider, startMode, newGameData, savedGameData);
            listener.Console = serverStarterModel.Console;
            proceed = serverStarterView.ShowModal() == ViewResult.Ok;
              }
              if(proceed)
              {
            // ----------
            // start game
            // ----------
            listener.Console = null;
            listener.GameIsRunning = true;
            switch(startMode)
            {
              case GameStartMode.NewGame:
                {
                  var players = CreatePlayerAccounts(thisPlayer.NickName, serverStarterModel.Players.ToList());
                  for(int i = 0; i < listener.PlayerConnections.Count; i++)
                    players[i+1].Channel = listener.PlayerConnections[i].NetClient;
                  StartGame(players, null, GameType.Host);
                }
                break;
              case GameStartMode.SavedGame:
                {
                  var players = CreatePlayerAccounts(thisPlayer.NickName, serverStarterModel.SavedGame);
                  for(int i = 0; i < listener.PlayerConnections.Count; i++)
                    players[i+1].Channel = listener.PlayerConnections[i].NetClient;
                  StartGame(players, serverStarterModel.SavedGame, GameType.Host);
                }
                break;
            }
              }
            }
              }
              finally
              {
            if(listener.IsStarted)
              listener.Stop();
              }
        }