private void Disconnect() { if (networkPeer != null) { networkPeer.killPeer(); networkPeer = null; } connected = false; Switcher.Switch(new ConnectionLostPage()); }
private void BackButtonClick(object sender, RoutedEventArgs e) { networkPeer.killPeer(); networkPeer = null; MultiplayerMode.ShouldBreakOut = true; Switcher.Switch(new MainPage()); }
private async Task StartNetworkGame(string playerName, LidgrenKulamiPeer.KulamiPeer networkPeer) { shouldBreakOut = false; bool shouldContinue = true; bool terminate = false; Random rnd = new Random(); DateTime start = DateTime.Now; DateTime end; bool keepWaiting; if (networkPeer.listener.connection == null) { keepWaiting = true; } else if (networkPeer.listener.connection.Status == Lidgren.Network.NetConnectionStatus.None) { keepWaiting = true; } else { keepWaiting = (networkPeer.listener.connection.Status != Lidgren.Network.NetConnectionStatus.Connected); } while (keepWaiting) { if (shouldBreakOut) { terminate = true; break; } await Task.Delay(1000); Console.WriteLine("Waiting for connection"); if (networkPeer.listener.connection == null) { keepWaiting = true; } else if (networkPeer.listener.connection.Status == Lidgren.Network.NetConnectionStatus.None) { keepWaiting = true; } else if (networkPeer.listener.connection.Status == Lidgren.Network.NetConnectionStatus.Disconnected) { keepWaiting = true; networkPeer.killPeer(); networkPeer = null; Console.WriteLine("reconnecting..."); networkPeer = new LidgrenKulamiPeer.KulamiPeer(3070); networkPeer.Start(); } else { keepWaiting = (networkPeer.listener.connection.Status != Lidgren.Network.NetConnectionStatus.Connected); } end = DateTime.Now; if ((end - start).TotalSeconds > 20) { shouldContinue = false; networkPeer.killPeer(); networkPeer = null; break; } } if (terminate) { Console.WriteLine("No Longer Looking For Connections. Exiting to Main Menu"); } else if (!shouldContinue) { Switcher.Switch(new NoConnectionsFoundPage()); } else { bool connectionLost = false; networkPeer.sendMove(playerName); string opponentName = networkPeer.getMove(); while (opponentName == null) { await Task.Delay(1000); opponentName = networkPeer.getMove(); } Switcher.Switch(new OpponentNamePage(opponentName)); await Task.Delay(3000); int networkingBoardNum = 0; int myRandomBoardNum = rnd.Next(1, 8); networkPeer.sendMove(myRandomBoardNum.ToString()); string move = networkPeer.getMove(); while (move == null) { if (networkPeer.listener.connection.Status == Lidgren.Network.NetConnectionStatus.Connected) { await Task.Delay(1000); move = networkPeer.getMove(); } else { connectionLost = true; break; } } if (!connectionLost) { int opponentRandomBoardNum = Convert.ToInt32(move); networkingBoardNum = (myRandomBoardNum + opponentRandomBoardNum) / 2; while (myRandomBoardNum == opponentRandomBoardNum) { myRandomBoardNum = rnd.Next(1, 8); networkPeer.sendMove(myRandomBoardNum.ToString()); move = networkPeer.getMove(); while (move == null) { if (networkPeer.listener.connection.Status == Lidgren.Network.NetConnectionStatus.Connected) { await Task.Delay(1000); move = networkPeer.getMove(); } else { connectionLost = true; break; } } opponentRandomBoardNum = Convert.ToInt32(move); } if (!connectionLost) { bool meFirst; if (myRandomBoardNum > opponentRandomBoardNum) { meFirst = true; } else { meFirst = false; } Switcher.Switch(new LANGamePage(networkPeer, networkingBoardNum, meFirst, playerName, opponentName)); } else { Switcher.Switch(new ConnectionLostPage()); } } else { Switcher.Switch(new ConnectionLostPage()); } } }