Skip to content

Commit

Permalink
activate Discord message hook for replacing simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Apr 26, 2024
1 parent 01bf355 commit ecbb085
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions scripts/Server/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
const MAX_MINUTES_FOR_INACTIVITY = 60;
const PUBLIC_WORKSPACE_TYPE = 0;
const ALIEN_PROJECT_WORKSPACE_TYPE = 1;
const PRIVATE_WORKSPACE_TYPE = 2;

function connectToDB()
Expand Down
21 changes: 15 additions & 6 deletions scripts/Server/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ function sendDiscordMessage($payload) {
// functions for Discord messages
function createAddResourceMessage($type, $simName, $userName, $simDesc, $width, $height, $particles) {
if ($type == 0) {
return createAddSimulationMessage($simName, $userName, $simDesc, $width, $height, $particles);
return createMessageForSimulation("New simulation added to the database", $simName, $userName, $simDesc, $width, $height, $particles);
}
else {
return createAddGenomeMessage($simName, $userName, $simDesc, $width, $height, $particles);
return createMessageForGenome("New genome added to the database", $simName, $userName, $simDesc, $width, $height, $particles);
}
}

function createAddSimulationMessage($simName, $userName, $simDesc, $width, $height, $particles) {
function createUpdateResourceMessage($type, $simName, $userName, $simDesc, $width, $height, $particles) {
if ($type == 0) {
return createMessageForSimulation("Simulation updated in the database", $simName, $userName, $simDesc, $width, $height, $particles);
}
else {
return createMessageForGenome("Genome updated in the database", $simName, $userName, $simDesc, $width, $height, $particles);
}
}

function createMessageForSimulation($message, $simName, $userName, $simDesc, $width, $height, $particles) {
$particlesString = $particles < 1000 ? "{$particles}" : strval((int)($particles/1000)) . " K";
return json_encode([
"username" => "alien-project",
Expand All @@ -33,7 +42,7 @@ function createAddSimulationMessage($simName, $userName, $simDesc, $width, $heig
"embeds" => [
[
"author" => [
"name" => "New simulation added to the database",
"name" => $message,
"icon_url" => "https://alien-project.org/alien-server/galaxy.png"
],
"title" => $simName,
Expand All @@ -60,15 +69,15 @@ function createAddSimulationMessage($simName, $userName, $simDesc, $width, $heig
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

function createAddGenomeMessage($simName, $userName, $simDesc, $width, $height, $particles) {
function createMessageForGenome($message, $simName, $userName, $simDesc, $width, $height, $particles) {
return json_encode([
"username" => "alien-project",
"avatar_url" => "https://alien-project.org/alien-server/logo.png",
"content" => "",
"embeds" => [
[
"author" => [
"name" => "New genome added to the database",
"name" => $message,
"icon_url" => "https://alien-project.org/alien-server/genome.png"
],
"title" => $simName,
Expand Down
25 changes: 12 additions & 13 deletions scripts/Server/replacesimulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ function closeAndExit($db) {
$pw = $_POST["password"];

if (!checkPw($db, $userName, $pw)) {
echo json_encode(["result"=>false]);
$db->close();
exit;
closeAndExit($db);
}

$obj = $db->query("SELECT u.ID as id FROM user u WHERE u.NAME='".addslashes($userName)."'")->fetch_object();
if (!$obj) {
echo json_encode(["result"=>false]);
$db->close();
exit;
closeAndExit($db);
}

$success = false;
Expand All @@ -35,11 +31,14 @@ function closeAndExit($db) {
$settings = $_POST['settings'];
$simId = $_POST['simId'];
$size = strlen($content);
$type = array_key_exists('type', $_POST) ? $_POST['type'] : 0;
$workspace = array_key_exists('workspace', $_POST) ? $_POST['workspace'] : 0;
$statistics = array_key_exists('statistics', $_POST) ? $_POST['statistics'] : "";

if ($userName != 'alien-project' && $workspace == 1) {
$obj = $db->query("SELECT sim.NAME as name, sim.TYPE as type, sim.FROM_RELEASE as workspace FROM simulation sim WHERE sim.ID='".addslashes($simId)."'")->fetch_object();
if (!$obj) {
closeAndExit($db);
}

if ($userName != 'alien-project' && $obj->workspace == ALIEN_PROJECT_WORKSPACE_TYPE) {
closeAndExit($db);
}

Expand All @@ -56,10 +55,10 @@ function closeAndExit($db) {
}

// create Discord message
//if ($workspace != PRIVATE_WORKSPACE_TYPE) {
// $discordPayload = createAddResourceMessage($type, $simName, $userName, $simDesc, $width, $height, $particles);
// sendDiscordMessage($discordPayload);
//}
if ($obj->workspace != PRIVATE_WORKSPACE_TYPE) {
$discordPayload = createUpdateResourceMessage($obj->type, $obj->name, $userName, $simDesc, $width, $height, $particles);
sendDiscordMessage($discordPayload);
}

echo json_encode(["result"=>true]);

Expand Down

0 comments on commit ecbb085

Please sign in to comment.