From a89d530cd086d044969b09bb420c2df7d0fdc4f3 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 11 Oct 2024 18:17:38 -0500 Subject: [PATCH] Work on world rendering --- database/auth.db | Bin 45056 -> 45056 bytes database/live.db | Bin 188416 -> 188416 bytes database/live.db-shm | Bin 32768 -> 0 bytes database/live.db-wal | Bin 379072 -> 0 bytes public/assets/css/dragon.css | 15 ++ public/assets/css/forms.css | 2 - src/controller/world.php | 46 +++-- src/helpers.php | 37 +++- src/util/database.php | 10 +- templates/components/char_select_box.php | 2 +- templates/pages/world/base.php | 236 ++++++++++++++--------- 11 files changed, 219 insertions(+), 129 deletions(-) delete mode 100644 database/live.db-shm delete mode 100644 database/live.db-wal diff --git a/database/auth.db b/database/auth.db index 7598b137bb7f74eb0d7115e5d129b90fbd738145..b9667c8a2676e9884a334461e3afa31e6dafeb22 100644 GIT binary patch delta 16 YcmZp8z|`=7X~QL1M&`|zWpyV306nt?$^ZZW delta 16 YcmZp8z|`=7X~QL1MuE+jWpyV306qZ**8l(j diff --git a/database/live.db b/database/live.db index d7abbac70f21ed0c818214a9e0a8546e3cc5a007..ef11fe4b7e66dbe70523aa0ca43573c654935bce 100644 GIT binary patch delta 236 zcmXAitqKBB5QXRd?CxCu&J_{Fu+ca2VQdDQ48xn`5oTGbdwm=!c8(j7Qi>an zV;3oAc0{vNsnaf-;Fv*&jbTnprZn?OozQQ(6ka+!rdrWhtxf(IQF lS!RWxt)NX@$OgBfSFXd0V)g$_!X*wG5mBPF5v{`5`X51iE5!f+ delta 227 zcmW-au?oU45QdX9jj65vaS+6z;QRPOZXJ9CL9*%SrbG~@=-?oooW#LFoSekLRlFYD z|K0s?@KuMrqM6FAQtR?8+VBD|JaEGWM{Kad5*~Ys4?Q=*+($JSnj)3f+QK;A&SbnZ zeCBmjW#1&+6CniX&*-M^eo<*xUP%%J-lTcrPMQ-U2?ALl$;`Y= zUPgWgc;VMhg6N8k1PGmf<;9OL(WSeK)X3>`;?wQT+vQb$+jYhLL-?2V!P!9s=f20q z=Vsg#iRHl?T>r))j^0!O1r$&~0R~X*mC!GD@ zc1sjcKmi5*Lm-i4Qb{M1Y;p-vKoKQ`DW{TZYN@A@W?E^Fbx-P|hdu@vVuUd!m|})G z7Fc41HKJ^?%`W>Ka?I&xSEm2?k#{PffC36Apnw7jD4>7>3Mim}0tzUgfC36Apnw7j qD4>7>3Mim}0tzUgfC36Apnw7jD4>7>3Mim}0tzUgfC379C-4Cq5Jw3B diff --git a/database/live.db-wal b/database/live.db-wal deleted file mode 100644 index bb590ed1326e66d110db6cdab163ba1b42930faf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 379072 zcmeI*dz4i5VZia7=guswGm^kTYC|GNW8#Ept$Iv|njDWwZLOlD21tBXw6sEwL1O|! zLVdPLY^*Wam}*ufM5NjT25qw~KXIjqb=I$=A+_^iC zkLS#cJ3D*t{e6DFzlQJm-4o_z=eK;|*P~lniY?{;N>jR%U7a00>mU8h$+JHC%#X`& z{r~sRIO#X99KLbnz3qinEp3I4mcq8ee-ye4PZm}bmK5$QbQU@a%YsKo`EUG3fB*pk z1PBlyK!5-N0t5(DNg!LuByzc2A$w5e^`LBf<+VLKyz)9cTdcemv-!$vzOcO|_;_wu zwzcweYc^YX4PGj*nQSVP$mepY;BpJW=d$U_+v(uP@#V*!?+Yf5_-tn9LnB{mEBr3_ zUSMhYdx6b`=Y#JBeo=TN_(ou9;qk)y;CX44M(wZ!2oNAZfB*pk1PBlyK!Cu&0-1C! zmnc-852`%3SDuGgo{N>|d?uaGB~q3DZ>>CME6kF^z1)`sBMqut0ym; znpiUL3B5oyCe<+s5FkK+009C72oNAZfWQF@MAi%B`{)G@{gZE;a{00!j2@sDc+LI- z2Rw%kK!5-N0t5&UAV7cs0RjXD7N}w`kO?*x2(}kU1i3(He}Umo{qmOYOle;?KrgV% z{sIF}o!=55K!5-N0t5&UAV7csfhr0dU@wrbTv}^7_+lU#-d|wCEl+Qrf8wHJ^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>S{ zUb!b=zav-O22yh%opFdQ!h}1Yr-WF zAV7cs0RjXF5FkK+0D+d42z!C>{RsZLd+yC+SKqooFJP$=AV7cs0RjXF5FkK+0D&PC zh^H4w2P5otKZ3bO-Lw1MSInNF7Z}31cWMFz2oNAZfB*pk1PBly(7!;0y+As2KY|O+ zeeBz-j{ox)^#c7*fZq@xK!5-N0t5&UAV7csfvO9{(hFomy}-^bD^KlNJ!+j^pz1U1 z1Ox~WAV7cs0RjXF5FkKcFapu_0@-jc@Wz)mw2wRNXBX=Q1~WDOh5!Kq1PBlyK!5-N z0t5&|BoIq4&>HFmMjZLce;&E`=3Dgw5zV8+5FkK+009C72oNAZfB=DL1)}Q(TEo4- z!AGyYchslve?~75?W8#x0RjXF5FkK+009C72oTsWfmnKhT&Nd#>8P;_=bk?8Z}kHE zHCsMRfB*pk1PBlyK!5-N0tDg`h^`mNg?oYjT3I|QHSWfQULdaNbRGf(2oNAZfB*pk z1PBly(1$=Qy+A(H3%uilfAnX!|L5sr^a6d%iMI$4AV7cs0RjXF5FkK+z|ads*9+vs zy}-k3@B6Et-f+@AdV!&zXjedh009C72oNAZfB*pkO)L;gFVGh11!kQz`RIko)(iCl zO}zFDod5v>1PBlyK!5-N0t9MVAi7?lE!+!yYxAaUD<@wzPcKl*OTe`eAV7cs0RjXF z5FkK+K*I~f(+i{m$MNMyX-apptFxo$rrTzXJ@3R_EqZ~5UttDFfB*pk1PBlyK!5-N z0`(~nVK0y>1_Y%t!=CR2-oAVEJ?9LYbGBZfJ{O0pCP07y0RjXF5FkK+0D%S(h^H4w z23oy(fh8l(dgiz*C;U(^&>-u`FbEJJK!5-N0t5&UAV8qe1S0GOlA&H;&K+|;{^;y; zp4SUB+Ojbo0t5&UAV7cs0RjXF5U4AGSbBkBp978gr^009C7 V2oNAZfB*pk%_9(9FEA`w{2z{*k0k&A diff --git a/public/assets/css/dragon.css b/public/assets/css/dragon.css index 66b0f2b..6a2a0c9 100644 --- a/public/assets/css/dragon.css +++ b/public/assets/css/dragon.css @@ -324,6 +324,10 @@ span.badge { background-color: #444c55; color: white; } + + &.green { + background-color: #a6e3a1; + } } .my-1 { margin-bottom: 0.25rem; margin-top: 0.25rem; } @@ -517,3 +521,14 @@ body::-webkit-scrollbar-thumb { border-color: #3D444C #2F353B #2C3137; box-shadow: 0px 1px 0px 0px rgba(255, 255, 255, 0.2) inset; } + +#canvas-container { + & > canvas { + display: block; + width: 100%; + height: 440px; + image-rendering: pixelated; + image-rendering: crisp-edges; + background-color: rgba(0, 0, 0, 0.5); + } +} diff --git a/public/assets/css/forms.css b/public/assets/css/forms.css index 5b77591..7ae6c25 100644 --- a/public/assets/css/forms.css +++ b/public/assets/css/forms.css @@ -86,8 +86,6 @@ & > span.selected { display: none; - margin-left: 1rem; - color: #a6e3a1; } } diff --git a/src/controller/world.php b/src/controller/world.php index ef5c110..de5d989 100644 --- a/src/controller/world.php +++ b/src/controller/world.php @@ -15,42 +15,46 @@ function world_controller_get() */ function world_controller_move_post() { - auth_only_and_must_have_character(); csrf_ensure(); + /* + This endpoint is used to move the character around the world. The client sends a POST request with the direction + they want to move the character. The server will update the character's position in the database and return the + new position to the client. + + We should only be using this endpoint as an AJAX request from the world page. Since we don't need all the character's + data to move them, we can just get and update their lcoation using the user's currently selected character ID. + */ + + ajax_only(); auth_only(); csrf_ensure(); + + define('directions', [ + [0, -1], // Up + [0, 1], // Down + [-1, 0], // Left + [1, 0] // Right + ]); // direction must exist - $direction = $_POST['direction'] ?? false; - - // direction must be valid; 0-3 are sent from the client - if (!is_numeric($direction) || $direction < 0 || $direction > 3 || $direction === false) router_error(999); + $d = (int) $_POST['direction'] ?? -1; // Update the character's position // 0 = up, 1 = down, 2 = left, 3 = right $x = location('x'); $y = location('y'); - switch ($direction) { - case 0: $y--; break; - case 1: $y++; break; - case 2: $x--; break; - case 3: $x++; break; + if (isset(directions[$d])) { + $x += directions[$d][0]; + $y += directions[$d][1]; + } else { + router_error(999); } - // Update the character's position $r = db_query(db_live(), 'UPDATE char_locations SET x = :x, y = :y WHERE char_id = :c', [ ':x' => $x, ':y' => $y, - ':c' => char('id') + ':c' => user('char_id') ]); - // If the query failed, throw an error if ($r === false) throw new Exception('Failed to move character. (wcmp)'); - // If this is an HTMX request, return the new world page - if (is_htmx()) { - echo render('pages/world/base'); - exit; - } - - // Redirect back to the world page - redirect('/world'); + json_response(['x' => $x, 'y' => $y]); } diff --git a/src/helpers.php b/src/helpers.php index 85d0a0b..a17b027 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -59,12 +59,7 @@ function csrf() */ function csrf_verify($token) { - if (hash_equals($_SESSION['csrf'] ?? '', $token)) { - $_SESSION['csrf'] = token(); - return true; - } - - return false; + return hash_equals($_SESSION['csrf'] ?? '', $token); } /** @@ -196,7 +191,7 @@ function location($field = '') $GLOBALS['location'] = db_query( db_live(), "SELECT * FROM char_locations WHERE char_id = :c", - [':c' => char('id')] + [':c' => user('char_id')] )->fetchArray(SQLITE3_ASSOC); } @@ -254,7 +249,33 @@ function ce($condition, $value, $or = '') /** * Get whether the request is an HTMX request. */ -function is_htmx(): bool +function is_htmx() { return isset($_SERVER['HTTP_HX_REQUEST']); } + +/** + * Get whether the request is an AJAX (fetch) request. + */ +function is_ajax() +{ + return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; +} + +/** + * Limit a request to AJAX only. + */ +function ajax_only() +{ + if (!is_ajax()) router_error(418); +} + +/** + * Return a JSON response with the given data. + */ +function json_response($data) +{ + header('Content-Type: application/json'); + echo json_encode($data); + exit; +} diff --git a/src/util/database.php b/src/util/database.php index 83c6cdc..e7cb560 100644 --- a/src/util/database.php +++ b/src/util/database.php @@ -1,5 +1,7 @@ "> - Active + Active diff --git a/templates/pages/world/base.php b/templates/pages/world/base.php index 55dec50..d7e7095 100644 --- a/templates/pages/world/base.php +++ b/templates/pages/world/base.php @@ -1,113 +1,163 @@ -

World

Use WASD keys to move the character

-

Current location: ,

+

Current location: ,

- - -
- - -
+
+ +
-