Пример #1
0
        public override void Prepare()
        {
            if (Creature.CreatureId == Connection.PlayerId)
            {
                return;
            }

            var player = Game.Instance.GetCreatureWithId(Connection.PlayerId) as IPlayer;

            if (player == null)
            {
                return;
            }

            if (AddedEffect != EffectT.None)
            {
                ResponsePackets.Add(new MagicEffectPacket
                {
                    Effect   = AddedEffect,
                    Location = Creature.Location
                });
            }

            ResponsePackets.Add(new AddCreaturePacket
            {
                Creature             = Creature,
                Location             = Creature.Location,
                AsKnown              = player.KnowsCreatureWithId(Creature.CreatureId),
                RemoveThisCreatureId = player.ChooseToRemoveFromKnownSet()
            });
        }
Пример #2
0
 public override void Prepare()
 {
     foreach (var packet in OutgoingPackets)
     {
         ResponsePackets.Add(packet);
     }
 }
 public override void Prepare()
 {
     ResponsePackets.Add(new CreatureChangedOutfitPacket
     {
         Creature = Creature
     });
 }
Пример #4
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            // No other content in message.
            var player = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            if (player == null)
            {
                return;
            }

            player.ClearPendingActions();

            var cooldownRemaining = player.CalculateRemainingCooldownTime(CooldownType.Move, DateTime.Now);

            if (!Game.Instance.RequestCreatureWalkToDirection(player, Direction, cooldownRemaining))
            {
                ResponsePackets.Add(new PlayerWalkCancelPacket
                {
                    Direction = player.Direction
                });

                ResponsePackets.Add(new TextMessagePacket
                {
                    Message = "Sorry, not possible.",
                    Type    = MessageType.StatusSmall
                });
            }
        }
Пример #5
0
 public override void Prepare()
 {
     ResponsePackets.Add(new AnimatedTextPacket {
         Location = Location,
         Text     = Text,
         Color    = TextColor
     });
 }
 public override void Prepare()
 {
     ResponsePackets.Add(new WorldLightPacket
     {
         Level = LightLevel,
         Color = LightColor
     });
 }
Пример #7
0
 public override void Prepare()
 {
     ResponsePackets.Add(new UpdateTilePacket
     {
         Location         = Location,
         DescriptionBytes = Description
     });
 }
Пример #8
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            var itemUsePacket = new ItemUsePacket(message);
            var player        = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            if (player == null)
            {
                return;
            }

            player.ClearPendingActions();

            // Before actually using the item, check if we're close enough to use it.
            if (itemUsePacket.FromLocation.Type == LocationType.Ground)
            {
                var locationDiff = itemUsePacket.FromLocation - player.Location;

                if (locationDiff.Z != 0)
                {
                    // it's on a different floor...
                    ResponsePackets.Add(new TextMessagePacket
                    {
                        Type    = MessageType.StatusSmall,
                        Message = "There is no way."
                    });

                    return;
                }

                if (locationDiff.MaxValueIn2D > 1)
                {
                    // Too far away to use it.
                    Location retryLoc;
                    var      directions = Game.Instance.Pathfind(player.Location, itemUsePacket.FromLocation, out retryLoc).ToArray();

                    player.SetPendingAction(new UseItemPlayerAction(player, itemUsePacket, retryLoc));

                    if (directions.Any())
                    {
                        player.AutoWalk(directions);
                    }
                    else
                    {
                        // we found no way...
                        ResponsePackets.Add(new TextMessagePacket
                        {
                            Type    = MessageType.StatusSmall,
                            Message = "There is no way."
                        });
                    }

                    return;
                }
            }

            new UseItemPlayerAction(player, itemUsePacket, itemUsePacket.FromLocation).Perform();
        }
Пример #9
0
        public bool PacketRcv(List <byte> packet)
        {
            var res = false;

            lock (lck)
            {
                ResponsePackets.Add(packet);

                if (State == ComRRConnectionStates.Sent)
                {
                    var cks = (byte)packet.Take(packet.Count - 1).Sum(a => a);

                    if (packet.Count != responseLength)
                    {
                        //length mismatch
                        var msg =
                            $"received packet length is {packet.Count} but required length is: {responseLength}";

                        error(msg, ComRRConnectionErrors.InvalidResponseLenght);
                    }
                    else if (packet[0] != responseHeader)
                    {
                        //invalid header
                        var msg =
                            $"received packet header is {packet[0]} but required header is: {responseHeader}";

                        error(msg, ComRRConnectionErrors.InvalidResponseHeader);
                    }
                    else if (packet[packet.Count - 1] != cks)
                    {
                        //invalid checksum
                        var msg =
                            $"received packet check sum is {packet[packet.Count - 1]} but requried check sum is: {cks}";

                        error(msg, ComRRConnectionErrors.InvalidResponseChecksum);
                    }
                    else
                    {
                        var data     = packet.Skip(1).Take(packet.Count - 2).ToArray();
                        var response = DecodeResponseFromData(data);
                        AfterDecodeResponseFromData(response);
                        res = true;
                    }
                }
                else
                {
                    var msg =
                        $"{nameof(State)} is {State} but required state is: {ComRRConnectionStates.Sent} in PacketRcv";

                    error(msg, ComRRConnectionErrors.InvalidState);
                }
            }

            return(res);
        }
Пример #10
0
 public override void Prepare()
 {
     ResponsePackets.Add(new CreatureSpeechPacket
     {
         ChannelId  = Channel,
         SenderName = Creature.Name,
         Location   = Creature.Location,
         SpeechType = SpeechType,
         Text       = Message,
         Time       = (uint)DateTime.Now.Ticks
     });
 }
Пример #11
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            var player = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            // No content.
            player?.StopWalking();
            player?.ClearPendingActions();

            if (player != null)
            {
                ResponsePackets.Add(new PlayerWalkCancelPacket
                {
                    Direction = player.ClientSafeDirection
                });
            }
        }
Пример #12
0
        public override void Prepare()
        {
            if (TurnedEffect != EffectT.None)
            {
                ResponsePackets.Add(new MagicEffectPacket
                {
                    Effect   = TurnedEffect,
                    Location = Creature.Location
                });
            }

            ResponsePackets.Add(new CreatureTurnedPacket
            {
                Creature = Creature
            });
        }
Пример #13
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            var    lookAtPacket = new LookAtPacket(message);
            IThing thing        = null;
            var    player       = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            if (player == null)
            {
                return;
            }

            Console.WriteLine($"LookAt {lookAtPacket.ThingId}.");

            if (lookAtPacket.Location.Type != LocationType.Ground || player.CanSee(lookAtPacket.Location))
            {
                // Get thing at location
                switch (lookAtPacket.Location.Type)
                {
                case LocationType.Ground:
                    thing = Game.Instance.GetTileAt(lookAtPacket.Location).GetThingAtStackPosition(lookAtPacket.StackPosition);
                    break;

                case LocationType.Container:
                    // TODO: implement containers.
                    // Container container = player.Inventory.GetContainer(location.Container);
                    // if (container != null)
                    // {
                    //    return container.GetItem(location.ContainerPosition);
                    // }
                    break;

                case LocationType.Slot:
                    thing = player.Inventory[(byte)lookAtPacket.Location.Slot];
                    break;
                }

                if (thing != null)
                {
                    ResponsePackets.Add(new TextMessagePacket
                    {
                        Type    = MessageType.DescriptionGreen,
                        Message = $"You see {thing.InspectionText}."
                    });
                }
            }
        }
Пример #14
0
        /// <inheritdoc/>
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            if (connection == null)
            {
                throw new System.ArgumentNullException(nameof(connection));
            }

            var authPacket = new AuthenticationPacket(message);

            var result = authPacket.Password.Equals(ServiceConfiguration.GetConfiguration().QueryManagerPassword);

            connection.IsAuthenticated = result;

            ResponsePackets.Add(new AuthenticationResultPacket {
                HadError = !result
            });
        }
Пример #15
0
        public override void Prepare()
        {
            var player = Game.Instance.GetCreatureWithId(Connection.PlayerId);

            if (player.CanSee(FromLocation) && FromStackpos < 10)
            {
                ResponsePackets.Add(new RemoveAtStackposPacket
                {
                    Location = FromLocation,
                    Stackpos = FromStackpos
                });
            }

            if (player.CanSee(ToLocation))
            {
                ResponsePackets.Add(new AddItemPacket
                {
                    Location = ToLocation,
                    Item     = Item
                });
            }
        }
Пример #16
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            // No further content on message.
            var player = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            if (player == null)
            {
                return;
            }

            // TODO: if player actually has permissions to change outfit.

            // TODO: get these based on sex and premium
            ushort chooseFromId = 128;
            ushort chooseToId   = 134;

            ResponsePackets.Add(new PlayerChooseOutfitPacket
            {
                CurrentOutfit = player.Outfit,
                ChooseFromId  = chooseFromId,
                ChooseToId    = chooseToId
            });
        }
Пример #17
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            // no further content
            var player = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            if (player == null)
            {
                return;
            }

            if (Game.Instance.AttemptLogout(player))
            {
                connection.Close();
            }
            else
            {
                ResponsePackets.Add(new TextMessagePacket
                {
                    Type    = MessageType.StatusSmall,
                    Message = "You may not logout (test message)"
                });
            }
        }
Пример #18
0
 private void RegisterReceivedPacket(SpheroPacket packet)
 {
     if (packet is SpheroResponsePacket)
     {
         ResponsePackets.Add(packet as SpheroResponsePacket);
         if (ResponsePackets.Count > _numberOfPacketsToLog)
         {
             ResponsePackets.RemoveAt(0);
         }
     }
     else if (packet is SpheroAsyncPacket)
     {
         AsyncPackets.Add(packet as SpheroAsyncPacket);
         if (AsyncPackets.Count > _numberOfPacketsToLog)
         {
             AsyncPackets.RemoveAt(0);
         }
     }
     else
     {
         throw new Exception("Unknown packet type. This should not happen.");
     }
 }
Пример #19
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            var packet = new ContainerUpPacket(message);
            var player = Game.Instance.GetCreatureWithId(connection.PlayerId) as Player;

            var currentContainer = player?.GetContainer(packet.ContainerId);

            if (currentContainer?.Parent == null)
            {
                return;
            }

            player?.OpenContainerAt(currentContainer.Parent, packet.ContainerId);

            ResponsePackets.Add(new ContainerOpenPacket
            {
                ContainerId  = (byte)currentContainer.Parent.GetIdFor(connection.PlayerId),
                ClientItemId = currentContainer.Parent.ThingId,
                HasParent    = currentContainer.Parent.Parent != null,
                Name         = currentContainer.Parent.Type.Name,
                Volume       = currentContainer.Parent.Volume,
                Contents     = currentContainer.Parent.Content
            });
        }
Пример #20
0
        public override void Prepare()
        {
            var player = Game.Instance.GetCreatureWithId(Connection.PlayerId);

            if (player == null || !player.CanSee(Creature) || !player.CanSee(Creature.Location))
            {
                return;
            }

            ResponsePackets.Add(new RemoveAtStackposPacket
            {
                Location = Creature.Location,
                Stackpos = OldStackPosition
            });

            if (RemoveEffect != EffectT.None)
            {
                ResponsePackets.Add(new MagicEffectPacket
                {
                    Location = Creature.Location,
                    Effect   = EffectT.Puff
                });
            }
        }
Пример #21
0
        protected override void InternalPerform()
        {
            var itemUsePacket = Packet as ItemUsePacket;

            IThing thingToUse = null;

            if (itemUsePacket == null)
            {
                return;
            }

            switch (itemUsePacket.FromLocation.Type)
            {
            case LocationType.Ground:
                thingToUse = Game.Instance.GetTileAt(itemUsePacket.FromLocation)?.GetThingAtStackPosition(itemUsePacket.FromStackPos);
                break;

            case LocationType.Container:
                var fromContainer = Player.GetContainer(itemUsePacket.FromLocation.Container);
                try
                {
                    thingToUse = fromContainer.Content[fromContainer.Content.Count - itemUsePacket.FromStackPos - 1];
                }
                catch (ArgumentOutOfRangeException)
                {
                }     // Happens when the content list does not contain the thing.
                break;

            case LocationType.Slot:
                try
                {
                    thingToUse = Player.Inventory[Convert.ToByte(itemUsePacket.FromLocation.Slot)];
                }
                catch
                {
                    // ignored
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (thingToUse == null)
            {
                // No thing to use found, exit here.
                return;
            }

            var thingAsItem      = thingToUse as IItem;
            var thingAsContainer = thingAsItem as IContainer;

            if (thingAsItem != null && thingAsItem.ChangesOnUse)
            {
                Functions.Change(ref thingToUse, thingAsItem.ChangeOnUseTo, 0);
            }
            else if (thingAsItem != null && thingAsItem.IsContainer && thingAsContainer != null)
            {
                var openContainerId = Player.GetContainerId(thingAsContainer);

                if (openContainerId < 0)
                {
                    // PlayerId doesn't have this container open.
                    switch (itemUsePacket.FromLocation.Type)
                    {
                    case LocationType.Ground:
                        Player.OpenContainer(thingAsContainer);
                        break;

                    case LocationType.Container:
                        Player.OpenContainerAt(thingAsContainer, itemUsePacket.Index);
                        break;

                    case LocationType.Slot:
                        Player.OpenContainerAt(thingAsContainer, itemUsePacket.Index);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    thingAsContainer.OnThingChanged += Player.CheckInventoryContainerProximity;

                    ResponsePackets.Add(new ContainerOpenPacket
                    {
                        ContainerId  = (byte)thingAsContainer.GetIdFor(Player.CreatureId),
                        ClientItemId = thingAsItem.ThingId,
                        HasParent    = thingAsContainer.Parent != null,
                        Name         = thingAsItem.Type.Name,
                        Volume       = thingAsContainer.Volume,
                        Contents     = thingAsContainer.Content
                    });
                }
                else
                {
                    // Close it.
                    Player.CloseContainerWithId((byte)openContainerId);
                    thingAsContainer.OnThingChanged -= Player.CheckInventoryContainerProximity;

                    ResponsePackets.Add(new ContainerClosePacket
                    {
                        ContainerId = (byte)openContainerId
                    });
                }
            }
            else
            {
                var useEvents = Game.Instance.EventsCatalog[ItemEventType.Use].Cast <UseItemEvent>();

                var candidate = useEvents.FirstOrDefault(e => e.ItemToUseId == itemUsePacket.ClientId && e.Setup(thingToUse, null, Player) && e.CanBeExecuted);

                if (candidate != null)
                {
                    // Execute all actions.
                    candidate.Execute();
                }
            }
        }
Пример #22
0
        public override void Prepare()
        {
            var player = Game.Instance.GetCreatureWithId(Connection.PlayerId) as IPlayer;

            if (player == null)
            {
                return;
            }

            var creature = Game.Instance.GetCreatureWithId(CreatureId);

            if (CreatureId == Connection.PlayerId)
            {
                if (WasTeleport) // TODO: revise; this had a contradicting condition on the source (< 10 vs >= 10)
                {
                    if (OldStackPosition < 10)
                    {
                        ResponsePackets.Add(new RemoveAtStackposPacket
                        {
                            Location = OldLocation,
                            Stackpos = OldStackPosition
                        });
                    }

                    ResponsePackets.Add(new MapDescriptionPacket
                    {
                        Origin           = NewLocation,
                        DescriptionBytes = Game.Instance.GetMapDescriptionAt(player, NewLocation)
                    });
                }
                else
                {
                    if (OldLocation.Z == 7 && NewLocation.Z > 7)
                    {
                        if (OldStackPosition < 10)
                        {
                            ResponsePackets.Add(new RemoveAtStackposPacket
                            {
                                Location = OldLocation,
                                Stackpos = OldStackPosition
                            });
                        }
                    }
                    else
                    {
                        ResponsePackets.Add(new CreatureMovedPacket
                        {
                            FromLocation = OldLocation,
                            FromStackpos = OldStackPosition,
                            ToLocation   = NewLocation
                        });
                    }

                    // floor change down
                    if (NewLocation.Z > OldLocation.Z)
                    {
                        // going from surface to underground
                        if (NewLocation.Z == 8)
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeDown)
                            {
                                DescriptionBytes = Game.Instance.GetMapFloorsDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 6), NewLocation.Z, (byte)(NewLocation.Z + 2), 18, 14, -1)
                            });
                        }

                        // going further down
                        else if (NewLocation.Z > OldLocation.Z && NewLocation.Z > 8 && NewLocation.Z < 14)
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeDown)
                            {
                                DescriptionBytes = Game.Instance.GetMapFloorsDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 6), (byte)(NewLocation.Z + 2), (byte)(NewLocation.Z + 2), 18, 14, -3)
                            });
                        }
                        else
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeDown)
                            {
                                DescriptionBytes = new byte[0] // no description needed.
                            });
                        }

                        // moving down a floor makes us out of sync, include east and south
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceEast)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X + 9), (ushort)(OldLocation.Y - 7), NewLocation.Z, NewLocation.IsUnderground, 1, 14)
                        });

                        // south
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceSouth)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y + 7), NewLocation.Z, NewLocation.IsUnderground, 18, 1)
                        });
                    }

                    // floor change up
                    else if (NewLocation.Z < OldLocation.Z)
                    {
                        // going to surface
                        if (NewLocation.Z == 7)
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeUp)
                            {
                                DescriptionBytes = Game.Instance.GetMapFloorsDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 6), 5, 0, 18, 14, 3)
                            });
                        }

                        // underground, going one floor up (still underground)
                        else if (NewLocation.Z > 7)
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeUp)
                            {
                                DescriptionBytes = Game.Instance.GetMapFloorsDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 6), (byte)(OldLocation.Z - 3), (byte)(OldLocation.Z - 3), 18, 14, 3)
                            });
                        }
                        else
                        {
                            ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.FloorChangeUp)
                            {
                                DescriptionBytes = new byte[0] // no description needed.
                            });
                        }

                        // moving up a floor up makes us out of sync, include west and north
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceWest)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 5), NewLocation.Z, NewLocation.IsUnderground, 1, 14)
                        });

                        // north
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceNorth)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X - 8), (ushort)(OldLocation.Y - 6), NewLocation.Z, NewLocation.IsUnderground, 18, 1)
                        });
                    }

                    if (OldLocation.Y > NewLocation.Y)
                    {
                        // north, for old x
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceNorth)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X - 8), (ushort)(NewLocation.Y - 6), NewLocation.Z, NewLocation.IsUnderground, 18, 1)
                        });
                    }
                    else if (OldLocation.Y < NewLocation.Y)
                    {
                        // south, for old x
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceSouth)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(OldLocation.X - 8), (ushort)(NewLocation.Y + 7), NewLocation.Z, NewLocation.IsUnderground, 18, 1)
                        });
                    }

                    if (OldLocation.X < NewLocation.X)
                    {
                        // east, [with new y]
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceEast)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(NewLocation.X + 9), (ushort)(NewLocation.Y - 6), NewLocation.Z, NewLocation.IsUnderground, 1, 14)
                        });
                    }
                    else if (OldLocation.X > NewLocation.X)
                    {
                        // west, [with new y]
                        ResponsePackets.Add(new MapPartialDescriptionPacket(GameOutgoingPacketType.MapSliceWest)
                        {
                            DescriptionBytes = Game.Instance.GetMapDescription(player, (ushort)(NewLocation.X - 8), (ushort)(NewLocation.Y - 6), NewLocation.Z, NewLocation.IsUnderground, 1, 14)
                        });
                    }
                }
            }
            else if (player.CanSee(OldLocation) && player.CanSee(NewLocation))
            {
                if (player.CanSee(creature))
                {
                    if (WasTeleport || OldLocation.Z == 7 && NewLocation.Z > 7 || OldStackPosition > 9)
                    {
                        if (OldStackPosition < 10)
                        {
                            ResponsePackets.Add(new RemoveAtStackposPacket
                            {
                                Location = OldLocation,
                                Stackpos = OldStackPosition
                            });
                        }

                        ResponsePackets.Add(new AddCreaturePacket
                        {
                            Location             = NewLocation,
                            Creature             = creature,
                            AsKnown              = player.KnowsCreatureWithId(CreatureId),
                            RemoveThisCreatureId = player.ChooseToRemoveFromKnownSet() // chooses a victim if neeeded.
                        });
                    }
                    else
                    {
                        ResponsePackets.Add(new CreatureMovedPacket
                        {
                            FromLocation = OldLocation,
                            FromStackpos = OldStackPosition,
                            ToLocation   = NewLocation
                        });
                    }
                }
            }
            else if (player.CanSee(OldLocation) && player.CanSee(creature))
            {
                if (OldStackPosition < 10)
                {
                    ResponsePackets.Add(new RemoveAtStackposPacket
                    {
                        Location = OldLocation,
                        Stackpos = OldStackPosition
                    });
                }
            }
            else if (player.CanSee(NewLocation) && player.CanSee(creature))
            {
                ResponsePackets.Add(new AddCreaturePacket
                {
                    Location             = NewLocation,
                    Creature             = creature,
                    AsKnown              = player.KnowsCreatureWithId(CreatureId),
                    RemoveThisCreatureId = player.ChooseToRemoveFromKnownSet() // chooses a victim if neeeded.
                });
            }

            // if (WasTeleport)
            // {
            //    ResponsePackets.Add(new MagicEffectPacket()
            //    {
            //        Location = NewLocation,
            //        Effect = Effect_t.BubbleBlue
            //    });
            // }
        }
Пример #23
0
 public void Clear()
 {
     ResponsePackets.Clear();
     AsyncPackets.Clear();
 }
Пример #24
0
        public override void HandleMessageContents(NetworkMessage message, Connection connection)
        {
            var playerLoginPacket = new PlayerLoginPacket(message);

            connection.SetXtea(playerLoginPacket.XteaKey);

            if (ServiceConfiguration.GetConfiguration().ReceivedClientVersionInt < ServiceConfiguration.GetConfiguration().ClientMinVersionInt ||
                playerLoginPacket.Version > ServiceConfiguration.GetConfiguration().ClientMaxVersionInt)
            {
                ResponsePackets.Add(new GameServerDisconnectPacket {
                    Reason = $"You need client version in between {ServiceConfiguration.GetConfiguration().ClientMinVersionString} and {ServiceConfiguration.GetConfiguration().ClientMaxVersionString} to connect to this server."
                });

                return;
            }

            if (Game.Instance.Status == WorldState.Creating)
            {
                ResponsePackets.Add(new GameServerDisconnectPacket
                {
                    Reason = "The game is just starting.\nPlease try again in a few minutes."
                });

                return;
            }

            using (var otContext = new OpenTibiaDbContext())
            {
                var failure = LoginFailureReason.None;

                var userRecord   = otContext.Users.FirstOrDefault(u => u.Login == playerLoginPacket.AccountNumber && u.Passwd.Equals(playerLoginPacket.Password));
                var playerRecord = otContext.Players.FirstOrDefault(p => p.Account_Nr == playerLoginPacket.AccountNumber && p.Charname.Equals(playerLoginPacket.CharacterName));

                if (userRecord == null || playerRecord == null)
                {
                    failure = LoginFailureReason.AccountOrPasswordIncorrect;
                }
                else
                {
                    // Check bannishment.
                    if (userRecord.Banished > 0 || userRecord.Bandelete > 0)
                    {
                        // Lift if time is up
                        if (userRecord.Bandelete > 0 || DateTime.FromFileTimeUtc(userRecord.Banished_Until) > DateTime.Now)
                        {
                            failure = LoginFailureReason.Bannished;
                        }
                        else
                        {
                            userRecord.Banished = 0;
                        }
                    }

                    // Check that no other characters from this account are logged in.
                    var anotherCharacterIsLoggedIn = otContext.Players.Any(p => p.Account_Nr == playerLoginPacket.AccountNumber && p.Online > 0 && !p.Charname.Equals(playerLoginPacket.CharacterName));

                    if (anotherCharacterIsLoggedIn)
                    {
                        failure = LoginFailureReason.AnotherCharacterIsLoggedIn;
                    }

                    // Check if game is open to public
                    if (Game.Instance.Status != WorldState.Open)
                    {
                        ResponsePackets.Add(new GameServerDisconnectPacket
                        {
                            Reason = "The game is not open to the public yet.\nCheck for news on our webpage."
                        });

                        return;
                    }
                }

                if (failure == LoginFailureReason.None)
                {
                    try
                    {
                        // Set player status to online.
                        // playerRecord.online = 1;

                        // otContext.SaveChanges();
                        var player = Game.Instance.Login(playerRecord, connection);

                        // set this to allow future packets from this connection.
                        connection.IsAuthenticated = true;
                        connection.PlayerId        = player.CreatureId;

                        ResponsePackets.Add(new SelfAppearPacket
                        {
                            CreatureId = player.CreatureId,
                            IsLogin    = true,
                            Player     = player
                        });

                        // Add MapDescription
                        ResponsePackets.Add(new MapDescriptionPacket
                        {
                            Origin           = player.Location,
                            DescriptionBytes = Game.Instance.GetMapDescriptionAt(player, player.Location)
                        });

                        ResponsePackets.Add(new MagicEffectPacket
                        {
                            Location = player.Location,
                            Effect   = EffectT.BubbleBlue
                        });

                        ResponsePackets.Add(new PlayerInventoryPacket {
                            Player = player
                        });
                        ResponsePackets.Add(new PlayerStatusPacket {
                            Player = player
                        });
                        ResponsePackets.Add(new PlayerSkillsPacket {
                            Player = player
                        });

                        ResponsePackets.Add(new WorldLightPacket {
                            Level = Game.Instance.LightLevel, Color = Game.Instance.LightColor
                        });

                        ResponsePackets.Add(new CreatureLightPacket {
                            Creature = player
                        });

                        // Adds a text message
                        ResponsePackets.Add(new TextMessagePacket
                        {
                            Type    = MessageType.StatusDefault,
                            Message = "This is a test message"
                        });

                        // std::string tempstring = g_config.getString(ConfigManager::LOGIN_MSG);
                        // if (tempstring.size() > 0)
                        // {
                        //    AddTextMessage(msg, MSG_STATUS_DEFAULT, tempstring.c_str());
                        // }

                        // if (player->getLastLoginSaved() != 0)
                        // {
                        //    tempstring = "Your last visit was on ";
                        //    time_t lastLogin = player->getLastLoginSaved();
                        //    tempstring += ctime(&lastLogin);
                        //    tempstring.erase(tempstring.length() - 1);
                        //    tempstring += ".";

                        // AddTextMessage(msg, MSG_STATUS_DEFAULT, tempstring.c_str());
                        // }
                        // else
                        // {
                        //    tempstring = "Welcome to ";
                        //    tempstring += g_config.getString(ConfigManager::SERVER_NAME);
                        //    tempstring += ". Please choose an outfit.";
                        //    sendOutfitWindow(player);
                        // }

                        // Add any Vips here.

                        // for (VIPListSet::iterator it = player->VIPList.begin(); it != player->VIPList.end(); it++)
                        // {
                        //    bool online;
                        //    std::string vip_name;
                        //    if (IOPlayer::instance()->getNameByGuid((*it), vip_name))
                        //    {
                        //        online = (g_game.getPlayerByName(vip_name) != NULL);
                        //
                        // msg->AddByte(0xD2);
                        // msg->AddU32(guid);
                        // msg->AddString(name);
                        // msg->AddByte(isOnline ? 1 : 0);
                        //    }
                        // }

                        // Send condition icons
                        ResponsePackets.Add(new PlayerConditionsPacket {
                            Player = player
                        });

                        return;
                    }
                    catch (Exception ex)
                    {
                        // TODO: propper logging
                        Console.WriteLine(ex);

                        failure = LoginFailureReason.InternalServerError;
                    }
                }

                if (failure != LoginFailureReason.None)
                {
                    ResponsePackets.Add(new GameServerDisconnectPacket
                    {
                        Reason = failure.ToString() // TODO: implement correctly.
                    });
                }
            }
        }