Verifying furniture with the Apartment Server

Today the server side of furniture placement begins.

I need to make sure of a few things...

  • The player has the item in question
  • The item is NOT placed
  • The item does not get placed on the client until confirmed by the server.
  • The item is saved to the database correctly and loads in the correct spot on relog.
  • The function handling data server-side should sanitize any strings. (Don't think there are any but this is very important to keep in mind with all incoming data.)
  • The functions on the server side have all their arguments statically typed.

If I get this done today I can finish my little midi radio furniture item and add some sound to the game. I should be able to do this inside an hour but we can't really account for odd bugs and such. Though we have logs all over the place to help with finding em.

Anyway, gonna noodle on my guitar and drink some caffeine so I can get to it.

Alright, written some queries to check if the furniture exists in the inventory and can be placed. Now I need to consider how to handle some things on the client here... if I just send back an "OK" they could swap the item in memory so it plops in whatever they want. This would be undone on relog and would hypothetically not be an issue server-side.

However I want to take a moment and consider it anyway, it would be foolish to trust the player. The player is in one of 3 categories; Idiot, Asshole, Just here to have fun. While I like the latter and wish everyone was like that I need to plan for the former, because I have been the former in a past life.

Knowing that furniture could have buffs or whatnot the player might try to force the furniture they want in the placement. I will handle any buffing or interaction with furniture server-side and make sure the item in question IS in fact the item the client claims so this shouldn't be a problem. I don't want to send superfluous data or do any pointless processing. I guess an "OK" will be fine for now. We'll see later if it is or not when I start hacking my own game.

Welp, here we go, probably breaking crap.

I can use a LIMIT statement on an UPDATE'S WHERE clause, right? Man it's been a while since I learned SQL and all these ORMs have caused a lot of the information to become quite hard to recall. Thankfully there's references available.

UPDATE apartment_house_inventory_item
SET in_use = 'true'
WHERE house_inv_id = '%s' AND item_id = '%s' ORDER BY in_use LIMIT 1;
I wonder how I screwed this one up.

Attempt one. Might require some parenthesis, I don't know right now. Everything is kinda foggy. There's a lot that has to succeed to even get to this point in the code so we'll see how it goes up to here.

Had a few things to hammer out client-side, I think I figured out most of that, but I seem to have screwed up and passed the wrong ID somewhere in the server. Time to dig through the PostgreSQL logs, may offer a clue.

Looks like I swapped the item id and apartment id somewhere.

Yep, it was flipped in the player data handler. Nice one brain.

It occurs to me that I have already extracted the data from the database by this point and have it stored on the player's node in the server. I could have just been working with that the whole time. I don't know why I didn't. Cloudy, tired brain forgot I had done all that already I guess.

This did not need to be as complicated as it was. Why, brain? Why have you forsaken me?

WELP, let's rewrite it in a much more efficient way. Dammit, brain. All I needed to do was create a function to convert the data stored in the player node to UPDATE statements or INSERT statements as the case may be.

You know what? I'ma do a little refactoring.

I will add a sub node for the apartment and it's inventory and all the management therein. Within it I will have all the functions to CRUD the inventory and then I will write a few functions in the DBMAN to deal with the data. Damn idiot.

This is why lack of sleep is bad... I am just glad I managed to catch it before I got too much further.

Well, I made the apartment node and got all that refactored, need to refactor my furniture placement function and inventory check.

But first I need to eat, I am getting swarthy.

Ate my rice, drank my tea, did some work.

It seems to have been placed.
I see.

So it's sending the wrong position data, I think. Also the ID is incorrect.

Yeah, figured that might happen.
0,0. Yep. That old chestnut. Wrong item ID too.

So, first I need to fix the query so it works right, then I need to figure out why I am getting bad data.

Well, that answers my early question on whether or not I could use a LIMIT clause on the end of that statement. Fair enough, time for docs.

Wow, my brain is just not even registering words right now. My eyes see letters but the words just don't get in. This is... I guess the stress is really getting to me now. I'm going to take a shower and come back to this...

Yeah, shower didn't help. Looking like I am not getting any better today... I am so frustrated and angry for some reason, probably stress and lack of sleep, really. Life's not going so great these last handful of years, let's just say. Moreso lately, it turns out.

Okay, for some reason I am having a really bad time writing this query. I could write one that selects the ID of the first thing that returns fitting my specifications, then update THAT row I guess. I was trying to write a CTE for it but for some reason I am just unable to think about it clearly right now.

Did it work?
It appears so.

While it may not be the most efficient and optimized way of doing it, it's what I am currently capable of. It will likely come back and bite me in the ass but at least I have a log of where it went wrong.

Inventory is updated on relog but I need to handle it when the item is placed.
Data looks fine in the DB.

Alright, now to add the code to update the inventory when an item is placed.

Should just be calling the update inventory RPC on the client, if I did it right... uh, let's give a character another table to test it with?

Basic Table 2 has no icon yet, but it's there in it's ? form.
Plop.

and I got a weird call from a restricted number which caused me to drop my phone on the escape key which closed the client (like it should for now) but yeah, let's try that one again, shall we?

This
... goes here.

Now to relog and make sure...

Alas, a duplicate.

I see. Already hacking my own game.

Hmm. Seems like the UPDATE isn't UPDATING the row. The code does work for the PlayerApartment node, but for whatever reason I screwed up something in the DB.

Well, that would explain it.

Should be 6.

It's selecting the wrong ID.

That might help.

This is why sleep is important, kids.

Careful, a wrasslin' match might break out in here.

So if I did it right there should be a basic table in the inventory when I relog but no question mark.

Must have done it right.

I still have to deal with removing the furniture as well, sending it back to the inventory.

To do that I will need to write a few more queries, of course. Wonder how many mistakes I will make here. Normally I am all for making mistakes and learning but today I am just salty and tired.

So to remove furniture and put it back in the inventory I guess I will use a right-click on a furniture item.

This means I will need some kind of click mask for the item and have a popup appear asking if you want to remove the item. On confirm, we will tell the server then queue_free() the item.

The server will then look up an item matching that description, remove it from the furniture, set in_use to false and update it in the DB.

I'm hoping I am not making any big mistakes, normally I wouldn't work on stuff like this when I am feeling so busted but I really just have to. I'll just have to fix my mistakes later.

Every furniture item will need a ClickMask set, better if it fits the item. I could write some code to sample the sprite and make a mask based on transparent pixels but this will require less work at runtime.

Clickmasks wired up.

Now to display a popup, then on confirm queue_free(), etc.

Confirmation.
Poof.

Only working on the client right now, gotta write some RPCs and DB queries.

And returned to inventory.

Now to relog again and see if we're all good.

Seems like it.

Welp. That's furniture placement... I think?

I'll just try placing and removing an item multiple times, that'll probably show me a bug.

He got robbed.
Returned to inventory
Further testing.

Okay, looks like we can place and pick up furniture.

Such a simple thing it would be if not for the server and database. But where would the fun be in that? It wouldn't be a MORPG either, now would it?

It's about 4pm and I am kaput. I will finish my little midi radio and clock out.

Radio. Sorta.
Radio friend.
Now we got space tables and tunes.

I set up an interaction node on furniture items that can be interacted with, so when you click them they fire their interaction. Other items without and interaction node will do nothing, of course.

So when I click the radio it turns on, when I click it again it turns off. Simple. Starts off, of course.

I got about half an hour, maybe I make a little particle emitter on here when it's on that shows music notes?

Nice. The acid's kicking in.
Or that.

Por que no los dos?

Added an animated texture with some randomness in the animation but no dice, I will have to figure that out later. Time to deploy the server and get the hell out of the office.