Skip to content

Commit

Permalink
chore: move legacy last game and presence writes to ResumePlayerSessi…
Browse files Browse the repository at this point in the history
…on (#1948)

* chore: move legacy last game and rich presence writes to ResumePlayerSession

* reduce writes

* restore last game id save

* 'IsFinal' => 0 for consistency
  • Loading branch information
luchaos authored Oct 28, 2023
1 parent 1a59edd commit 66a52b0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 118 deletions.
41 changes: 28 additions & 13 deletions app/Platform/Actions/ResumePlayerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,42 @@ public function execute(
->orderByDesc('id')
->first();

if ($playerSession) {
// if the session hasn't been updated in the last 10 minutes, start a new session
if ($timestamp->diffInMinutes($playerSession->rich_presence_updated_at) < 10) {
$playerSession->duration = max(1, $timestamp->diffInMinutes($playerSession->created_at));
if ($presence) {
$playerSession->rich_presence = $presence;
}
$playerSession->rich_presence_updated_at = $timestamp > $playerSession->rich_presence_updated_at ? $timestamp : $playerSession->rich_presence_updated_at;
$playerSession->save(['touch' => true]);

PlayerSessionResumed::dispatch($user, $game, $presence);

return $playerSession;
if ($user->LastGameID !== $game->id) {
expireRecentlyPlayedGames($user->User);
// TODO deprecated, read from last player_sessions entry where needed
$user->LastGameID = $game->id;
$user->save();
}

// if the session hasn't been updated in the last 10 minutes resume session
if ($playerSession && $timestamp->diffInMinutes($playerSession->rich_presence_updated_at) < 10) {
$playerSession->duration = max(1, $timestamp->diffInMinutes($playerSession->created_at));
if ($presence) {
$playerSession->rich_presence = $presence;

// TODO deprecated, read from last player_sessions entry where needed
$user->RichPresenceMsg = utf8_sanitize($presence);
$user->RichPresenceMsgDate = Carbon::now();
$user->save();
}
$playerSession->rich_presence_updated_at = $timestamp > $playerSession->rich_presence_updated_at ? $timestamp : $playerSession->rich_presence_updated_at;
$playerSession->save(['touch' => true]);

PlayerSessionResumed::dispatch($user, $game, $presence);

return $playerSession;
}

// provide a default presence for the new session if none was provided
if (!$presence) {
$presence = 'Playing ' . $game->title;
}

// TODO deprecated, read from last player_sessions entry where needed
$user->RichPresenceMsg = utf8_sanitize($presence);
$user->RichPresenceMsgDate = Carbon::now();
$user->save();

// create new session
$playerSession = new PlayerSession([
'user_id' => $user->id,
Expand Down
21 changes: 0 additions & 21 deletions public/dorequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,6 @@ function DoRequestError(string $error, ?int $status = 200, ?string $code = null)

PlayerSessionHeartbeat::dispatch($user, Game::find($gameID), $activityMessage);

// legacy rich presence support (deprecated - see ResumePlayerSession)
if (isset($activityMessage) && $user->LastGameID == $gameID) {
$user->RichPresenceMsg = utf8_sanitize($activityMessage);
$user->RichPresenceMsgDate = Carbon::now();
}

$user->LastLogin = Carbon::now();
$user->save();

$response['Success'] = true;
}
break;
Expand Down Expand Up @@ -314,18 +305,6 @@ function DoRequestError(string $error, ?int $status = 200, ?string $code = null)
return DoRequestError("Unknown game");
}

if ($user->LastGameID != $gameID) {
expireRecentlyPlayedGames($user->User);
$user->LastGameID = $gameID;
}

// legacy rich presence support (deprecated - see ResumePlayerSession)
$user->RichPresenceMsg = "Playing {$game->Title}";
$user->RichPresenceMsgDate = Carbon::now();

$user->LastLogin = Carbon::now();
$user->save();

PlayerSessionHeartbeat::dispatch($user, $game);
$response['Success'] = true;
break;
Expand Down
166 changes: 82 additions & 84 deletions tests/Feature/Api/V1/UserSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ public function testGetUserSummary(): void
'ContribYield' => random_int(50, 1000),
'Created' => Carbon::now()->subMonths(2),
'LastLogin' => Carbon::now()->subDays(5),
'LastGameID' => $game->ID,
'RichPresenceMsg' => 'Hi',
]);
/** @var Game $game2 */
$game2 = Game::factory()->create([
Expand Down Expand Up @@ -179,7 +177,7 @@ public function testGetUserSummary(): void
'ContribYield' => $user->ContribYield,
'Rank' => 2,
'TotalRanked' => 2, // $this->user and $user
'LastGameID' => $user->LastGameID,
'LastGameID' => $game->id,
'LastGame' => [
'ID' => $game->ID,
'Title' => $game->Title,
Expand All @@ -195,9 +193,9 @@ public function testGetUserSummary(): void
'Developer' => $game->Developer,
'Genre' => $game->Genre,
'Released' => $game->Released,
'IsFinal' => false,
'IsFinal' => 0,
],
'RichPresenceMsg' => 'Hi',
'RichPresenceMsg' => 'Playing ' . $game->title,
'RecentlyPlayedCount' => 2,
'RecentlyPlayed' => [
[
Expand Down Expand Up @@ -267,91 +265,91 @@ public function testGetUserSummary(): void

// repeat the call, but only ask for one game
$this->get($this->apiUrl('GetUserSummary', ['u' => $user->User, 'g' => 1]))
->assertSuccessful()
->assertJson([
'ID' => $user->ID,
'TotalPoints' => $user->RAPoints,
'TotalSoftcorePoints' => $user->RASoftcorePoints,
'TotalTruePoints' => $user->TrueRAPoints,
'Permissions' => $user->Permissions,
'MemberSince' => $user->Created->__toString(),
'Untracked' => $user->Untracked,
'UserPic' => '/UserPic/' . $user->User . '.png',
'Motto' => $user->Motto,
'UserWallActive' => $user->UserWallActive,
'ContribCount' => $user->ContribCount,
'ContribYield' => $user->ContribYield,
'Rank' => 2,
'TotalRanked' => 2, // $this->user and $user
'LastGameID' => $user->LastGameID,
'LastGame' => [
'ID' => $game->ID,
'Title' => $game->Title,
'ConsoleID' => $system->ID,
'ConsoleName' => $system->Name,
'ForumTopicID' => $game->ForumTopicID,
'Flags' => 0,
'ImageIcon' => $game->ImageIcon,
'ImageTitle' => $game->ImageTitle,
'ImageIngame' => $game->ImageIngame,
'ImageBoxArt' => $game->ImageBoxArt,
'Publisher' => $game->Publisher,
'Developer' => $game->Developer,
'Genre' => $game->Genre,
'Released' => $game->Released,
'IsFinal' => false,
],
'RichPresenceMsg' => 'Hi',
'RecentlyPlayedCount' => 1,
'RecentlyPlayed' => [
[
'GameID' => $game2->ID,
'Title' => $game2->Title,
->assertSuccessful()
->assertJson([
'ID' => $user->ID,
'TotalPoints' => $user->RAPoints,
'TotalSoftcorePoints' => $user->RASoftcorePoints,
'TotalTruePoints' => $user->TrueRAPoints,
'Permissions' => $user->Permissions,
'MemberSince' => $user->Created->__toString(),
'Untracked' => $user->Untracked,
'UserPic' => '/UserPic/' . $user->User . '.png',
'Motto' => $user->Motto,
'UserWallActive' => $user->UserWallActive,
'ContribCount' => $user->ContribCount,
'ContribYield' => $user->ContribYield,
'Rank' => 2,
'TotalRanked' => 2, // $this->user and $user
'LastGameID' => $game->id,
'LastGame' => [
'ID' => $game->ID,
'Title' => $game->Title,
'ConsoleID' => $system->ID,
'ConsoleName' => $system->Name,
'ImageIcon' => $game2->ImageIcon,
'LastPlayed' => $activity2->lastupdate->__toString(),
'ForumTopicID' => $game->ForumTopicID,
'Flags' => 0,
'ImageIcon' => $game->ImageIcon,
'ImageTitle' => $game->ImageTitle,
'ImageIngame' => $game->ImageIngame,
'ImageBoxArt' => $game->ImageBoxArt,
'Publisher' => $game->Publisher,
'Developer' => $game->Developer,
'Genre' => $game->Genre,
'Released' => $game->Released,
'IsFinal' => 0,
],
],
'LastActivity' => [
'ID' => $activity2->ID,
'timestamp' => $activity2->timestamp->__toString(),
'lastupdate' => $activity2->lastupdate->__toString(),
'activitytype' => '3',
'User' => $user->User,
'data' => $game2->ID,
'data2' => null,
],
'Status' => 'Online',
'Awarded' => [
$game->ID => [
'NumPossibleAchievements' => 3,
'PossibleScore' => $publishedAchievements->get(0)->Points +
$publishedAchievements->get(1)->Points +
$publishedAchievements->get(2)->Points,
'NumAchievedHardcore' => 1,
'ScoreAchievedHardcore' => $earnedAchievement->Points,
'NumAchieved' => 1,
'ScoreAchieved' => $earnedAchievement->Points,
'RichPresenceMsg' => 'Playing ' . $game->title,
'RecentlyPlayedCount' => 1,
'RecentlyPlayed' => [
[
'GameID' => $game2->ID,
'Title' => $game2->Title,
'ConsoleID' => $system->ID,
'ConsoleName' => $system->Name,
'ImageIcon' => $game2->ImageIcon,
'LastPlayed' => $activity2->lastupdate->__toString(),
],
],
],
'RecentAchievements' => [
$game->ID => [
$earnedAchievement->ID => [
'ID' => $earnedAchievement->ID,
'Title' => $earnedAchievement->Title,
'Description' => $earnedAchievement->Description,
'Points' => $earnedAchievement->Points,
'BadgeName' => $earnedAchievement->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '1',
'DateAwarded' => $unlockTime->__toString(),
'HardcoreAchieved' => '1',
'LastActivity' => [
'ID' => $activity2->ID,
'timestamp' => $activity2->timestamp->__toString(),
'lastupdate' => $activity2->lastupdate->__toString(),
'activitytype' => '3',
'User' => $user->User,
'data' => $game2->ID,
'data2' => null,
],
'Status' => 'Online',
'Awarded' => [
$game->ID => [
'NumPossibleAchievements' => 3,
'PossibleScore' => $publishedAchievements->get(0)->Points +
$publishedAchievements->get(1)->Points +
$publishedAchievements->get(2)->Points,
'NumAchievedHardcore' => 1,
'ScoreAchievedHardcore' => $earnedAchievement->Points,
'NumAchieved' => 1,
'ScoreAchieved' => $earnedAchievement->Points,
],
],
],
]);
'RecentAchievements' => [
$game->ID => [
$earnedAchievement->ID => [
'ID' => $earnedAchievement->ID,
'Title' => $earnedAchievement->Title,
'Description' => $earnedAchievement->Description,
'Points' => $earnedAchievement->Points,
'BadgeName' => $earnedAchievement->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '1',
'DateAwarded' => $unlockTime->__toString(),
'HardcoreAchieved' => '1',
],
],
],
]);
}

public function testGetUserSummaryLimitRecentAchievements(): void
Expand Down

0 comments on commit 66a52b0

Please sign in to comment.