<?php
/*
Bryan
Exile Server Manager
*/
session_start();
require 'updateServers.php';
require 'steamauth/userInfo.php';
// Get some variables
$serverKey = $_POST["index"];
$type = $_POST["type"];
$server = $SERVERS[$serverKey];
$schema = $server["database_schema"];
$steamID = $steamprofile['steamid'];
$esm_db_schema = DB_ESM;
if ($type == "select")
{
$id = $_POST["id"];
// Time for the territories
$result = mysqli_query($exile_db,
"SELECT
t.name,
t.radius,
t.level,
t.build_rights,
t.moderators,
t.last_paid_at,
t.protected,
t.flag_stolen,
(SELECT COUNT(*) FROM $schema.construction c WHERE c.territory_id = $id) AS object_count,
t.owner_uid,
(
SELECT
t2.price
FROM
$esm_db_schema.territory t2
WHERE
t2.server_key = '$serverKey'
AND
t2.level = t.level + 1
) AS price,
(
SELECT
t2.objects
FROM
$esm_db_schema.territory t2
WHERE
t2.server_key = '$serverKey'
AND
t2.level = t.level
) AS max_objects,
(
SELECT
s.price_per_object
FROM
$esm_db_schema.servers s
WHERE
server_key = '$serverKey'
) AS price_per_object,
(
SELECT
s.territory_life_time
FROM
$esm_db_schema.servers s
WHERE
server_key = '$serverKey'
) AS territory_life_time
FROM
$schema.territory t
WHERE
t.id = $id");
// Yeah yeah
if (!$result) { die(json_encode(["result" => RESULT_FAILED, "return" => FAILED_QUERY])); }
// Array time!
$t_array = mysqli_fetch_assoc($result);
/*
Time to replace all the UIDs with names.
So this is a bit interesting because my PHP coding is so bad. xD
ArmA arrays are like javascript arrays in format style
["UID","UID","UID"]
However, in order to store them in the database, they must be stored in a TEXT field, which is string.
*/
// Get the UID
$ownerUID = $t_array["owner_uid"];
// Replace the UID with the name of the player based in the account table
$t_array["owner_uid"] = ["uid" => $ownerUID, "name" => mysqli_fetch_assoc(mysqli_query($exile_db, "SELECT name FROM $schema.account WHERE uid = '$ownerUID'"))['name']];
// Process these arrays to make it so they can be converted into a PHP array. Basically strip "["UID","UID","UID"]" to "UID,UID,UID,UID"
$t_array["build_rights"] = explode(",", str_replace(["[", "]", "\""], "", $t_array["build_rights"]));
$t_array["moderators"] = explode(",", str_replace(["[", "]", "\""], "", $t_array["moderators"]));
// Loop through the build_rights array
for ($i = 0; $i < count($t_array["build_rights"]); $i++)
{
// Get the UID
$modUID = $t_array["build_rights"][$i];
// Get the name and replace
$t_array["build_rights"][$i] = ["uid" => $modUID, "name" => mysqli_fetch_assoc(mysqli_query($exile_db, "SELECT name FROM $schema.account WHERE uid = '$modUID'"))['name']];
}
// Loop through the moderators/members
for ($i = 0; $i < count($t_array["moderators"]); $i++)
{
// UID
$memUID = $t_array["moderators"][$i];
// To name
$t_array["moderators"][$i] = ["uid" => $memUID, "name" => mysqli_fetch_assoc(mysqli_query($exile_db, "SELECT name FROM $schema.account WHERE uid = '$memUID'"))['name']];
}
// Force an update
if (!mysqli_query($esm_db, "UPDATE servers SET online_players = NULL WHERE server_key = '$serverKey'"))
{
die(json_encode(["result" => RESULT_FAILED, "return" => FAILED_QUERY]));
}
// Get a update of the players
if (!mysqli_query($esm_db, "INSERT INTO requests (recipient_key, function, parameter) VALUES ('$serverKey', 'ExileServerManager_object_player_getOnline', '[]')"))
{
die(json_encode(["result" => RESULT_FAILED, "return" => FAILED_QUERY]));
}
// Wait until the server has updated the php, timeout after 10 seconds
$time = time();
// So it's a wait until... It will time out after 3 seconds if it didn't get a response
do
{
usleep(100000);
}
while ((mysqli_fetch_assoc(mysqli_query($esm_db, "SELECT online_players FROM servers WHERE server_key = '$serverKey'"))["online_players"] === null) && (time() < ($time + 3)));
// Set it, either we have the players or we have an empty array
$result = mysqli_fetch_assoc(mysqli_query($esm_db, "SELECT online_players FROM servers WHERE server_key = '$serverKey'"))["online_players"];
$t_array["online_players"] = ($result !== null) ? json_decode($result) : [];
// Return
die(json_encode(["result" => RESULT_SUCCESS, "return" => $t_array]));
}
else
{
// Time for the territories
$result = mysqli_query($exile_db,
"SELECT
t.id,
t.name,
t.protected
FROM
$schema.territory t
WHERE
t.deleted_at IS NULL
AND
(t.owner_uid = '$steamID' OR t.build_rights LIKE '%$steamID%' OR t.moderators LIKE '%$steamID%')");
// Yeah yeah
if (!$result) { die(json_encode(["result" => RESULT_FAILED, "return" => FAILED_QUERY])); }
// Array time!
$t_array = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Return
die(json_encode(["result" => RESULT_SUCCESS, "return" => $t_array]));
}
?>