Пример #1
0
        void client_PacketReceived(object sender, PacketReceivedEventArgs data)
        {
            if (data.Protocol == ProtocolType.Tcp)
            {
                switch (data.Packet.PacketType)
                {
                case PacketType.Connect:
                    switch (data.Packet.Content)
                    {
                    case "FULL":
                        MessageBox.Show("The Server you were trying to join is full.", "Server full");
                        clientState = ClientState.Disconnected;
                        break;

                    case "UNAVAILABLE":
                        MessageBox.Show("The chosen Nickname is already in use.", "Nickname unavailable");
                        clientState = ClientState.Disconnected;
                        break;

                    default:
                        string[] players = data.Packet.Content.Split('|');

                        foreach (string player in players)
                        {
                            AddPlayer(player);
                        }

                        clientState = ClientState.Connected;

                        SetStatus("Connected");
                        break;
                    }
                    break;

                case PacketType.PowerUp:
                    dataGridPlayers.Invoke(new MethodInvoker(() =>
                    {
                        MessageBox.Show(data.Packet.Content);
                    }));
                    break;

                case PacketType.Race:
                    string[] packetPart = data.Packet.Content.Split('|');

                    gameClient.SetupRace(Int32.Parse(packetPart[0]), Int32.Parse(packetPart[1]));
                    break;

                case PacketType.Join:
                    AddPlayer(data.Packet.Content);
                    break;

                case PacketType.Disconnect:
                    if (data.Packet.Content == participant.Nickname)
                    {
                        client.Disconnect();
                        participants.Clear();
                        UpdateList();
                        clientState = ClientState.Disconnected;
                        SetStatus("Not Connected");
                        MessageBox.Show("You were disconnected by the server", "Disconnected");
                    }
                    else
                    {
                        RemovePlayer(data.Packet.Content);
                    }
                    break;
                }
            }
            else if (data.Protocol == ProtocolType.Udp)
            {
                string[] responseParts = data.Packet.Content.Split('|');

                Library.Settings.Unserialize(responseParts[0]);

                /*bool updateList = false;
                 *
                 * if (responseParts.Count() - 1 != playerCount)
                 * {
                 *  // New player, add it to the list
                 *
                 *  updateList = true;
                 *
                 *  playerCount = responseParts.Count() - 1;
                 *
                 *  foreach (string responsePlayer in responseParts.Skip(1))
                 *  {
                 *      string[] playerPart = responsePlayer.Split(';');
                 *
                 *      var existingPlayer = from p in participants
                 *                           where p.Nickname == playerPart[0]
                 *                           select p;
                 *
                 *      if (existingPlayer == null || existingPlayer.Count() == 0)
                 *      {
                 *          participants.Add(new Participant()
                 *          {
                 *              Nickname = playerPart[0],
                 *              X = float.Parse(playerPart[1]),
                 *              Y = float.Parse(playerPart[2]),
                 *              Z = float.Parse(playerPart[3]),
                 *              SpeedX = float.Parse(playerPart[4]),
                 *              SpeedY = float.Parse(playerPart[5]),
                 *              SpeedZ = float.Parse(playerPart[6]),
                 *              VectorX1 = float.Parse(playerPart[7]),
                 *              VectorY1 = float.Parse(playerPart[8]),
                 *              VectorZ1 = float.Parse(playerPart[9]),
                 *              VectorX2 = float.Parse(playerPart[10]),
                 *              VectorY2 = float.Parse(playerPart[11]),
                 *              VectorZ2 = float.Parse(playerPart[12]),
                 *              PowerUpType = int.Parse(playerPart[13]),
                 *              PowerUpWhiteBricks = int.Parse(playerPart[14])
                 *          });
                 *      }
                 *      else
                 *      {
                 *          participant.X = float.Parse(playerPart[1]);
                 *          participant.Y = float.Parse(playerPart[2]);
                 *          participant.Z = float.Parse(playerPart[3]);
                 *          participant.SpeedX = float.Parse(playerPart[4]);
                 *          participant.SpeedY = float.Parse(playerPart[5]);
                 *          participant.SpeedZ = float.Parse(playerPart[6]);
                 *          participant.VectorX1 = float.Parse(playerPart[7]);
                 *          participant.VectorY1 = float.Parse(playerPart[8]);
                 *          participant.VectorZ1 = float.Parse(playerPart[9]);
                 *          participant.VectorX2 = float.Parse(playerPart[10]);
                 *          participant.VectorY2 = float.Parse(playerPart[11]);
                 *          participant.VectorZ2 = float.Parse(playerPart[12]);
                 *          participant.PowerUpType = int.Parse(playerPart[13]);
                 *          participant.PowerUpWhiteBricks = int.Parse(playerPart[14]);
                 *      }
                 *  }
                 * }
                 * else
                 * {*/
                foreach (string responsePlayer in responseParts.Skip(1))
                {
                    string[] playerPart = responsePlayer.Split(';');

                    var foundPlayers = from p in participants
                                       where p.Nickname == playerPart[0]
                                       select p;

                    if (foundPlayers != null && foundPlayers.Count() == 1)
                    {
                        Participant foundPlayer = foundPlayers.First();

                        foundPlayer.X                  = float.Parse(playerPart[1]);
                        foundPlayer.Y                  = float.Parse(playerPart[2]);
                        foundPlayer.Z                  = float.Parse(playerPart[3]);
                        foundPlayer.SpeedX             = float.Parse(playerPart[4]);
                        foundPlayer.SpeedY             = float.Parse(playerPart[5]);
                        foundPlayer.SpeedZ             = float.Parse(playerPart[6]);
                        foundPlayer.VectorX1           = float.Parse(playerPart[7]);
                        foundPlayer.VectorY1           = float.Parse(playerPart[8]);
                        foundPlayer.VectorZ1           = float.Parse(playerPart[9]);
                        foundPlayer.VectorX2           = float.Parse(playerPart[10]);
                        foundPlayer.VectorY2           = float.Parse(playerPart[11]);
                        foundPlayer.VectorZ2           = float.Parse(playerPart[12]);
                        foundPlayer.PowerUpType        = int.Parse(playerPart[13]);
                        foundPlayer.PowerUpWhiteBricks = int.Parse(playerPart[14]);
                    }
                }
                //}

                /*if (updateList)
                 * {
                 *  dataGridPlayers.Invoke(new MethodInvoker(() =>
                 *  {
                 *      dataGridPlayers.DataSource = participants.ToList();
                 *
                 *      dataGridPlayers.Columns[1].Visible = false;
                 *      dataGridPlayers.Columns[2].Visible = false;
                 *  }));
                 * }*/

                foreach (Participant enemy in participants.Where(p => p.Nickname != participant.Nickname))
                {
                    /*if (powerUpsUsed[i] != enemy.PowerUpsUsed)
                     * {
                     *  powerUpsUsed[i] = enemy.PowerUpsUsed;
                     *
                     *  //gameClient.UsePowerUp(enemy.PowerUpType, enemy.PowerUpWhiteBricks, i + 1);
                     * }*/

                    gameClient.drivers[1].X         = enemy.X;
                    gameClient.drivers[1].Y         = enemy.Y;
                    gameClient.drivers[1].Z         = enemy.Z;
                    gameClient.drivers[1].SpeedX    = enemy.SpeedX;
                    gameClient.drivers[1].SpeedY    = enemy.SpeedY;
                    gameClient.drivers[1].SpeedZ    = enemy.SpeedZ;
                    gameClient.drivers[1].Forward_X = enemy.VectorX1;
                    gameClient.drivers[1].Forward_Y = enemy.VectorY1;
                    gameClient.drivers[1].Forward_Z = enemy.VectorZ1;
                    gameClient.drivers[1].Up_X      = enemy.VectorX2;
                    gameClient.drivers[1].Up_Y      = enemy.VectorY2;
                    gameClient.drivers[1].Up_Z      = enemy.VectorZ2;
                    //if (GetActiveWindowTitle() == "LEGO Racers")
                    //{
                    //    g.DrawString("Knoest:\nX: " + enemy.X + "\nY: " + enemy.Y + "\nZ: " + enemy.Z, font, brush, new PointF(20, 20));
                    //}

                    break;
                }
            }
        }