Passed
Push — master ( 2405d0...6f7c46 )
by Mattia
03:35
created

CheckUuid::updateAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Console\Commands;
6
7
use Carbon\Carbon;
8
use Illuminate\Console\Command;
9
use Minepic\Helpers\Storage\Files\SkinsStorage;
10
use Minepic\Minecraft\MojangClient;
11
use Minepic\Models\Account;
12
13
class CheckUuid extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'minepic:check-uuid';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Check old uuid.';
28
29
    /** @var MojangClient */
30
    private MojangClient $mojangClient;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @param MojangClient $mojangClient
36
     *
37
     * @throws \Throwable
38
     *
39
     * @return int
40
     */
41
    public function handle(MojangClient $mojangClient): int
42
    {
43
        $this->mojangClient = $mojangClient;
44
        $this->info('Selecting old uuid...');
45
46
        $results = $this->getAccountsIds();
47
48
        foreach ($results as $result) {
49
            /** @var \Minepic\Models\Account $account */
50
            $account = Account::find($result->id);
51
            $this->info("Checking {$account->username} [{$account->uuid}]...");
52
            try {
53
                $this->updateAccount($account);
54
                $this->updateAccountSkin($account);
55
            } catch (\Exception $e) {
56
                ++$account->fail_count;
57
                $account->update([
58
                    'fail_count' => $account->fail_count,
59
                ]);
60
                $this->warn("\tFailed. Fail count: {$account->fail_count}");
61
                if ($account->fail_count > 10) {
62
                    $account->stats()->delete();
63
                    $account->delete();
64
                    $this->error("\tDELETED {$account->uuid}!");
65
                } else {
66
                    $account->save();
67
                }
68
            }
69
            $this->line('################################################');
70
        }
71
72
        return 0;
73
    }
74
75
    /**
76
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|Account[]
77
     */
78
    private function getAccountsIds()
79
    {
80
        return Account::query()
81
            ->select(['id'])
82
            ->whereDate('updated_at', '<', Carbon::now()->subDays(28)->toDateTimeString())
83
            ->orderBy('updated_at', 'ASC')
84
            ->take(300)
85
            ->get();
86
    }
87
88
    /**
89
     * @param Account $account
90
     *
91
     * @throws \Throwable
92
     */
93
    private function updateAccount(Account $account)
94
    {
95
        $accountApiData = $this->mojangClient->getUuidInfo($account->uuid);
96
        $this->info("\tUUID Valid");
97
98
        // Update database
99
        $account->update([
100
            'username' => $accountApiData->getUsername(),
101
            'skin' => $accountApiData->getSkin(),
102
            'cape' => $accountApiData->getCape(),
103
            'fail_count' => 0,
104
        ]);
105
        $account->refresh();
106
        $this->info("\tData updated");
107
    }
108
109
    /**
110
     * @param Account $account
111
     *
112
     * @throws \Throwable
113
     */
114
    private function updateAccountSkin(Account $account)
115
    {
116
        try {
117
            $skinData = $this->mojangClient->getSkin($account->skin);
118
            SkinsStorage::save($account->uuid, $skinData);
119
            $this->info("\tSkin png updated");
120
        } catch (\Exception $e) {
121
            SkinsStorage::copyAsSteve($account->uuid);
122
            $this->error("\tUsing Steve as skin");
123
            $this->error("\t".$e->getMessage());
124
        }
125
    }
126
}
127