Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2 #1

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ on:
PluginVersion:
description: 'Plugin Version'
required: true
default: 'v1.0.0'
default: '1.0.0'

env:
PLUGIN_NAME: Sessions
DOTNET_VERSION: 8.0.x
PATH_PLUGIN: addons/counterstrikesharp/plugins/
START_VERSION: 1.0.0
USE_V_VERSION: true
USE_V_VERSION: false

jobs:
version:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ compiled/
*.sln
.vs/
DataBaseService.cs
SqlExample.cs
SqlExample.cs
1.1/
.vscode/
21 changes: 0 additions & 21 deletions .vscode/tasks.json

This file was deleted.

37 changes: 18 additions & 19 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using CounterStrikeSharp.API.Core;
using System.Text.Json.Serialization;

namespace Core
namespace Sessions;

public class CoreConfig : BasePluginConfig
{
public class CoreConfig : BasePluginConfig
{
public override int Version { get; set; } = 1;
public override int Version { get; set; } = 1;

[JsonPropertyName("DatabaseType")]
public string DatabaseType { get; set; } = "postgres";
[JsonPropertyName("DatabaseType")]
public string DatabaseType { get; set; } = "postgres";

[JsonPropertyName("DatabaseHost")]
public string DatabaseHost { get; set; } = "";
[JsonPropertyName("DatabaseHost")]
public string DatabaseHost { get; set; } = "";

[JsonPropertyName("DatabasePort")]
public int DatabasePort { get; set; } = 5432;
[JsonPropertyName("DatabasePort")]
public int DatabasePort { get; set; } = 5432;

[JsonPropertyName("DatabaseUser")]
public string DatabaseUser { get; set; } = "";
[JsonPropertyName("DatabaseUser")]
public string DatabaseUser { get; set; } = "";

[JsonPropertyName("DatabasePassword")]
public string DatabasePassword { get; set; } = "";
[JsonPropertyName("DatabasePassword")]
public string DatabasePassword { get; set; } = "";

[JsonPropertyName("DatabaseName")]
public string DatabaseName { get; set; } = "";
[JsonPropertyName("DatabaseName")]
public string DatabaseName { get; set; } = "";

[JsonPropertyName("DatabaseKeepAlive")]
public int DatabaseKeepAlive { get; set; } = 30;
}
[JsonPropertyName("DatabaseKeepAlive")]
public int DatabaseKeepAlive { get; set; } = 30;
}
32 changes: 0 additions & 32 deletions Database.cs

This file was deleted.

57 changes: 57 additions & 0 deletions IDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Extensions.Logging;

namespace Sessions;

public interface IDatabase
{
string BuildConnectionString(CoreConfig config);

Task<ServerSQL> GetServerAsync(string serverIp, ushort serverPort);
Task<MapSQL> GetMapAsync(string mapName);
Task<PlayerSQL> GetPlayerAsync(ulong steamId);
Task<SessionSQL> GetSessionAsync(int playerId, int serverId, int mapId, string ip);

void CreateTablesAsync();
void UpdateSessionsBulkAsync(int[] sessionIds);
void UpdateSeenAsync(int playerId);
}

public interface IDatabaseFactory
{
IDatabase Database { get; }
}

public class DatabaseFactory : IDatabaseFactory
{
private readonly IDatabase _database;
private readonly ILogger _logger;

public DatabaseFactory(CoreConfig config)
{
CheckConfig(config);

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
_logger = loggerFactory.CreateLogger<DatabaseFactory>();

_database = config.DatabaseType switch
{
"postgres" => new PostgresService(config, _logger),
"mysql" => new SqlService(config, _logger),
_ => throw new InvalidOperationException("Database type is not supported"),
};
}

public IDatabase Database => _database;

private static void CheckConfig(CoreConfig config)
{
if (string.IsNullOrWhiteSpace(config.DatabaseType) ||
string.IsNullOrWhiteSpace(config.DatabaseHost) ||
string.IsNullOrWhiteSpace(config.DatabaseUser) ||
string.IsNullOrWhiteSpace(config.DatabaseName) ||
config.DatabasePort == 0)
{
throw new InvalidOperationException("Database is not set in the configuration file");
}
}
}
77 changes: 77 additions & 0 deletions IDatabaseQueries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace Sessions;

public interface IDatabaseQueries
{
string CreateServers { get; }
string CreateMaps { get; }
string CreatePlayers { get; }
string CreateSessions { get; }

string SelectServer { get; }
string InsertServer { get; }

string SelectMap { get; }
string InsertMap { get; }

string SelectPlayer { get; }
string InsertPlayer { get; }

string InsertSession { get; }
string UpdateSession { get; }
string UpdateSeen { get; }
}

public abstract class Queries : IDatabaseQueries
{
public abstract string CreateServers { get; }
public abstract string CreateMaps { get; }
public abstract string CreatePlayers { get; }
public abstract string CreateSessions { get; }

public abstract string SelectServer { get; }
public abstract string InsertServer { get; }

public abstract string SelectMap { get; }
public abstract string InsertMap { get; }

public abstract string SelectPlayer { get; }
public abstract string InsertPlayer { get; }

public abstract string InsertSession { get; }
public abstract string UpdateSession { get; }
public abstract string UpdateSeen { get; }

public IEnumerable<string> GetCreateQueries()
{
yield return CreateServers;
yield return CreateMaps;
yield return CreatePlayers;
yield return CreateSessions;
}
}

public class ServerSQL()
{
public int Id { get; set; }

public MapSQL? Map { get; set; }
}

public class MapSQL()
{
public short Id { get; set; }
}

public class PlayerSQL()
{
public int Id { get; set; }
public DateTime FirstSeen { get; set; }
public DateTime LastSeen { get; set; }

public SessionSQL? Session { get; set; }
}

public class SessionSQL()
{
public int Id { get; set; }
}
2 changes: 1 addition & 1 deletion Ip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Runtime.InteropServices;
using CounterStrikeSharp.API.Core;

namespace Core;
namespace Sessions;

public class Ip
{
Expand Down
Loading