Day 19
Started working on implementing state handling for the Creeps. I now have them correctly facing the targets harvesting energy from sources and upgrading the room controller by processing the actionLog
object in their JSON to determine their current state.
But while testing the state changes I ran into the old issue of my test room getting screwed up again. This time though, I noticed that it seemed to happen when I added more road construction sites to the room. When I removed them the problem resolved itself.
So I decided to dig a little deeper and discovered that it was a problem with my network layer, specifically the processing of the websocket data for the room event streams.
When the server sends data over the websocket it’s in one of two formats:-
A space delimited string for command responses
"[command] [param...0] [param...n]"
Or a JSON array for stream events
[{"<channel>:<stream>", { <JSON Object> } ]"
I’ve been using a regular expression to check for a match on the stream event format and when that match fails I just assume it’s a space delimited command response.
But it turns out that there was am “undocumented feature” in my regular expression for the stream event matching and the capture I was using for the JSON object part which caused it to fail to parse the JSON correctly.
So, I’ve been studying up on PCRE (PHP style) regular expressions. Regular expressions have always been an area I’ve struggled with so I would really appreciate it if there’s anyone reading this with more experience than me who might take a quick look at what I’ve come up with and perhaps give some advice on the correct way to achieve the same results.
For example, the JSON will be in the following formats:
"[{"room:W9N8", { Any valid JSON here }]"
or
"[{"user:02c131b4b35fc35/console", { Any valid JSON here }]"
My goal is to capture 3 different parts:
- The channel - “room” or “user”
- The stream - “W9N8” or “02c131b4b35fc35/console”
- The JSON object -in this case, anything after the “,” and contained in the “{ }” but ending at the closing array bracket “]”
I’ve come up with the following RegExp
^\["(\w+):([\w\/]+)"({.+})\]$
Which seems to work but I am not 100% sure that the last capture group “({.+})” will capture the entire JSON object and not get confused because the JSON object can have internal arrays [] and objects {} so I am concerned that the parser might stop when it hits the first “]” or “}” instead of capturing the entire object. The entire string is delimited by “[” and “]”
Anyway, if any regular expression experts are interested in taking a look I have some test data and my regex here and would really appreciate your input.
Cheers,
Shecks