Exemple #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            playerCounter = 0;
              ServerStarterModel serverStarter = new ServerStarterModel(serverPlayer);
              ServerListener listener = new ServerListener(serverPlayer, logicStarter.ServicesProvider, PORT);
              listener.PlayerConnected += new Action<object, ServiceNetClient, PlayerAccountData>(listener_PlayerConnected);
              listener.Start(false, serverDeck, GameStartMode.NewGame);

              Thread.Sleep(100);

              string errorMessage;
              connector1.Connect(IPAddress.Loopback, PORT, string.Empty, GameStartMode.NewGame, clientDeck1, out errorMessage, out opponentData1);
              connector2.Connect(IPAddress.Loopback, PORT, string.Empty, GameStartMode.NewGame, clientDeck2, out errorMessage, out opponentData2);
        }
        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();
              }
        }