Best code ever!.. NOT!

The company I work for has used a couple of 3rd parties to develop a some of our projects. The code you get back is usually quite bad to begin with, but yesterday I found the ultimate in bad code.

How not to code:

ActionScript

  1. switch(imageList.length){
  2. case 1:
  3. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[0]));
  4. rowNum = 1;
  5. break;
  6. case 2:
  7. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[0]));
  8. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[1]));
  9. rowNum = 1;
  10. break;
  11. case 3:
  12. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  13. this._imageList.push(chooseType(pcurlImage3, curlImage3, typeArray[1]));
  14. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[2]));
  15. break;
  16. case 4:
  17. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[0]));
  18. this._imageList.push(chooseType(pcurlImage4, curlImage4, typeArray[1]));
  19. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[2]));
  20. this._imageList.push(chooseType(pcurlImage9, curlImage9, typeArray[3]));
  21. break;
  22. case 5:
  23. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[0]));
  24. this._imageList.push(chooseType(pcurlImage3, curlImage3, typeArray[1]));
  25. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[2]));
  26. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[3]));
  27. this._imageList.push(chooseType(pcurlImage9, curlImage9, typeArray[4]));
  28. break;
  29. case 6:
  30. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  31. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[1]));
  32. this._imageList.push(chooseType(pcurlImage2, curlImage2, typeArray[2]));
  33. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[3]));
  34. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[4]));
  35. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[5]));
  36. break;
  37. case 7:
  38. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  39. this._imageList.push(chooseType(pcurlImage2, curlImage2, typeArray[1]));
  40. this._imageList.push(chooseType(pcurlImage4, curlImage4, typeArray[2]));
  41. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[3]));
  42. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[4]));
  43. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[5]));
  44. this._imageList.push(chooseType(pcurlImage8, curlImage8, typeArray[6]));
  45. break;
  46. case 8:
  47. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  48. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[1]));
  49. this._imageList.push(chooseType(pcurlImage2, curlImage2, typeArray[2]));
  50. this._imageList.push(chooseType(pcurlImage3, curlImage3, typeArray[3]));
  51. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[4]));
  52. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[5]));
  53. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[6]));
  54. this._imageList.push(chooseType(pcurlImage8, curlImage8, typeArray[7]));
  55. break;
  56. case 9:
  57. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  58. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[1]));
  59. this._imageList.push(chooseType(pcurlImage2, curlImage2, typeArray[2]));
  60. this._imageList.push(chooseType(pcurlImage3, curlImage3, typeArray[3]));
  61. this._imageList.push(chooseType(pcurlImage4, curlImage4, typeArray[4]));
  62. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[5]));
  63. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[6]));
  64. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[7]));
  65. this._imageList.push(chooseType(pcurlImage8, curlImage8, typeArray[8]));
  66. break;
  67. case 10:
  68. this._imageList.push(chooseType(pcurlImage0, curlImage0, typeArray[0]));
  69. this._imageList.push(chooseType(pcurlImage1, curlImage1, typeArray[1]));
  70. this._imageList.push(chooseType(pcurlImage2, curlImage2, typeArray[2]));
  71. this._imageList.push(chooseType(pcurlImage3, curlImage3, typeArray[3]));
  72. this._imageList.push(chooseType(pcurlImage4, curlImage4, typeArray[4]));
  73. this._imageList.push(chooseType(pcurlImage5, curlImage5, typeArray[5]));
  74. this._imageList.push(chooseType(pcurlImage6, curlImage6, typeArray[6]));
  75. this._imageList.push(chooseType(pcurlImage7, curlImage7, typeArray[7]));
  76. this._imageList.push(chooseType(pcurlImage8, curlImage8, typeArray[8]));
  77. this._imageList.push(chooseType(pcurlImage9, curlImage9, typeArray[9]));
  78. break;
  79. }

Quake Live Signature Generator

Generate your Quake Live signature here: http://ansuz.nl/toys/quakelive/

So why did I build it?
I love playing video games and I love Quake Live (QL). So I was a little disappointed to find out that there was no signature generator available yet for Quake Live. After a bit of ‘Googling’ I found a post on how to show your stats on mIRC. The example used a socket connection to retrieve QL profile page and using good ol’ web scraping technologies to retrieve all relevant data.

How?
I started building this in Flex using a socket connection to load the QL profile. I then managed to retrieve all relevant date and store it in a usable way. When I uploaded the app, I found out you need a special cross domain policy file for socket connections on your server. Also, this socket cross domain policy needs to be served from port 843. As my hosting provider doesn’t give me control to that level I had to find something else. Quite obviously a URLLoader did the trick. It’s funny how you can get quite mislead by an example you find somewhere else.

After that I realised that most forums don’t allow you to use Flash in your signatures and if they do, they usually limit it to something like 15KB. My signature is nowhere near 15K.

I decided to finally take the smart approach and use PHP to retrieve the QL profile and generate an XML file with all relevant data. After that I started messing around with the GD Lib to create a PNG version of the signature. The script that creates the image first loads the XML and then creates the signature, you can see the result of that above. As you can see this image is quite large, so I’m planning to create a medium and a small version in the near future.

All in all getting data from point A, modifying it at point B and using it at C is a lot of fun!

Links:

imagettftext resulting in blurry text

It’s been a while since I used PHP and I’m finally doing something with it again. I’m working on creating dynamic forum signatures and of course need to use some text in that. I’m using the imagettftext() method to create my text using the Arial font. I was getting strange blurred result and reading the documentation suggested I should turn the anti-aliassing off. It turn out that the GDLib JPG compression is pretty bad and caused all the blurring. When I switched to PNG all blurryness was gone.

Solution: Make sure you use PNG, not JPEG.

Flex Builder issue

I keep on getting the following error:

Buildfile: C:\Projects\*****\flex\ant\build.xml
compile_main_debug:
[exec] Loading configuration file C:\flex3sdk\frameworks\flex-config.xml
[exec] Error: C:\Projects\*****\flex\bin-debug\main.swf (The process cannot access the file because it is being used by another process)
[exec] java.io.FileNotFoundException: C:\Projects\******\flex\bin-debug\main.swf (The process cannot access the file because it is being used by another process)
[exec]     at java.io.FileOutputStream.open(Native Method)
[exec]     at java.io.FileOutputStream.<init>(Unknown Source)
[exec]     at java.io.FileOutputStream.<init>(Unknown Source)
[exec]     at flex2.tools.Compiler.mxmlc(Compiler.java:283)
[exec]     at flex2.tools.Compiler.main(Compiler.java:53)

BUILD FAILED
C:\Projects\******\flex\ant\build.xml:44: exec returned: 1

Total time: 4 seconds

This occurs whenever Flex Builder fails to launch the application and you try to compile after that.

The only way to solve it at the moment is to close Flex Builder. When it’s closed the application gets actually launched in the stand alone Flash Player. Close the SA Flash Player. Start Flex again and compile.

Or just open up Windows Task Manager and kill FlashPlayer.exe.

Available resolution in the browser

I’m working on a redesign of my homepage and completely forgot how much screen real-estate is available within a browser to display your website. A quick Google serach told me the following:

Screen resolution – available in browser
640×480 – 600×300
800×600 – 760×420
832×624 – 795×470
1024×768 – 955×600

Original forum post:
Available browser space for different screen resolution

Some interestion information about browser usage, including most common screen resolution, etc can be found at W3C’s Browser Information page.

Colors and bitwise operators

I found this very interesting post called “Hex colors & bitwise operators” on www.tracestatement.com a while back and I’ve been meaning to blog about it.

ActionScript

  1. // From hex value to r,g,b values
  2. var hex:uint = 0xFF8811;
  3. var r:uint = rgb &gt;&gt; 16;
  4. var g:uint = rgb &gt;&gt; 8 &amp; 0xFF;
  5. var b:uint = rgb &amp; 0xFF;
  6.  
  7. //From r,g,b values to hex value
  8. hex = (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;

This tiny bit of code allows you to rapidly subtract the Red, Green and Blue values from a hexadecimal colour code. Once you have the R, G, B bits you can then manipulate them and once done convert them back to a single Hex value.

An important thing to remember is bitmasking, this can be used to switch a set of bits either on or off.