Skip to content

Commit

Permalink
Code cleanup (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
caioavidal committed Dec 26, 2024
1 parent 755fbe3 commit 7455930
Show file tree
Hide file tree
Showing 50 changed files with 216 additions and 213 deletions.
6 changes: 3 additions & 3 deletions data/extensions/Players/Loaders/GodLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public override IPlayer Load(PlayerEntity playerEntity)
if (Guard.IsNull(playerEntity)) return null;

var town = GetTown(playerEntity);

var playerLocation =
new Location((ushort)playerEntity.PosX, (ushort)playerEntity.PosY, (byte)playerEntity.PosZ);

var currentTile = GetCurrentTile(playerLocation);

var newPlayer = new God(
Expand Down Expand Up @@ -66,7 +66,7 @@ public override IPlayer Load(PlayerEntity playerEntity)
Guild = GuildStore.Get((ushort)(playerEntity.GuildMember?.GuildId ?? 0)),
GuildLevel = (ushort)(playerEntity.GuildMember?.RankId ?? 0)
};

newPlayer.SetCurrentTile(currentTile);
newPlayer.AddInventory(ConvertToInventory(newPlayer, playerEntity));
var god = CreatureFactory.CreatePlayer(newPlayer);
Expand Down
2 changes: 1 addition & 1 deletion data/extensions/Players/Loaders/TutorLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override IPlayer Load(PlayerEntity playerEntity)
var town = GetTown(playerEntity);
var playerLocation =
new Location((ushort)playerEntity.PosX, (ushort)playerEntity.PosY, (byte)playerEntity.PosZ);

var currentTile = GetCurrentTile(playerLocation);
var newPlayer = new Tutor(
(uint)playerEntity.Id,
Expand Down
8 changes: 4 additions & 4 deletions data/extensions/Spells/Support/Knight/BloodRage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public override bool OnCast(ICombatActor actor, string words, out InvalidOperati
error = InvalidOperation.None;

if (actor is not IPlayer player) return false;

player.AddSkillBonus(SkillType.Axe, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.AddSkillBonus(SkillType.Sword, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.AddSkillBonus(SkillType.Club, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.AddSkillBonus(SkillType.Fist, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));

actor.DisableShieldDefense();
actor.IncreaseDamageReceived(15);
return true;
Expand All @@ -31,12 +31,12 @@ public override bool OnCast(ICombatActor actor, string words, out InvalidOperati
public override void OnEnd(ICombatActor actor)
{
if (actor is not IPlayer player) return;

player.RemoveSkillBonus(SkillType.Axe, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.RemoveSkillBonus(SkillType.Sword, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.RemoveSkillBonus(SkillType.Club, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));
player.RemoveSkillBonus(SkillType.Fist, (sbyte)Math.Abs(player.Skills[SkillType.Axe].Level * 0.35));

actor.EnableShieldDefense();
actor.DecreaseDamageReceived(15);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Linq;
using NeoServer.Data.Entities;
using NeoServer.Game.Common;
using NeoServer.Game.Common.Contracts.World.Tiles;
using NeoServer.Game.Common.Location.Structs;
using NeoServer.Game.Common.Results;
using NeoServer.Game.Creatures;
using NeoServer.Game.World;
using NeoServer.Loaders.Guilds;
using NeoServer.Loaders.Interfaces;
using NeoServer.Server.Common.Contracts;
Expand All @@ -20,9 +17,9 @@ namespace NeoServer.Server.Commands.Player;
public class PlayerLogInCommand : ICommand
{
private readonly ILogger _logger;
private readonly PlayerLocationResolver _playerLocationResolver;
private readonly IGameServer game;
private readonly GuildLoader guildLoader;
private readonly PlayerLocationResolver _playerLocationResolver;
private readonly IEnumerable<IPlayerLoader> playerLoaders;

public PlayerLogInCommand(IGameServer game, IEnumerable<IPlayerLoader> playerLoaders, GuildLoader guildLoader,
Expand All @@ -41,7 +38,7 @@ public Result Execute(PlayerEntity playerRecord, IConnection connection)
if (playerRecord is null)
//todo validations here
return Result.Fail(InvalidOperation.PlayerNotFound);

if (!game.CreatureManager.TryGetLoggedPlayer((uint)playerRecord.Id, out var player))
{
if (playerLoaders.FirstOrDefault(x => x.IsApplicable(playerRecord)) is not { } playerLoader)
Expand All @@ -51,11 +48,11 @@ public Result Execute(PlayerEntity playerRecord, IConnection connection)

var playerLocation = _playerLocationResolver.GetPlayerLocation(playerRecord);
if (playerLocation == Location.Zero) return Result.Fail(InvalidOperation.PlayerLocationInvalid);

playerRecord.PosX = playerLocation.X;
playerRecord.PosY = playerLocation.Y;
playerRecord.PosZ = playerLocation.Z;

player = playerLoader.Load(playerRecord);
}

Expand All @@ -64,7 +61,7 @@ public Result Execute(PlayerEntity playerRecord, IConnection connection)
player.Login();
player.Vip.LoadVipList(playerRecord.Account.VipList.Select(x => ((uint)x.PlayerId, x.Player?.Name)));
_logger.Information("Player {PlayerName} logged in", player.Name);

return Result.Success;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public void Execute(ICreature creature, ICondition c)
if (!game.CreatureManager.GetPlayerConnection(creature.CreatureId, out var connection)) return;

ushort icons = 0;
foreach (var condition in player.Conditions)
{
icons |= (ushort)ConditionIconParser.Parse(condition.Key);
}
foreach (var condition in player.Conditions) icons |= (ushort)ConditionIconParser.Parse(condition.Key);

connection.OutgoingPackets.Enqueue(new ConditionIconPacket(icons));
connection.Send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ public Location GetPlayerLocation(PlayerEntity playerEntity)
: null;

if (playerTile is not null) return location;

foreach (var neighbour in location.Neighbours)
{
world.TryGetTile(ref location, out var neighbourTile);
if (neighbourTile is IDynamicTile && PlayerEnterTileRule.Rule.CanEnter(neighbourTile, neighbour))
{
return location;
}
}

var town = GetTown(playerEntity);
if (town is null) return Location.Zero;

var townLocation = town.Coordinate.Location;

playerTile = world.TryGetTile(ref townLocation, out var townTile) && townTile is IDynamicTile townDynamicTile &&
PlayerEnterTileRule.Rule.CanEnter(townDynamicTile, townLocation)
PlayerEnterTileRule.Rule.CanEnter(townDynamicTile, townLocation)
? townDynamicTile
: null;

Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationServer/NeoServer.Server/Tasks/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Start(CancellationToken token)
Task.Run(async () =>
{
while (await _reader.WaitToReadAsync(token))

{
if (token.IsCancellationRequested) _writer.Complete();
// Fast loop around available jobs
Expand Down
2 changes: 1 addition & 1 deletion src/Database/NeoServer.Data/Entities/WorldEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public class WorldEntity
public string Name { get; set; }
public string Ip { get; set; }
public int Port { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Database/NeoServer.Data/Seeds/WorldModelSeed.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NeoServer.Data.Entities;
using System;

namespace NeoServer.Data.Seeds;

Expand Down
8 changes: 4 additions & 4 deletions src/GameWorldSimulator/NeoServer.Game.Combat/Spells/Spell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ public abstract class BaseSpell : ISpell
public bool InvokeOn(ICombatActor actor, ICombatActor onCreature, string words, out InvalidOperation error)
{
if (!CanBeUsedBy(actor, out error)) return false;

if (!onCreature.HasCondition(ConditionType))
if (!OnCast(onCreature, words, out error))
return false;

AddCondition(onCreature);

if (actor is IPlayer) AddCooldown(actor);

OnSpellInvoked?.Invoke(onCreature, this);
if (actor is IPlayer player) player.ConsumeMana(Mana);

return true;
}

public bool Invoke(ICombatActor actor, string words, out InvalidOperation error)
{
if (!CanBeUsedBy(actor, out error)) return false;

if (!actor.HasCondition(ConditionType))
if (!OnCast(actor, words, out error))
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public bool IsNextTo(ITile dest)
bool TryGetStackPositionOfThing(IPlayer player, IThing thing, out byte stackPosition);
byte GetCreatureStackPositionIndex(IPlayer observer);
bool HasFlag(TileFlags flag);

public bool CanEnter(ICreature creature)
{
if (creature is not IWalkableCreature walkableCreature) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public enum CooldownType
Advertise,
WalkAround,
UseItem,
SupportSpell,
SupportSpell
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public virtual bool CanEnter(ITile tile, ICreature creature)
!dynamicTile.HasFlag(TileFlags.Unpassable),
dynamicTile.Ground is not null);
}

public virtual bool CanEnter(ITile tile, Location location)
{
if (tile is not IDynamicTile dynamicTile) return false;
Expand Down Expand Up @@ -73,6 +74,7 @@ public override bool CanEnter(ITile tile, ICreature creature)
!dynamicTile.HasFlag(TileFlags.Unpassable),
dynamicTile.Ground is not null);
}

public override bool CanEnter(ITile tile, Location location)
{
if (tile is not IDynamicTile dynamicTile) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public void Execute(IWalkableCreature creature, Location location)
if (creature.Location == location) return;

var destination = location;

if (map[location] is not IDynamicTile { FloorDirection: FloorChangeDirection.None } tile)
{
tile = FindNeighbourTile(creature, location);
}

if (destination == Location.Zero) return;
if (tile is null || !creature.TileEnterRule.CanEnter(tile, creature)) return;
Expand All @@ -36,12 +34,8 @@ public void Execute(IWalkableCreature creature, Location location)
private IDynamicTile FindNeighbourTile(IWalkableCreature creature, Location location)
{
foreach (var neighbour in location.Neighbours)
{
if (map[neighbour] is IDynamicTile toTile)
{
return toTile;
}
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ protected CombatActor(ICreatureType type, IMapTool mapTool, IOutfit outfit = nul
{
}

public abstract int DefendUsingShield(int attack);
public abstract int DefendUsingArmor(int attack);
public bool IsShieldDefenseEnabled { get; private set; } = true;
public byte DamageReceivedPercentage { get; private set; }

public abstract int DefendUsingShield(int attack);
public abstract int DefendUsingArmor(int attack);

public void AddCondition(ICondition condition)
{
var result = Conditions.TryAdd(condition.Type, condition);
Expand Down Expand Up @@ -348,6 +349,26 @@ public Result Attack(ICombatActor enemy, ICombatAttack attack, CombatAttackValue
return Result.Success;
}

public void DisableShieldDefense()
{
IsShieldDefenseEnabled = false;
}

public void EnableShieldDefense()
{
IsShieldDefenseEnabled = true;
}

public void IncreaseDamageReceived(byte percentage)
{
DamageReceivedPercentage += percentage;
}

public void DecreaseDamageReceived(byte percentage)
{
DamageReceivedPercentage -= percentage;
}

public abstract bool HasImmunity(Immunity immunity);

public virtual bool CanBlock(DamageType damage)
Expand Down Expand Up @@ -437,18 +458,6 @@ protected void InvokeAttackCanceled()
{
OnAttackCanceled?.Invoke(this);
}

public void DisableShieldDefense() => IsShieldDefenseEnabled = false;
public void EnableShieldDefense() => IsShieldDefenseEnabled = true;
public void IncreaseDamageReceived(byte percentage)
{
DamageReceivedPercentage += percentage;
}

public void DecreaseDamageReceived(byte percentage)
{
DamageReceivedPercentage -= percentage;
}

#region Events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public ushort GetRawSkillLevel(SkillType skillType)
var skillLevel = hasSkill ? skill.Level : 1;
return (ushort)Math.Max(0, skillLevel);
}

public ushort GetSkillLevel(SkillType skillType)
{
var hasSkill = Skills.TryGetValue(skillType, out var skill);
Expand Down Expand Up @@ -415,7 +416,7 @@ public void ChangeSecureMode(byte mode)
public override int DefendUsingShield(int attack)
{
if (!IsShieldDefenseEnabled) return attack;

var defense = Inventory.TotalDefense * Skills[SkillType.Shielding].Level *
(DefenseFactor / 100d) - attack / 100d * ArmorRating * (Vocation.Formula?.Defense ?? 1f);

Expand Down
11 changes: 9 additions & 2 deletions src/GameWorldSimulator/NeoServer.Game.Creatures/Player/Skill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ public Skill(SkillType type, ushort level = 0, double count = 0)

public sbyte Bonus { get; private set; }

public void AddBonus(sbyte increase) => Bonus = (sbyte)(Bonus + increase);
public void RemoveBonus(sbyte decrease) => Bonus = (sbyte)(Bonus - decrease);
public void AddBonus(sbyte increase)
{
Bonus = (sbyte)(Bonus + increase);
}

public void RemoveBonus(sbyte decrease)
{
Bonus = (sbyte)(Bonus - decrease);
}

public SkillType Type { get; }
public ushort Level { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion src/GameWorldSimulator/NeoServer.Game.World/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public void PlaceCreature(ICreature creature)
{
if (this[creature.Location] is not IDynamicTile tile) return;

if(!tile.CanEnter(creature)) return;
if (!tile.CanEnter(creature)) return;

if (tile.HasCreature)
foreach (var location in tile.Location.Neighbours)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public bool HasFlag(TileFlags flag)
{
return ((uint)flag & Flags) != 0;
}

public bool BlockMissile => HasFlag(TileFlags.BlockMissile);

public Location Location { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ private void SetGround(IGround ground)

TileOperationEvent.OnChanged(this, ground, operations);
}

public Result<OperationResultList<ICreature>> AddCreature(ICreature creature)
{
if (creature is not IWalkableCreature walkableCreature)
Expand Down
1 change: 0 additions & 1 deletion src/Loaders/NeoServer.Loaders/Interfaces/IPlayerLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NeoServer.Data.Entities;
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Results;

namespace NeoServer.Loaders.Interfaces;

Expand Down
Loading

0 comments on commit 7455930

Please sign in to comment.