diff --git a/lib/Nitrapi/Order/Pricing/DimensionPricing.php b/lib/Nitrapi/Order/Pricing/DimensionPricing.php index 8ee51fc..201b16a 100644 --- a/lib/Nitrapi/Order/Pricing/DimensionPricing.php +++ b/lib/Nitrapi/Order/Pricing/DimensionPricing.php @@ -48,7 +48,16 @@ public function getPrice($rentalTime, Service &$service = null) { } if (is_array($prices) && isset($prices['price'])) { - return (int)$prices['price']; + $price = (int)$prices['price']; + $advice = $information['advice']; + + if ($advice > $price) { + $advice -= (($advice - $price) * (50.0 / 100)); + } + + $price -= $advice; + + return $price; } throw new PricingException("No price for selected dimensions not found."); diff --git a/lib/Nitrapi/Order/Pricing/PartPricing.php b/lib/Nitrapi/Order/Pricing/PartPricing.php index 95610d7..2df189b 100644 --- a/lib/Nitrapi/Order/Pricing/PartPricing.php +++ b/lib/Nitrapi/Order/Pricing/PartPricing.php @@ -59,6 +59,9 @@ public function getPrice($rentalTime, Service &$service = null) { $totalPrice += $bestPrice; } + //100% + $totalPrice -= $prices['advice']; + return $totalPrice; } diff --git a/lib/Nitrapi/Order/Pricing/Pricing.php b/lib/Nitrapi/Order/Pricing/Pricing.php index 4f2b495..0f89a87 100644 --- a/lib/Nitrapi/Order/Pricing/Pricing.php +++ b/lib/Nitrapi/Order/Pricing/Pricing.php @@ -21,6 +21,13 @@ abstract class Pricing implements PricingInterface { */ protected $prices = null; + /** + * Currency for price calculation + * + * @var array + */ + protected $currency = null; + /** * Will be overwritten by parents */ @@ -40,6 +47,15 @@ public function __construct(Nitrapi &$nitrapi, Location $location) $this->locationId = $location->getId(); } + /** + * This can be used only for pricing information + * + * @var $currency + */ + public function setCurrency($currency = null) { + $this->currency = $currency; + } + /** * Changes the location id * @@ -94,7 +110,7 @@ public static function getLocation(Nitrapi &$nitrapi, $id) { * @return mixed */ public function getPrices(Service &$service = null) { - $cacheName = $this->locationId; + $cacheName = $this->locationId . "/" . $this->currency; if ($service instanceof Service) $cacheName .= "/" . $service->getId(); if (isset($this->prices[$cacheName])) { return $this->prices[$cacheName]; @@ -108,6 +124,10 @@ public function getPrices(Service &$service = null) { $query['sale_service'] = $service->getId(); } + if (!empty($this->currency) && (!($service instanceof Service))) { + $query['currency'] = $this->currency; + } + $this->prices[$cacheName] = $this->nitrapi->dataGet("/order/pricing/" . $this->getProduct(), null, [ 'query' => $query ])['prices']; @@ -122,6 +142,7 @@ public function getPrices(Service &$service = null) { * @param $rentalTime */ public function getExtendPriceForService(Service &$service, $rentalTime) { + $this->setCurrency(null); //use user currency return $this->prices[$this->locationId] = $this->nitrapi->dataGet("/order/pricing/" . $this->getProduct(), null, [ 'query' => [ 'method' => 'extend', @@ -138,6 +159,7 @@ public function getExtendPriceForService(Service &$service, $rentalTime) { * @return bool */ public function extendService(Service &$service, $rentalTime) { + $this->setCurrency(null); //use user currency $price = $this->getExtendPriceForService($service, $rentalTime); $orderArray = [ 'price' => $price, @@ -158,6 +180,7 @@ public function extendService(Service &$service, $rentalTime) { * @return bool */ public function orderService($rentalTime) { + $this->setCurrency(null); //use user currency if ($this instanceof PartPricing) { $this->checkDependencies(); $orderArray = [ @@ -193,6 +216,7 @@ public function orderService($rentalTime) { * @return mixed */ public function getSwitchPrice(Service &$service, $rentalTime) { + $this->setCurrency(null); //use user currency return $this->getPrice($rentalTime, $service); } @@ -204,6 +228,7 @@ public function getSwitchPrice(Service &$service, $rentalTime) { * @return bool */ public function switchService(Service &$service, $rentalTime) { + $this->setCurrency(null); //use user currency if ($this instanceof PartPricing) { $this->checkDependencies(); $orderArray = [ diff --git a/lib/Nitrapi/Order/Pricing/Products/CloudServer.php b/lib/Nitrapi/Order/Pricing/Products/CloudServer.php new file mode 100644 index 0000000..b6f59ed --- /dev/null +++ b/lib/Nitrapi/Order/Pricing/Products/CloudServer.php @@ -0,0 +1,17 @@ +additionals['image_id'] = $image->getId(); + } + +} \ No newline at end of file diff --git a/lib/Nitrapi/Services/CloudServers/CloudServer.php b/lib/Nitrapi/Services/CloudServers/CloudServer.php index 83cdbe5..e0ee496 100644 --- a/lib/Nitrapi/Services/CloudServers/CloudServer.php +++ b/lib/Nitrapi/Services/CloudServers/CloudServer.php @@ -2,6 +2,7 @@ namespace Nitrapi\Services\CloudServers; +use Nitrapi\Common\Exceptions\NitrapiException; use Nitrapi\Nitrapi; use Nitrapi\Services\Service; @@ -25,12 +26,121 @@ public function refresh() { /** * Returns informations about the gameserver * - * @return mixed + * @return CloudServerDetails */ public function getDetails() { return new CloudServerDetails($this->info['cloud_server']); } + /** + * Returns the password if it's still available. + * After the password has been received it will + * be permanently deleted from the Nitrado database. + * + * @return mixed + */ + public function getInitialPassword() { + $url = "services/" . $this->getId() . "/cloud_servers/password"; + $password = $this->getApi()->dataGet($url); + + if (isset($password['password'])) { + return $password['password']; + } + + return null; + } + + /** + * Boots a turned down server. + * + * @return bool + */ + public function doBoot() { + $url = "services/" . $this->getId() . "/cloud_servers/boot"; + $this->getApi()->dataPost($url); + return true; + } + + /** + * Sends a shutdown command via ACPI. + * + * @return bool + */ + public function doShutdown() { + $url = "services/" . $this->getId() . "/cloud_servers/shutdown"; + $this->getApi()->dataPost($url); + return true; + } + + /** + * Sends a reboot command via ACPI. + * + * @return bool + */ + public function doReboot() { + $url = "services/" . $this->getId() . "/cloud_servers/reboot"; + $this->getApi()->dataPost($url); + return true; + } + + /** + * This method resets your server immediately. + * This action might result in data loss. + * + * @return bool + */ + public function doHardReset() { + $url = "services/" . $this->getId() . "/cloud_servers/hard_reset"; + $this->getApi()->dataPost($url); + return true; + } + + /** + * Returns the noVNC console endpoint. + * + * @return array + */ + public function getConsole() { + $url = "services/" . $this->getId() . "/cloud_servers/console"; + return $this->getApi()->dataGet($url); + } + + /** + * Changes the PTR record of a specific IPv4 address. + * + * @param string $ip + * @param string $hostname + * @return bool + */ + public function changePTRRecord($ip, $hostname) { + $url = "services/" . $this->getId() . "/cloud_servers/ptr/" . $ip; + $this->getApi()->dataPost($url, [ + 'hostname' => $hostname + ]); + return true; + } + + /** + * Changes the hostname of the server. + * If no hostname has been provided, it will be reset to default. + * + * @param string $hostname + * @return bool + */ + public function changeHostname($hostname = null) { + $url = "services/" . $this->getId() . "/cloud_servers/hostname"; + $this->getApi()->dataPost($url, [ + 'hostname' => $hostname + ]); + return true; + } + + /** + * Returns a full list with all available images. + * + * @param Nitrapi $nitrapi + * @return array + */ public static function getAvailableImages(Nitrapi &$nitrapi) { $images = $nitrapi->dataGet('/information/cloud_servers/images'); $imgs = []; @@ -38,4 +148,38 @@ public static function getAvailableImages(Nitrapi &$nitrapi) { $imgs[] = new Image($image['id'], $image['name'], $image['is_windows']); return $imgs; } + + /** + * Returns the daily traffic usage of the last 30 days. + * + * @return array + */ + public function getTrafficStatistics() { + $url = "services/" . $this->getId() . "/cloud_servers/traffic"; + return $this->getApi()->dataGet($url)['traffic']; + } + + /** + * Triggers a reinstallation. + * Optional you can pass a new image. + * + * DANGER! This deletes all your data! + * + * @param Image|null $image + */ + public function doReinstall(Image $image = null) { + $url = "services/" . $this->getId() . "/cloud_servers/reinstall"; + + $data = []; + if ($image instanceof Image) { + if ($image->isWindows() && !$this->getDetails()->getHardwareInfo()['windows']) { + throw new NitrapiException("You need to rent the windows option to install a windows image."); + } + + $data['image_id'] = $image->getId(); + } + + $this->getApi()->dataPost($url, $data); + return true; + } } \ No newline at end of file diff --git a/lib/Nitrapi/Services/CloudServers/CloudServerDetails.php b/lib/Nitrapi/Services/CloudServers/CloudServerDetails.php index 125faec..63a304e 100644 --- a/lib/Nitrapi/Services/CloudServers/CloudServerDetails.php +++ b/lib/Nitrapi/Services/CloudServers/CloudServerDetails.php @@ -30,21 +30,72 @@ public function getHostname() { } /** - * Returns the Cloud Server ip + * Returns the main ip address of the server * * @return string */ - public function getIP() { - return (string)$this->data['ip']; + public function getMainIP() { + foreach ($this->getIPs() as $ip) { + if ($ip['main_ip'] && $ip['version'] == 4) { + return $ip['address']; + } + } + + return null; + } + + /** + * Returns the Cloud Server ips + * + * @return array + */ + public function getIPs() { + return (array)$this->data['ips']; + } + + /** + * Returns the Hardware information + * + * @return array + */ + public function getHardwareInfo() { + return (array)$this->data['hardware']; + } + + /** + * Return true if the initial password is available + * + * @return boolean + */ + public function isPasswordAvailable() { + return (bool)$this->data['password_available']; + } + + /** + * Return true if the bandwdith is currently limited + * + * @return boolean + */ + public function isBandwidthLimited() { + return (bool)$this->data['bandwidth_limited']; + } + + /** + * Returns the ID of the currently installed image. + * + * @return int + */ + public function getImageId() { + return $this->data['image']['id']; } /** - * Returns the initial admin password + * Returns the name of the currently installed image, as displayed to the user. * * @return string */ - public function getInitialPassword() { - return (string)$this->data['initial_password']; + public function getImageName() { + return $this->data['image']['name']; } } \ No newline at end of file diff --git a/lib/Nitrapi/Services/Gameservers/FileServer/FileServer.php b/lib/Nitrapi/Services/Gameservers/FileServer/FileServer.php index 27c9557..3281e87 100644 --- a/lib/Nitrapi/Services/Gameservers/FileServer/FileServer.php +++ b/lib/Nitrapi/Services/Gameservers/FileServer/FileServer.php @@ -200,6 +200,30 @@ public function downloadFile($file, $path, $name) { return true; } + /** + * Reads a part from a specific file + * + * @param $file + * @param $offset + * @param $count + * @return string + */ + public function readPartFromFile($file, $offset = 0, $count = null) { + $download = $this->downloadToken($file); + + // Here we use the GuzzleClient API directly. This is intended, but + // should remain a special case. Don't copy this code. + $response = $this->service->getApi()->request('GET', $download['token']['url'], [ + 'query' => [ + 'token' => $download['token']['token'], + 'offset' => $offset, + 'count' => $count, + ] + ]); + + return $response->getBody()->getContents(); + } + /** * Reads a specific file * @@ -235,6 +259,22 @@ public function deleteFile($file) { return true; } + /** + * Returns stat infos by file array + * + * @param $files + * @return array + */ + public function statFiles(array $files) { + $url = "/services/".$this->service->getId()."/gameservers/file_server/stat"; + + return $this->service->getApi()->dataGet($url, null, [ + 'query' => [ + 'files' => $files + ] + ])['entries']; + } + /** * Gets the file size of the given path * diff --git a/lib/Nitrapi/Services/Gameservers/GameserverDetails.php b/lib/Nitrapi/Services/Gameservers/GameserverDetails.php index e72a3bf..d710adc 100644 --- a/lib/Nitrapi/Services/Gameservers/GameserverDetails.php +++ b/lib/Nitrapi/Services/Gameservers/GameserverDetails.php @@ -296,4 +296,28 @@ public function getLogFiles() { public function getConfigFiles() { return $this->data['game_specific']['config_files']; } + + /** + * Returns the last update status + * + * @return string + */ + public function getUpdateStatus() { + return $this->data['game_specific']['update_status']; + } + + /** + * Returns the last update status + * + * @return \DateTime + */ + public function getLastUpdate() { + if (empty($this->data['game_specific']['last_update'])) { + return null; + } + + $dateTime = new \DateTime(); + $dateTime->setTimestamp(strtotime($this->data['game_specific']['last_update'])); + return $dateTime; + } } \ No newline at end of file diff --git a/lib/Nitrapi/Services/Service.php b/lib/Nitrapi/Services/Service.php index a9487ea..523f7b3 100644 --- a/lib/Nitrapi/Services/Service.php +++ b/lib/Nitrapi/Services/Service.php @@ -9,8 +9,10 @@ abstract class Service protected $api; protected $id; + protected $location_id; protected $status; protected $user_id; + protected $username; protected $delete_date; protected $suspend_date; protected $start_date; @@ -18,18 +20,27 @@ abstract class Service protected $websocket_token; protected $roles; - const SERVICE_STATUS_INSTALLING = 1; - const SERVICE_STATUS_ACTIVE = 2; - const SERVICE_STATUS_SUSPENDED = 3; - const SERVICE_STATUS_DELETED = 4; - const SERVICE_STATUS_ADMINLOCKED = 5; - const SERVICE_STATUS_ADMINLOCKED_SUSPENDED = 6; + const SERVICE_STATUS_INSTALLING = 'installing'; + const SERVICE_STATUS_ACTIVE = 'active'; + const SERVICE_STATUS_SUSPENDED = 'suspended'; + const SERVICE_STATUS_DELETED = 'deleted'; + const SERVICE_STATUS_ADMINLOCKED = 'adminlocked'; + const SERVICE_STATUS_ADMINLOCKED_SUSPENDED = 'adminlocked_suspended'; public function __construct(Nitrapi &$api, array &$data) { $this->setApi($api); $this->loadData($data); } + /** + * Returns the current location id + * + * @return int + */ + public function getLocationId() { + return $this->location_id; + } + /** * Returns the service status * @@ -46,7 +57,7 @@ public function getStatus() { */ public function getSuspendDate() { $datetime = new \DateTime(); - $datetime->setTimestamp((int)$this->suspend_date); + $datetime->setTimestamp(strtotime($this->suspend_date)); return $datetime; } @@ -57,7 +68,7 @@ public function getSuspendDate() { */ public function getDeleteDate() { $datetime = new \DateTime(); - $datetime->setTimestamp((int)$this->delete_date); + $datetime->setTimestamp(strtotime($this->delete_date)); return $datetime; } @@ -68,7 +79,7 @@ public function getDeleteDate() { */ public function getStartDate() { $datetime = new \DateTime(); - $datetime->setTimestamp((int)$this->start_date); + $datetime->setTimestamp(strtotime($this->start_date)); return $datetime; } diff --git a/lib/Nitrapi/Services/Voiceservers/Types/Teamspeak3.php b/lib/Nitrapi/Services/Voiceservers/Types/Teamspeak3.php index 43a7c83..6cf0778 100644 --- a/lib/Nitrapi/Services/Voiceservers/Types/Teamspeak3.php +++ b/lib/Nitrapi/Services/Voiceservers/Types/Teamspeak3.php @@ -41,9 +41,11 @@ public function deleteWhitelist($id) { return true; } - public function enableLogView() { + public function enableLogView($group) { $url = "services/" . $this->service->getId() . "/voiceservers/teamspeak3/enable_log_view"; - return $this->service->getApi()->dataPost($url, []); + return $this->service->getApi()->dataPost($url, [ + 'group' => $group + ]); } public function cleanupUsers($groups, $days) {