Skip to content

Commit a18e1e4

Browse files
committed
add recentgames, fix some status things
1 parent f553c86 commit a18e1e4

14 files changed

+180
-7
lines changed

src/HypixelPHP.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Plancke\HypixelPHP\responses\Leaderboards;
3030
use Plancke\HypixelPHP\responses\player\Player;
3131
use Plancke\HypixelPHP\responses\PlayerCount;
32+
use Plancke\HypixelPHP\responses\RecentGames;
3233
use Plancke\HypixelPHP\responses\Resource;
3334
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
3435
use Plancke\HypixelPHP\responses\Status;
@@ -375,6 +376,7 @@ protected function handle($cached, $responseSupplier, $constructor) {
375376
}
376377
}
377378
} catch (Exception $exception) {
379+
$this->getLogger()->log(LOG_ERR, $exception->getMessage());
378380
$this->getLogger()->log(LOG_ERR, $exception->getTraceAsString());
379381
}
380382

@@ -496,6 +498,35 @@ function () use ($key, $val) {
496498
return null;
497499
}
498500

501+
/**
502+
* @param array $pairs
503+
* @return null|Response|RecentGames
504+
* @throws HypixelPHPException
505+
*/
506+
public function getRecentGames($pairs = []) {
507+
$this->checkPairs($pairs);
508+
509+
foreach ($pairs as $key => $val) {
510+
if ($val == null || $val == '') continue;
511+
512+
if ($key == FetchParams::RECENT_GAMES_BY_UUID) {
513+
if (InputType::getType($val) !== InputType::UUID) {
514+
throw new InvalidUUIDException($val);
515+
}
516+
$val = Utilities::ensureNoDashesUUID($val);
517+
518+
return $this->handle(
519+
$this->getCacheHandler()->getRecentGames((string)$val),
520+
function () use ($key, $val) {
521+
return $this->getFetcher()->fetch(FetchTypes::RECENT_GAMES, ['key' => $this->getAPIKey(), $key => $val]);
522+
},
523+
$this->getProvider()->getRecentGames()
524+
);
525+
}
526+
}
527+
return null;
528+
}
529+
499530
/**
500531
* @param array $pairs
501532
* @return null|Response|Friends

src/cache/CacheHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Plancke\HypixelPHP\responses\Leaderboards;
1515
use Plancke\HypixelPHP\responses\player\Player;
1616
use Plancke\HypixelPHP\responses\PlayerCount;
17+
use Plancke\HypixelPHP\responses\RecentGames;
1718
use Plancke\HypixelPHP\responses\Resource;
1819
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
1920
use Plancke\HypixelPHP\responses\Status;
@@ -193,6 +194,18 @@ public abstract function getStatus($uuid);
193194
*/
194195
public abstract function setStatus(Status $status);
195196

197+
/**
198+
* @param $uuid
199+
* @return RecentGames|null
200+
*/
201+
public abstract function getRecentGames($uuid);
202+
203+
/**
204+
* @param RecentGames $recentGames
205+
* @return void
206+
*/
207+
public abstract function setRecentGames(RecentGames $recentGames);
208+
196209
/**
197210
* @param $key
198211
* @return KeyInfo|null

src/cache/CacheTimes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class CacheTimes {
1919
const PLAYER_COUNT = 'playerCount';
2020
const BOOSTERS = 'boosters';
2121
const STATUS = 'status';
22+
const RECENT_GAMES = 'recentGames';
2223
const KEY_INFO = 'keyInfo';
2324
const FRIENDS = 'friends';
2425
const WATCHDOG = 'watchdog';

src/cache/CacheTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ abstract class CacheTypes {
2121

2222
const STATUS = 'status';
2323
const API_KEYS = 'api_keys';
24+
const RECENT_GAMES = 'recent_games';
2425

2526
const SKYBLOCK_PROFILES = 'skyblock_profiles';
2627

src/cache/impl/FlatFileCacheHandler.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Plancke\HypixelPHP\responses\Leaderboards;
1515
use Plancke\HypixelPHP\responses\player\Player;
1616
use Plancke\HypixelPHP\responses\PlayerCount;
17+
use Plancke\HypixelPHP\responses\RecentGames;
1718
use Plancke\HypixelPHP\responses\Resource;
1819
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
1920
use Plancke\HypixelPHP\responses\Status;
@@ -247,6 +248,25 @@ public function setStatus(Status $status) {
247248
$this->_setCache(CacheTypes::STATUS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($status->getUUID()), $status);
248249
}
249250

251+
/**
252+
* @param $uuid
253+
* @return RecentGames|null
254+
*/
255+
public function getRecentGames($uuid) {
256+
return $this->wrapProvider(
257+
$this->getHypixelPHP()->getProvider()->getRecentGames(),
258+
$this->_getCache(CacheTypes::RECENT_GAMES . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid))
259+
);
260+
}
261+
262+
/**
263+
* @param RecentGames $recentGames
264+
* @throws InvalidArgumentException
265+
*/
266+
public function setRecentGames(RecentGames $recentGames) {
267+
$this->_setCache(CacheTypes::STATUS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($recentGames->getUUID()), $recentGames);
268+
}
269+
250270
/**
251271
* @param $key
252272
* @return KeyInfo|null

src/cache/impl/MongoCacheHandler.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace Plancke\HypixelPHP\cache\impl;
44

5+
use InvalidArgumentException;
56
use MongoDB\Client;
67
use MongoDB\Database;
78
use Plancke\HypixelPHP\cache\CacheTimes;
89
use Plancke\HypixelPHP\cache\CacheTypes;
9-
use Plancke\HypixelPHP\exceptions\InvalidArgumentException;
1010
use Plancke\HypixelPHP\HypixelPHP;
1111
use Plancke\HypixelPHP\responses\friend\Friends;
1212
use Plancke\HypixelPHP\responses\guild\Guild;
1313
use Plancke\HypixelPHP\responses\KeyInfo;
1414
use Plancke\HypixelPHP\responses\player\Player;
15+
use Plancke\HypixelPHP\responses\RecentGames;
1516
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
1617
use Plancke\HypixelPHP\responses\Status;
1718
use Plancke\HypixelPHP\util\CacheUtil;
@@ -67,6 +68,7 @@ public function ensureIndexes() {
6768
$db->selectCollection(CacheTypes::FRIENDS)->createIndex(['record.uuid' => 1], ['background' => true]);
6869

6970
$db->selectCollection(CacheTypes::STATUS)->createIndex(['record.uuid' => 1], ['background' => true]);
71+
$db->selectCollection(CacheTypes::RECENT_GAMES)->createIndex(['record.uuid' => 1], ['background' => true]);
7072

7173
$db->selectCollection(CacheTypes::SKYBLOCK_PROFILES)->createIndex(['record.profile_id' => 1], ['background' => true]);
7274

@@ -334,6 +336,29 @@ public function setStatus(Status $status) {
334336
);
335337
}
336338

339+
/**
340+
* @param $uuid
341+
* @return RecentGames|null
342+
*/
343+
public function getRecentGames($uuid) {
344+
return $this->wrapProvider(
345+
$this->getHypixelPHP()->getProvider()->getRecentGames(),
346+
$this->selectDB()->selectCollection(CacheTypes::RECENT_GAMES)->findOne(
347+
['record.uuid' => (string)$uuid], self::FIND_OPTIONS
348+
)
349+
);
350+
}
351+
352+
/**
353+
* @param RecentGames $recentGames
354+
* @throws InvalidArgumentException
355+
*/
356+
public function setRecentGames(RecentGames $recentGames) {
357+
$this->selectDB()->selectCollection(CacheTypes::RECENT_GAMES)->replaceOne(
358+
['record.uuid' => (string)$recentGames->getUUID()], $this->objToArray($recentGames), self::UPDATE_OPTIONS
359+
);
360+
}
361+
337362
/**
338363
* @param $key
339364
* @return KeyInfo|null

src/cache/impl/NoCacheHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Plancke\HypixelPHP\responses\Leaderboards;
1212
use Plancke\HypixelPHP\responses\player\Player;
1313
use Plancke\HypixelPHP\responses\PlayerCount;
14-
use Plancke\HypixelPHP\responses\skyblock\SkyBlockAuctions;
14+
use Plancke\HypixelPHP\responses\RecentGames;
1515
use Plancke\HypixelPHP\responses\skyblock\SkyBlockProfile;
1616
use Plancke\HypixelPHP\responses\Status;
1717
use Plancke\HypixelPHP\responses\WatchdogStats;
@@ -75,6 +75,13 @@ function getStatus($uuid) {
7575
return null;
7676
}
7777

78+
public function getRecentGames($uuid) {
79+
return null;
80+
}
81+
82+
public function setRecentGames(RecentGames $recentGames) {
83+
}
84+
7885
function setKeyInfo(KeyInfo $keyInfo) {
7986
}
8087

src/fetch/FetchParams.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class FetchParams {
1919

2020
const FRIENDS_BY_UUID = 'uuid';
2121
const STATUS_BY_UUID = 'uuid';
22+
const RECENT_GAMES_BY_UUID = 'uuid';
2223

2324
/**
2425
* @return array
@@ -37,6 +38,7 @@ public static function values() {
3738

3839
self::FRIENDS_BY_UUID,
3940
self::STATUS_BY_UUID,
41+
self::RECENT_GAMES_BY_UUID,
4042
];
4143
}
4244
}

src/fetch/FetchTypes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class FetchTypes {
1919
const BOOSTERS = 'boosters';
2020
const LEADERBOARDS = 'leaderboards';
2121
const STATUS = 'status';
22+
const RECENT_GAMES = 'recentGames';
2223
const KEY = 'key';
2324
const WATCHDOG_STATS = 'watchdogStats';
2425
const PLAYER_COUNT = 'playerCount';
@@ -35,6 +36,7 @@ public static function values() {
3536
self::BOOSTERS,
3637
self::LEADERBOARDS,
3738
self::STATUS,
39+
self::RECENT_GAMES,
3840
self::KEY,
3941
self::WATCHDOG_STATS,
4042
self::PLAYER_COUNT,

src/fetch/adapter/ResponseAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function adaptResponse($fetch, $keyValues, Response $response) {
3838
case FetchTypes::FRIENDS:
3939
return $this->attachKeyValues($keyValues, $this->wrapRecord($response->setData(['list' => $response->getData()['records']])));
4040
case FetchTypes::STATUS:
41-
return $this->attachKeyValues($keyValues, $this->remapField('status', $response));
41+
return $this->attachKeyValues($keyValues, $this->remapField('session', $response));
42+
case FetchTypes::RECENT_GAMES:
43+
return $this->attachKeyValues($keyValues, $this->wrapRecord($response));
4244
case FetchTypes::SKYBLOCK_PROFILE:
4345
return $this->remapField('profile', $response);
4446

@@ -83,6 +85,7 @@ protected function attachKeyValues($keyValues, Response $response) {
8385
$data = $response->getData();
8486
if (array_key_exists('record', $data) && is_array($data['record'])) {
8587
$data['record'] = array_merge($data['record'], $keyValues);
88+
unset($data['record']['key']);
8689
}
8790
return $response->setData($data);
8891
}

0 commit comments

Comments
 (0)