<?php
namespace StylesFactory\UltimateServerList\Widget;
use XF\Widget\AbstractWidget;
class Server extends AbstractWidget
{
private function query($servers) {
$data_string = json_encode($servers);
$ch = curl_init('https://cache.stylesfactory.pl/v1/servers/check');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($result, true);
return is_array($decoded) ? $decoded : [];
}
public function render()
{
$db = \XF::db();
$db_results = $db->fetchAllKeyed('SELECT * FROM xf_server_list ORDER BY display_order ASC', 'server_host');
$servers = [];
$params = [];
foreach ($db_results as $entry) {
$params[] = ['type'=> $entry['server_type'],'host'=> $entry['server_host']];
}
$results = $this->query($params);
foreach ($results as $key => $value) {
$servers[] = array(
'title' => $db_results[$key]['title'],
'owner' => $db_results[$key]['owner'],
'maxplayers' => (trim($db_results[$key]['maxplayers']) !== '') ? $db_results[$key]['maxplayers'] : $value['maxplayers'],
'active' => $db_results[$key]['active'],
'type' => $db_results[$key]['server_type'],
'host' => $key,
'query' => $value
);
}
$ServerMag = $this->MagazineS();
$ServerListing = $ServerMag->ListujMnie();
$viewParams = [
'servers' => $servers,
'rozpisuj' => $results,
];
return $this->renderer('sf_server_list', $viewParams);
}
protected function MagazineS()
{
return $this->repository('StylesFactory\UltimateServerList:Servers');
}
}