void listener_PlayerConnected(object sender, ServiceNetClient client, PlayerAccountData connectedPlayer) { if(++playerCounter < 2) return; if(InvokeRequired) Invoke(new Action<object, ServiceNetClient, PlayerAccountData>(listener_PlayerConnected), sender, client, connectedPlayer); else { ServerListener listener = (ServerListener)sender; if(listener.PlayerConnections != null) { listener.Dispose(); clientStarter1.Start(null); clientStarter2.Start(null); PlayerInfo[] players = new PlayerInfo[] { serverPlayer, clientPlayer2, clientPlayer1 }; DeckItem[] decks = new DeckItem[] { serverDeck, clientDeck2, clientDeck1 }; Thread.Sleep(1000); StartGame(listener.PlayerConnections, players.ToList(), decks.ToList(), new string[] { string.Empty, string.Empty, string.Empty }, GameType.Host, false); } } }
void netMessageService_ClientConnected(object sender, ServiceNetClient client) { using(var connectionMessagesHandler = new ServerConnectionMessagesHandler(client)) { if(!WaitHandshake(connectionMessagesHandler)) { if(Console != null) Console.WriteLine(MessageCategory.Warning, "Opponent handshake not received"); } else { ConnectionResult result; string refuseReason; ValidatePlayer(connectionMessagesHandler, out result, out refuseReason); HandshakeResponse handshakeResponse = new HandshakeResponse() { Deck = new DeckDataContract(deck), PlayerInfo = Player, ConnectionResult = result, }; Thread.Sleep(100); client.SendMessage(handshakeResponse); if(result != ConnectionResult.Accepted) { if(Console != null) Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " refused: ", refuseReason)); } else { if(!WaitPlayerConfirm(connectionMessagesHandler)) { if(Console != null) Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " hasn't confirmed the connection.")); } else { client.Disconnected += new EventHandler(client_Disconnected); lock(syncObject) connections.Add(client, connectionMessagesHandler); PlayerAccountData connectedPlayer = new PlayerAccountData() { Info = connectionMessagesHandler.Handshake.PlayerInfo, Deck = new DeckItem(connectionMessagesHandler.Handshake.Deck), Password = connectionMessagesHandler.Handshake.Password }; OnPlayerConnected(client, connectedPlayer); } } } } }
public void StartSolitaire() { PlayerAccountData thisPlayer = new PlayerAccountData(); DeckRoom deckRoomLogic = new DeckRoom(this); thisPlayer.Deck = deckRoomLogic.Run(); if(thisPlayer.Deck != null) { List<PlayerAccountData> players = new List<PlayerAccountData>(); thisPlayer.Info = CreateLocalPlayerInfo(); thisPlayer.Deck.MainCards.Shuffle(); thisPlayer.Password = thisPlayer.Info.NickName; players.Add(thisPlayer); PlayerAccountData opponentPlayer = new PlayerAccountData(); opponentPlayer.Info = (PlayerInfo)thisPlayer.Info.Clone(); opponentPlayer.Info.NickName = "Opponent"; opponentPlayer.Deck = ServicesProvider.DecksService.LoadDeck(thisPlayer.Deck.Category, thisPlayer.Deck.Name); opponentPlayer.Deck.MainCards.Shuffle(); opponentPlayer.Password = opponentPlayer.Info.NickName; players.Add(opponentPlayer); #if DEBUG PlayerAccountData debugPlayer = new PlayerAccountData(); debugPlayer.Info = (PlayerInfo)thisPlayer.Info.Clone(); debugPlayer.Info.NickName = "Debug"; debugPlayer.Deck = ServicesProvider.DecksService.LoadDeck(thisPlayer.Deck.Category, thisPlayer.Deck.Name); debugPlayer.Deck.MainCards.Shuffle(); debugPlayer.Password = debugPlayer.Info.NickName; players.Add(debugPlayer); #endif var gameModel = new GameModel(gameItem); var gameView = viewFactory.CreateGameView(); using(GameCommandHandler gameCommandHandler = new GameCommandHandler(gameModel, thisPlayer.Info.NickName, servicesProvider)) { var gameController = new GameController(gameCommandHandler, gameModel, gameView, players, GameType.Solitaire, servicesProvider, true); gameController.ShowOptionsRequested += new EventHandler(gameController_ShowOptionsRequested); gameView.ShowModal(); } } }
protected virtual void OnPlayerConnected(ServiceNetClient client, PlayerAccountData connectedPlayer) { if(PlayerConnected != null) PlayerConnected(this, client, connectedPlayer); }