Skip to content

Commit

Permalink
Rename ScaleIndex to Scale
Browse files Browse the repository at this point in the history
  • Loading branch information
turecross321 committed Nov 3, 2023
1 parent 70d67a4 commit 9542749
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 235 deletions.
78 changes: 36 additions & 42 deletions SoundShapesServer/Database/GameDatabaseContext.Levels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ namespace SoundShapesServer.Database;

public partial class GameDatabaseContext
{
public GameLevel CreateLevel(GameUser user, PublishLevelRequest request, PlatformType uploadPlatform, bool createEvent = true, string? levelId = null)
public GameLevel CreateLevel(GameUser user, PublishLevelRequest request, PlatformType uploadPlatform,
bool createEvent = true, string? levelId = null)
{
levelId ??= GenerateLevelId();
GameLevel level = new GameLevel()
GameLevel level = new()
{
Id = levelId,
Author = user,
Expand All @@ -30,7 +31,7 @@ public GameLevel CreateLevel(GameUser user, PublishLevelRequest request, Platfor
ModificationDate = request.CreationDate,
FileSize = request.FileSize,
Visibility = LevelVisibility.Public,
UploadPlatform = uploadPlatform,
UploadPlatform = uploadPlatform
};

_realm.Write(() =>
Expand All @@ -39,9 +40,9 @@ public GameLevel CreateLevel(GameUser user, PublishLevelRequest request, Platfor
user.LevelsCount = user.Levels.Count();
});

if (createEvent)
if (createEvent)
CreateEvent(user, EventType.LevelPublish, uploadPlatform, EventDataType.Level, level.Id);

return level;
}

Expand All @@ -61,10 +62,7 @@ private void SetLevelPlayTime(GameLevel level)
{
long totalPlayTime = level.LeaderboardEntries.AsEnumerable().Sum(e => e.PlayTime);

_realm.Write(() =>
{
level.TotalPlayTime = totalPlayTime;
});
_realm.Write(() => { level.TotalPlayTime = totalPlayTime; });
}

public void UploadLevelResources(IDataStore dataStore, GameLevel level, byte[] levelFile, byte[] thumbnailFile,
Expand All @@ -74,7 +72,7 @@ public void UploadLevelResources(IDataStore dataStore, GameLevel level, byte[] l
UploadLevelResource(dataStore, level, thumbnailFile, FileType.Image);
UploadLevelResource(dataStore, level, soundFile, FileType.Sound);
}

public bool AnalyzeLevel(GameLevel level, byte[] levelFile)
{
SSLevel? ssLevel = SSLevel.FromLevelFile(levelFile);
Expand All @@ -86,34 +84,33 @@ public bool AnalyzeLevel(GameLevel level, byte[] levelFile)
level.FileSize = levelFile.Length;
level.Bpm = ssLevel.Bpm;
level.TransposeValue = ssLevel.TransposeValue;
level.ScaleIndex = ssLevel.ScaleIndex;
level._Scale = ssLevel.ScaleIndex;
level.TotalScreens = ssLevel.ScreenData.Count();
level.TotalEntities = ssLevel.Entities.Count() + ssLevel.EntitiesB.Count();
level.HasCar = ssLevel.EntitiesB.Any(e => e.EntityType == "Platformer_EntityPacks_GameStuff_CarCheckpoint");
level.HasExplodingCar = ssLevel.EntitiesB.Any(e => e.EntityType == "Platformer_EntityPacks_GameStuff_ExplodingCarCheckpoint");
level.HasExplodingCar = ssLevel.EntitiesB.Any(e =>
e.EntityType == "Platformer_EntityPacks_GameStuff_ExplodingCarCheckpoint");
level.HasUfo = ssLevel.EntitiesB.Any(e => e.EntityType == "Platformer_EntityPacks_GameStuff_UFOCheckpoint");
level.HasFirefly = ssLevel.EntitiesB.Any(e => e.EntityType == "Platformer_EntityPacks_GameStuff_FireflyCheckpoint");

level.HasFirefly =
ssLevel.EntitiesB.Any(e => e.EntityType == "Platformer_EntityPacks_GameStuff_FireflyCheckpoint");
});

return true;
}

public ApiOkResponse UploadLevelResource(IDataStore dataStore, GameLevel level,
byte[] file, FileType fileType)
{
if (fileType == FileType.Image && !file.IsPng())
return ApiBadRequestError.FileIsNotPng;

if (fileType == FileType.Level)
{
if (!AnalyzeLevel(level, file))
return ApiBadRequestError.CorruptLevel;
}


string key = GetLevelResourceKey(level, fileType);
dataStore.WriteToStore(key, file);

SetLevelFilePath(level, fileType, key);
return new ApiOkResponse();
}
Expand Down Expand Up @@ -147,34 +144,28 @@ private static void RemoveLevelResources(GameLevel level, IDataStore dataStore)
if (level.ThumbnailFilePath != null) dataStore.RemoveFromStore(level.ThumbnailFilePath);
if (level.SoundFilePath != null) dataStore.RemoveFromStore(level.SoundFilePath);
}

public void RemoveLevel(GameLevel level, IDataStore dataStore)
{
RemoveLevelResources(level, dataStore);
RemoveAllReportsWithContentLevel(level);
RemoveEventsOnLevel(level);

_realm.Write(() =>
{
foreach (GameAlbum album in level.Albums)
{
album.Levels.Remove(level);
}
foreach (GameAlbum album in level.Albums) album.Levels.Remove(level);
_realm.RemoveRange(level.DailyLevels);
_realm.RemoveRange(level.Likes);
_realm.RemoveRange(level.LeaderboardEntries);
_realm.Remove(level);
});
}

public void AddCompletionToLevel(GameUser user, GameLevel level)
{
if (!level.UniqueCompletions.Contains(user)) AddUniqueCompletion(user, level);

_realm.Write(() =>
{
level.CompletionCount++;
});

_realm.Write(() => { level.CompletionCount++; });
}

private void AddUniqueCompletion(GameUser user, GameLevel level)
Expand All @@ -186,16 +177,14 @@ private void AddUniqueCompletion(GameUser user, GameLevel level)
user.CompletedLevelsCount = user.CompletedLevels.Count();
});
}

public void SetLevelDifficulty(GameLevel level)
{
_realm.Refresh();

_realm.Write(() =>
{
level.Difficulty = CalculateLevelDifficulty(level);
});
_realm.Write(() => { level.Difficulty = CalculateLevelDifficulty(level); });
}

public void AddDeathsToLevel(GameUser user, GameLevel level, int deaths)
{
_realm.Write(() =>
Expand All @@ -204,15 +193,18 @@ public void AddDeathsToLevel(GameUser user, GameLevel level, int deaths)
user.Deaths += deaths;
});
}

public GameLevel? GetLevelWithId(string id) => _realm.All<GameLevel>().FirstOrDefault(l => l.Id == id);

public GameLevel? GetLevelWithId(string id)
{
return _realm.All<GameLevel>().FirstOrDefault(l => l.Id == id);
}

private IQueryable<GameLevel> GetLevelsWithIds(IEnumerable<string> ids)
{
List<GameLevel> levels = new();

// ReSharper disable once LoopCanBeConvertedToQuery
foreach (string levelId in ids)
foreach (string levelId in ids)
{
GameLevel? level = GetLevelWithId(levelId);
if (level != null) levels.Add(level);
Expand All @@ -221,12 +213,14 @@ private IQueryable<GameLevel> GetLevelsWithIds(IEnumerable<string> ids)
return levels.AsQueryable();
}

public PaginatedList<GameLevel> GetPaginatedLevels(LevelOrderType order, bool descending, LevelFilters filters, int from, int count, GameUser? accessor)
public PaginatedList<GameLevel> GetPaginatedLevels(LevelOrderType order, bool descending, LevelFilters filters,
int from, int count, GameUser? accessor)
{
return new PaginatedList<GameLevel>(GetLevels(order, descending, filters, accessor), from, count);
}

public IQueryable<GameLevel> GetLevels(LevelOrderType order, bool descending, LevelFilters filters, GameUser? accessor)
public IQueryable<GameLevel> GetLevels(LevelOrderType order, bool descending, LevelFilters filters,
GameUser? accessor)
{
return _realm.All<GameLevel>().FilterLevels(this, filters, accessor).OrderLevels(order, descending);
}
Expand Down
Loading

0 comments on commit 9542749

Please sign in to comment.