Skip to content

Commit

Permalink
Merge pull request #959 from proditis/master
Browse files Browse the repository at this point in the history
Regenerate the player badge when there are updates to the player
  • Loading branch information
proditis authored Aug 15, 2023
2 parents 3931abd + 230adf2 commit 7ef47db
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
10 changes: 6 additions & 4 deletions docs/console-commands/Frontend_Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ Generate profile badge images for all active players.
Usage: `./frontend/yii generator/all-badges [owner]`
* `owner` optional user id to change the ownership of the generated images

## badges($owner=0)
Update the badges for players that had activity during the past 26 hours
## badges($owner=0,$interval=86400,$limit=200)
Update the badges for players that had activity during the past 24 hours

Usage: `./frontend/yii generator/badges [owner]`
Usage: `./frontend/yii generator/badges [owner] [interval] [limit]`

* `owner` optional user id to change the ownership of the generated images
* `owner` optional local user to change the ownership of the generated image files (default uid 0)
* `interval` optional time in seconds to check that the badges are older than (default older than 86400 seconds)
* `limit` limit the operation to this number of players (default 200)

## urls($domain)
Generate all available registered URL routes on the system for easier testing of the activated endpoints.
Expand Down
26 changes: 14 additions & 12 deletions frontend/commands/GeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,33 +288,35 @@ public function actionAllBadges($owner=0)
}
}

public function actionBadges($owner=0, $interval=1440)
public function actionBadges($owner=0, $interval=86400,$limit=200)
{
$streamPlayers=Stream::find()->select(['player_id'])->distinct()->where(['>=','ts',new \yii\db\Expression('NOW() - INTERVAL '.$interval.' MINUTE')]);
foreach($streamPlayers->all() as $item)
$players=Player::find()->active();
$processed=0;
foreach($players->all() as $item)
{
echo "Processing user: ",$item->player->username;
$avatarPath=\Yii::getAlias('@app/web/images/avatars').'/'.$item->player->profile->id.'.png';
if(file_exists($avatarPath))
$avatarPath=\Yii::getAlias('@app/web/images/avatars').'/'.$item->profile->id.'.png';
$path=\Yii::getAlias('@app/web/images/avatars/badges/').$item->profile->id.'.png';
if($processed>intval($limit))
break;
if(file_exists($avatarPath) && (file_exists($path) && filemtime($path)<(time()-intval($interval))))
{
$image=\app\components\Img::profile($item->player->profile);
$processed++;
echo "Processing user: ",$item->username;
$image=\app\components\Img::profile($item->profile);
if($image!==false)
{
$path=\Yii::getAlias('@app/web/images/avatars/badges/').$item->player->profile->id.'.png';
try {
imagepng($image,$path);
chown($path, $owner);
imagedestroy($image);
echo " badge generated\n";

} catch (\Exception $e) {
echo " failed avatar [",$avatarPath,"] not found\n";
echo " ",$e->getMessage(),"\n";
}
}
}
else
{
echo " failed avatar [",$avatarPath,"] not found\n";
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions frontend/controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,23 @@ public function actionBadge(int $id)
->set('Content-type', 'image/png');

Yii::$app->response->format = Response::FORMAT_RAW;
if(file_exists(\Yii::getAlias('@app/web/images/avatars/badges/').'/'.$profile->id.'.png'))
$playerBadgeFname=\Yii::getAlias('@app/web/images/avatars/badges/').'/'.$profile->id.'.png';
if(file_exists($playerBadgeFname) && filemtime($playerBadgeFname)>(time()-86400))
{
return file_get_contents(\Yii::getAlias('@app/web/images/avatars/badges/').'/'.$profile->id.'.png');
return file_get_contents($playerBadgeFname);
}
// Clear file cache since we are about to generate the file
clearstatcache(true,$playerBadgeFname);

try {
$image=\app\components\Img::profile($profile);

if($image==false)
return $this->redirect(['/']);


ob_start();
imagepng($image);
imagepng($image,\Yii::getAlias('@app/web/images/avatars/badges/').'/'.$profile->id.'.png');
imagepng($image,$playerBadgeFname);
imagedestroy($image);
return ob_get_clean();
} catch (\Exception $e) {
Expand Down Expand Up @@ -251,6 +254,7 @@ public function actionSettings()
$settingsForm->uploadedAvatar->saveAs($fname);
$settingsForm->uploadedAvatar=null;
}
$profile->genBadge();
$settingsForm->save();
$settingsForm->reset();
}
Expand Down
16 changes: 16 additions & 0 deletions frontend/models/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,20 @@ public function metric($key)
->bindValue(':metric',$key)
->queryScalar());
}

public function genBadge()
{
try {
$image=\app\components\Img::profile($this);

if($image==false)
return false;

imagepng($image,\Yii::getAlias('@app/web/images/avatars/badges/').'/'.$this->id.'.png');
imagedestroy($image);
} catch (\Exception $e) {
return false;
}
return true;
}
}

0 comments on commit 7ef47db

Please sign in to comment.