From 99bc7f4ce2c682b312cd9f7e06e34a2b3bbeba72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 1 Sep 2022 17:15:53 +0200 Subject: [PATCH 01/13] init api --- .gitignore | 3 ++- api/.env | 1 + api/.htaccess | 15 +++++++++++ api/index.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++ api/utils/cors.php | 37 +++++++++++++++++++++++++++ composer.json | 6 ++--- 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 api/.env create mode 100644 api/.htaccess create mode 100644 api/index.php create mode 100644 api/utils/cors.php diff --git a/.gitignore b/.gitignore index 62caf91..dd1fdf3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ dist-ssr *.sln *.sw? -public/vendor +# php +/api/vendor diff --git a/api/.env b/api/.env new file mode 100644 index 0000000..bc721a1 --- /dev/null +++ b/api/.env @@ -0,0 +1 @@ +PRODUCTION=false diff --git a/api/.htaccess b/api/.htaccess new file mode 100644 index 0000000..daad08f --- /dev/null +++ b/api/.htaccess @@ -0,0 +1,15 @@ + + Order Allow,Deny + Deny from all + + + + RewriteEngine On + + # RewriteCond %{HTTPS} off + # RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] + + RewriteCond %{REQUEST_FILENAME} !-f + RewriteCond %{REQUEST_FILENAME} !-d + RewriteRule . index.php [L] + diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000..90dc7fb --- /dev/null +++ b/api/index.php @@ -0,0 +1,63 @@ +load(); + +function env($key) { + global $_ENV; + return array_key_exists($key, $_ENV) ? $_ENV[$key] : null; +} + +$router = new \Bramus\Router\Router(); + +$data = json_decode(file_get_contents("php://input"), true); +$routesDirectory = __DIR__ . "/routes"; + +$getFolderContentUtil = null; +$getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentUtil, $router, $data) { + $files = scandir($dir); + $filter = '/\.php$/'; + + foreach($files as $key => $value) { + $path = realpath($dir.DIRECTORY_SEPARATOR.$value); + $pathWithoutRoot = str_replace($routesDirectory, '', $path); + $pathWithoutRelativeRoot = str_replace($dir, '', $path); + + if(!is_dir($path)) { + if(empty($filter) || preg_match($filter, $path)) { + $error = false; + + try { + $response = require $path; + } catch(exception $e) { + $error = $e; + } + + if(!empty($response) && is_array($response)) { + foreach($response as $key => $fn) { + $router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error) { + $responseResult = $fn($data, getallheaders(), $error, $router); + if($responseResult) { + echo json_encode($responseResult); + } + }); + } + } + } + } else if($value != "." && $value != "..") { + $router->mount($pathWithoutRelativeRoot, function() use($getFolderContentUtil, $path, $filter) { + $getFolderContentUtil($path); + }); + } + } +}; + +$getFolderContentUtil($routesDirectory); + +$router->run(); diff --git a/api/utils/cors.php b/api/utils/cors.php new file mode 100644 index 0000000..b2c6a43 --- /dev/null +++ b/api/utils/cors.php @@ -0,0 +1,37 @@ + Date: Thu, 1 Sep 2022 17:22:56 +0200 Subject: [PATCH 02/13] Fix api --- api/routes/gamedetails.php | 11 +++++++ composer.lock | 59 ++++++++++++++++---------------------- package.json | 3 +- 3 files changed, 37 insertions(+), 36 deletions(-) create mode 100644 api/routes/gamedetails.php diff --git a/api/routes/gamedetails.php b/api/routes/gamedetails.php new file mode 100644 index 0000000..f2813e4 --- /dev/null +++ b/api/routes/gamedetails.php @@ -0,0 +1,11 @@ + function($data) { + return [ + "message" => "hello api" + ]; + } + +]; diff --git a/composer.lock b/composer.lock index 222872d..767d33b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,69 +4,58 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5e6e41d564a162322502fd4108ce44cf", + "content-hash": "f9a46f2ec9409fb54193fe711c55753b", "packages": [ { - "name": "firebase/php-jwt", - "version": "v6.2.0", + "name": "bramus/router", + "version": "1.5", "source": { "type": "git", - "url": "https://github.com/firebase/php-jwt.git", - "reference": "d28e6df83830252650da4623c78aaaf98fb385f3" + "url": "https://github.com/bramus/router.git", + "reference": "6adc0182dc6b0ebc22fd10543f65a36de10d8c05" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/firebase/php-jwt/zipball/d28e6df83830252650da4623c78aaaf98fb385f3", - "reference": "d28e6df83830252650da4623c78aaaf98fb385f3", + "url": "https://api.github.com/repos/bramus/router/zipball/6adc0182dc6b0ebc22fd10543f65a36de10d8c05", + "reference": "6adc0182dc6b0ebc22fd10543f65a36de10d8c05", "shasum": "" }, "require": { - "php": "^7.1||^8.0" + "php": ">=5.3.0" }, "require-dev": { - "guzzlehttp/guzzle": "^6.5||^7.4", - "phpspec/prophecy-phpunit": "^1.1", - "phpunit/phpunit": "^7.5||^9.5", - "psr/cache": "^1.0||^2.0", - "psr/http-client": "^1.0", - "psr/http-factory": "^1.0" - }, - "suggest": { - "paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present" + "friendsofphp/php-cs-fixer": "~2.14", + "phpunit/php-code-coverage": "~2.0", + "phpunit/phpunit": "~4.0" }, "type": "library", "autoload": { - "psr-4": { - "Firebase\\JWT\\": "src" + "psr-0": { + "Bramus": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "BSD-3-Clause" + "MIT" ], "authors": [ { - "name": "Neuman Vong", - "email": "neuman+pear@twilio.com", - "role": "Developer" - }, - { - "name": "Anant Narayanan", - "email": "anant@php.net", - "role": "Developer" + "name": "Bram(us) Van Damme", + "email": "bramus@bram.us", + "homepage": "http://www.bram.us" } ], - "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", - "homepage": "https://github.com/firebase/php-jwt", + "description": "A lightweight and simple object oriented PHP Router", + "homepage": "https://github.com/bramus/router", "keywords": [ - "jwt", - "php" + "router", + "routing" ], "support": { - "issues": "https://github.com/firebase/php-jwt/issues", - "source": "https://github.com/firebase/php-jwt/tree/v6.2.0" + "issues": "https://github.com/bramus/router/issues", + "source": "https://github.com/bramus/router/tree/1.5" }, - "time": "2022-05-13T20:54:50+00:00" + "time": "2020-10-26T09:50:48+00:00" }, { "name": "graham-campbell/result-type", diff --git a/package.json b/package.json index 6de2a1f..45afb62 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "dev": "vite", "build": "vite build", - "preview": "vite preview" + "preview": "vite preview", + "php": "php -S localhost:8000 -t ./api" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.1", From 025f220809381cba0fa94b648de648fb7b7b9efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 1 Sep 2022 17:44:33 +0200 Subject: [PATCH 03/13] add database --- api/.env | 5 +++ api/index.php | 25 +++++++----- api/routes/gamedetails.php | 4 +- composer.json | 3 +- composer.lock | 78 +++++++++++++++++++++++++++++++++++++- 5 files changed, 102 insertions(+), 13 deletions(-) diff --git a/api/.env b/api/.env index bc721a1..7208794 100644 --- a/api/.env +++ b/api/.env @@ -1 +1,6 @@ PRODUCTION=false + +DB_HOST="hz-mysql6" +DB_USER="mysql85033" +DB_PASSWORD="troglodyt" +DB_DATABASE="mysql122279" diff --git a/api/index.php b/api/index.php index 90dc7fb..8ba706a 100644 --- a/api/index.php +++ b/api/index.php @@ -3,6 +3,8 @@ require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/utils/cors.php'; +use Medoo\Medoo; + cors(); header('Content-Type: application/json; charset=utf-8'); @@ -16,11 +18,21 @@ function env($key) { $router = new \Bramus\Router\Router(); +$conn = new mysqli(env("DB_HOST"), env("DB_USER"), env("DB_PASSWORD"), env("DB_DATABASE")); +/* +$database = new Medoo([ + 'type' => 'mysql', + 'host' => env("DB_HOST"), + 'database' => env("DB_DATABASE"), + 'username' => env("DB_USER"), + 'password' => env("DB_PASSWORD") +]); +*/ $data = json_decode(file_get_contents("php://input"), true); $routesDirectory = __DIR__ . "/routes"; $getFolderContentUtil = null; -$getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentUtil, $router, $data) { +$getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentUtil, $router, $data, $database) { $files = scandir($dir); $filter = '/\.php$/'; @@ -32,17 +44,12 @@ $getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentU if(!is_dir($path)) { if(empty($filter) || preg_match($filter, $path)) { $error = false; - - try { - $response = require $path; - } catch(exception $e) { - $error = $e; - } + $response = require $path; if(!empty($response) && is_array($response)) { foreach($response as $key => $fn) { - $router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error) { - $responseResult = $fn($data, getallheaders(), $error, $router); + $router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error, $database) { + $responseResult = $fn($data, getallheaders(), $database, $error, $router); if($responseResult) { echo json_encode($responseResult); } diff --git a/api/routes/gamedetails.php b/api/routes/gamedetails.php index f2813e4..21da317 100644 --- a/api/routes/gamedetails.php +++ b/api/routes/gamedetails.php @@ -2,10 +2,10 @@ return [ - "get" => function($data) { + "get" => function($data, $headers, $db) { return [ "message" => "hello api" ]; } - + ]; diff --git a/composer.json b/composer.json index 1614aea..8e6fff0 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ }, "require": { "bramus/router": "1.5", - "vlucas/phpdotenv": "^5.4" + "vlucas/phpdotenv": "^5.4", + "catfan/medoo": "^2.1" } } diff --git a/composer.lock b/composer.lock index 767d33b..1716197 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f9a46f2ec9409fb54193fe711c55753b", + "content-hash": "a72d198969a21ad6ddd90355a3227085", "packages": [ { "name": "bramus/router", @@ -57,6 +57,82 @@ }, "time": "2020-10-26T09:50:48+00:00" }, + { + "name": "catfan/medoo", + "version": "v2.1.7", + "source": { + "type": "git", + "url": "https://github.com/catfan/Medoo.git", + "reference": "74be58f80d330d1075ef3bfd0fb3d057349597ae" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/catfan/Medoo/zipball/74be58f80d330d1075ef3bfd0fb3d057349597ae", + "reference": "74be58f80d330d1075ef3bfd0fb3d057349597ae", + "shasum": "" + }, + "require": { + "ext-pdo": "*", + "php": ">=7.3" + }, + "require-dev": { + "phpunit/phpunit": "^9.0" + }, + "suggest": { + "ext-pdo_dblib": "For MSSQL or Sybase database on Linux/UNIX platform", + "ext-pdo_mysql": "For MySQL or MariaDB database", + "ext-pdo_oci": "For Oracle database", + "ext-pdo_pqsql": "For PostgreSQL database", + "ext-pdo_sqlite": "For SQLite database", + "ext-pdo_sqlsrv": "For MSSQL database on both Window/Liunx platform" + }, + "type": "framework", + "autoload": { + "psr-4": { + "Medoo\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Angel Lai", + "email": "angel@medoo.in" + } + ], + "description": "The lightweight PHP database framework to accelerate development", + "homepage": "https://medoo.in", + "keywords": [ + "database", + "database library", + "lightweight", + "mariadb", + "mssql", + "mysql", + "oracle", + "php framework", + "postgresql", + "sql", + "sqlite" + ], + "support": { + "issues": "https://github.com/catfan/Medoo/issues", + "source": "https://github.com/catfan/Medoo" + }, + "funding": [ + { + "url": "https://paypal.me/AngelaonLai", + "type": "custom" + }, + { + "url": "https://opencollective.com/medoo", + "type": "open_collective" + } + ], + "time": "2022-07-02T04:27:50+00:00" + }, { "name": "graham-campbell/result-type", "version": "v1.0.4", From 1ca64e40ae11264eb89bf61f695b2b5ee27f7981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Fri, 2 Sep 2022 15:34:29 +0200 Subject: [PATCH 04/13] working database --- api/.env | 2 +- api/index.php | 4 +--- api/routes/gamedetails.php | 1 + public/.env | 39 -------------------------------------- public/index.php | 15 --------------- 5 files changed, 3 insertions(+), 58 deletions(-) delete mode 100644 public/.env diff --git a/api/.env b/api/.env index 7208794..72ef434 100644 --- a/api/.env +++ b/api/.env @@ -1,6 +1,6 @@ PRODUCTION=false -DB_HOST="hz-mysql6" +DB_HOST="db.db030.webglobe.com" DB_USER="mysql85033" DB_PASSWORD="troglodyt" DB_DATABASE="mysql122279" diff --git a/api/index.php b/api/index.php index 8ba706a..30cef15 100644 --- a/api/index.php +++ b/api/index.php @@ -18,8 +18,6 @@ function env($key) { $router = new \Bramus\Router\Router(); -$conn = new mysqli(env("DB_HOST"), env("DB_USER"), env("DB_PASSWORD"), env("DB_DATABASE")); -/* $database = new Medoo([ 'type' => 'mysql', 'host' => env("DB_HOST"), @@ -27,7 +25,7 @@ $database = new Medoo([ 'username' => env("DB_USER"), 'password' => env("DB_PASSWORD") ]); -*/ + $data = json_decode(file_get_contents("php://input"), true); $routesDirectory = __DIR__ . "/routes"; diff --git a/api/routes/gamedetails.php b/api/routes/gamedetails.php index 21da317..8a5c30f 100644 --- a/api/routes/gamedetails.php +++ b/api/routes/gamedetails.php @@ -3,6 +3,7 @@ return [ "get" => function($data, $headers, $db) { + var_dump($db); return [ "message" => "hello api" ]; diff --git a/public/.env b/public/.env deleted file mode 100644 index 42a89c3..0000000 --- a/public/.env +++ /dev/null @@ -1,39 +0,0 @@ -#database configuration - -#database host -DB_HOST="localhost" -#database user -DB_USER="root" -#database user password -DB_PASS="" -#database name -DB_NAME="test" -#database port -DB_PORT="mysql" - -#should connect to database? (false if you don't want to use database) -DB_ENABLED=false - -#production flag (true if you want to use production settings) -PRODUCTION=false -#error reporting (true if you want to report errors) -ERROR_REPORTING=true - -#character set for whole application -CHARSET="utf-8" - -#should force redirect to https? (true if you want to use https) -FORCE_HTTPS=false - -#jwt token authentication - -#jwt key for token authentication -#online secure passwords generator - https://www.grc.com/passwords.htm -#generate key by PHP - $jwtKey = bin2hex(random_bytes(32)); -JWT_KEY="" -#encryption algorithm for jwt token -JWT_ALGO="HS256" -#jwt token server name -SERVER_NAME="http://localhost" -#jwt token expiration time in seconds -JWT_EXPIRATION_TIME=3600 diff --git a/public/index.php b/public/index.php index b8bef02..e3a6413 100644 --- a/public/index.php +++ b/public/index.php @@ -1,18 +1,3 @@ load(); - -function env($key, $default = null) { - global $envFile; - - if(isset($envFile[$key])) { - return $envFile[$key]; - } - - return $default; -} - -header('Content-Type: text/html; charset=' . env("CHARSET")); require_once(__DIR__ . '/index.html'); From 47e829bc78fc81b98e5baf73c8ade24d35b4a2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Fri, 2 Sep 2022 15:47:58 +0200 Subject: [PATCH 05/13] get game details --- api/routes/game/details.php | 13 +++++++++ api/routes/gamedetails.php | 12 -------- src/lib/utils/api.js | 57 +++++++++++++++++++++++++++++++++++++ src/routes/game.svelte | 11 +++++++ vite.config.js | 9 ++++++ 5 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 api/routes/game/details.php delete mode 100644 api/routes/gamedetails.php create mode 100644 src/lib/utils/api.js diff --git a/api/routes/game/details.php b/api/routes/game/details.php new file mode 100644 index 0000000..30cb620 --- /dev/null +++ b/api/routes/game/details.php @@ -0,0 +1,13 @@ + function($data, $headers, $db) { + + return $db->select('games4', '*', [ + 'url' => $data["gameurl"] + ])[0]; + + } + +]; diff --git a/api/routes/gamedetails.php b/api/routes/gamedetails.php deleted file mode 100644 index 8a5c30f..0000000 --- a/api/routes/gamedetails.php +++ /dev/null @@ -1,12 +0,0 @@ - function($data, $headers, $db) { - var_dump($db); - return [ - "message" => "hello api" - ]; - } - -]; diff --git a/src/lib/utils/api.js b/src/lib/utils/api.js new file mode 100644 index 0000000..2191833 --- /dev/null +++ b/src/lib/utils/api.js @@ -0,0 +1,57 @@ +async function send({ method, path, body, token, headers }) { + const opts = { method, headers: {} } + opts.headers['Content-Type'] = 'application/json' + + if (body) { + opts.body = JSON.stringify(body) + } + + if (headers) { + opts.headers = Object.fromEntries(headers.map(({ key, value }) => [key, value])) + } + + if (token) { + opts.headers['Authorization'] = `Bearer ${token}` + } + + const res = fetch(path, opts) + .then(async (r) => { + if (r.status >= 200 && r.status < 400) { + return await r.text() + } else { + return await r.text() + } + }) + .then((str) => { + try { + return JSON.parse(str) + } catch (err) { + return str + } + }) + + return res +} + +export function get(path, token = null) { + return send({ method: 'GET', path, body: null, token }) +} + +export function del(path, token = null) { + return send({ method: 'DELETE', path, body: null, token }) +} + +export function post(path, body, token = null) { + return send({ method: 'POST', path, body, token }) +} + +export function put( + path, + body, + token = null, + headers = [] +) { + return send({ method: 'PUT', path, body, token, headers }) +} + +export const hostName = 'http://localhost:8000' diff --git a/src/routes/game.svelte b/src/routes/game.svelte index 5e34c27..af5d70f 100644 --- a/src/routes/game.svelte +++ b/src/routes/game.svelte @@ -5,7 +5,18 @@ import Section from '../lib/Section.svelte' import Button from '../lib/Button.svelte' + import * as api from '$lib/utils/api' + let assets = Array.from(Array(4).keys()).map((_, i) => `/assets/temp/kar${i + 1}.png`) + + ;(async () => { + console.log(api.hostName) + const gameData = await api.post(`${api.hostName}/game/details`, { + gameurl: 'ludva-hra5', + }) + + console.log(gameData) + })() diff --git a/vite.config.js b/vite.config.js index b2c7b0a..e9e7307 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,9 +1,18 @@ import { defineConfig } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte' import sveltePreprocess from 'svelte-preprocess' +import path from 'path' // https://vitejs.dev/config/ export default defineConfig({ + server: { + port: 3003 + }, + resolve: { + alias: { + $lib: path.resolve(__dirname, 'src', 'lib'), + }, + }, plugins: [ svelte({ preprocess: sveltePreprocess({ From c730a6d19f0bb2b7aad3254c27e28b7b11800a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Fri, 2 Sep 2022 16:11:52 +0200 Subject: [PATCH 06/13] get whole game details --- api/routes/game/details.php | 46 +++++++++++++++++++++++++++++++++++-- src/routes/game.svelte | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/api/routes/game/details.php b/api/routes/game/details.php index 30cb620..809da2d 100644 --- a/api/routes/game/details.php +++ b/api/routes/game/details.php @@ -1,13 +1,55 @@ function($data, $headers, $db) { + $imagesHostName = 'https://geohry.skolazdola.cz/'; - return $db->select('games4', '*', [ - 'url' => $data["gameurl"] + $gameUrl = $data['gameurl']; + + $gameDetails = $db->select('games4', '*', [ + 'url' => $gameUrl ])[0]; + $gameDetails["questions"] = $db->select('questions4', '*', [ + 'url' => $gameUrl + ]); + + $thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg"; + $gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null; + + foreach($gameDetails["questions"] as &$question) { + $questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg"; + $question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null; + } + + return $gameDetails; } ]; diff --git a/src/routes/game.svelte b/src/routes/game.svelte index af5d70f..18de6d7 100644 --- a/src/routes/game.svelte +++ b/src/routes/game.svelte @@ -12,7 +12,7 @@ ;(async () => { console.log(api.hostName) const gameData = await api.post(`${api.hostName}/game/details`, { - gameurl: 'ludva-hra5', + gameurl: 'pisek-mesto-kralovske', }) console.log(gameData) From a51abc7ec648065e7da0a10407a2d946d3b75855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Fri, 2 Sep 2022 16:22:39 +0200 Subject: [PATCH 07/13] prevent unset --- api/routes/game/details.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/routes/game/details.php b/api/routes/game/details.php index 809da2d..0dd5d69 100644 --- a/api/routes/game/details.php +++ b/api/routes/game/details.php @@ -31,6 +31,10 @@ return [ "post" => function($data, $headers, $db) { $imagesHostName = 'https://geohry.skolazdola.cz/'; + if(!isset($data['gameurl']) || empty($data['gameurl'])) return [ + "error" => "Property 'gameurl' is not set." + ]; + $gameUrl = $data['gameurl']; $gameDetails = $db->select('games4', '*', [ From 6a983426952f3c860ef62215d759915297154687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= <39367469+lewis-wow@users.noreply.github.com> Date: Wed, 7 Sep 2022 16:46:38 +0200 Subject: [PATCH 08/13] Fix hostName --- src/lib/utils/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/utils/api.js b/src/lib/utils/api.js index 2191833..c20c4c4 100644 --- a/src/lib/utils/api.js +++ b/src/lib/utils/api.js @@ -54,4 +54,4 @@ export function put( return send({ method: 'PUT', path, body, token, headers }) } -export const hostName = 'http://localhost:8000' +export const hostName = 'https://erant.cz/api' From 5f71c3a05843a40bb891a158e1d721de2df58c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 8 Sep 2022 09:19:43 +0200 Subject: [PATCH 09/13] Fix cors --- .gitignore | 1 + api/.htaccess | 15 ---- api/routes/game/details.php | 59 ------------ composer.json | 2 +- package-lock.json | 140 +++++++++++++++++++++++++++++ package.json | 3 +- public/.htaccess | 15 ++-- {api => public/api}/.env | 0 {api => public/api}/index.php | 12 +-- public/api/routes/game/details.php | 74 +++++++++++++++ {api/utils => public}/cors.php | 10 +-- public/index.php | 29 +++++- src/lib/utils/api.js | 13 +-- src/routes/game.svelte | 1 - 14 files changed, 268 insertions(+), 106 deletions(-) delete mode 100644 api/.htaccess delete mode 100644 api/routes/game/details.php rename {api => public/api}/.env (100%) rename {api => public/api}/index.php (87%) create mode 100644 public/api/routes/game/details.php rename {api/utils => public}/cors.php (84%) diff --git a/.gitignore b/.gitignore index dd1fdf3..20e96dc 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ dist-ssr *.sw? # php +/public/vendor /api/vendor diff --git a/api/.htaccess b/api/.htaccess deleted file mode 100644 index daad08f..0000000 --- a/api/.htaccess +++ /dev/null @@ -1,15 +0,0 @@ - - Order Allow,Deny - Deny from all - - - - RewriteEngine On - - # RewriteCond %{HTTPS} off - # RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] - - RewriteCond %{REQUEST_FILENAME} !-f - RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule . index.php [L] - diff --git a/api/routes/game/details.php b/api/routes/game/details.php deleted file mode 100644 index 0dd5d69..0000000 --- a/api/routes/game/details.php +++ /dev/null @@ -1,59 +0,0 @@ - function($data, $headers, $db) { - $imagesHostName = 'https://geohry.skolazdola.cz/'; - - if(!isset($data['gameurl']) || empty($data['gameurl'])) return [ - "error" => "Property 'gameurl' is not set." - ]; - - $gameUrl = $data['gameurl']; - - $gameDetails = $db->select('games4', '*', [ - 'url' => $gameUrl - ])[0]; - - $gameDetails["questions"] = $db->select('questions4', '*', [ - 'url' => $gameUrl - ]); - - $thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg"; - $gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null; - - foreach($gameDetails["questions"] as &$question) { - $questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg"; - $question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null; - } - - return $gameDetails; - } - -]; diff --git a/composer.json b/composer.json index 8e6fff0..ae334d7 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "config": { - "vendor-dir": "api/vendor" + "vendor-dir": "public/vendor" }, "require": { "bramus/router": "1.5", diff --git a/package-lock.json b/package-lock.json index b46d7fa..da28d06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "geohry", "version": "0.0.0", "dependencies": { + "axios": "^0.27.2", "svelte-routing": "^1.6.0" }, "devDependencies": { @@ -95,6 +96,11 @@ "node": ">= 8" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/autoprefixer": { "version": "10.4.7", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.7.tgz", @@ -128,6 +134,15 @@ "postcss": "^8.1.0" } }, + "node_modules/axios": { + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "dependencies": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -245,6 +260,17 @@ "fsevents": "~2.3.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -282,6 +308,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -685,6 +719,38 @@ "node": ">=8" } }, + "node_modules/follow-redirects": { + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fraction.js": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.2.0.tgz", @@ -888,6 +954,25 @@ "node": ">=12" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", @@ -1515,6 +1600,11 @@ "picomatch": "^2.0.4" } }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "autoprefixer": { "version": "10.4.7", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.7.tgz", @@ -1529,6 +1619,15 @@ "postcss-value-parser": "^4.2.0" } }, + "axios": { + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "requires": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + } + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1600,6 +1699,14 @@ "readdirp": "~3.6.0" } }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1626,6 +1733,11 @@ "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", "dev": true }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -1833,6 +1945,21 @@ "to-regex-range": "^5.0.1" } }, + "follow-redirects": { + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "fraction.js": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.2.0.tgz", @@ -1986,6 +2113,19 @@ "sourcemap-codec": "^1.4.8" } }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, "min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", diff --git a/package.json b/package.json index 45afb62..340ddde 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "dev": "vite", "build": "vite build", "preview": "vite preview", - "php": "php -S localhost:8000 -t ./api" + "php": "php -S localhost:8000 -t ./public" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.1", @@ -20,6 +20,7 @@ "vite": "^3.0.0" }, "dependencies": { + "axios": "^0.27.2", "svelte-routing": "^1.6.0" } } diff --git a/public/.htaccess b/public/.htaccess index daad08f..833f494 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -1,15 +1,10 @@ - + Order Allow,Deny Deny from all - - RewriteEngine On +RewriteEngine On - # RewriteCond %{HTTPS} off - # RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] - - RewriteCond %{REQUEST_FILENAME} !-f - RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule . index.php [L] - +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule . index.php [L] diff --git a/api/.env b/public/api/.env similarity index 100% rename from api/.env rename to public/api/.env diff --git a/api/index.php b/public/api/index.php similarity index 87% rename from api/index.php rename to public/api/index.php index 30cef15..3fc8db0 100644 --- a/api/index.php +++ b/public/api/index.php @@ -1,11 +1,6 @@ 'mysql', 'host' => env("DB_HOST"), @@ -49,7 +42,10 @@ $getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentU $router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error, $database) { $responseResult = $fn($data, getallheaders(), $database, $error, $router); if($responseResult) { - echo json_encode($responseResult); + $responseResult["status"] = array_key_exists("status", $responseResult) ? $responseResult["status"] : 200; + http_response_code($responseResult["status"]); + echo json_encode($responseResult["data"]); + exit(); } }); } diff --git a/public/api/routes/game/details.php b/public/api/routes/game/details.php new file mode 100644 index 0000000..3d2b55e --- /dev/null +++ b/public/api/routes/game/details.php @@ -0,0 +1,74 @@ + function($data, $headers, $db) { + function remoteFileExists($url) { + $curl = curl_init($url); + + //don't fetch the actual page, you only want to check the connection is ok + curl_setopt($curl, CURLOPT_NOBODY, true); + + //do request + $result = curl_exec($curl); + + $ret = false; + + //if request did not fail + if ($result !== false) { + //if request was ok, check response code + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + + if ($statusCode == 200) { + $ret = true; + } + } + + curl_close($curl); + + return $ret; + } + + $imagesHostName = 'https://geohry.skolazdola.cz/'; + + if(!isset($data['gameurl']) || empty($data['gameurl'])) return [ + "data" => [ + "success" => false, + "message" => "Property 'gameurl' is not set." + ] + ]; + + $gameUrl = $data['gameurl']; + + $gameDetails = $db->select('games4', '*', [ + 'url' => $gameUrl + ]); + + if(!$gameDetails) return [ + "data" => [ + "success" => false, + "message" => "Invalid 'gameurl'." + ] + ]; + + $gameDetails = $gameDetails[0]; + + $gameDetails["questions"] = $db->select('questions4', '*', [ + 'url' => $gameUrl + ]); + + $thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg"; + $gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null; + + foreach($gameDetails["questions"] as &$question) { + $questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg"; + $question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null; + } + + return [ + "success" => true, + "data" => $gameDetails + ]; + } + +]; diff --git a/api/utils/cors.php b/public/cors.php similarity index 84% rename from api/utils/cors.php rename to public/cors.php index b2c6a43..cb7bd5f 100644 --- a/api/utils/cors.php +++ b/public/cors.php @@ -1,5 +1,4 @@ setBasePath('/'); + +$router->mount("/api", function() use($router) { + require_once __DIR__ . '/api/index.php'; +}); + +$router->set404('/api(/.*)?', function() { + header('HTTP/1.1 404 Not Found'); + header('Content-Type: application/json'); + + echo json_encode([ + "message" => "Route not defined." + ]); +}); + +$router->get("/.*", function() { + header('Content-Type: text/html; charset=UTF-8'); + echo "frontend!"; +}); + + +$router->run(); diff --git a/src/lib/utils/api.js b/src/lib/utils/api.js index 2191833..fb1c0b5 100644 --- a/src/lib/utils/api.js +++ b/src/lib/utils/api.js @@ -1,13 +1,18 @@ +export const hostName = 'https://erant.cz/api' + async function send({ method, path, body, token, headers }) { - const opts = { method, headers: {} } - opts.headers['Content-Type'] = 'application/json' + const opts = { method, headers: new Headers(), mode: 'cors' } + opts.headers.append('Content-Type', 'application/json') + opts.headers.append('Access-Control-Allow-Origin', '*') if (body) { opts.body = JSON.stringify(body) } if (headers) { - opts.headers = Object.fromEntries(headers.map(({ key, value }) => [key, value])) + for (const [k, v] of Object.entries(headers)) { + opts.headers.append(k, v) + } } if (token) { @@ -53,5 +58,3 @@ export function put( ) { return send({ method: 'PUT', path, body, token, headers }) } - -export const hostName = 'http://localhost:8000' diff --git a/src/routes/game.svelte b/src/routes/game.svelte index 18de6d7..c9990e3 100644 --- a/src/routes/game.svelte +++ b/src/routes/game.svelte @@ -10,7 +10,6 @@ let assets = Array.from(Array(4).keys()).map((_, i) => `/assets/temp/kar${i + 1}.png`) ;(async () => { - console.log(api.hostName) const gameData = await api.post(`${api.hostName}/game/details`, { gameurl: 'pisek-mesto-kralovske', }) From f8f8f53de90760f17b158589559df219fd5e632d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 8 Sep 2022 09:36:26 +0200 Subject: [PATCH 10/13] question parsing --- package-lock.json | 153 +++------------------------------ package.json | 4 +- src/lib/utils/parseQuestion.js | 16 ++++ src/routes/game.svelte | 3 +- 4 files changed, 32 insertions(+), 144 deletions(-) create mode 100644 src/lib/utils/parseQuestion.js diff --git a/package-lock.json b/package-lock.json index da28d06..3149f77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,8 +8,8 @@ "name": "geohry", "version": "0.0.0", "dependencies": { - "axios": "^0.27.2", - "svelte-routing": "^1.6.0" + "svelte-routing": "^1.6.0", + "yallist": "^4.0.0" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.1", @@ -96,11 +96,6 @@ "node": ">= 8" } }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" - }, "node_modules/autoprefixer": { "version": "10.4.7", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.7.tgz", @@ -134,15 +129,6 @@ "postcss": "^8.1.0" } }, - "node_modules/axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", - "dependencies": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" - } - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -260,17 +246,6 @@ "fsevents": "~2.3.2" } }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -308,14 +283,6 @@ "node": ">=0.10.0" } }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -719,38 +686,6 @@ "node": ">=8" } }, - "node_modules/follow-redirects": { - "version": "1.15.1", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", - "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/fraction.js": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.2.0.tgz", @@ -954,25 +889,6 @@ "node": ">=12" } }, - "node_modules/mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dependencies": { - "mime-db": "1.52.0" - }, - "engines": { - "node": ">= 0.6" - } - }, "node_modules/min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", @@ -1534,6 +1450,11 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "dev": true }, + "node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + }, "node_modules/yaml": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", @@ -1600,11 +1521,6 @@ "picomatch": "^2.0.4" } }, - "asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" - }, "autoprefixer": { "version": "10.4.7", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.7.tgz", @@ -1619,15 +1535,6 @@ "postcss-value-parser": "^4.2.0" } }, - "axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", - "requires": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" - } - }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1699,14 +1606,6 @@ "readdirp": "~3.6.0" } }, - "combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "requires": { - "delayed-stream": "~1.0.0" - } - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1733,11 +1632,6 @@ "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", "dev": true }, - "delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" - }, "detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -1945,21 +1839,6 @@ "to-regex-range": "^5.0.1" } }, - "follow-redirects": { - "version": "1.15.1", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", - "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==" - }, - "form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "requires": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - } - }, "fraction.js": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.2.0.tgz", @@ -2113,19 +1992,6 @@ "sourcemap-codec": "^1.4.8" } }, - "mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" - }, - "mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "requires": { - "mime-db": "1.52.0" - } - }, "min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", @@ -2467,6 +2333,11 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "dev": true }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + }, "yaml": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", diff --git a/package.json b/package.json index 340ddde..0cd73d1 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "vite": "^3.0.0" }, "dependencies": { - "axios": "^0.27.2", - "svelte-routing": "^1.6.0" + "svelte-routing": "^1.6.0", + "yallist": "^4.0.0" } } diff --git a/src/lib/utils/parseQuestion.js b/src/lib/utils/parseQuestion.js new file mode 100644 index 0000000..126ff44 --- /dev/null +++ b/src/lib/utils/parseQuestion.js @@ -0,0 +1,16 @@ + +export default (question, questionType) => { + switch (questionType.toLowerCase()) { + case 'text': + return question + case 'number': + return Number.parseFloat(question) + case 'choice': + return question.split(/;\s*/).map(item => ({ + label: item.startsWith('*') ? item.substring(1) : item, + value: item.startsWith('*') ? true : false + })) + default: + return null + } +} diff --git a/src/routes/game.svelte b/src/routes/game.svelte index c9990e3..2023c99 100644 --- a/src/routes/game.svelte +++ b/src/routes/game.svelte @@ -6,6 +6,7 @@ import Button from '../lib/Button.svelte' import * as api from '$lib/utils/api' + import parseQuestion from '$lib/utils/parseQuestion' let assets = Array.from(Array(4).keys()).map((_, i) => `/assets/temp/kar${i + 1}.png`) @@ -14,7 +15,7 @@ gameurl: 'pisek-mesto-kralovske', }) - console.log(gameData) + console.log(parseQuestion(gameData.questions[0].answer, gameData.questions[0].type)) })() From 51d73fac2b5ef25636a20a20b495389ae4c202d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 8 Sep 2022 13:10:11 +0200 Subject: [PATCH 11/13] add google analytics --- index.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.html b/index.html index 13496b3..50684fa 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,14 @@ Title + + + From 9c850ac88b909ad669397d60d1872c7cbe228374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludv=C3=ADk=20Prokopec?= Date: Thu, 8 Sep 2022 13:10:57 +0200 Subject: [PATCH 12/13] Update title --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 50684fa..08cd2ac 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@ - Title + Erant diff --git a/vite.config.js b/vite.config.js index b2c7b0a..e9e7307 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,9 +1,18 @@ import { defineConfig } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte' import sveltePreprocess from 'svelte-preprocess' +import path from 'path' // https://vitejs.dev/config/ export default defineConfig({ + server: { + port: 3003 + }, + resolve: { + alias: { + $lib: path.resolve(__dirname, 'src', 'lib'), + }, + }, plugins: [ svelte({ preprocess: sveltePreprocess({