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

PostgreSQL support #501

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Refresh.GameServer/Authentication/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public TokenGame TokenGame

public string IpAddress { get; set; }

public ObjectId UserId { get; set; }
public GameUser User { get; set; }
}
11 changes: 6 additions & 5 deletions Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ public void AddIpVerificationRequest(GameUser user, string ipAddress)
{
GameIpVerificationRequest request = new()
{
User = user,
IpAddress = ipAddress,
CreatedAt = this._time.Now,
};

this.Write(() =>
{
user.IpVerificationRequests.Add(request);
this.GameIpVerificationRequests.Add(request);
});
}

Expand All @@ -158,22 +159,22 @@ public void SetApprovedIp(GameUser user, string ipAddress)
this.Write(() =>
{
user.CurrentVerifiedIp = ipAddress;
user.IpVerificationRequests.Clear();
this.GameIpVerificationRequests.RemoveRange(this.GameIpVerificationRequests.Where(r => r.User == user));
});
}

public void DenyIpVerificationRequest(GameUser user, string ipAddress)
{
IEnumerable<GameIpVerificationRequest> requests = user.IpVerificationRequests.Where(r => r.IpAddress == ipAddress);
IEnumerable<GameIpVerificationRequest> requests = this.GameIpVerificationRequests.Where(r => r.IpAddress == ipAddress && r.User == user);
this.Write(() =>
{
foreach (GameIpVerificationRequest request in requests)
{
user.IpVerificationRequests.Remove(request);
this.GameIpVerificationRequests.Remove(request);
}
});
}

public DatabaseList<GameIpVerificationRequest> GetIpVerificationRequestsForUser(GameUser user, int count, int skip)
=> new(user.IpVerificationRequests, skip, count);
=> new(this.GameIpVerificationRequests.Where(r => r.User == user), skip, count);
}
3 changes: 2 additions & 1 deletion Refresh.GameServer/Database/GameDatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class GameDatabaseContext : RealmDatabaseContext
private RealmDbSet<GameAsset> GameAssets => new(this._realm);
private RealmDbSet<GameNotification> GameNotifications => new(this._realm);
private RealmDbSet<GamePhoto> GamePhotos => new(this._realm);
private RealmDbSet<GameIpVerificationRequest> GameIpVerificationRequests => new(this._realm);
private RealmDbSet<GameAnnouncement> GameAnnouncements => new(this._realm);
private RealmDbSet<QueuedRegistration> QueuedRegistrations => new(this._realm);
private RealmDbSet<EmailVerificationCode> EmailVerificationCodes => new(this._realm);
Expand All @@ -53,7 +54,7 @@ public partial class GameDatabaseContext : RealmDatabaseContext
private RealmDbSet<GameReview> GameReviews => new(this._realm);
private RealmDbSet<DisallowedUser> DisallowedUsers => new(this._realm);

internal GameDatabaseContext(IDateTimeProvider time)
public GameDatabaseContext(IDateTimeProvider time)
{
this._time = time;
}
Expand Down
4 changes: 2 additions & 2 deletions Refresh.GameServer/Database/GameDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ protected GameDatabaseProvider(IDateTimeProvider time)
this._time = time;
}

protected override ulong SchemaVersion => 131;
protected override ulong SchemaVersion => 1;

protected override string Filename => "refreshGameServer.realm";
protected override string Filename => "refreshGameServer-new.realm";

protected override List<Type> SchemaTypes { get; } = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static GameLevelResponse FromHash(string hash, DataContext dataContext)
public static GameLevelResponse? FromOld(GameLevel? old, DataContext dataContext)
{
if (old == null) return null;

GameLevelResponse response = new()
{
LevelId = old.LevelId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace Refresh.GameServer.Types.UserData;

#nullable disable

public partial class GameIpVerificationRequest : IEmbeddedObject
public partial class GameIpVerificationRequest : IRealmObject
{
public GameUser User { get; set; }

public string IpAddress { get; set; }
public DateTimeOffset CreatedAt { get; set; }
}
2 changes: 0 additions & 2 deletions Refresh.GameServer/Types/UserData/GameUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public partial class GameUser : IRealmObject, IRateLimitUser

#nullable disable
public IList<GameComment> ProfileComments { get; }

public IList<GameIpVerificationRequest> IpVerificationRequests { get; }
#nullable restore

public string BetaPlanetsHash { get; set; } = "0";
Expand Down
101 changes: 101 additions & 0 deletions Refresh.LegacyDatabase/GameDatabaseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Bunkum.RealmDatabase;
using Realms;
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Database;
using Refresh.GameServer.Time;
using Refresh.GameServer.Types;
using Refresh.GameServer.Types.Activity;
using Refresh.GameServer.Types.Assets;
using Refresh.GameServer.Types.Comments;
using Refresh.GameServer.Types.Contests;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.Levels.SkillRewards;
using Refresh.GameServer.Types.Notifications;
using Refresh.GameServer.Types.Relations;
using Refresh.GameServer.Types.Report;
using Refresh.GameServer.Types.Reviews;
using Refresh.GameServer.Types.UserData;
using Refresh.GameServer.Types.UserData.Leaderboard;
using GamePhoto = Refresh.GameServer.Types.Photos.GamePhoto;
using GamePhotoSubject = Refresh.GameServer.Types.Photos.GamePhotoSubject;

namespace Refresh.LegacyDatabase;

public class GameDatabaseProvider : RealmDatabaseProvider<GameDatabaseContext>
{
private readonly IDateTimeProvider _time;

public GameDatabaseProvider()
{
this._time = new SystemDateTimeProvider();
}

protected GameDatabaseProvider(IDateTimeProvider time)
{
this._time = time;
}

protected override ulong SchemaVersion => 127;

protected override string Filename => "refreshGameServer.realm";

protected override List<Type> SchemaTypes { get; } = new()
{
typeof(GameUser),
typeof(GameLocation),
typeof(UserPins),
typeof(Token),
typeof(GameLevel),
typeof(GameSkillReward),
typeof(GameComment),
typeof(CommentRelation),
typeof(FavouriteLevelRelation),
typeof(QueueLevelRelation),
typeof(FavouriteUserRelation),
typeof(PlayLevelRelation),
typeof(UniquePlayLevelRelation),
typeof(RateLevelRelation),
typeof(Event),
typeof(GameSubmittedScore),
typeof(GameAsset),
typeof(GameNotification),
typeof(GamePhoto),
typeof(GamePhotoSubject),
typeof(GameIpVerificationRequest),
typeof(GameAnnouncement),
typeof(QueuedRegistration),
typeof(EmailVerificationCode),
typeof(RequestStatistics),
typeof(SequentialIdStorage),
typeof(GameContest),
//grief report items
typeof(GameReport),
typeof(InfoBubble),
typeof(Marqee),
typeof(Player),
typeof(Rect),
typeof(ScreenElements),
typeof(ScreenRect),
typeof(Slot),
typeof(GameReview),
typeof(DisallowedUser),
};

public override void Warmup()
{
using GameDatabaseContext context = this.GetContext();
_ = context.GetTotalLevelCount();
}

protected override GameDatabaseContext CreateContext()
{
return new GameDatabaseContext(this._time);
}

protected override void Migrate(Migration migration, ulong oldVersion)
{
if (oldVersion != SchemaVersion)
throw new InvalidOperationException($"The legacy database must be fully up to date before migration." +
$"Expected version is {SchemaVersion}, but the database file was at {oldVersion}.");
}
}
13 changes: 13 additions & 0 deletions Refresh.LegacyDatabase/Refresh.LegacyDatabase.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Refresh.GameServer\Refresh.GameServer.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions Refresh.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefreshTests.GameServer", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Refresh.Common", "Refresh.Common\Refresh.Common.csproj", "{A3D76B31-8732-4134-907B-F36773DF70D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Refresh.LegacyDatabase", "Refresh.LegacyDatabase\Refresh.LegacyDatabase.csproj", "{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -37,6 +39,12 @@ Global
{A3D76B31-8732-4134-907B-F36773DF70D3}.Release|Any CPU.Build.0 = Release|Any CPU
{A3D76B31-8732-4134-907B-F36773DF70D3}.DebugLocalBunkum|Any CPU.ActiveCfg = Debug|Any CPU
{A3D76B31-8732-4134-907B-F36773DF70D3}.DebugLocalBunkum|Any CPU.Build.0 = Debug|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.Release|Any CPU.Build.0 = Release|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.DebugLocalBunkum|Any CPU.ActiveCfg = Debug|Any CPU
{EB712F6C-8CE2-4CAC-9327-7E8517BFF750}.DebugLocalBunkum|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
EndGlobalSection
Expand Down
Loading