private void Connect() { AgainPanel.Visible = false; AgainButton.Enabled = false; Draw = false; Server = new Client(); Server.Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); SetStatus("Connecting to " + ConnectTo + "..."); Server.Connect(ConnectTo, 1991); Server.Received += new Client.ReceiveDelegate(Server_Received); Server.Connected += new Client.ConnectDelegate(delegate { try { Invoke((MethodInvoker)delegate { AgainPanel.Visible = false; AgainButton.Enabled = false; }); } catch { } }); Server.Closed += new Client.CloseDelegate(delegate(object o, Client.CloseEventArgs ea) { if (!GameOver) { SetStatus("Connection failed", ErrorColor); } try { Invoke((MethodInvoker)delegate { AgainPanel.Visible = true; AgainButton.Enabled = true; }); } catch { } }); }
void Client_Received(object sender, Client.TransmitEventArgs e) { if (e.Text.StartsWith("#D ")) { switch (e.Text.Substring(3)) { case "U": DirectionQueue.Add(Direction.Up); break; case "D": DirectionQueue.Add(Direction.Down); break; case "L": DirectionQueue.Add(Direction.Left); break; case "R": DirectionQueue.Add(Direction.Right); break; } if (DirectionQueue.Count > 2) { DirectionQueue = DirectionQueue.Skip(DirectionQueue.Count - 2).Take(2).ToList(); } } if (e.Text == "#Turbo on") { if (this.Turbo > 0) { this.TurboEnabled = true; } } if (e.Text == "#Turbo off") { this.TurboEnabled = false; } if (e.Text == "#MaxTurbo") { this.Turbo = 100; } }
protected void AbortGame(object sender, Client.CloseEventArgs e) { if (Aborting) { return; } Clock.Stop(); CountDown.Stop(); this.Aborting = true; try { foreach (SnakePlayer P in Players) { try { P.Disconnect(); } catch { P.Abort(); } } } catch { } finally { GameOver(this, new EventArgs()); MessageLogged(this, new LogEventArgs("Players disconnected.")); } }
void Server_Received(object sender, Client.TransmitEventArgs e) { if (e.Text.StartsWith("#Player ")) { Me = int.Parse(e.Text.Substring(8)); if (Me == 1) { SetStatus("Waiting for an opponent..."); } } if (e.Text.StartsWith("#Countdown ")) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { for (int i = 0; i < int.Parse(e.Text.Substring(11)); i++) { SetStatus("Get ready: " + (3 - i).ToString(), WarningColor); Thread.Sleep(1000); } })); } if (e.Text == "#Draw") { GameOver = true; SetStatus("It's a DRAW!", WarningColor); } if (e.Text.StartsWith("#Winner ")) { GameOver = true; if (int.Parse(e.Text.Substring(8)) == Me) { SetStatus("You're the WINNER!", SuccessColor); } else { SetStatus("You LOST!", ErrorColor); } } if (e.Text.StartsWith("#Status ")) { string[] pqq = e.Text.Substring(8).Split('\t'); Wall = FromRepresentation(pqq[0]); Food = FromRepresentation(pqq[1]); Turbo = FromRepresentation(pqq[2]); Delay = FromRepresentation(pqq[3]); SnakeOne = FromRepresentation(pqq[4]); SnakeTwo = FromRepresentation(pqq[5]); TurboEnabled = pqq[6] == "E"; TurboCounter = int.Parse(pqq[7]); ClearStatus(); Draw = true; RePaint(); } }
/// <summary> /// Called when the connection is closed or dropped. Updates the connection status. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">A Client.CloseEventArgs object that contains the reason the connection was closed.</param> protected void ControlSocket_Closed(object sender, Client.CloseEventArgs e) { this._Connected = false; }