Archive for the 'Code' Category

OnLive could change the video game industry

Wednesday, March 25th, 2009

I feel it’s my geek given duty to make a post about this presentation. I was lucky to finally have the time to watch their hour long presentation and Q&A session at the Game Developer Conference 2009 (which ends a couple of days from today). They could have not picked a better place to finally demo their technology.

In short, they’ve introduced a huge new concept to the video game industry, I’d call it “Cloud Gaming” to not only host the games, but also host the processing juice. You won’t need a console anymore, they keep the hardware to execute and stream the game to your screen. They support TV (with a miniconsole), PC and Mac.

So bear with me, they say they have solved the issue that you’re thinking about now, Lag. The people behind this worked on apple to create Quicktime, and they identified differences between what it takes to compress linear (regular) video, vs Interactive Video. They say their compression algorithm doesn’t take seconds of lag (like when you stream over a webcam), but miliseconds. They have custom chips to process the graphics, and I bet they might even built their own network protocol right on top of IP.

So what are some of the implications of this:

  • We’ll all be able to finally play Crysis and even more demanding games on low end PCs
  • No more buying more hardware, no more upgrading your PC to be able to run games, no more buying consoles
  • All your games live on the platform, so you can play from any computer, and you’ll keep the state of your game until the last time you hit the Pause button
  • Your friends can see you play, live. I bet we’ll be able to see live tournaments, we’ll start seeing a new breed of famous people get more attention, the Elite gamers. Imagine seeing the best Call of Duty player in the world playing live
  • New Genres of video games will emerge on this platform, maybe even new genres of entertainment, think new Live Broadcast shows where participants use an avatar to either act or compete (game show)
  • Game Developers not need to think of the rendering limitations that they might have nowadays, and will be able to design games that could only be imagined in the past. Render quality only thought for movies will now exist for video games, think of virtual reality now
  • There’s about 100 million PCs/Macs/Laptops out there that are not ready today to play high end games, now they’ll have the possibility of playing virtually any game by installing a 1Mb plugin from OnLive.com
  • Takes Piracy out of the Business Equation
  • A bigger gaming audience makes an even better case for companies placing advertisement in video games, maybe there will be a lot more high end free games with bigger audiences, think the next Grand Theft Auto coming out for free with superb real life like graphics rendering, all ad sponsored and free to the consumer. The amount of people that you could have playing a great game for free would make other developers think twice about charging for their games and having their virtual worlds ad sponsored.
  • No more installs
  • Now multiplayer will have almost no latency since all players live inside their datacenter, you only get the latency of your ISP if there’s any
  • Nintendo, Sony and Microsoft must be shitting their pants
  • Services that sell used games are going to be selling vintage and their business will be reduced

However I think there will always be room for the old consoles. This is the biggest entertainment industry in the world, we have grown up with consoles for almost 30 years and there’s a lot of changes to push into people’s minds:

  • How do you convince me, that finally made up my mind after years and dropped $500 on a PS3 to play with my friends in latin america online to switch to this, if my friends will probably have no way to even have access to the system in years to come?
  • How do you convince PC gamers that rather pay for the hardware and pirate the games? (There’s plenty of those, probably the majority of the PC gaming population outside the US never plays for a PC game, and doesn’t get into consoles because they pirate the games) into renting or buying games on the cloud?
  • How do you convince all the people that they should switch when their gaming experience depends entirely on being connected to the internet. So If the ISP is having issues I can’t play? isn’t my console awesome?

It seems that many of these complains are similar to all the complains brought upon business models that didn’t exist online and that are now thriving. This presentation left me with my mouth wide open, and I highly recommend you watch it. You’ll be blown away by the power of the UI, and how as you Browse for games, you can even see how other people are playing live, it’s like streaming video is nothing for OnLive. Really sick technology.

In the case that they succeed, I just can’t wait for them to have competition by the existing big brands, it’s going to get so interesting once cloud gaming becomes the defacto platform, maybe we as consumers will end up playing games for free, all sponsored with in game ads.

Just by listening to the guy if you’re a techie, your mind will start to fly to barely start to imagine the awesomeness of the technology that should be behind this. I can imagine anything from custom virtualization technology, to custom GPUs, custom network cards, custom network protocols on top of IP, deals with major internet backbone networks, ISPs, deals with game publishers, sick level API development, incomprehensible comprehension technology for my retard brain… when I see shit like this, I always think… how the hell is there people that still believe in god? Mankind is the closest thing there is to something like that! I’m thankful for people in this world that can think so big.

The service is supposed to launch next Winter 2009, but you can sign up to be a beta tester

Delete All Direct Messages of your Twitter Account at once (or at least try!)

Friday, February 13th, 2009

Since Twitter doesn’t provide with a “Delete All Direct Messages” functionality, Here’s a Python script that attempts to delete all the direct messages stored on your Twitter account.

Limitations
The only problem with it is that given the limitations of the Twitter REST API, I was forced to send a request per message to be deleted, and it seems that Twitter will only allow 100 requests per hour (per client).

So in theory you will be able to delete 100 an hour, although I have seen it delete over 400 messages Twitter gets all grumpy on me.

Usage
Just save the script as twitter_delete_direct_messages.py, open a terminal and run it:

python twitter_delete_direct_messages.py

After that just follow the instructions on screen and enjoy as messages get wiped out.

If you find this useful, you can thank me by following me on Twitter.

Get The Script

Using a linear array as a bidimensional matrix

Tuesday, January 27th, 2009

Often times I find the need to use a list or linear array as if it was a table.

Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array.

Let me illustrate, with an example. You have a string that defines the elements of a game board, and you want to work using (x,y) coordinates.

s="xxxxx@xx@xxx@@xx"

If you were to look at it as a matrix (width=4), it’d be something like this

s="xxxx
   x@xx
   @xxx
   @@xx"

However, I can’t do
s[x,y], since it’s a linear array, it’s a string.

You need to convert from (x,y) to a number that represents an index in the array.

This is very simple:

width=4
def getNforXY(x,y):
  return x + width*y

What if you want to do it backwards. What if you need to know what’s the X and Y for a given index N in the string?

def getXYforN(n):
  y = int(n/width)
  x = n - width/y
  return (x,y)

Cheers

jQuery tag wrapping

Friday, December 19th, 2008

Many times you’ll be working on something in HTML that could be long and repetitive, and then for some reason you need to edit the entire thing to wrap each of the tags on some other tags, a common example would be to link many elements.

Take this example, there’s a bunch of photos, they’re all contained in divs that have a css class called “photos”, inside we just throw
a bunch of <img> tags, and all of a sudden our boss tells us he wans to have every image on that page linked to “http://domain.com/photos”. jQuery to the rescue, no need to wrap each <img> with <a href=”"> by hand, you can do it all in one line

So if the code looks like this:

       <div class="photos">
            <img src="http://domain.com/0670027481_m.jpg"/>
            <img src="http://domain.com/211161aa99_m.jpg"/>
            <img src="http://domain.com/173bb0cd6_m.jpg"/>
        </div>
        <div class="photos">
            <img src="http://domain.com/841fd90b5b_m.jpg"/>
            <img src="http://domain.com/52dda2cee5_m.jpg"/>
            <img src="http://domain.com/b569399599_m.jpg"/>
        </div>
        <div class="photos">
            <img src="http://domain.com/8806e863a4_m.jpg"/>
            <img src="http://domain.com/5e43aa95fe_m.jpg"/>
            <img src="http://domain.com/68a74a088c_m.jpg"/>
        </div>

        <div class="tshirt-photos">
            <img src="http://farm4.static.flickr.com/3149/3113619519_72ce82c545_m.jpg"/>
            <img src="http://farm4.static.flickr.com/3236/3105792155_99f2388869_m.jpg"/>
            <img src="http://farm3.static.flickr.com/2114/2372582266_765842fae9_m.jpg"/>
        </div>

With jQuery you can match all the <img> elements, and wrap them all with “<a href=”http://domain.com/photos”></a>” in a single line of code:

$(document).ready(function() { $(".photos img").wrap('<a href="http://domain.com/photos">') })

So to explain how it works, when the document is ready (when it’s finished loading) the function inside is called back, it will use the jQuery selector $(“.photos img”) to match all the <img> tags contained withing elements of css class “photos”, then it applies the wrap() function, which will wrap the matched elements with the given html tags.

About jQuery

jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. It was released January 2006 at BarCamp NYC by John Resig.

Dual licensed under the MIT License and the GNU General Public License, jQuery is free and open source software.

jQuery contains the following features:

  • DOM element selections
  • DOM traversal and modification, (including support for CSS 1-3 and basic XPath)
  • Events
  • CSS manipulation
  • Effects and animations
  • Ajax
  • Extensibility
  • Utilities – such as browser version and the each function.
  • JavaScript Plugins

jQuery Documentation

Python Script to Update Wordpress in One Step

Wednesday, November 26th, 2008

During the past week, I think I had to update all my wordpress instances twice, and it’s become really annoying doing this manually. I’ve written a python script which I’ll share with you.

How I keep my wordpress updated by hand
I tend to keep my wp-content folder outside of my wordpress installation for 2 reasons:

1. I don’t like to loose my themes, plugins and customizations
2. I like to keep all my customization changes under subversion

So, if I had my wordpress installation say at:
/home/user/public_html/blog

I’d keep my wp-content folder for that here:

/home/user/public_html/wp-content-for-blog

So when I upgrade my blog, I always remove the original wp-content folder that comes along wordpress, and I symlink my hard worked on wp-content folder that lives outside to the freshly unzipped wordpress folder.

user@machine:~/public_html/blog$ ls -l
...
lrwxrwxr-x 1 user www    54 2008-11-26 09:29 wp-content -> /home/user/public_html/wp-content-for-blog
...

So what I endup doing all the time, is downloading the latest.zip to ~/public_html/, it will unzip under ~/public_html/wordpress, and then I’ll copy the current ~/public_html/blog/wp-config.php to ~/public_html/wordpress, then I’ll remove the default ~/public_html/wordpress/wp-content and symlink the outer wp-content with all my customizations, themes and plugins to it. Once done, I’ll make a backup of the old wordpress folder, and then I’ll rename wordpress folder to the name of the blog folder, and it’s all done.

It’s simple, but when you have to do it for 5 blogs, every week, it’s not fun anymore.

The Update Script

So here’s a script to do it in one step. If you’re not using my symlinked technique, this will do it for you, you only need to specify the full path to the folder where you want to keep your current wp-content folder outside the new installation before you apply the update, and the name of the folder where your current blog lives. The script below will have its configuration variables towards the beginning set so that they are in line with the example I’ve been talking about.

#!/usr/bin/python
#########################################################################################
#
# upgrade_wordpress.py - Script to automatically upgrade your wordpress installation.
#
# Requirements:
#   - Python 2.4 or older
#   - Wordpress should already be installed
#   - CURL (sudo apt-get install curl)
#
# Author: Angel (Gubatron) Leon
# LICENSE: See the GPL2 license.
# 2008
#########################################################################################
import os

#########################################################################################
#Config (relative to the folder where this script will be run from)
#########################################################################################

#The current folder where the blog lives
BLOG_FOLDER='blog'

#
# The first time you run the script, it will try to make a copy of your
# current wp-content folder outside. Copy here the location of where
# the wp-content folder with your themes and plugins should exist.
#
# After it unzips, it will remove the default wp-content folder from
# the new installation, and it will symlink the external wp-content
# That way you don't ever have to worry about loosing your customizations
# and plugins.
#
WP_CONTENT_OUTSIDE_COPY_FOLDER="/home/user/public_html/wp-content-for-blog"

#This is where a backup of your current blog will be
BLOG_FOLDER_BACKUP_FOLDER=BLOG_FOLDER+'.old'

#Where to download the wordpress latest.zip from
WORDPRESS_LATEST_ZIP_URL='http://wordpress.org/latest.zip'

#### DO NOT MODIFY AFTER THESE LINES ####

def downloadWordpress(url=WORDPRESS_LATEST_ZIP_URL):
    if os.path.exists('latest.zip'):
        print "Removing old latest.zip"
        os.remove('latest.zip')

    #Try to download with CURL
    print "Attempting to download latest.zip from wordpress.org"
    os.system('curl %s -o latest.zip' % url)

    if not os.path.exists('latest.zip'):
        os.system('wget ' + url)

    return os.path.exists('latest.zip')

def dirExists(dirName):
    return os.path.exists(dirName) and os.path.isdir(dirName)

def backupBlog(currentBlogFolder=BLOG_FOLDER,
               wpContentOriginalFolder=WP_CONTENT_OUTSIDE_COPY_FOLDER,
               backupFolder=BLOG_FOLDER_BACKUP_FOLDER):

    #Remove any previous backups
    if os.path.exists(backupFolder) and os.path.isdir(backupFolder):
        print "Removing previous backup folder"
        os.system('rm -fr ' + backupFolder)

    #Copy the current blog folder into a backup folder just in case.
    #We won't do any database backups for now.
    print "Creating new backup folder"
    os.system('cp -r %s %s' % (currentBlogFolder,backupFolder))

    #Check for the copy of wp-content outside the blog, if it doesn't exist
    #we'll make it for the first time.
    if not dirExists(wpContentOriginalFolder):
        print "Creating outside copy of wp-content"
        os.system('cp -r %s %s' % (os.path.join(currentBlogFolder,'wp-content'),
                                   wpContentOriginalFolder))

    #Copy the latest wp-config.php outside to the current folder
    print "Copying your latest wp-config.php outside"
    os.system('cp %s .' % (os.path.join(currentBlogFolder,'wp-config.php')))

    backupFolderExists = dirExists(backupFolder)
    wpContentFolderExists = dirExists(wpContentOriginalFolder)
    configFileExists = os.path.exists('wp-config.php')

    return backupFolderExists and wpContentOriginalFolder and configFileExists

def upgradeBlog(currentBlogFolder=BLOG_FOLDER,
                backupFolder=BLOG_FOLDER_BACKUP_FOLDER,
                url=WORDPRESS_LATEST_ZIP_URL,
                wpContentOriginalFolder=WP_CONTENT_OUTSIDE_COPY_FOLDER):

    if not downloadWordpress(url):
        print "Could not download latest.zip, aborting."
        return False

    if not backupBlog(currentBlogFolder,wpContentOriginalFolder,backupFolder):
        print "Could not backup blog or wp-config.ph, aborting."
        return False

    if currentBlogFolder == 'wordpress':
        print "The current blog folder cannot be 'wordpress, aborting."
        return False

    #1. If a wordpress/ folder exists, wipe it.
    if dirExists('wordpress'):
        print "Removing old wordpress folder"
        os.system('rm -fr wordpress')

    if dirExists('%s.delete' % currentBlogFolder):
        print "Removing old %s.delete folder" % currentBlogFolder
        os.system('rm -fr %s.delete folder' % currentBlogFolder)

    #2. Unzip new copy
    os.system('unzip latest.zip')

    if not dirExists('wordpress'):
        print "Could not unzip the wordpress installation, aborting."
        return False

    #1. Copy wp-config.php into the new installation
    os.system('cp wp-config.php wordpress/')

    #2. Remove the default wp-content folder
    os.system('rm -fr wordpress/wp-content')

    #3. Symlink the original wp-content that lives outside
    os.system('ln -s %s wordpress/wp-content' % (wpContentOriginalFolder))

    #4. Verify symlink was created
    if not (os.path.exists('wordpress/wp-content') and os.path.islink('wordpress/wp-content')):
        print "Could not create symlink to wp-content, aborting."
        return False

    #5. Move original folder to folder.delete, and make this wordpress folder the current folder.
    os.system('mv %s %s.delete' % (currentBlogFolder,currentBlogFolder))

    if not dirExists(currentBlogFolder + ".delete"):
        print "Could not rename current folder for later deletion, aborting."
        return False

    #6. Rename the new installation as the current blog
    os.system('mv %s %s' % ('wordpress',currentBlogFolder))

    if dirExists('wordpress'):
        print "ALERT: The wordpress folder still exists."
        return False

    if not dirExists(currentBlogFolder):
        print "ALERT: The blog doesn't exist, recover from the backup folder %s please" % (backupFolder)
        return False

    #7 Cleanup
    os.system('rm -fr %s.delete' % (currentBlogFolder))

    return True

if __name__ == '__main__':
    upgradeBlog()

Requirements

  • shell access to the machine where you have your wordpress installed
  • a python interpreter installed
  • curl (sudo apt-get install curl) to download the zip. If you don’t have it it’ll attempt to use wget
  • Installation

  • Right outside your wordpress installation folder, create a new file called upgrade_wordpress.py
  • Copy and paste the script inside that file
  • Edit the configuration variables to point to the name of your wordpress installation folder, and give it a full path to where you want to keep your wp-content folder (including the name of the folder, so if you want to name it the same way, you could do for example /home/user/wp-content and it’ll be saved right under your home)
  • Usage:

    python upgrade_wordpress.py

    The script is very fault proof, it will always try to abort in case something is not going the way it’s expected. At the end of the day it’ll also leave a backup copy of your current blog in case something goes bad, you can always recover.

    Cola: Real-Time Remote Pair coding

    Tuesday, November 25th, 2008

    This is not new, but I hadn’t seen it, so maybe you didn’t either, I’ll let the video speak for itself, I’m speechless.

    Thanks to Daniel Chang for sharing this with me.


    Cola: Real-Time Shared Editing from Mustafa K. Isik on Vimeo.