Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/Controllers/Common/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ public IActionResult GetGameDataByGameForDateRange(Viking viking, [FromForm] int
[Route("ContentWebService.asmx/GetPeriodicGameDataByGame")] // used by Math Blaster and WoJS (probably from 24 hours ago to now)
[VikingSession(UseLock = true)]
public IActionResult GetPeriodicGameDataByGame(Viking viking, [FromForm] int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, int score, bool buddyFilter, string apiKey) {
return Ok(gameDataService.GetGameData(viking, gameId, isMultiplayer, difficulty, gameLevel, key, count, AscendingOrder, buddyFilter, apiKey, DateTime.Now.AddHours(-24), DateTime.Now));
return Ok(gameDataService.GetDailyGameData(viking, gameId, isMultiplayer, difficulty, gameLevel, key, count, AscendingOrder, buddyFilter, apiKey));
}

[HttpPost]
Expand Down
1 change: 1 addition & 0 deletions src/Model/GameDataPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class GameDataPair {
public int GameDataId { get; set; }
public string Name { get; set; } = null!;
public int Value { get; set; }
public int DailyValue { get; set; }
[JsonIgnore]
public virtual GameData GameData { get; set; } = null!;
}
49 changes: 43 additions & 6 deletions src/Services/GameDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public bool SaveGameData(Viking viking, int gameId, bool isMultiplayer, int diff
viking.GameData.Add(gameData);

}
gameData.DatePlayed = DateTime.UtcNow;

SavePairs(gameData, xmlDocumentData);
gameData.DatePlayed = DateTime.UtcNow;
ctx.SaveChanges();

return true;
}

Expand Down Expand Up @@ -70,6 +72,33 @@ public GameDataSummary GetGameData(Viking viking, int gameId, bool isMultiplayer

return GetSummaryFromResponse(viking, isMultiplayer, difficulty, gameLevel, key, selectedData);
}

public GameDataSummary GetDailyGameData(Viking viking, int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, bool buddyFilter, string apiKey) {
IQueryable<GameDataPair> query = ctx.GameData
.Where(x =>
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

if (AscendingOrder) query = query.OrderBy(e => e.Value);
else query = query.OrderByDescending(e => e.Value);

List<GameDataResponse> selectedData;
if (ClientVersion.GetVersion(apiKey) <= ClientVersion.Max_OldJS)
// use DisplayName instead of Name
selectedData = query.Select(e => new GameDataResponse(
XmlUtil.DeserializeXml<AvatarData>(e.GameData.Viking.AvatarSerialized).DisplayName, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue)
).Take(count).ToList();
else
selectedData = query.Select(e => new GameDataResponse(
e.GameData.Viking.Name, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue)
).Take(count).ToList();

return GetSummaryFromResponse(viking, isMultiplayer, difficulty, gameLevel, key, selectedData);
}

// ByUser for JumpStart's My Scores
public GameDataSummary GetGameDataByUser(Viking viking, int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, string apiKey) {
Expand Down Expand Up @@ -141,12 +170,20 @@ private GameDataSummary GetSummaryFromResponse(Viking viking, bool isMultiplayer
private void SavePairs(Model.GameData gameData, string xmlDocumentData) {
foreach (var pair in GetGameDataPairs(xmlDocumentData)) {
GameDataPair? dbPair = gameData.GameDataPairs.FirstOrDefault(x => x.Name == pair.Name);
if (dbPair == null)

// If Name == "time" then (existing <= incoming) needs to be false (effectively (existing > incoming), as time should function).
// if Name is anything else, then second condition as normal.
bool newBest = (dbPair == null) || ((pair.Name == "time") != (dbPair.Value <= pair.Value));

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;
dbPair = pair;
} else if (newBest) dbPair.Value = pair.Value;

if (
newBest || // Surpassed Score (or Unset)
gameData.DatePlayed.Date != DateTime.UtcNow.Date // Another Day
) dbPair.DailyValue = pair.Value;
}
}

Expand Down