Yurdle/src/server.php

214 lines
5 KiB
PHP
Raw Normal View History

2024-01-07 00:37:32 +03:00
<?php
use Yosymfony\Toml\Toml;
2024-01-07 13:15:08 +03:00
include __DIR__ . '/todays.php';
include __DIR__ . '/../config.php';
2024-01-07 00:37:32 +03:00
require __DIR__ . '/../vendor/autoload.php';
$parser = new Toml();
2024-01-07 12:47:16 +03:00
$dock_local_test = 1;
2024-01-07 00:37:32 +03:00
2024-01-07 15:35:18 +03:00
# Fetch Remote data and update data.toml (running each 10 min with cronjob)
2024-01-08 19:05:46 +03:00
function testData($toml)
{
global $parser;
try {
$array = $parser::Parse($toml);
} catch (Exception $e) {
$ans = 'Caught exception: ' . $e->getMessage() . "\n";
return $ans;
}
return 0;
}
2024-01-07 00:37:32 +03:00
function updateRemoteData()
{
2024-01-07 12:47:16 +03:00
global $DATAACCESS_TOKEN;
2024-01-07 15:35:18 +03:00
global $APP_NAME;
2024-01-07 12:47:16 +03:00
$string = file_get_contents("https://git.aliberksandikci.com.tr/api/v1/repos/ifl/YurdleBackend/raw/data.toml?access_token=" . $DATAACCESS_TOKEN);
if ($string === FALSE) {
2024-01-08 19:05:46 +03:00
echo "fileError: Could not read the file or could not get the file from server";
return;
} else if (testData($string) != 0) {
echo "parseError: " . testData($string);
return;
2024-01-07 12:47:16 +03:00
} else {
$file = __DIR__ . "/../data.toml";
file_put_contents($file, $string, LOCK_EX);
}
2024-01-07 15:35:18 +03:00
chmod("$file", 0700);
chown("$file", $APP_NAME);
2024-01-07 12:47:16 +03:00
2024-01-07 15:35:18 +03:00
parseTOML(); // not necessary
2024-01-08 19:05:46 +03:00
echo "success";
2024-01-07 00:37:32 +03:00
}
2024-01-07 15:35:18 +03:00
# Choose the todays person (running each day with cronjob)
2024-01-07 00:37:32 +03:00
function chooseTodayPerson()
{
2024-01-07 15:35:18 +03:00
global $APP_NAME;
$data = parseTOML()["data"];
2024-01-07 00:37:32 +03:00
2024-01-07 15:35:18 +03:00
$personArr = array();
foreach ($data as $key => $value) {
array_push($personArr, $key);
}
print_r($personArr);
srand();
$random_number = random_int(0, count($personArr) - 1);
$choosen = $personArr[$random_number];
// TODO control each person if already choosed recently? (make all persons avaliable AFTER all of them choosed )
$string = "<?php \$TODAYS_PERSON = \"$choosen\";";
$file = __DIR__ . "/todays.php";
file_put_contents($file, $string, LOCK_EX);
chmod("$file", 0700);
chown("$file", $APP_NAME);
2024-01-07 00:37:32 +03:00
}
# Send all person names to frontend
function getAllPersonNames()
{
$data = parseTOML()["data"];
$arr = array();
foreach ($data as $d) {
array_push($arr, $d["isim_soyisim"]);
}
return $arr;
}
2024-01-07 03:27:44 +03:00
function getAllCriterias()
{
2024-01-07 00:37:32 +03:00
$data = parseTOML()["public"];
return $data["gozukecek_kriterler"];
}
2024-01-07 03:27:44 +03:00
function getAllNotices()
{
$data = parseTOML()["public"];
return $data["kullanici_notlari"];
}
2024-01-07 00:37:32 +03:00
# send data update date and latest person update date
function sendDataDate()
{
# 1: Get data.toml modified time
# 2: Get latest person update, (should ebe TSI 03.00!)
date_default_timezone_set('Europe/Istanbul');
setlocale(LC_ALL, 'tr_TR');
2024-01-07 03:27:44 +03:00
$filename = "data.toml";
2024-01-07 00:37:32 +03:00
if (file_exists(__DIR__ . "/../" . $filename)) {
$arr[0] = date("d/m/Y H:i:s", filemtime(__DIR__ . "/../" . $filename));
}
2024-01-07 03:27:44 +03:00
$filename = "todays.php";
if (file_exists(__DIR__ . "/" . $filename)) {
$arr[1] = date("d/m/Y H:i:s", filemtime(__DIR__ . "/" . $filename));
2024-01-07 00:37:32 +03:00
}
2024-01-07 03:27:44 +03:00
2024-01-07 00:37:32 +03:00
return $arr;
}
2024-01-07 03:27:44 +03:00
# Aliberk Sandıı -> aliberk_sandikci24
function reversePerson($str)
{
$data = parseTOML()["data"];
foreach ($data as $key => $value) {
if ($value["isim_soyisim"] == $str) {
return $key;
}
}
}
2024-01-07 00:37:32 +03:00
# compare a person with todays person, returns equalities (0-red, 1-orange, 2-green, 10-orange(küçük), 12-orange(büyük))
function comparePerson($person)
{
global $TODAYS_PERSON;
$statGuess = getAPersonStats($person);
$statTodays = getAPersonStats($TODAYS_PERSON);
$kriterler = parseTOML()["public"]["kriterler"];
// $RESULT = array_fill(0, count($kriterler), 0);
for ($i = 0; $i < count($kriterler); $i++) {
$curCrit = array_keys($kriterler)[$i];
switch (array_values($kriterler)[$i]) {
case 'comp':
2024-01-07 03:27:44 +03:00
$RESULT[$curCrit] = [compareCOMP($statGuess[$curCrit], $statTodays[$curCrit]), $statGuess[$curCrit]];
2024-01-07 00:37:32 +03:00
break;
case 'arr':
2024-01-07 03:27:44 +03:00
$RESULT[$curCrit] = [compareARR($statGuess[$curCrit], $statTodays[$curCrit], $curCrit), $statGuess[$curCrit]];
2024-01-07 00:37:32 +03:00
break;
default:
2024-01-07 03:27:44 +03:00
$RESULT[$curCrit] = [compareEQ($statGuess[$curCrit], $statTodays[$curCrit]), $statGuess[$curCrit]];
2024-01-07 00:37:32 +03:00
break;
}
}
return $RESULT;
}
# compare 2 data in EQ format
function compareEQ($guess, $todays)
{
if ($guess == $todays) {
return 2;
} else {
return 0;
}
}
# compare 2 data in COMP format
function compareCOMP($guess, $todays)
{
if ($guess == $todays) {
return 2;
} else if ($guess <= $todays) {
return 12;
} else {
return 10;
}
}
# compare 2 data in ARR format
function compareARR($guess, $todays, $krit)
{
$equalNum = 0;
foreach ($guess as $key1 => $value1) {
foreach ($todays as $key2 => $value2) {
if ($value1 == $value2) {
$equalNum++;
}
}
}
if ($equalNum == 0) {
return 0;
2024-01-07 15:35:18 +03:00
} else if ($equalNum == count($todays) && $equalNum == count($guess)) {
2024-01-07 00:37:32 +03:00
return 2;
} else if ($equalNum >= 1) {
return 1;
} else {
return 0;
}
}
# get a person stat, can be used in other functions
function getAPersonStats($person)
{
return parseTOML()["data"][$person];
}
# parse toml
function parseTOML()
{
global $parser;
2024-01-07 13:15:08 +03:00
$array = $parser::ParseFile(__DIR__ . "/../data.toml");
return $array;
2024-01-07 00:37:32 +03:00
}
2024-01-07 16:46:33 +03:00
if ($_POST['updateData'] != null && $_POST['updateData'] == "yes") {
updateRemoteData();
2024-01-08 19:05:46 +03:00
}