You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.4 KiB
PHP
131 lines
3.4 KiB
PHP
<?php
|
|
require_once 'php/servers/servers-rooms.php';
|
|
require_once 'php/utils/utils.php';
|
|
require_once 'php/servers/room-listings-api.php';
|
|
|
|
/**
|
|
* Filters Session Communities.
|
|
*/
|
|
class RoomSieve {
|
|
/**
|
|
* @var CommunityRoom[] $rooms;
|
|
*/
|
|
private array $rooms;
|
|
|
|
/**
|
|
* @var CommunityRoom[] $stickied
|
|
*/
|
|
private array $stickies;
|
|
|
|
/**
|
|
* @param CommunityRoom[] $rooms
|
|
* @param CommunityRoom[] $stickied
|
|
*/
|
|
private function __construct(array $rooms, array $stickied = []) {
|
|
$this->rooms = $rooms;
|
|
$this->stickies = $stickied;
|
|
}
|
|
|
|
public const TOP_DEFAULT = 35;
|
|
|
|
/**
|
|
* @param CommunityRoom[] $rooms
|
|
*/
|
|
public static function takeRooms(array $rooms) {
|
|
return new RoomSieve($rooms);
|
|
}
|
|
|
|
public function saveStickies() {
|
|
$stickied = CommunityRoom::get_stickied_rooms($this->rooms, $rest);
|
|
$rooms = $rest;
|
|
return $this->cloneWith($rooms, $stickied);
|
|
}
|
|
|
|
/**
|
|
* @param CommunityRoom[] $rooms
|
|
*/
|
|
public function addRooms(array $rooms) {
|
|
return $this->cloneWith(array_merge($this->rooms, $rooms));
|
|
}
|
|
|
|
public function apply(Closure $filter) {
|
|
return $this->cloneWith($filter($this->rooms));
|
|
}
|
|
|
|
public function getWithStickies() {
|
|
return [...$this->stickies, ...$this->rooms];
|
|
}
|
|
|
|
public function getWithoutStickies() {
|
|
return $this->saveStickies()->getRooms();
|
|
}
|
|
|
|
public function getRooms() {
|
|
return $this->rooms;
|
|
}
|
|
|
|
public function onlyTop(int $count = RoomSieve::TOP_DEFAULT) {
|
|
$rooms = $this->rooms;
|
|
CommunityRoom::sort_rooms_num($rooms, 'active_users', reverse: true);
|
|
return $this->cloneWith(array_slice($rooms, 0, $count));
|
|
}
|
|
|
|
public function exceptTop(int $count = RoomSieve::TOP_DEFAULT) {
|
|
$rooms = $this->rooms;
|
|
CommunityRoom::sort_rooms_num($rooms, 'active_users', reverse: true);
|
|
return $this->cloneWith(array_slice($rooms, $count));
|
|
}
|
|
|
|
private static function isIndexApproved(CommunityRoom $room): bool {
|
|
return (
|
|
!$room->rated_nsfw() &&
|
|
$room->write &&
|
|
!$room->has_poor_staff_rating() &&
|
|
!empty($room->description)
|
|
);
|
|
}
|
|
|
|
public function applyStandardSort() {
|
|
$rooms = $this->rooms;
|
|
CommunityRoom::sort_rooms_str($rooms, 'name');
|
|
CommunityRoom::sort_rooms_by_server($rooms);
|
|
return new RoomSieve($rooms, $this->stickies);
|
|
}
|
|
|
|
public function applyPreferentialSort() {
|
|
$rooms = $this->rooms;
|
|
CommunityRoom::sort_rooms_num($rooms,'created');
|
|
usort($rooms, function($a, $b) {
|
|
return empty($b->description) - empty($a->description);
|
|
});
|
|
usort($rooms, function(CommunityRoom $a, CommunityRoom $b) {
|
|
return sign($a->get_numeric_staff_rating() - $b->get_numeric_staff_rating());
|
|
});
|
|
return new RoomSieve(array_reverse($rooms), $this->stickies);
|
|
}
|
|
|
|
public function indexApproved() {
|
|
$rooms = array_values(array_filter($this->rooms, function($room) {
|
|
return RoomSieve::isIndexApproved($room);
|
|
}));
|
|
return new RoomSieve($rooms, $this->stickies);
|
|
}
|
|
|
|
public function indexNonApproved() {
|
|
$rooms = array_values(array_filter($this->rooms, function($room) {
|
|
return !RoomSieve::isIndexApproved($room);
|
|
}));
|
|
return new RoomSieve($rooms, $this->stickies);
|
|
}
|
|
|
|
private function clone(): self {
|
|
return $this->cloneWith();
|
|
}
|
|
|
|
private function cloneWith($rooms = null, $stickies = null): self {
|
|
$clone = new RoomSieve($rooms ?? $this->rooms, $stickies ?? $this->stickies);
|
|
return $clone;
|
|
}
|
|
}
|
|
?>
|