Skip to content

Commit

Permalink
Removed server_id and map_id from aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed May 25, 2024
1 parent b718018 commit 752ca26
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public partial class Sessions
public override string ModuleName => "Sessions";
public override string ModuleDescription => "Track player sessions";
public override string ModuleAuthor => "github.com/oscar-wos/Sessions";
public override string ModuleVersion => "1.2.7";
public override string ModuleVersion => "1.2.8";

public required IDatabase _database;
public readonly Ip _ip = new();
Expand Down
3 changes: 1 addition & 2 deletions IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Sessions;
public interface IDatabase
{
string BuildConnectionString(CoreConfig config);
//string SerializeString(string value);

Task<ServerSQL> GetServerAsync(string serverIp, ushort serverPort);
Task<MapSQL> GetMapAsync(string mapName);
Expand All @@ -17,7 +16,7 @@ public interface IDatabase
void UpdateSessionsBulkAsync(int[] playerIds, int[] sessionIds);
void UpdateSeen(int playerId);

void InsertAlias(int sessionId, int playerId, int serverId, int mapId, string alias);
void InsertAlias(int sessionId, int playerId, string alias);
void InsertMessage(int sessionId, int playerId, int mapId, MessageType messageType, string message);
}

Expand Down
10 changes: 4 additions & 6 deletions PostgreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public async void UpdateSessionsBulkAsync(int[] playerIds, int[] sessionIds)

try
{
foreach (int player in playerIds)
await _connection.ExecuteAsync(_queries.UpdateSeen, new { PlayerId = player }, transaction: tx);
foreach (int playerId in playerIds)
await _connection.ExecuteAsync(_queries.UpdateSeen, new { PlayerId = playerId }, transaction: tx);

foreach (int sessionId in sessionIds)
await _connection.ExecuteAsync(_queries.UpdateSession, new { SessionId = sessionId }, transaction: tx);
Expand Down Expand Up @@ -180,15 +180,14 @@ public void UpdateSeen(int playerId)
}
}

public void InsertAlias(int sessionId, int playerId, int serverId, int mapId, string alias)
public void InsertAlias(int sessionId, int playerId, string alias)
{
try
{
NpgsqlCommand command = new(_queries.InsertAlias, _connection);

command.Parameters.AddWithValue("@SessionId", sessionId);
command.Parameters.AddWithValue("@PlayerId", playerId);
command.Parameters.AddWithValue("@MapId", mapId);
command.Parameters.AddWithValue("@Alias", alias);

command.ExecuteNonQuery();
Expand Down Expand Up @@ -256,7 +255,6 @@ end_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
id BIGSERIAL PRIMARY KEY,
session_id BIGINT NOT NULL,
player_id INT NOT NULL,
map_id SMALLINT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
alias VARCHAR(256)
)";
Expand Down Expand Up @@ -285,6 +283,6 @@ message VARCHAR(512)
public override string UpdateSeen => "UPDATE players SET last_seen = NOW() WHERE id = @PlayerId";

public override string SelectAlias => "SELECT id, alias FROM aliases WHERE player_id = @PlayerId ORDER BY id DESC LIMIT 1";
public override string InsertAlias => "INSERT INTO aliases (session_id, player_id, map_id, alias) VALUES (@SessionId, @PlayerId, @MapId, @Alias)";
public override string InsertAlias => "INSERT INTO aliases (session_id, player_id, alias) VALUES (@SessionId, @PlayerId, @Alias)";
public override string InsertMessage => "INSERT INTO messages (session_id, player_id, map_id, message_type, message) VALUES (@SessionId, @PlayerId, @MapId, @MessageType, @Message)";
}
14 changes: 7 additions & 7 deletions Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public override void Load(bool hotReload)
ushort port = (ushort)ConVar.Find("hostport")!.GetPrimitiveValue<int>();
_server = _database!.GetServerAsync(ip, port).GetAwaiter().GetResult();

RegisterListener<Listeners.OnMapStart>(async mapName =>
_server.Map = await _database.GetMapAsync(mapName)
RegisterListener<Listeners.OnMapStart>(mapName =>
_server.Map = _database.GetMapAsync(mapName).GetAwaiter().GetResult()
);

RegisterListener<Listeners.OnClientAuthorized>((playerSlot, steamId) =>
Expand All @@ -50,16 +50,16 @@ public override void Load(bool hotReload)
CCSPlayerController? playerController = Utilities.GetPlayerFromUserid(@event.Userid);
if (playerController == null || !playerController.IsValid || playerController.IsBot
|| @event.Text == null || !_players.TryGetValue(playerController.Slot, out PlayerSQL? value)
|| value.Session == null || _server.Map == null)
|| @event.Text == null || _server == null || _server.Map == null
|| !_players.TryGetValue(playerController.Slot, out PlayerSQL? value) || value.Session == null)
return HookResult.Continue;
MessageType messageType = @event.Teamonly ? MessageType.TeamChat : MessageType.Chat;
_database.InsertMessage(value.Session.Id, value.Id, _server.Map.Id, messageType, @event.Text);
return HookResult.Continue;
}, HookMode.Post);
}, HookMode.Pre);

_timer = AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT);

if (!hotReload)
Expand Down Expand Up @@ -99,6 +99,6 @@ public async Task CheckAlias(int playerSlot, string alias)
alias = Regex.Replace(alias, @"[0\\]x[\da-fA-F]{2}", string.Empty);

if (recentAlias == null || recentAlias.Alias != alias)
_database.InsertAlias(player.Session!.Id, player.Id, _server!.Id, _server.Map!.Id, alias);
_database.InsertAlias(player.Session!.Id, player.Id, alias);
}
}
10 changes: 4 additions & 6 deletions SqlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public async void UpdateSessionsBulkAsync(int[] playerIds, int[] sessionIds)

try
{
foreach (int player in playerIds)
await _connection.ExecuteAsync(_queries.UpdateSeen, new { PlayerId = player }, transaction: tx);
foreach (int playerId in playerIds)
await _connection.ExecuteAsync(_queries.UpdateSeen, new { PlayerId = playerId }, transaction: tx);

foreach (int sessionId in sessionIds)
await _connection.ExecuteAsync(_queries.UpdateSession, new { SessionId = sessionId }, transaction: tx);
Expand All @@ -182,15 +182,14 @@ public void UpdateSeen(int playerId) {
}
}

public void InsertAlias(int sessionId, int playerId, int serverId, int mapId, string alias)
public void InsertAlias(int sessionId, int playerId, string alias)
{
try
{
MySqlCommand command = new(_queries.InsertAlias, _connection);

command.Parameters.AddWithValue("@SessionId", sessionId);
command.Parameters.AddWithValue("@PlayerId", playerId);
command.Parameters.AddWithValue("@MapId", mapId);
command.Parameters.AddWithValue("@Alias", alias);

command.ExecuteNonQuery();
Expand Down Expand Up @@ -258,7 +257,6 @@ end_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
session_id BIGINT UNSIGNED NOT NULL,
player_id INT UNSIGNED NOT NULL,
map_id SMALLINT UNSIGNED NOT NULL,
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
alias VARCHAR(64) COLLATE utf8_unicode_520_ci
)";
Expand Down Expand Up @@ -287,6 +285,6 @@ message VARCHAR(128) COLLATE utf8_unicode_520_ci
public override string UpdateSeen => "UPDATE players SET last_seen = NOW() WHERE id = @PlayerId";

public override string SelectAlias => "SELECT id, alias FROM aliases WHERE player_id = @PlayerId ORDER BY id DESC LIMIT 1";
public override string InsertAlias => "INSERT INTO aliases (session_id, player_id, map_id, alias) VALUES (@SessionId, @PlayerId, @MapId, @Alias)";
public override string InsertAlias => "INSERT INTO aliases (session_id, player_id, alias) VALUES (@SessionId, @PlayerId, @Alias)";
public override string InsertMessage => "INSERT INTO messages (session_id, player_id, map_id, message_type, message) VALUES (@SessionId, @PlayerId, @MapId, @MessageType, @Message)";
}

0 comments on commit 752ca26

Please sign in to comment.