PCRE and newlines (in PHP)

I just wasted far too much time trying to work out why a regex would work for my example test cases but not with real data. Turns out “.”, DOT_ALL, and newlines are more complicated than I realised.

My problem essentially came down to not knowing that the IMAP messages I was working with used CRLF and that my OS/shell/applications were silently converting text to LF when I was constructing my test cases.

nikic has a good post covering many details with PCRE and newlines

Posted on

Guildwars 2 BWE Impressions

The BWE is now over. I thought I’d record some of my thoughts (good and bad) from my play through this weekend. Overall I played ~25 hours split over two characters, a level 14 char Elementalist and a level 21 Norn Guardian.

Note: I’ve been following GW2 for a while now, and while getting hyped up, I still knew what to expect for the most part.. With that, I had high hopes and I think they were almost all met.

Being in Australia the latency wasn’t great. Not horrible, but there was a delay on using all my abilities that made the game feel a little sluggish. I got used to it in PvE, but it might affect PvP a lot more.

Graphical the game looked great, and for a GW player getting to see familiar places was pretty awesome (e.g. Temple of Ages, and the Wizards tower off the coast of the Kessex Hills).

Performance was pretty poor on my system (laptop with a GTX-485), but this is widely reported and being worked on.

I’m still not 100% sold on skills being tied to weapons. Some combinations seem very situational and others seem very balanced with no real need to weapon swap at all (e.g. Greatsword with the Guardian). I wonder if getting rid of the auto-attack for another skill would help even out some of the weapon sets (while moving the auto-attack to an innate ability). The Elementalist however was great with it’s wide selection of skills regardless of weapon.

I was surprised at the personal stories, I was expecting a separate story for each race will little flavour quests based on your character questions, but while levelling with another Norn we had completely different stories through out the BWE (last quest was level 18).

However, the pacing of the personal stories seemed a little odd, it was very hard to keep up in level if you just stayed in your races zones (even if you did ever heart/event/POI/etc). Going to another zone for a while was a simple solution but not really intuitive. I wonder if it would be better if their personal quests made you explore (or at least travel) to other zones?

I enjoyed most hearts and events (the swamp event was pretty damn awesome). Though with many people they turned into zergs, I think 3-6 people was the most enjoyable.

Overflow… Overflow and parties… I’m told it will be fixed by release. I certainly hope so.

In general I thought the UI was OK. It wasn’t very intuitive but it was functional. I would like to see a better skill selection system, an explanation to exactly what the core stats do, and a guild panel that makes sense (I’m still not sure if you invite a character or an account).

Not sold on the Gem store either. Most things in there I didn’t mind, but some of the boosts and things like bag slots irked me a little. Maybe with the Gold → Gem conversion is won’t be that bad.

Crafting was way too complicated, I can see what they were going for, but there’s just too many intermediate items and crafting materials.

That’s it for now, I’m sure I’ll update as more things come to mind.

Posted on

Guild Wars Hall of Monuments

Today, over six years after I first played Guild Wars I’ve achieved 30 / 50 points in the Hall of Monuments.

Monument Collection (Text)

Posted on

Post-Increment Variable Bug

Found this little bug today in some old (untested code). I was amused that the code would have worked using pre-increment operator (and yet, still been wrong).

# Simplified example, in the original code this was 3 lines long
# Assume use_record_map = false;
record_id = (use_record_map) ? lookup_record_id(foo) : record_id++;

Pythons lack of ++ / — operators makes a little more sense now (one extra character in exchange for removing several type of bugs. Even if they are rather obvious bugs).

Posted on

New Car :)

Posted on

Guild Wars: E/Me Vaettir Farm

E/Me Obsidian Flesh Vaettir Farm (HM)

Skills

  • OgVFI7QIlMPkLo0n2wXlBgMwSgWF
  • Elemental Lord (Optional)
  • Glyph of Swiftness
  • Obsidian Flesh
  • Stoneflesh Aura
  • Mantra of Earth
  • Wastrel’s Worry
  • Arcane Echo
  • Storm Djinn’s Haste (Optional)

Attributes

  • 12 + 3 + 1 Earth Magic
  • 3 + 1 Energy Storage
  • 4 Air Magic
  • 4 Inspiration Magic
  • 11 Domination

Weapon

  • +20% Enchantment length

Armor

  • 5x Geomancer Runes (I think you can get away with a +10 earth shield instead)
  • Sup Earth Magic
  • Minor Energy Storage

Notes

  • If you get attacked by the Wurm in Bjora Marches, just put up Obsidian Flesh right away and you’ll be fine
  • When you have all 60 mobs drag them in between the building + wall to stop them scattering
  • Be careful not to let them body block you
  • Elemental Lord isn’t required, and only gives you an extra 1s on Obsidian flesh.
  • A faster, nicer A/Me run can be found here: http://www.youtube.com/watch?v=zZ1h2GoOpag

In other news, currently at 29 / 50 points in the Hall of Monuments.

Posted on

Guild Wars: Capturing a Black Widow spider

Some more random Fraps footage this time from Guild Wars. Been busy getting points in the Hall of Monuments.

Nothing impressive, but I had never done much end-game content, and maybe it will help save someone else some gold after seeing how easy it is.

I took 6 heros: Three Discordway Necros, Prot Monk, Panic Mesmer and a SoS Ritulist. Ump came with me on his Barrage Ranger, and I had some stupid Ele/R spec (with unspent points…).

The entire run took less than 30 minutes and I’m sure if you had a real build and knew where you were going, it would be very fast.

Walk-through:

  • Clear out the spawn area without accepting any quests
  • Take the Clear the Chamber quest from the spawn area and clear out the new mobs
  • Head north/west and take out all the mobs on the way
  • Talk to the Reaper of the Labyrinth and take the quest Restoring Grenth’s Monuments
  • Head south until you reach the Reaper of the Forgotten Vale
  • Clear out any nearby Coldfire Nights to the north
  • Take the quest Wrathful Spirits and kill all the Wrathful Spirits (Making sure not to let the Mayor die)
  • Hand in the quest and capture your spider (remember to flag the Heros well away from you, or they’ll kill the spider as you start to charm it)

Recorded 2011-09-20

Posted on

Diablo 2 Fun

Just some fraps footage I had on my HDD taking up space

I follow Umpa around as he kills Lilith, Uber Izual, Uber Duriel, Uber Mephisto, Uber Baal and finally Pandemonium Diablo.

Recorded 2nd July 2011

US West Ladder

My Character

Posted on

Roman Numerals

From reddit.com/r/programingchallenges:

I googled this and I haven’t found a similar challenge, so I’d like to pose this question to you all!

Let’s say I give you a range from 1 to 2000. Within this range, find the number that yields the most characters. I asked a friend of mine and he worked out that 1888 has a lot of characters (MDCCCLXXXVIII).

Solution

import time

SYMBOLS = [
    ('M',   1000),
    ('CM',  900),
    ('D',   500),
    ('CD',  400),
    ('C',   100),
    ('XC',  90),
    ('L',   50),
    ('XL',  40),    
    ('X',   10),
    ('IX',  9),
    ('V',   5),
    ('IV',  4),
    ('I',   1)]

def roman_numeral(number):
    roman_number = [];
    for (symbol, value) in SYMBOLS:
        while value <= number:
            roman_number.append(symbol)
            number -= value
    return ''.join(roman_number);


start = time.time();

pairs = [(i, roman_numeral(i)) for i in range(1, 2000)]
pairs.sort(lambda a,b: cmp(len(a[1]), len(b[1])))

print 'Longest roman numeral for numbers 1-2000 = %d -> %s' %  \
    (pairs[-1][0], pairs[-1][1])

print 'Took: %.2fsec' % (time.time() - start,)

Output

Longest roman numeral for numbers 1-2000 = 1888 -> MDCCCLXXXVIII
Took: 0.14sec

Notes

Turns out historically there wasn’t a strict set of rules for Roman numerals, for example IV and IIII are both valid representations of the number 4. Only recent rules have added limits on the number of repeated characters and what values can be subtracted from other values. [Reference][2].

[2]: http://en.wikipedia.org/wiki/Roman_numerals#Reading Roman numerals

Posted on

IP Address Range PHP Class

I’m not sure how useful this piece of code really is, but it gave me the chance to write some funky PHP code (have a look at the next() method).

Example usage

<?php

// Single host:
foreach(new IpRange('10.10.10.10') as $ip)
{
    echo $ip . "\n"
}

// >>> 10.10.10.10


// All hosts on a private network:
foreach(new IpRange('192.168.0.1-255') as $ip)
{
    echo $ip . "\n";
}

// >>> 192.168.0.1
// >>> 192.168.0.2
// >>> ...
// >>> 192.168.0.254
// >>> 192.168.0.255


// All normal (not broadcast, or multicast) IP addresses:
foreach(new IpRange('1-232.0-255.0-255.0-255') as $k => $v)
{
    echo "$k => $v\n";
}

// >>> 0 => 1.0.0.0
// >>> 1 => 1.0.0.1
// >>> 2 => 1.0.0.2
// >>> ...
// >>> 7315795 => 1.111.161.83
// >>> 7315796 => 1.111.161.84
// >>> etc

Source

<?php

    /*

    Copyright (c) 2011, Matthew Davey <matthewd@project-2501.net>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

        Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.

        Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    */


    /*

    IpRange Class

    Given an IP address where each octet can either be a number between 0 and
    255, or a range i.e. 100-200.  Return an iterable object that returns all
    IP addresses in between.


    Examples
    --------


    Single host:
        foreach(new IpRange('10.10.10.10') as $ip)
        {
            echo $ip . "\n"
        }

        >>> 10.10.10.10


    All hosts on a private network:
        foreach(new IpRange('192.168.0.1-255') as $ip)
        {
            echo $ip . "\n";
        }

        >>> 192.168.0.1
        >>> 192.168.0.2
        >>> ...
        >>> 192.168.0.254
        >>> 192.168.0.255


    All normal (not broadcast, or multicast) IP addresses:
        foreach(new IpRange('1-232.0-255.0-255.0-255') as $key => $value)
        {
            echo "$key => $value\n";
        }

        >>> 0 => 1.0.0.0
        >>> 1 => 1.0.0.1
        >>> 2 => 1.0.0.2
        >>> ...
        >>> 7315795 => 1.111.161.83
        >>> 7315796 => 1.111.161.84
        >>> etc

    */

    class IpRange implements Iterator
    {
        protected $count; // Current position, used as the key

        protected $a; // ranges for each octet, l => low, h => high
        protected $b; //
        protected $c; // Most significant to least is aaa.bbb.ccc.ddd
        protected $d; //

        protected $ca; // current value of a
        protected $cb; // current value of b
        protected $cc; // current value of c
        protected $cd; // current value of d

        protected $isValid; // flag set when next() is call at end of ranges

        public function __construct($string)
        {
            if(preg_match('#^(\d+|\d+-\d+)\.(\d+|\d+-\d+)\.(\d+|\d+-\d+)\.(\d+|\d+-\d+)$#', $string, $matches) !== 1)
            {
                throw new InvalidArgumentException('Invalid format.  Each octet should either be a number between 0 and 255, or a range "40-120"');
            }

            // Parse each octet and find the low/high values (high === low if there is no range specified)
            foreach(array(1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd') as $i => $position)
            {
                $range = $matches[$i];

                if(strpos($range, '-') !== false)
                {
                    list($low, $high) = explode('-', $range);
                }
                else
                {
                    list($low, $high) = array($range, $range);
                }

                // Check the IP address is at least mostly valid.  We don't need to check for <0 as our regex will reject it first.
                if($high > 255) throw new InvalidArgumentException("Invalid IP address.  The octet '$high' cannot be greater than 255");

                // If given a range like 255-0, flip the high/low value over
                if($low > $high) list($high, $low) = array($low, $high);

                $this->{$position} = array('l' => $low, 'h' => $high);
            }

            $this->rewind();
        }

        public function current()
        {
            return "{$this->ca}.{$this->cb}.{$this->cc}.{$this->cd}";
        }

        public function key()
        {
            return $this->count;
        }

        public function rewind()
        {
            $this->ca = $this->a['l'];
            $this->cb = $this->b['l'];
            $this->cc = $this->c['l'];
            $this->cd = $this->d['l'];

            $this->count = 0;

            $this->isValid = true;
        }

        public function valid()
        {
            return $this->isValid;
        }

        public function next()
        {
            $this->count++;

            // Least significant to most.  Null is our guard.
            foreach(array('d', 'c', 'b', 'a', null) as $position)
            {
                // Check if unable to generate the next IP address
                if($position === null)
                {
                    $this->isValid = false;
                    break;
                }

                // Check if the value is less than the maximum for this
                // position, if so increment the value and stop.  Otherwise set
                // this position to its lowest value, and continue onto the next
                // highest position.
                if($this->{"c$position"} < $this->{$position}['h'])
                {
                    $this->{"c$position"}++;
                    break;
                }
                else
                {
                    $this->{"c$position"} = $this->{$position}['l'];
                }
            }
        }
    }

Posted on