diff options
author | sijanec <anton@sijanec.eu> | 2020-12-22 12:52:50 +0100 |
---|---|---|
committer | sijanec <anton@sijanec.eu> | 2020-12-22 12:52:50 +0100 |
commit | a76aeb54af03da859c006a88fa7178dc54dd11d7 (patch) | |
tree | 8e5d7691bdcb57571fed231592290141a89d95e7 /ru | |
parent | kemijsko ravnotežje, pregledne naloge (diff) | |
download | sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar.gz sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar.bz2 sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar.lz sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar.xz sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.tar.zst sola-gimb-2-a76aeb54af03da859c006a88fa7178dc54dd11d7.zip |
Diffstat (limited to 'ru')
-rw-r--r-- | ru/kolo/api.php | 131 |
1 files changed, 120 insertions, 11 deletions
diff --git a/ru/kolo/api.php b/ru/kolo/api.php index c085435..64dd35c 100644 --- a/ru/kolo/api.php +++ b/ru/kolo/api.php @@ -4,46 +4,155 @@ $dbhost = "localhost"; $dbdb = "kolo"; $dbpw = "kolo"; + $conn = new mysqli($dbhost, $dbuser, $dbpw, $dbdb); header("Content-Type: application/json"); + if ($conn->connect_error) { + http_response_code(500); + exit('{"napaka":true,"obvestilo":"Napaka strežnika pri povezavi v podatkovno zbirko."}'); + } + $query = "CREATE TABLE IF NOT EXISTS kolesa ( + omogoceno INT, + ime VARCHAR(420), + admin VARCHAR(420) + ); + CREATE TABLE IF NOT EXISTS ljudje ( + ime VARCHAR(420) PRIMARY KEY, + izbranec VARCHAR(420), + kolo VARCHAR(420), + krneki VARCHAR(420) + )"; + $result = $conn->query($query); + var_dump($result); + var_dump($conn->error); if (empty($_REQUEST['k'])) { http_response_code(400); exit ('{"napaka":true,"obvestilo":"Ne vem, na katero kolo se sklicuješ, ker manjka parameter k."}'); } switch ($_REQUEST['m']) { case "r": /* registracija */ - if (empty($_REQUEST['m'])) { + if (empty($_REQUEST['i'])) { + http_response_code(400); + exit ('{"napaka":true,"obvestilo":"Ne vem, kako ti je ime, ker manjka parameter i"}'); + } + $stmt2 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime`=?"); + $stmt2->bind_param("s", $_REQUEST["k"]); + $stmt2->execute(); + $stmt2->bind_result($omogoceno); + $stmt2->fetch(); + if ($omogoceno != 0) { http_response_code(400); - exit ('{"napaka":true,"obvestilo":"Ne vem, kako ti je ime, ker manjka parameter m"}'); + exit ('{"napaka":true,"obvestilo":"Ne morem registrirati novih ljudi, ker se je vrtenje koles že začelo.}'); } - /* sedaj naredimo SQL */ + $stmt2->close(); + $stmt = $conn->prepare("INSERT INTO ljudje (ime, kolo) VALUES (?, ?) ON DUPLICATE KEY UPDATE `krneki`='123'"); + $stmt->bind_param("ss", $_REQUEST["i"], $_REQUEST["k"]); + $stmt->execute(); + $stmt->close(); + http_response_code(201); + exit ('{"napaka":false,"obvestilo":"Uporabnik registriran!"}'); break; case "z": /* zakleni kolo, omogoči vrtenje, naključno razporedi osebe */ + $stmt = $conn->prepare("SELECT admin FROM kolesa WHERE `ime`='?'"); + $stmt->bind_param("s", $_REQUEST["k"]); + $stmt->execute(); + $stmt->bind_result($admin_pass); + $stmt->fetch(); if ($_REQUEST['g'] !== $admin_pass) { http_response_code(400); exit ('{"napaka":true,"obvestilo":"Ne bom zaklenil tega kolesa, ker niste administrator."}'); } + $stmt->close(); + $stmt = $conn->prepare("UPDATE kolesa SET `omogoceno`=1 WHERE `ime` = '?'"); + $stmt->bind_param("s", $_REQUEST["k"]); + $stmt->execute(); + $stmt->close(); + $stmt2 = $conn->prepare("SELECT ime FROM ljudje WHERE `kolo`=?"); + $stmt2->bind_param("s", $_REQUEST["k"]); + $stmt2->execute(); + $result = $stmt->get_result(); + $ljudje = $result->fetch_all(MYSQLI_ASSOC); + $ljudje2 = $ljudje; + shuffle($ljudje); + for ($i = 0; $i < sizeof($ljudje);) { + if ($ljudje[$i] == $ljudje2[$i]) { + shuffle($ljudje); + $i = 0; + continue; + } + $i++; + } + $stmt3 = $conn->prepare("UPDATE ljudje SET `izbranec`=? WHERE `ime`=?"); + for ($i = 0; $i < sizeof($ljudje); $i++) { + $stmt3->bind_param("ss", $ljudje[$i], $ljudje2[$i]); + $stmt3->execute(); + } + $stmt3->close(); + $stmt2->close(); + http_response_code(200); + exit ('{"napaka":false,"obvestilo":"uspešno zaklenil kolo."}'); break; case "u": /* srčni utrip */ $r = array(); - $r["dovoli_vrtenje"] = false; - $r["si_administrator"] = false; - $r["moznosti_na_kolesu"] = false; + $stmt2 = $conn->prepare("SELECT ime FROM ljudje WHERE `kolo`=?"); + $stmt2->bind_param("s", $_REQUEST["k"]); + $stmt2->execute(); + $result = $stmt->get_result(); + $ljudje = $result->fetch_all(MYSQLI_ASSOC); + $r["moznosti_na_kolesu"] = $ljudje; + $stmt = $conn->prepare("SELECT admin FROM kolesa WHERE `ime`=?"); + $stmt->bind_param("s", $_REQUEST["k"]); + $stmt->execute(); + $stmt->bind_result($admin_pass); + $stmt->fetch(); + if ($admin_pass === $_REQUEST["g"]) { + $r["si_administrator"] = true; + } else { + $r["si_administrator"] = false; + $r["si_administrator"] = false; + } + $stmt3 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime` = ?"); + $stmt3->bind_param("s", $_REQUEST["k"]); + $stmt3->execute(); + $stmt3->bind_result($omogoceno); + $stmt3->fetch(); + if ($omogoceno !== 1) { + $r["dovoli_vrtenje"] = false; + } else { + $r["dovoli_vrtenje"] = true; + } + $r["napaka"] = false; + $stmt2->close(); + $stmt->close(); + $stmt3->close(); http_response_code(200); exit (json_encode($r)); break; case "v": /* vrti */ - if (true /* a je vrtenje nedovoljeno */) { + $stmt3 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime` = ?"); + $stmt3->bind_param("s", $_REQUEST["k"]); + $stmt3->execute(); + $stmt3->bind_result($omogoceno); + $stmt3->fetch(); + if ($omogoceno !== 1 /* a je vrtenje nedovoljeno */) { http_response_code(400); exit ('{"napaka":true,"obvestilo":"Ne morem vrteti, ker vrtenja še ni odobril administrator"}'); } - if (true /* če je uporabnik že zavrtel kolo */) { + if (empty($_REQUEST["i"])) { http_response_code(400); - exit ('{"napaka":true,"obvestilo":"Kolesa ne morem vrteti, ker ste ga enkrat že zavrteli."}'); + exit ('{"napaka":true,"obvestilo":"Ne morem vrteti, ker nimam vašega imena."}'); } - /* pridobi obstoječo vrednost iz tabele in jo označi za taken */ + $stmt2 = $conn->prepare("SELECT izbranec FROM `ljudje` WHERE `ime` = ?"); + $stmt2->bind_param("s", $_REQUEST["i"]); + $stmt2->execute(); + $stmt2->bind_result($izbranec); + $stmt2->fetch(); + $r["izbranec"] = $izbranec; + $r["napaka"] = false; + $stmt3->close(); + $stmt2->close(); http_response_code(200); - exit (); + exit(json_encode($r)); break; default: http_response_code(400); |