Skip to content

Commit

Permalink
fix: account for deleted player in rank/removal guild logic (#1889)
Browse files Browse the repository at this point in the history
* fix: account for deleted player in rank/removal guild logic

* guildcontroller fixes
  • Loading branch information
lodicolo authored Sep 2, 2023
1 parent e5b96f9 commit 20df5d6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 59 deletions.
25 changes: 21 additions & 4 deletions Intersect.Server/Database/Logging/Entities/GuildHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,34 @@ public enum GuildActivityType
/// <param name="initiator">The id of the player who caused this activity (or null if caused by the api or something else).</param>
/// <param name="type">The type of message we are sending.</param>
/// <param name="meta">Any other info regarding this activity</param>
public static void LogActivity(Guid guildId, Player player, Player initiator, GuildActivityType type, string meta = "")
public static void LogActivity(
Guid guildId,
Player player,
Player initiator,
GuildActivityType type,
string meta = ""
) =>
LogActivity(
guildId,
player?.UserId ?? default,
player?.Id ?? default,
player?.Client?.GetIp() ?? string.Empty,
initiator,
type,
meta
);

public static void LogActivity(Guid guildId, Guid userId, Guid playerId, string playerIp, Player initiator, GuildActivityType type, string meta = "")
{
if (Options.Instance.Logging.GuildActivity)
{
DbInterface.Pool.QueueWorkItem(new Action<GuildHistory>(Log), new GuildHistory
{
GuildId = guildId,
TimeStamp = DateTime.UtcNow,
UserId = player?.Client?.User?.Id ?? Guid.Empty,
PlayerId = player?.Id ?? Guid.Empty,
Ip = player?.Client?.GetIp(),
UserId = userId,
PlayerId = playerId,
Ip = playerIp,
InitiatorId = initiator?.Id ?? Guid.Empty,
Type = type,
Meta = meta,
Expand Down
132 changes: 81 additions & 51 deletions Intersect.Server/Database/PlayerData/Players/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,44 +268,50 @@ public void AddMember(Player player, int rank, Player initiator = null)
/// <summary>
/// Removes a player from the guild.
/// </summary>
/// <param name="player">The player to remove from the guild.</param>
/// <param name="targetId">The ID of the player to remove from the guild.</param>
/// <param name="targetPlayer">The player to remove from the guild.</param>
/// <param name="initiator">The player who initiated this change (null if done by the api or some other method).</param>
public void RemoveMember(Player player, Player initiator = null, GuildActivityType action = GuildActivityType.Left)
public void RemoveMember(Guid targetId, Player targetPlayer, Player initiator = null, GuildActivityType action = GuildActivityType.Left)
{
if (player != null)
if (targetPlayer == default)
{
using (var context = DbInterface.CreatePlayerContext(readOnly: false))
targetPlayer = Player.Find(targetId);
}

using (var context = DbInterface.CreatePlayerContext(readOnly: false))
{
var dbPlayer = context.Players.Where(p => p.Id == targetId && p.DbGuild.Id == this.Id).Include(p => p.DbGuild).FirstOrDefault();
if (dbPlayer != default)
{
var dbPlayer = context.Players.Where(p => p.Id == player.Id && p.DbGuild.Id == this.Id).Include(p => p.DbGuild).FirstOrDefault();
if (dbPlayer != null)
{
dbPlayer.DbGuild = null;
dbPlayer.GuildRank = 0;
dbPlayer.DbGuild = default;
dbPlayer.GuildRank = 0;

context.ChangeTracker.DetectChanges();
context.SaveChanges();
context.ChangeTracker.DetectChanges();
context.SaveChanges();
}

player.Guild = null;
player.GuildRank = 0;
player.GuildInvite = null;
if (targetPlayer != default)
{
targetPlayer.Guild = default;
targetPlayer.GuildRank = 0;
targetPlayer.GuildInvite = default;

if (player.BankInterface != null && player.GuildBank)
{
player.BankInterface.Dispose();
}
if (targetPlayer.BankInterface != null && targetPlayer.GuildBank)
{
targetPlayer.BankInterface.Dispose();
}
}

Members.TryRemove(player.Id, out GuildMember removed);

// Send our new guild list to everyone that's online.
UpdateMemberList();
Members.TryRemove(targetId, out GuildMember _);

// Send our entity data to nearby players.
PacketSender.SendEntityDataToProximity(Player.FindOnline(player.Id));
// Send our new guild list to everyone that's online.
UpdateMemberList();

LogActivity(Id, player, initiator, action);
}
}
// Send our entity data to nearby players.
PacketSender.SendEntityDataToProximity(Player.FindOnline(targetId));

LogActivity(Id, targetPlayer?.UserId ?? default, targetId, targetPlayer?.Client?.GetIp() ?? string.Empty, initiator, action);
}
}

Expand Down Expand Up @@ -340,50 +346,74 @@ public void UpdateMemberList()
/// <param name="id">The player to set the rank for.</param>
/// <param name="rank">The integer index of the rank to assign.</param>
/// <param name="initiator">The player who initiated this change (null if done by the api or some other method).</param>
public void SetPlayerRank(Guid id, int rank, Player initiator = null) => SetPlayerRank(Player.Find(id), rank, initiator);
public void SetPlayerRank(Guid id, int rank, Player initiator = null) => SetPlayerRank(id, default, rank, initiator);

/// <summary>
/// Sets a player's guild rank.
/// </summary>
/// <param name="player">The player to set the rank for.</param>
/// <param name="targetId">The player to set the rank for.</param>
/// <param name="targetPlayer">The player to set the rank for.</param>
/// <param name="rank">The integer index of the rank to assign.</param>
/// <param name="initiator">The player who initiated this change (null if done by the api or some other method).</param>
public void SetPlayerRank(Player player, int rank, Player initiator = null)
public void SetPlayerRank(Guid targetId, Player targetPlayer, int rank, Player initiator = null)
{
if (targetPlayer == default)
{
targetPlayer = Player.Find(targetId);
}

using (var context = DbInterface.CreatePlayerContext(readOnly: false))
{
var dbPlayer = context.Players.Where(p => p.Id == player.Id && p.DbGuild.Id == this.Id).FirstOrDefault();
if (dbPlayer != null)
var dbPlayer = context.Players.FirstOrDefault(p => p.Id == targetId && p.DbGuild.Id == this.Id);
var origRank = dbPlayer?.GuildRank;
if (dbPlayer != default)
{
var origRank = dbPlayer.GuildRank;

dbPlayer.GuildRank = rank;

context.ChangeTracker.DetectChanges();
context.SaveChanges();
}

player.GuildRank = rank;
if (targetPlayer != default)
{
targetPlayer.GuildRank = rank;
}

if (Members.TryGetValue(player.Id, out GuildMember val))
{
val.Rank = rank;
}
if (Members.TryGetValue(targetId, out GuildMember val))
{
val.Rank = rank;
}

// Send our new guild list to everyone that's online.
UpdateMemberList();
// Send our new guild list to everyone that's online.
UpdateMemberList();

// Send our entity data to nearby players.
PacketSender.SendEntityDataToProximity(Player.FindOnline(player.Id));
// Send our entity data to nearby players.
PacketSender.SendEntityDataToProximity(Player.FindOnline(targetId));

//Log this change
if (origRank < rank)
{
LogActivity(Id, player, initiator, GuildActivityType.Demoted, rank.ToString());
}
else if (origRank > rank)
{
LogActivity(Id, player, initiator, GuildActivityType.Promoted, rank.ToString());
}
//Log this change
if (!origRank.HasValue || origRank < rank)
{
LogActivity(
Id,
targetPlayer?.UserId ?? default,
targetId,
targetPlayer?.Client?.GetIp() ?? string.Empty,
initiator,
GuildActivityType.Demoted,
rank.ToString()
);
}
else if (origRank > rank)
{
LogActivity(
Id,
targetPlayer?.UserId ?? default,
targetId,
targetPlayer?.Client?.GetIp() ?? string.Empty,
initiator,
GuildActivityType.Promoted,
rank.ToString()
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Intersect.Server/Networking/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2691,7 +2691,7 @@ public void HandlePacket(Client client, UpdateGuildMemberPacket packet)
mem.StartCommonEventsWithTrigger(CommonEventTrigger.GuildMemberKicked, guild.Name, member.Name);
}

guild.RemoveMember(Player.Find(packet.Id), player, GuildHistory.GuildActivityType.Kicked);
guild.RemoveMember(packet.Id, default, player, GuildHistory.GuildActivityType.Kicked);
}
else
{
Expand Down Expand Up @@ -2891,7 +2891,7 @@ public void HandlePacket(Client client, GuildLeavePacket packet)
member.StartCommonEventsWithTrigger(CommonEventTrigger.GuildMemberLeft, guild.Name, player.Name);
}

guild.RemoveMember(player, null, GuildHistory.GuildActivityType.Left);
guild.RemoveMember(player.Id, player, null, GuildHistory.GuildActivityType.Left);

// Send the newly updated player information to their surroundings.
PacketSender.SendEntityDataToProximity(player);
Expand Down
4 changes: 2 additions & 2 deletions Intersect.Server/Web/RestApi/Routes/V1/GuildController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public object Kick(Guid guildId, LookupKey lookupKey)
);
}

guild.RemoveMember(player, null, Database.Logging.Entities.GuildHistory.GuildActivityType.Kicked);
guild.RemoveMember(player.Id, player, default, Database.Logging.Entities.GuildHistory.GuildActivityType.Kicked);

return player;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public object SetRank(Guid guildId, LookupKey lookupKey, [FromBody] GuildRank gu
);
}

guild.SetPlayerRank(player, guildRank.Rank, null);
guild.SetPlayerRank(player.Id, player, guildRank.Rank, default);

return player;
}
Expand Down

0 comments on commit 20df5d6

Please sign in to comment.