Handling player inputs on neocities
How do I get user's commands off neocities and onto my hard drive?
Neocities only serves static resources. There's no server-side scripting at all. Which is nice, it means you don't get people forging absolute terror on your backend. Everything is served as-is. A user visits a site, you give them exactly what you gave everyone else.
I want to put my text adventure up on neocities.
Why not put it on a 'real' website? I doesn't have the same CHARM. I want it to feel olde and rustique.
I can do this, since each page of the adventure is completely static. It's just HTML with a little CSS.
The problem comes in when I want a user to be able to actually submit commands, like the text parser adventures of yore. Since neocities has no server-side scripting, this has to be processed off-site, on my own server. And then stored and retrieved later, so I can read the responses and write an update.
So, I've written a small server (literally about 200 lines of Rust) to handle and store submissions, the neocities site can just serve a small amount of JavaScript to handle sending a message over there.
The Details
The browser's view of this is very simple. When the user types and hits 'Go!' at the bottom of the lastest page of the adventure, JavaScript has to:
- send a very small request to a server somewhere in the cloud
- Wait for the server to reply
- Tell the user their command has(n't) been submitted
Simple.
The server which handles submissions has to do a little more work though. It has to:
- Wait for an incoming request
- Check if its been sent from the expected website (my neocities)
- Parse and read the request
- Write the user's input to disk, to be read later
Not to mention that, at some point, this will need to be able to:
- block malicious IP addresses
- perform rate-limiting
- etc. etc. you get the dril
I haven't done this yet because I genuinely don't expect more than four people to ever submit commands. I love you all though xoxo
If you want to see the code for it, you can't ehehehehe. I may put it up online amongst a greater corpus in the future. If I do, I'll update this post.
The future
In the future, the scope of this (currently tiny) project may or may not expand to include:
- an API to read/delete submissions as an authenticated user
- a web frontend to do this in the browser
- more fleshed-out support, for distinct adventures or explicit pages
- support for different origins (so different sites can host their own adventures)
- more advanced responses
- automatically closing the input box when submissions for that page are closed
And so on and so forth. If it turns out this program is useful, I might end up doing this stuff.
Anyway. Salutations. Have a nice day.