Yurdle/src/server.php
2024-01-07 12:47:16 +03:00

211 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Yosymfony\Toml\Toml;
include 'todays.php';
include '../config.php';
require __DIR__ . '/../vendor/autoload.php';
$parser = new Toml();
$dock_local_test = 1;
# Fetch remote data each 1 hour
function updateRemoteData()
{
# PREREQUIRITIES Create a cronjob or a systemd service for update it regularly !
# GET FROM GIT WITH ACCESS TOKEN AND SAVE TO data.toml
global $DATAACCESS_TOKEN;
$string = file_get_contents("https://git.aliberksandikci.com.tr/api/v1/repos/ifl/YurdleBackend/raw/data.toml?access_token=" . $DATAACCESS_TOKEN);
if ($string === FALSE) {
echo "Could not read the file.";
} else {
$file = __DIR__ . "/../data.toml";
file_put_contents($file, $string, LOCK_EX);
}
# REVIEW MAKE SURE FILE IS CHMOD 600 AND NOT ACCESSIBLE FROM USERSIDE !!!
parseTOML(); # parse again after update
}
# Choose the todays person, everyday TSI 03:00
function chooseTodayPerson()
{
# PREREQUIRIITES with cronjob or systemd, run this function everyday TSI 03.00
# parse toml
# get users array length
# get a random number
# make this person todays person, change $TODAYS_PERSON variable in today.php
}
# Send all person names to frontend
function getAllPersonNames()
{
# Parse Toml
# get `users` array
# get isim_soyisim of each user in `users` array and add to newArr
# NewArr
# send these as arry to frontend
# aliberk_sandikci24 -> "Aliberk Sandıı
// APPROACH 1: (needs public-> users array)
// $users = parseTOML()["public"]["users"];
// $arr = array();
// foreach ($users as $user) {
// array_push($arr, parseTOML()["data"][$user]["isim_soyisim"]);
// }
// return $arr;
// APPROACH 2:
$data = parseTOML()["data"];
$arr = array();
foreach ($data as $d) {
array_push($arr, $d["isim_soyisim"]);
}
return $arr;
}
function getAllCriterias()
{
$data = parseTOML()["public"];
return $data["gozukecek_kriterler"];
}
function getAllNotices()
{
$data = parseTOML()["public"];
return $data["kullanici_notlari"];
}
# 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');
$filename = "data.toml";
if (file_exists(__DIR__ . "/../" . $filename)) {
$arr[0] = date("d/m/Y H:i:s", filemtime(__DIR__ . "/../" . $filename));
}
$filename = "todays.php";
if (file_exists(__DIR__ . "/" . $filename)) {
$arr[1] = date("d/m/Y H:i:s", filemtime(__DIR__ . "/" . $filename));
}
return $arr;
}
# Aliberk Sandıı -> aliberk_sandikci24
function reversePerson($str)
{
$data = parseTOML()["data"];
foreach ($data as $key => $value) {
if ($value["isim_soyisim"] == $str) {
return $key;
}
}
}
# 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':
$RESULT[$curCrit] = [compareCOMP($statGuess[$curCrit], $statTodays[$curCrit]), $statGuess[$curCrit]];
break;
case 'arr':
$RESULT[$curCrit] = [compareARR($statGuess[$curCrit], $statTodays[$curCrit], $curCrit), $statGuess[$curCrit]];
break;
default:
$RESULT[$curCrit] = [compareEQ($statGuess[$curCrit], $statTodays[$curCrit]), $statGuess[$curCrit]];
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;
} else if ($equalNum == count($todays)) {
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;
global $dock_local_test;
if ($dock_local_test) {
$array = $parser::ParseFile("../data.toml");
return $array;
} else {
$array = $parser::ParseFile("data.toml");
return $array;
}
}
updateRemoteData();
comparePerson('aliberk_sandikci24');