Game update!

I have made a great amount of progress on my game so far, at least as I see it. But before we get to that — and we will! — I wanted to show off my new living room workspace. I’m fortunate to live in a 4 1/2 (two bedroom) apartment by myself, so I have a spare room. The spare room has been the office for just about a year, now, and that’s where I primarily do, you know, work. My previous remote-working experiences have shown me that I’m way too likely to work too hard and too long if the work area is in my living area, so I keep work and life as separate as possible. (Although this past week, I’ve worked exclusively from my living room because I have an air conditioner in here but not in the office. As a result, I’m pretty sure I worked about 42 or 43 hours this week. Oops?)

As for my gaming, my writing, my coding, all of these things have always happened in my living room where my desktop computer is located. Recently, I got rid of the desk I’d had for like almost 20 years, in favour of an identical desk to the one I have in the office. Then, I also got a VIVO dual monitor, desk-mounted stand for my screens in the living room, as well as a boom arm for my Yeti Blue microphone. Oh, and a great little under-the-desk headphone holder, which I’m loving.

Here’s what it all looks like.

Desk, dual monitor stand, boom microphone stand, headphone holder
New living room workspace setup!

And I’m adoring it. Sure, the wires are absolutely revolting, but I’m not going to cable manage anything just yet, because I’m on the cusp of getting a new computer. I’m going to be attempting to assemble it myself from the various component parts. I’m not much of a hardware person (although I’ve installed RAM, a secondary HD, a primary HD and a video card), so my friend, Andrew, has sworn to me that he’ll FaceTime me and walk me through the assembly if I need a hand. So with a new computer imminent, cable-managing right now is dumb. Once that’s all set up, though, you can bet your bippy I’ll be doing my best to hide all those unsightly cables.

So the new workspace is pretty great for the time being and having the monitors mounted to the stand is AMAZING. I’ve reclaimed so much desk space I hadn’t even realized I was missing. I genuinely like sitting here and my HUE lights just make me happy. 🙂

Okay, so that’s the update on the workspace in my living room. Now, on to the game stuff!

Once upon a time, I used to chat on IRC (Internet Relay Chat) pretty consistently. Like, I’d go to bed for the night, but just leave my computer and IRC client running, and I would always have my IRC channels in the background while I was working. I mean, I was working as an independent contractor while I was the Chatting Online Guide at About.com, sooooo I’d have to say that was pretty reasonable. Anyway, I made friends online, as one does, and several of them were in Australia. Australia has a pretty significant time difference between the East Coast of North America and, well, virtually anywhere in Australia, so getting to chat with the Aussies usually meant someone was up late or up early.

One day, I woke up and stumbled to my desk and blearily tabbed over to mIRC and, in one of my channels, one of the Aussies had been freaking out several hours prior and had eventually gone all-caps in her exclamations and said “WAKE UP I NEED TO ENTHUSE!!!!!!!”

I have had this deep need to enthuse about my game for a couple of days now, and I don’t really have a lot of people in my life who, a) care about this little project of mine, and b) know anything about programming. So that’s why I’m enthusing here, I guess. 😉

Updating my personal site last week is what got me itching to code this week, because I’d played around with self-submitting forms, which is currently how I’m testing out question/answer functionality. So I sat down to try to finish getting PDO to work for me on my test page.

I’d also made a change to my docker environment in that I created three database users that are limited in their permissions — one can edit the users table, one can edit the tokens table and one can read the questions table. So that involved making sure I was using the limited questions user rather than the root user, too.

For some reason, my MySQL PDO command wasn’t asking one question in isolation, which pointed to an issue with my select statement, which, in turn, pointed to an issue with how I was “randomly” selecting a question row. Once that was sorted, my question was being asked! Hooray! Being able to pull in that question from the array felt pretty great. Of course, in order to get to the point where I was able to replicate what I’d done when I was messing around back in January, I was going to need to be able to check the answer and inform the user if they were right or not.

So my first major issue was “how to carry over the user’s answer to the page upon submission?”, but it ended up not being that hard because I remembered I could just grab the $_POST[‘playeranswer’] data and stash that in a variable. I also strtolower() it, because all the database answers are in lowercase, so for a match, the cases need to match, too.

That worked, but if I wanted to also display the last question, along with its correct answer, and the user’s own answer (plus the correct/incorrect validation), I was going to need to pass along the row from the last question. So I pass that along, currently, in a hidden form field so I can snag that from the $_POST[‘lastquestionrow’] field.

When you load the page on a GET, you don’t have any of that, but when you load it on a POST, you now get last question row and player answer stored into their proper variables. Then, the database is called and we start getting into the fun stuff:

  • hey, here was the row for the last question, please display for me the question and the answer
  • also here’s what the player said, so show that
  • if they match, say so and grant the player a point, announce new score
  • if they don’t match, say so, don’t grant a point, repeat score
  • if they don’t match but player has no score, say so, don’t grant a point and let the player know they currently have no score

Now, for the most part, this worked just fine. However, I realized, after having done this, why my teachers in my certificate always recommended I go for a does not equal comparison rather than a strict equality comparison. It’s because a strict equality comparison is messed up, yo. I noticed, through my testing, that I was occasionally getting false positives while going through answers that had more than one possible answer and if one of the possible answers has more than one word. So like, what’s the capital of Canada was fine, because that’s Ottawa. But for asking about the capital of the province of Quebec, that’s where issues cropped up. Your choices are “quebec” or “quebec city”. And yet, somehow, with my earlier logic, “city” was acceptable.

In another example, “flash” was an acceptable answer instead of “barry allen” or “the flash”.

And, in the weirdest example, the name “david” was accepted when neither answer was even remotely close to “david”.

So there was something wrong with my comparison logic. At first, I thought it was just that I was using == instead of ===, so I thought okay, stick in a third equals sign there and I’m good! That’s what makes it a strict comparison, right? Right. Should be sorted!

Alas, no, the issues continued, but I went to bed.

The next day, the strange bugginess was in the back of my head all day long. When I sat down to code a bit after work, I really could have rewritten the logic to test for a failed match rather than a positive match, but I was curious as to what in tarnation was going on. How is “david” considered equivalent to one of two completely unrelated names? (Adam and Carter, if you’re curious.)

I sat down and did more robust testing, writing down all the weird results. So I pulled out my Programming PHP O’Reilly book and looked up comparisons, because everything I’d googled was telling me that === should do the trick.

And this is where I learned about the string compare function: strcmp()

The way I understand it is that strcmp() will compare two strings and will return a 0 if they’re equal. So, this then meant I needed to tweak my logic again. A simple “if $string1 === ($string2 || $altString2)” type of statement wasn’t going to work. I needed to funnel the player’s last answer into a use of strcmp() and compare it with the last answer as well as the last alt answer, if the alt answer existed. So here’s what I ended up with.

$mainVerificationCheck = strcmp($lastPlayerAnswer, $lastAnswer);
if ($altAnswerExists == true) {
$altVerificationCheck = strcmp($lastPlayerAnswer, $lastAltAnswer);
}
if(($mainVerificationCheck === 0) || ($altVerificationCheck === 0))

Basically, that says, here’s the mainVerificationCheck variable, which is comprised of a value returned by using the string compare function. By default, this compares the variable containing the last player answer as well as the last correct answer from the last question. Additionally, if the last question had an alternate answer (like in our Quebec City/Quebec question), create a variable called altVerificationCheck and use the string compare to test the last player answer against the last alternate answer.

Then, if the main check returns a 0 (which means a match) or if the alt check returns a 0, then do stuff. (In this case, it’s notify the user they’re right and their points have increased.)

I’ve been playing around with Instagram stories and captured my bug hunting and fix. Here are the pics from that.

Funky bugs in my game.
Bug hunting!

 

=== or strcmp()?
strcmp()? What is this?

 

Fixed my comparison issue
strcmp() for the win!

 

So yeah, I got a lot done over the last couple of days. I worked on stuff on Thursday night and then Friday night well into Saturday morning. And I was going to start in on registration and user creation tonight — my Docker containers are up and running, I have a fresh branch of master that I’ve checked out to a new branch called registration, but I’ve had a headache pretty much all day Saturday and it’s like, four in the morning on Sunday, so I should probably go to bed. I’d have gone to bed way earlier but my headaches are largely sinus-based, so lying down is actually super painful when my sinus cavities are full. Gravity helps the head to drain, I guess!

So my headache meds seem to be kicking in and I probably won’t be able to get back to coding until at least the week of the 6th, because a friend of mine’s getting married on the 4th, and I’m a bridesmaid, so I have a lot of stuff to do this week, then I go to Ontario for the wedding on the weekend. Looking forward to finalizing my computer build either this week or next. I think the new computer will encourage me to do even more coding, and possibly not at ridiculously late hours of the night!

When I started talking about this project, back in January, I was thinking an alpha version might be available in last September.

… yeah, not sure that’s going to happen, hahaha. The base functionality of questions and answers? Sure. But not the game itself.

Anyway, the next task is going to be user registration and login, so I can start saving stuff to the user table and saving stuff to sessions. I want to not have repeat questions, so I’m going to save a certain number of questions in the session (I think?) and check that when doing the random question picker. I think.

Maybe. We’ll see how this goes. I’ll definitely keep you, whoever you are, dear reader, posted on my progress.

 

The Great SSL Weekend Extravaganza

This past weekend, I embarked upon a task of positively Herculean proportions. With Chrome 68 coming out and the continued emphasis by Google to ensure HTTPS is the default on sites, rather than HTTP, I decided to go ahead and add SSL certificates from Let’s Encrypt to, oh, 13 domains that I host, both for myself and for friends and family. (I had a lot of fun posting to my Instagram story about it, too!)

I’d done a test run with one of my domains earlier in the year and, though it wasn’t without a challenge or two, it seemed to go just fine.

The trouble, of course, came when dealing with various WordPress sites (including this one!) because how do you change ALL THE LINKS in ALL THE POSTS and ALL THE PAGES to HTTPS from HTTP? Well, thankfully, there are database queries and free plugins.

While I’ve generated and installed the certificates for all thirteen domains, one of them basically has no traffic, so I haven’t updated that WordPress database yet. Then, there’s another one that is just too daunting for me to bother with right now — two WordPress installations, plus my own hand-coding elsewhere, plus redirects galore… So I said to hell with it for now. I haven’t forced SSL on for either, but both are accessible via HTTPS. When I have more time, I’ll go through them both and force SSL on, after dealing with updating the references in the databases.

In addition to that, I redid my personal homepage at juliemartin.org. Not only does it also have SSL enabled (and forced), but it’s also mobile-responsive! It’s not as pretty as I’d like it, but it’s not bad for a quick redo. I also used PHP Mailer to create a contact form. Additionally, I put in a hidden field that humans can’t see (owing to CSS) as a honeypot for spammers and if that field is filled out, then the form fails to send.

It was in the creation of this form that I realized how much the form’s enctype matters, because if you choose the wrong one, it’s entirely possible that your $_POST array will be completely empty. It worked fine on my own local environment, but I suspect it was getting wiped out by my host’s mod_security in Apache, because I did get a couple of 406 errors, though they weren’t consistent. I spent a good amount of time on Saturday evening wondering why the hell my $_POST array was empty, checking everything, from making sure the form was set to POST to double, triple, quadruple checking the field names.

In the end, the enctype was the issue and everything ran beautifully once I fixed that up. I used Postman to test out a variety of differences between my browser’s POST request and finally found it.

Then I was able to easily code in the hidden field and the failure if there’s anything in it. (Fun fact: I’m pretty certain that the placeholder text in a form does, indeed, count when your conditional is if (!empty ($_POST[’emailaddress’]) though it would most certainly require more testing.)

Overall, I did a crapton of stuff this weekend, I learned a ton of stuff and my brain hurts sufficiently.

In other news, making moves towards ordering parts for a new computer. I’m getting pretty excited about it, not gonna lie.

Next post, pics of my living room setup, I promise. I just got a desk-mounted dual monitor stand from Amazon, which I am in love with and really makes my workspace look awesome.

Until next time!

PDO, why must you hate me so?

My glorious four-day weekend is just about over and I did a bunch of coding and no writing. I did, however, sit happily in air-conditioned bliss. When the humidity makes it feel like 46C (114F), you kind of don’t want to move, much less leave an air-conditioned room.

I made a lot of progress on switching things over to PDO from mysqli, but it’s still not done. I’m still flailing around, trying to figure out wtf the problems are that I’m experiencing. Here are some of the issues I’ve had so far this weekend, all of which are resolved:

  • I had an errant space in <?php. And that borked everything that came after it. Why I didn’t see the space between the question mark and the p is unknown.
  • I couldn’t figure out how to copy a file to the directory above (and thus, outside) the web directory in my PHP Docker container.
  • I further couldn’t figure out how to actually edit a file in that directory once I was able to copy it, but then inserted a nano install in my Dockerfile. (My original linux/unix experiences were heavy on pine, tin and pico.)
  • I decided to actually create and use limited-access users for the database calls (as one should) and then had a hell of a time ensuring they could connect to the MySQL Docker container.
  • Even with limited-access users being valid users, I couldn’t run a simple select statement.

All that’s been taken care of. Now it’s a matter of actually writing the code to get a question pulled from the database, displayed to the user and then matching their typed answer with the answer in the database. I gave it a couple of half-hearted attempts tonight, once I finally got one of my database users to properly make a select statement, but figured I’d accomplished enough.

A lot of it this weekend was making sure things were being done right. It’s hilarious how I’m still not to the point where I was when I was writing things with mysqli. Just need more time. This is not going to be simple and it’s not going to be solved quickly.

Speaking of “quickly”, the process of starting up my Docker containers is not so much with the speed. I obviously desperately need to update my computer, because there’s not a lot going on in that startup process, but it takes — and I timed it — upwards of eight minutes to get both the PHP and MySQL containers running. Now, this didn’t pose a huge problem in PHP II, because the containers were configured by our teacher, so we literally did docker-compose build and docker-compose up to start up and then docker-compose down when we were done. So it’s one thing to spend eight minutes or thereabouts waiting for your containers to come up when you do it once, but a whole other thing to do it numerous times in a day. Come to think of it, there’s probably a way to refresh the configuration/etc without killing the containers and starting anew, but that bears more investigation.

And speaking of refreshing configurations and such, I spent a couple of hours updating my IRC server, services and proxy monitor on Sunday. I’m a terrible person because I don’t update often and, when I do, it’s usually because a new major release has come out. As such, I generally have to start from scratch because various updates are incompatible with my existing  configuration file. But not this time! So I backed everything up (like, twice) and then proceeded to install over my IRC server with the new version and then restarted the server and that worked fine. Same with services. Same with the proxy monitor.

I’d set aside three hours for it and it barely took two — and that included all the backing up and reading I was doing to ensure I knew what the hell I was doing. It went so smoothly! I fully expect the earth to open up and swallow it, now.

And on that note, I should go to bed. Back to work tomorrow and I’ll be working from my air-conditioned living room instead of my non-air-conditioned office.

I’ll post some pics of my living room desk with and without my work laptop next time! I’m really loving how it’s turned out.