Monday, 28 March 2016

Why I gave up my MMO for Stardew Valley

If you're a gamer then you've probably heard of Stardew Valley, the Indie-made, RPG/Agriculture/Crafting, 2D sprite-based game developed by just one guy.
At first glance it may not look like much. I was very sceptical upon purchase, but after the raving reviews, and me seeking something easy-going in my downtime it seemed like a good fit. Yeah.. downtime. Right. I'm about 30 hours in now and I can't pull my eyes away from it. I haven't logged into my MMO for 2 weeks because I'm just wondering what's going to happen in Stardew Valley.

This is not just some farming game where you plant crops and wait for them to grow. In fact, I've done very little farming. Most of my time is spent fighting monsters and collecting minerals in the mines, at the beach attempting to haul in legendary fish, and even trying to woo Haley so she'll accept my marriage proposal (Spoiler: she did).
Stardew Valley is simply amazing. You never know what's around the corner; will there be a letter in the mailbox in the morning? Will someone be at your door? What adventures await you in the mines? What happens if I unlock X? There's just so much to do that I can't write it all down here.
The RPG elements come into play because you can actually level your stats, like Combat, Foraging, Farming, Fishing. After level 5 and 10 in each you get to pick a profession. For example, with Fishing at level 5 you can choose to receive 25% gold on Fish sold, or better chances with traps.
So what turned into my "just on the side" game, has actually become my one and only game. I'm hooked.

I'm actually going to leave this review half-finished because just talking about this game is making me want to open it back up, so I'll leave you with a link to Steam where you can buy it and try for yourself. If what I've written above sounds even remotely interesting to you, then you will not be disappointed!

Stardew Valley on Steam for Windows - http://store.steampowered.com/app/413150/


Free DNS Hosting with Afraid.org

I'm not being sponsored or commissioned to write this in any way or form, but I just have to recommend http://freedns.afraid.org/ if you're looking for reliable DNS hosting with exceptional support.
Josh, the guy running freedns.afraid.org has been hosting my DNS for over six years now and I've not once had any problems, and offers everything I would ever need. There's even some freebie subdomains you may use if you don't own your own domain!
While the service is free, you can subscribe for added benefits, like extra subdomains, stealth flags, and Wildcard DNS. This, of course, is purely optional. The site has no annoying ads - he relies solely on premium members, but if you don't want to upgrade, then no problem. The cheapest package is only $5/month, which is very little considering the amazing service provided.

Checking for element changes in the DOM

There's a few different ways to check for element changes in the DOM. One of them is kind of deprecated and the other, known as MutatationObserver, while neat, won't work below IE 11. The following sample checks for our empty element "foo", waits until it detects a 'p' element is added then will stop its timer and alert you. I've set it to 750ms, but you can tweak that to whatever your needs are.

I needed something like this to dynamically add a class to the first line in an unordered list that was generated by a third party tool. I had to wait until the information was actually passed back before I could do anything with it, because obviously at the beginning it never existed in the DOM.

var fooCheck = setInterval(function() {
    console.log("Looking for foo");
    if (document.querySelector('#foo p')) {
        console.log("Found foo!");
        clearInterval(fooCheck);
    }
}, 750);

And that's it. The document.querySelector('#foo p') line is similar to doing a $('#foo').find('p') in jQuery. So it's checking to see if a "p" element was added. We can simulate adding one by using something like the following

// simulation of adding an element to #foo after 6 seconds
var foo = document.querySelector('#foo');
setTimeout(function() {
    var p = document.createElement('p'),
        t = document.createTextNode('Hello, Foo!');

    p.appendChild(t);

    // the following line adds our new element to the DOM
    // which should trigger our check to stop running
    foo.appendChild(p);
}, 6000);

Sunday, 27 March 2016

Ajax with plain Javascript

I know jQuery is pretty awesome, but sometimes I just like to do things myself if the project is small enough and doesn't need everything it has to offer. Being a tinkerer, I like to know what's going on in the background anyway. Here's a small class you can use to build your own ajax function without the need for jQuery at all

(function() {
    // prototype to return an array of keys from an associative array
    Object.prototype.keys = function() {
        var keys = [];
        for (var k in this) keys.push(k);
         return keys;
    };

    // main Ajax class
    var Ajax = {
       // turns an associative array into a valid url parameter structure
       'param': function(obj) {
            var parts = [];
            for (var i in obj) {
                if (obj.hasOwnProperty(i)) {
                    parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
                }
            }
            return parts.join("&");
        },

        // add a parseJSON function if you want also
        'parseJSON': function(str) { return JSON.parse(str); },

        // shortcut to our Ajax functionality
        'ajax': function(t, r, p, fn) { this._loadAjax(t, r, p, fn); },

        // the main ajax code - where the magic happens
        _loadAjax: function(o, obj=false) {
            var xmlHttp,
                t = 'get',
                r = o.url;

            if (o.hasOwnProperty('type'))
                t = o.type;

            if (window.XMLHttpRequest)
                xmlHttp = new XMLHttpRequest();

            // wait for the ajax response to take place
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    // return the response into a function, or add it directly into DOM
                    if (obj) {
                        if (typeof obj == 'function')
                            obj(xmlHttp.responseText);
                        else
                            obj.innerHTML = xmlHttp.responseText;
                    }
                    else {
                        console.error("Last ajax parameter must be a node to insert data to or a function");
                    }
                }
            };

            // convert our parameters into url query string style
            if (o.hasOwnProperty('params')) {
                if (Object.keys(o.params).length > 0)
                    r = r + "?" + this.param(o.params);
                }

            xmlHttp.open(t.toUpperCase(), r, true);
            xmlHttp.send();
        }
    };

    // make the Ajax class global so we can access it anywhere
    window.Ajax = Ajax;
})();

Now we have the Ajax class, to use it you can simply do...

Ajax.ajax({
    'type': 'get',
    'url': 'http://localhost/api/foo',
    'params': {
        'user_id': 5,
        'status': 'active',
    }
}, function(data) {
    // parse the json response and list keys with values
    var res = Ajax.parseJSON(data),
        key;

    for (key in res) {
        console.log("%s => %s", key, res[key]);
    }
});

Saturday, 26 March 2016

Chrome OS as a developer workstation

I've always been fascinated by Chrome OS, and Chrome devices in general (Chromebooks, Chromebase, Chromebox). They are attractive, fast and affordable. I've always used Google products, so going full Chrome OS just seemed natural to me anyway, but being a developer I wondered if it would work. The short answer is 'yes'. I bought the LG Chromebase, which is basically like an iMac, but for Chrome OS. It's a monitor and computer in one basically. For just £200 I received the Chromebase, a keyboard and mouse - pretty good value, and I've been using this setup at work as my main developer machine.

There she is in all her glory. You can get more information on the Chromebase here. As far as I know, LG is the only supplier of them at the time of writing, but I believe Acer has one in the works, just not available in the UK yet. I'll be testing for a bit longer, and if it works out I may end up upgrading to the Samsung Chromebox instead, which offers more RAM and a much better processor. You get 4GB RAM and an i5 instead of 2GB and Celeron. You can see the Chromebox here.


I'll talk briefly about the pros and cons I discovered while working with Chrome OS as my main workstation.


The Pros

  • It's fast. Like, real fast. I get into work, flick the switch and I'm ready to go. Chrome OS itself is built to be light, and with the SSD it just makes things even quicker.
  • It's beautiful. Aesthetics is not important to everyone, but if I'm going to be looking at the same thing for 8 hours a day I want it to be appealing. Chrome OS has you covered there.
  • It's secure. Some people don't like this OS because "you can't do anything", but that's only true if you leave everything as is. By flipping the developer switch on and unlocking your file system, you'll get access to everything you need. I say unlocking your file system because out of the box, it will be read only. I kind of like this idea in a way, as long as there is a way of making it writable when you need, and there is.
  • Restoring is easy. Somehow bodged your system? No worries. Chrome OS makes it easy to install a recovery system to a USB drive and get you up and running in no time. Just make sure you know what model your device is.

The Bads

  • The devices generally come with a very small amount of local storage space. Most of them are 16GB standard, but you can buy 32GB ones too. Developer environments can sometimes be huge, so this isn't really enough. To get around this I actually do a majority of my work on a remote Linux box I ssh into from Chrome OS, so most of the storage is on that.
  • Not everything is apparent at first. Chrome OS, while Linux at it's core, does not mean you can just jump in and begin developing. As mentioned above, you need to think about storage for one. You'll also need to flick on developer mode if you want to do anything useful. You will also want to create a script to unlock the file system and make it writable. By default you'll only be able to write to the Downloads folder. Then there's the problem of "What do I use for X instead of my usual Y".
  • Some things are just really hard to achieve. As an example, we use CalDAV at work to sync our calendars. If we used Google Calendar in our business I'd be laughing all the way to the bank, but unfortunately that isn't the case. So far I haven't been able to find a decent CalDAV alternative on Chrome OS. There was definitely no native apps that I could find. Photo editing applications are also limited when put next to other programs, like Gimp. Sometimes the alternatives you do find are acceptable, but not optimal. You just have to deal with it.

There is probably more points to go on each list, but these are the primary ones I could find personally. Your mileage may vary.

Conclusion

I'm having a lot of fun putting this device to the test as a replacement for my developer machine. The storage space and sometimes lack of application alternatives is probably the most daunting thing to me, but I've gotten by so far. If I decide to continue using it, I'll most likely write my own software to replace what I feel is missing, like the lack of CalDAV support. It's been a week so far, and I've found nothing serious enough for me to go back to my old desktop. If your business is based heavily around Google products, this could make an ideal workstation for you. For the price you can't really go wrong either, and there seems to be a lot of support for them out there if you need it as the market grows, and it is.