mirror of
https://github.com/SoDOff-Project/sodoff.git
synced 2025-10-11 16:28:50 -07:00
Heavily optimized the entire PR.
This commit is contained in:
parent
66bb9f0a82
commit
17709423b6
@ -19,8 +19,6 @@ public class DBContext : DbContext {
|
|||||||
public DbSet<RoomItem> RoomItems { get; set; } = null!;
|
public DbSet<RoomItem> RoomItems { get; set; } = null!;
|
||||||
public DbSet<GameData> GameData { get; set; } = null!;
|
public DbSet<GameData> GameData { get; set; } = null!;
|
||||||
public DbSet<GameDataPair> GameDataPairs { get; set; } = null!;
|
public DbSet<GameDataPair> GameDataPairs { get; set; } = null!;
|
||||||
public DbSet<DailyHighscore> DailyHighscores { get; set; } = null!;
|
|
||||||
public DbSet<DailyHighscorePair> DailyHighscorePairs { get; set; } = null!;
|
|
||||||
public DbSet<AchievementPoints> AchievementPoints { get; set; } = null!;
|
public DbSet<AchievementPoints> AchievementPoints { get; set; } = null!;
|
||||||
public DbSet<ProfileAnswer> ProfileAnswers { get; set; } = null!;
|
public DbSet<ProfileAnswer> ProfileAnswers { get; set; } = null!;
|
||||||
public DbSet<MMORole> MMORoles { get; set; } = null!;
|
public DbSet<MMORole> MMORoles { get; set; } = null!;
|
||||||
@ -131,9 +129,6 @@ public class DBContext : DbContext {
|
|||||||
builder.Entity<Viking>().HasMany(v => v.GameData)
|
builder.Entity<Viking>().HasMany(v => v.GameData)
|
||||||
.WithOne(e => e.Viking);
|
.WithOne(e => e.Viking);
|
||||||
|
|
||||||
builder.Entity<Viking>().HasMany(v => v.DailyHighscores)
|
|
||||||
.WithOne(e => e.Viking);
|
|
||||||
|
|
||||||
builder.Entity<Viking>().HasMany(v => v.SavedData)
|
builder.Entity<Viking>().HasMany(v => v.SavedData)
|
||||||
.WithOne(e => e.Viking);
|
.WithOne(e => e.Viking);
|
||||||
|
|
||||||
@ -227,15 +222,6 @@ public class DBContext : DbContext {
|
|||||||
builder.Entity<GameDataPair>().HasOne(e => e.GameData)
|
builder.Entity<GameDataPair>().HasOne(e => e.GameData)
|
||||||
.WithMany(e => e.GameDataPairs);
|
.WithMany(e => e.GameDataPairs);
|
||||||
|
|
||||||
builder.Entity<DailyHighscore>().HasOne(e => e.Viking)
|
|
||||||
.WithMany(e => e.DailyHighscores);
|
|
||||||
|
|
||||||
builder.Entity<DailyHighscore>().HasMany(e => e.ScorePairs)
|
|
||||||
.WithOne(e => e.DailyScore);
|
|
||||||
|
|
||||||
builder.Entity<DailyHighscorePair>().HasOne(e => e.DailyScore)
|
|
||||||
.WithMany(e => e.ScorePairs);
|
|
||||||
|
|
||||||
// Others ..
|
// Others ..
|
||||||
builder.Entity<Image>().HasOne(s => s.Viking)
|
builder.Entity<Image>().HasOne(s => s.Viking)
|
||||||
.WithMany(e => e.Images)
|
.WithMany(e => e.Images)
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace sodoff.Model;
|
|
||||||
|
|
||||||
public class DailyHighscore {
|
|
||||||
[Key]
|
|
||||||
public int Id { get; set; }
|
|
||||||
public int VikingId { get; set; }
|
|
||||||
public int GameId { get; set; }
|
|
||||||
public int Difficulty { get; set; }
|
|
||||||
public int GameLevel { get; set; }
|
|
||||||
public bool IsMultiplayer { get; set; }
|
|
||||||
public virtual ICollection<DailyHighscorePair> ScorePairs { get; set; } = null!;
|
|
||||||
public virtual Viking Viking { get; set; } = null!;
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace sodoff.Model;
|
|
||||||
|
|
||||||
public class DailyHighscorePair {
|
|
||||||
[Key]
|
|
||||||
public int Id { get; set; }
|
|
||||||
public int DailyScoreId { get; set; }
|
|
||||||
public string Name { get; set; } = null!;
|
|
||||||
public int Value { get; set; }
|
|
||||||
public DateTime DatePlayed { get; set; }
|
|
||||||
public virtual DailyHighscore DailyScore { get; set; } = null!;
|
|
||||||
}
|
|
@ -7,5 +7,6 @@ public class GameDataPair {
|
|||||||
public int GameDataId { get; set; }
|
public int GameDataId { get; set; }
|
||||||
public string Name { get; set; } = null!;
|
public string Name { get; set; } = null!;
|
||||||
public int Value { get; set; }
|
public int Value { get; set; }
|
||||||
|
public int DailyValue { get; set; }
|
||||||
public virtual GameData GameData { get; set; } = null!;
|
public virtual GameData GameData { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,6 @@ public class Viking {
|
|||||||
public virtual ICollection<PairData> PairData { get; set; } = null!;
|
public virtual ICollection<PairData> PairData { get; set; } = null!;
|
||||||
public virtual ICollection<InventoryItem> InventoryItems { get; set; } = null!;
|
public virtual ICollection<InventoryItem> InventoryItems { get; set; } = null!;
|
||||||
public virtual ICollection<GameData> GameData { get; set; } = null!;
|
public virtual ICollection<GameData> GameData { get; set; } = null!;
|
||||||
public virtual ICollection<DailyHighscore> DailyHighscores { get; set; } = null!;
|
|
||||||
public virtual ICollection<ProfileAnswer> ProfileAnswers { get; set; } = null!;
|
public virtual ICollection<ProfileAnswer> ProfileAnswers { get; set; } = null!;
|
||||||
public virtual ICollection<SavedData> SavedData { get; set; } = null!;
|
public virtual ICollection<SavedData> SavedData { get; set; } = null!;
|
||||||
public virtual ICollection<Party> Parties { get; set; } = null!;
|
public virtual ICollection<Party> Parties { get; set; } = null!;
|
||||||
|
@ -31,20 +31,9 @@ public class GameDataService {
|
|||||||
viking.GameData.Add(gameData);
|
viking.GameData.Add(gameData);
|
||||||
|
|
||||||
}
|
}
|
||||||
gameData.DatePlayed = DateTime.UtcNow;
|
|
||||||
|
|
||||||
DailyHighscore? daily = viking.DailyHighscores.FirstOrDefault(x => x.GameId == gameId && x.IsMultiplayer == isMultiplayer && x.Difficulty == difficulty && x.GameLevel == gameLevel);
|
SavePairs(gameData, xmlDocumentData);
|
||||||
if (daily == null) {
|
gameData.DatePlayed = DateTime.UtcNow;
|
||||||
daily = new DailyHighscore {
|
|
||||||
GameId = gameId,
|
|
||||||
Difficulty = difficulty,
|
|
||||||
GameLevel = gameLevel,
|
|
||||||
IsMultiplayer = isMultiplayer,
|
|
||||||
ScorePairs = new List<DailyHighscorePair>()
|
|
||||||
};
|
|
||||||
viking.DailyHighscores.Add(daily);
|
|
||||||
}
|
|
||||||
SavePairs(gameData, daily, xmlDocumentData);
|
|
||||||
ctx.SaveChanges();
|
ctx.SaveChanges();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -85,9 +74,12 @@ public class GameDataService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public GameDataSummary GetDailyGameData(Viking viking, int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, bool buddyFilter, string apiKey) {
|
public GameDataSummary GetDailyGameData(Viking viking, int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, bool buddyFilter, string apiKey) {
|
||||||
IQueryable<DailyHighscorePair> query = ctx.DailyHighscores
|
IQueryable<GameDataPair> query = ctx.GameData
|
||||||
.Where(x => x.GameId == gameId && x.IsMultiplayer == false && x.Difficulty == difficulty && x.GameLevel == gameLevel)
|
.Where(x =>
|
||||||
.SelectMany(e => e.ScorePairs).Where(x => x.DatePlayed == DateTime.Today && x.Name == key);
|
x.GameId == gameId && x.IsMultiplayer == false &&
|
||||||
|
x.Difficulty == difficulty && x.GameLevel == gameLevel &&
|
||||||
|
x.DatePlayed.Date == DateTime.UtcNow.Date
|
||||||
|
).SelectMany(e => e.GameDataPairs).Where(x => x.Name == key);
|
||||||
|
|
||||||
// TODO: Buddy filter
|
// TODO: Buddy filter
|
||||||
|
|
||||||
@ -98,11 +90,11 @@ public class GameDataService {
|
|||||||
if (ClientVersion.GetVersion(apiKey) <= ClientVersion.Max_OldJS)
|
if (ClientVersion.GetVersion(apiKey) <= ClientVersion.Max_OldJS)
|
||||||
// use DisplayName instead of Name
|
// use DisplayName instead of Name
|
||||||
selectedData = query.Select(e => new GameDataResponse(
|
selectedData = query.Select(e => new GameDataResponse(
|
||||||
XmlUtil.DeserializeXml<AvatarData>(e.DailyScore.Viking.AvatarSerialized).DisplayName, e.DailyScore.Viking.Uid, e.DatePlayed, false, false, e.Value)
|
XmlUtil.DeserializeXml<AvatarData>(e.GameData.Viking.AvatarSerialized).DisplayName, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue)
|
||||||
).Take(count).ToList();
|
).Take(count).ToList();
|
||||||
else
|
else
|
||||||
selectedData = query.Select(e => new GameDataResponse(
|
selectedData = query.Select(e => new GameDataResponse(
|
||||||
e.DailyScore.Viking.Name, e.DailyScore.Viking.Uid, e.DatePlayed, false, false, e.Value)
|
e.GameData.Viking.Name, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue)
|
||||||
).Take(count).ToList();
|
).Take(count).ToList();
|
||||||
|
|
||||||
return GetSummaryFromResponse(viking, isMultiplayer, difficulty, gameLevel, key, selectedData);
|
return GetSummaryFromResponse(viking, isMultiplayer, difficulty, gameLevel, key, selectedData);
|
||||||
@ -175,36 +167,23 @@ public class GameDataService {
|
|||||||
return gameData;
|
return gameData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SavePairs(Model.GameData gameData, DailyHighscore daily, string xmlDocumentData) {
|
private void SavePairs(Model.GameData gameData, string xmlDocumentData) {
|
||||||
foreach (var pair in GetGameDataPairs(xmlDocumentData)) {
|
foreach (var pair in GetGameDataPairs(xmlDocumentData)) {
|
||||||
GameDataPair? dbPair = gameData.GameDataPairs.FirstOrDefault(x => x.Name == pair.Name);
|
GameDataPair? dbPair = gameData.GameDataPairs.FirstOrDefault(x => x.Name == pair.Name);
|
||||||
if (dbPair == null)
|
|
||||||
gameData.GameDataPairs.Add(pair);
|
|
||||||
else if (pair.Name == "time" && dbPair.Value > pair.Value)
|
|
||||||
dbPair.Value = pair.Value;
|
|
||||||
else if (pair.Name != "time" && dbPair.Value <= pair.Value)
|
|
||||||
dbPair.Value = pair.Value;
|
|
||||||
|
|
||||||
DailyHighscorePair? dailyPair = daily.ScorePairs.FirstOrDefault(x => x.Name == pair.Name);
|
// If Name == "time" then (existing <= incoming) needs to be false (effectively (existing > incoming), as time should function).
|
||||||
if (dailyPair == null) {
|
// if Name is anything else, then second condition as normal.
|
||||||
daily.Difficulty = gameData.Difficulty;
|
bool newBest = (dbPair == null) || ((pair.Name == "time") != (dbPair.Value <= pair.Value));
|
||||||
daily.GameLevel = gameData.GameLevel;
|
|
||||||
dailyPair = new DailyHighscorePair {
|
if (dbPair == null) {
|
||||||
Name = pair.Name,
|
gameData.GameDataPairs.Add(pair);
|
||||||
Value = pair.Value,
|
dbPair = pair;
|
||||||
DatePlayed = DateTime.Today
|
} else if (newBest) dbPair.Value = pair.Value;
|
||||||
};
|
|
||||||
daily.ScorePairs.Add(dailyPair);
|
if (
|
||||||
} else if (
|
newBest || // Surpassed Score (or Unset)
|
||||||
(pair.Name == "time" && dailyPair.Value > pair.Value) || // Better Time
|
gameData.DatePlayed.Date != DateTime.UtcNow.Date // Another Day
|
||||||
(pair.Name != "time" && dailyPair.Value <= pair.Value) || // Better Score
|
) dbPair.DailyValue = pair.Value;
|
||||||
dailyPair.DatePlayed != DateTime.Today // Another Day
|
|
||||||
) {
|
|
||||||
daily.Difficulty = gameData.Difficulty;
|
|
||||||
daily.GameLevel = gameData.GameLevel;
|
|
||||||
dailyPair.DatePlayed = DateTime.Today;
|
|
||||||
dailyPair.Value = pair.Value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user