private void WaitForSpectators() { Thread wait = new Thread(() => { while (true) { Client spectator = HandlePlayer(); spectator.lineIndex = this.Players.Lines.Length - 1; AddText(Players, spectator.name + " - Spectator"); spectator.SendInt(1); this.spectators.Add(spectator); } }); wait.Start(); }
private Client playGame(Client[] players) { SetLocations(); Client firstPlayer = players[0]; Client secondPlayer = players[1]; Thread.Sleep(5000); firstPlayer.SendName(secondPlayer.name); secondPlayer.SendName(firstPlayer.name); Thread.Sleep(2000); for (int i = 3; i > 0; i--) { firstPlayer.SendInt(i); secondPlayer.SendInt(i); Thread.Sleep(1000); } Thread server = new Thread(() => { Server(); }); Thread firstPlayerLitsener = new Thread(() => { LitsenToFirstPlayer(firstPlayer); }); Thread secondPlayerLitsener = new Thread(() => { LitsenToSecondPlayer(secondPlayer); }); Thread validateConnection = new Thread(() => { ValidateConnection(firstPlayer, secondPlayer); }); server.Start(); firstPlayerLitsener.Start(); secondPlayerLitsener.Start(); validateConnection.Start(); while (true) { if (this.winner != 0) { server.Abort(); firstPlayerLitsener.Abort(); secondPlayerLitsener.Abort(); validateConnection.Abort(); if (winner == 1) { if (!secondPlayer.IsConnected()) { firstPlayer.SendServer(true, WhoWins.you, this.ballLeft, this.ballTop, this.secondPlayerRacketTop); } else { firstPlayer.SendServer(false, WhoWins.you, this.ballLeft, this.ballTop, this.secondPlayerRacketTop); secondPlayer.SendServer(false, WhoWins.enemy, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop); } return(firstPlayer); } else if (winner == 2) { if (!firstPlayer.IsConnected()) { secondPlayer.SendServer(true, WhoWins.you, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop); } else { firstPlayer.SendServer(false, WhoWins.enemy, this.ballLeft, this.ballTop, this.secondPlayerRacketTop); secondPlayer.SendServer(false, WhoWins.you, this.playgroundRight - this.ballLeft - this.ballWidth, this.ballTop, this.firstPlayerRacketTop); } return(secondPlayer); } } } }