Getting matches from cache (1 month old) #139

Merged
OpzekerIT merged 10 commits from dev into main 2023-11-22 14:33:00 +00:00
2 changed files with 70 additions and 40 deletions

View file

@ -9,10 +9,10 @@ $ogDescription = "Dive into the detailed match stats of DTCH Clan in PUBG. Explo
<body> <body>
<?php <?php
include './includes/navigation.php'; include './includes/navigation.php';
include './includes/header.php'; include './includes/header.php';
?> ?>
<main> <main>
<section> <section>
@ -20,18 +20,18 @@ $ogDescription = "Dive into the detailed match stats of DTCH Clan in PUBG. Explo
<?php <?php
include './config/config.php'; include './config/config.php';
$players_matches = json_decode(file_get_contents('./data/player_matches.json'), true); $players_matches = json_decode(file_get_contents('./data/cached_matches.json'), true);
$players = json_decode(file_get_contents('./config/clanmembers.json'), true);
// Display buttons for each player // Display buttons for each player
echo "<form method='get' action=''>"; echo "<form method='get' action=''>";
foreach ($players_matches as $player_data) { foreach ($players['clanMembers'] as $player) {
if (isset($player_data['playername'])) {
$player_name = $player_data['playername']; echo "<button type='submit' name='selected_player' value='$player' class='btn'>$player</button>";
echo "<button type='submit' name='selected_player' value='$player_name' class='btn'>$player_name</button>";
}
} }
echo "</form><br>"; echo "</form><br>";
$selected_player = $_GET['selected_player'] ?? $players_matches[0]['playername']; $selected_player = $_GET['selected_player'] ?? $players[0];
echo "<form method='get' action=''> echo "<form method='get' action=''>
<input type='submit' name='filter_by_match_type' value='all' class='btn'> <input type='submit' name='filter_by_match_type' value='all' class='btn'>
<input type='submit' name='filter_by_match_type' value='airoyale' class='btn'> <input type='submit' name='filter_by_match_type' value='airoyale' class='btn'>
@ -57,32 +57,30 @@ $ogDescription = "Dive into the detailed match stats of DTCH Clan in PUBG. Explo
"Tiger_Main" => "Taego" "Tiger_Main" => "Taego"
); );
// Display the player's match stats // Display the player's match stats
foreach ($players_matches as $player_data) { echo "<h2>Recent Matches for $selected_player</h2>";
if (isset($player_data['playername']) && $player_data['playername'] === $selected_player) { echo "<table border='1' class='sortable'>";
echo "<tr><th>Match Date</th><th>Game Mode</th><th>Match Type</th><th>Map</th><th>Kills</th><th>Damage Dealt</th><th>Time Survived</th><th>win Place</th></tr>";
echo "<h2>Recent Matches for $selected_player</h2>"; foreach ($players_matches as $match) {
echo "<table border='1' class='sortable'>"; if ($match['stats']['name'] === $selected_player) {
echo "<tr><th>Match Date</th><th>Game Mode</th><th>Match Type</th><th>Map</th><th>Kills</th><th>Damage Dealt</th><th>Time Survived</th><th>win Place</th></tr>";
foreach ($player_data['player_matches'] as $match) {
if (isset($_GET['filter_by_match_type'])) {
if ($_GET['filter_by_match_type'] !== 'all' && $match['matchType'] !== $_GET['filter_by_match_type']) {
continue;
}
if (isset($_GET['filter_by_match_type'])) {
if ($_GET['filter_by_match_type'] !== 'all' && $match['matchType'] !== $_GET['filter_by_match_type']) {
continue;
} }
$date = new DateTime($match['createdAt']); }
$date->modify('+1 hours'); $date = new DateTime($match['createdAt']);
$formattedDate = $date->format('m-d H:i:s'); $date->modify('+1 hours');
$formattedDate = $date->format('m-d H:i:s');
$matchType = $match['matchType']; $matchType = $match['matchType'];
$gameMode = $match['gameMode']; $gameMode = $match['gameMode'];
$mapName = isset($mapNames[$match['mapName']]) ? $mapNames[$match['mapName']] : $match['mapName']; $mapName = isset($mapNames[$match['mapName']]) ? $mapNames[$match['mapName']] : $match['mapName'];
$kills = $match['stats']['kills']; $kills = $match['stats']['kills'];
$damage = number_format($match['stats']['damageDealt'], 0, '.', ''); $damage = number_format($match['stats']['damageDealt'], 0, '.', '');
$timeSurvived = $match['stats']['timeSurvived']; $timeSurvived = $match['stats']['timeSurvived'];
$winPlace = $match['stats']['winPlace']; $winPlace = $match['stats']['winPlace'];
echo "<tr> echo "<tr>
<td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $formattedDate . "</a></td> <td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $formattedDate . "</a></td>
<td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $gameMode . "</a></td> <td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $gameMode . "</a></td>
<td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $matchType . "</a></td> <td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $matchType . "</a></td>
@ -93,11 +91,13 @@ $ogDescription = "Dive into the detailed match stats of DTCH Clan in PUBG. Explo
<td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $winPlace . "</a></td> <td><a href='matchinfo.php?matchid=" . $match['id'] . "'>" . $winPlace . "</a></td>
</tr>"; </tr>";
}
echo "</table><br>";
} }
} }
echo "</table><br>";
?> ?>
</section> </section>
</main> </main>

View file

@ -12,7 +12,7 @@ new-lock -by "get_matches"
# Read the content of the file as a single string # Read the content of the file as a single string
$fileContent = Get-Content -Path "$scriptroot/../config/config.php" -Raw $fileContent = Get-Content -Path "$scriptroot/../config/config.php" -Raw
$players = (Get-Content -Path "$scriptroot/../config/clanmembers.json" | ConvertFrom-Json).clanmembers
# Use regex to match the apiKey value # Use regex to match the apiKey value
if ($fileContent -match "\`$apiKey\s*=\s*\'([^\']+)\'") { if ($fileContent -match "\`$apiKey\s*=\s*\'([^\']+)\'") {
$apiKey = $matches[1] $apiKey = $matches[1]
@ -34,10 +34,11 @@ foreach ($player in $player_data) {
$playermatches = @() $playermatches = @()
foreach ($match in $lastMatches) { foreach ($match in $lastMatches) {
Write-Host "Getting match for $($player.attributes.name) match: $match " Write-Host "Getting match for $($player.attributes.name) match: $match "
if(Test-Path "$scriptroot/../data/matches/$match.json"){ if (Test-Path "$scriptroot/../data/matches/$match.json") {
write-output "Getting $match from cache" write-output "Getting $match from cache"
$stats = get-content "$scriptroot/../data/matches/$match.json" | convertfrom-json $stats = get-content "$scriptroot/../data/matches/$match.json" | convertfrom-json
}else{ }
else {
$stats = Invoke-RestMethod -Uri "https://api.pubg.com/shards/steam/matches/$match" -Method GET -Headers $headers $stats = Invoke-RestMethod -Uri "https://api.pubg.com/shards/steam/matches/$match" -Method GET -Headers $headers
$stats | ConvertTo-Json -Depth 100 | Out-File "$scriptroot/../data/matches/$match.json" $stats | ConvertTo-Json -Depth 100 | Out-File "$scriptroot/../data/matches/$match.json"
} }
@ -66,8 +67,8 @@ foreach ($player in $player_data) {
if (test-path "$scriptroot/../data/player_matches.json") { if (test-path "$scriptroot/../data/player_matches.json") {
$old_player_data = get-content "$scriptroot/../data/player_matches.json" | convertfrom-json -Depth 100 $old_player_data = get-content "$scriptroot/../data/player_matches.json" | convertfrom-json -Depth 100
$new_ids = ($player_matches.player_matches | where-object {$_.stats.winplace -eq 1}).id $new_ids = ($player_matches.player_matches | where-object { $_.stats.winplace -eq 1 }).id
$old_ids = ($old_player_data.player_matches | where-object {$_.stats.winplace -eq 1}).id $old_ids = ($old_player_data.player_matches | where-object { $_.stats.winplace -eq 1 }).id
$new_win_matches = ((Compare-Object -ReferenceObject $old_ids -DifferenceObject $new_ids) | Where-Object { $_.SideIndicator -eq '=>' }).InputObject | Select-Object -Unique $new_win_matches = ((Compare-Object -ReferenceObject $old_ids -DifferenceObject $new_ids) | Where-Object { $_.SideIndicator -eq '=>' }).InputObject | Select-Object -Unique
$new_win_matches = $old_player_data.new_win_matches + $new_win_matches | Select-Object -Unique $new_win_matches = $old_player_data.new_win_matches + $new_win_matches | Select-Object -Unique
$player_matches += [PSCustomObject]@{ $player_matches += [PSCustomObject]@{
@ -76,8 +77,6 @@ if (test-path "$scriptroot/../data/player_matches.json") {
} }
$currentDateTime = Get-Date $currentDateTime = Get-Date
# Get current timezone # Get current timezone
@ -92,5 +91,36 @@ $playermatches += [PSCustomObject]@{
$player_matches | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_matches.json" $player_matches | convertto-json -Depth 100 | out-file "$scriptroot/../data/player_matches.json"
write-output 'Cleaning matches'
$matchfiles = Get-ChildItem "$scriptroot/../data/matches" -Filter *.json
$player_matches_object = @()
foreach ($file in $matchfiles) {
$filecontent = get-content $file | convertfrom-json
$matchfiledate = $filecontent.data.attributes.createdAt
if ($matchfiledate -lt (get-date).AddMonths(-1)) {
write-output "archiving $matchfiledate"
Move-Item -Path $file -Destination "$scriptroot/../data/matches/archive"
}
else {
$result = ($filecontent.included | where-object { $_.type -eq 'participant' } | Where-Object { $players -contains $_.attributes.stats.name })
$filecontent.data.id
$result.count
$player_matches_cached = ($filecontent.included | where-object { $_.type -eq 'participant' } | Where-Object { $players -contains $_.attributes.stats.name }).attributes.stats
if ($null -ne $player_matches_cached) {
$player_matches_object += [PSCustomObject]@{
matchType = $filecontent.data.attributes.matchType
gameMode = $filecontent.data.attributes.gameMode
createdAt = $filecontent.data.attributes.createdAt
mapName = $filecontent.data.attributes.mapName
id = $filecontent.data.id
stats = $player_matches_cached
}
}
write-output "NEW $matchfiledate"
}
}
$player_matches_object | convertto-json -Depth 100 | out-file "$scriptroot/../data/cached_matches.json"
remove-lock remove-lock
Stop-Transcript Stop-Transcript