Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.16 KB | None | 0 0
  1. #region
  2.  
  3. using db;
  4. using System;
  5. using System.Linq;
  6. using System.Text;
  7. using wServer.networking.cliPackets;
  8. using wServer.networking.svrPackets;
  9. using wServer.realm;
  10. using wServer.realm.worlds;
  11. using FailurePacket = wServer.networking.svrPackets.FailurePacket;
  12.  
  13. #endregion
  14.  
  15. namespace wServer.networking.handlers
  16. {
  17.     internal class HelloHandler : PacketHandlerBase<HelloPacket>
  18.     {
  19.         public override PacketID ID
  20.         {
  21.             get { return PacketID.HELLO; }
  22.         }
  23.  
  24.         protected override void HandlePacket(Client client, HelloPacket packet)
  25.         {
  26.             string[] ip = client.Socket.RemoteEndPoint.ToString().Split(':');
  27.             if (Client.SERVER_VERSION != packet.BuildVersion)
  28.             {
  29.                 client.SendPacket(new FailurePacket
  30.                 {
  31.                     ErrorId = 0,
  32.                     ErrorDescription = "server.update_client"
  33.                 });
  34.                 client.SendPacket(new FailurePacket
  35.                 {
  36.                     ErrorId = 4,
  37.                     ErrorDescription = Client.SERVER_VERSION
  38.                 });
  39.                 client.Disconnect();
  40.                 return;
  41.             }
  42.             client.Manager.Database.DoActionAsync(db =>
  43.             {
  44.                 if ((client.Account = db.Verify(packet.GUID, packet.Password, Manager.GameData)) == null)
  45.                 {
  46.                     log.Info(@"Account not verified.");
  47.                     client.Account = Database.CreateGuestAccount(packet.GUID);
  48.  
  49.                     if (client.Account == null)
  50.                     {
  51.                         log.Info(@"Account is null!");
  52.                         client.SendPacket(new FailurePacket
  53.                         {
  54.                             ErrorDescription = "Invalid account."
  55.                         });
  56.                         client.Disconnect();
  57.                         return;
  58.                     }
  59.                 }
  60.                 if (client.Account.NameChosen == false)
  61.                 {
  62.                     client.SendPacket(new FailurePacket
  63.                     {
  64.                         ErrorId = 0,
  65.                         ErrorDescription = "You must choose a name!"
  66.                     });
  67.                     client.Disconnect();
  68.                     return;
  69.                 }
  70.                 if (!client.Account.IsGuestAccount)
  71.                 {
  72.                     int? timeout = null;
  73.  
  74.                     if (DateTime.Now <= Program.WhiteListTurnOff)
  75.                     {
  76.                         if (!IsWhiteListed(client.Account.Rank))
  77.                         {
  78.                             client.SendPacket(new FailurePacket
  79.                             {
  80.                                 ErrorId = 0,
  81.                                 ErrorDescription = "You are not whitelisted!"
  82.                             });
  83.                             client.Disconnect();
  84.                             return;
  85.                         }
  86.                     }
  87.  
  88.                     if (db.CheckIP(ip[0]))
  89.                     {
  90.                         client.SendPacket(new FailurePacket
  91.                         {
  92.                             ErrorId = 0,
  93.                             ErrorDescription = "You are IP-Banned!"
  94.                         });
  95.                         client.Disconnect();
  96.                         return;
  97.                     }
  98.                     if (db.CheckAccountInUse(client.Account, ref timeout))
  99.                     {
  100.                         if (timeout == null)
  101.                         {
  102.                             client.SendPacket(new FailurePacket
  103.                             {
  104.                                 ErrorId = 0,
  105.                                 ErrorDescription = "Account in use."
  106.                             });
  107.                         }
  108.                         else
  109.                         {
  110.                             client.SendPacket(new FailurePacket
  111.                             {
  112.                                 ErrorId = 0,
  113.                                 ErrorDescription = "Account in use. (" + timeout + " seconds until timeout.)"
  114.                             });
  115.                         }
  116.                         client.Disconnect();
  117.                         return;
  118.                     }
  119.                 }
  120.                 log.Info(@"Client trying to connect!");
  121.                 client.ConnectedBuild = packet.BuildVersion;
  122.                 if (!client.Manager.TryConnect(client))
  123.                 {
  124.                     client.Account = null;
  125.                     client.SendPacket(new FailurePacket
  126.                     {
  127.                         ErrorDescription = "Failed to connect."
  128.                     });
  129.                     client.Disconnect();
  130.                     log.Warn(@"Failed to connect.");
  131.                 }
  132.                 else
  133.                 {
  134.                     log.Info(@"Client loading world");
  135.                     if (packet.GameId == World.NEXUS_LIMBO)
  136.                         packet.GameId = World.NEXUS_ID;
  137.                     World world = client.Manager.GetWorld(packet.GameId);
  138.                     if (world == null && packet.GameId == World.TUT_ID)
  139.                         world = client.Manager.AddWorld(new Tutorial(false));
  140.                     if (world == null)
  141.                     {
  142.                         client.SendPacket(new FailurePacket
  143.                         {
  144.                             ErrorId = 1,
  145.                             ErrorDescription = "Invalid world."
  146.                         });
  147.                         client.Disconnect();
  148.                         return;
  149.                     }
  150.                     if (world.NeedsPortalKey)
  151.                     {
  152.                         if (!world.PortalKey.SequenceEqual(packet.Key))
  153.                         {
  154.                             client.SendPacket(new FailurePacket
  155.                             {
  156.                                 ErrorId = 1,
  157.                                 ErrorDescription = "Invalid Portal Key"
  158.                             });
  159.                             client.Disconnect();
  160.                             return;
  161.                         }
  162.                         if (world.PortalKeyExpired)
  163.                         {
  164.                             client.SendPacket(new FailurePacket
  165.                             {
  166.                                 ErrorId = 1,
  167.                                 ErrorDescription = "Portal key expired."
  168.                             });
  169.                             client.Disconnect();
  170.                             return;
  171.                         }
  172.                     }
  173.                     log.Info(@"Client joined world " + world.Id);
  174.                     if (packet.MapInfo.Length > 0) //Test World
  175.                         (world as Test).LoadJson(Encoding.Default.GetString(packet.MapInfo));
  176.  
  177.                     if (world.IsLimbo)
  178.                     world = world.GetInstance(client);
  179.                     db.StoreIP(ip[0]);
  180.                     client.Random = new wRandom(world.Seed);
  181.                     client.TargetWorld = world.Id;
  182.                     client.SendPacket(new MapInfoPacket
  183.                     {
  184.                         Width = world.Map.Width,
  185.                         Height = world.Map.Height,
  186.                         Name = world.Name,
  187.                         Seed = world.Seed,
  188.                         ClientWorldName = world.ClientWorldName,
  189.                         Difficulty = world.Difficulty,
  190.                         Background = world.Background,
  191.                         AllowTeleport = world.AllowTeleport,
  192.                         ShowDisplays = world.ShowDisplays,
  193.                         Music = world.GetMusic(),
  194.                         ClientXML = world.ClientXml,
  195.                         ExtraXML = Manager.GameData.AdditionXml
  196.                     });
  197.                     client.Stage = ProtocalStage.Handshaked;
  198.                 }
  199.             });
  200.         }
  201.  
  202.         private bool IsWhiteListed(int rank)
  203.         {
  204.             if (Program.WhiteList)
  205.             {
  206.                 if (rank > 0)
  207.                     return true;
  208.                 return false;
  209.             }
  210.             return true;
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement