quarta-feira, 1 de fevereiro de 2012

How to make a High Scores board to your Flash game using Google App Engine - Part 1

In this tutorial you will learn how to make an online high scores board to your flash game using Google App Engine infrastructure. It'll be a two posts tutorial, on this first you'll build the server in Python and in the second post the client in AS3.

First, you need to install Python 2.5 and Google App Engine. Both sites are full of tutorials on how to do this in many operating systems.

Google App Engine is a platform for developing and hosting web applications. So, to make things easier, our flash game will interact with our app as it were a web site. How can we do this? Using the UrlRequest and UrlLoader classes

I'm not going to cover more than the really necessary part of Google App Engine framework for making a high scores board. Let's examine these data storage examples of Google's page
from google.appengine.ext import db

class Greeting(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
We can easily adapt this to our needs:
class Score(db.Model):
     name = db.StringProperty()
     points = db.IntegerProperty()  

This one is bit more confusing, but if you have some experience with web development you can understand why we can turn this
class Guestbook(webapp.RequestHandler):
    def post(self):
      guestbook_name = self.request.get('guestbook_name')
      greeting = Greeting(parent=guestbook_key(guestbook_name))

      if users.get_current_user():
        greeting.author = users.get_current_user()

      greeting.content = self.request.get('content')
      greeting.put()
      self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))

to this
class SendScore(webapp.RequestHandler):
    def post(self):
        score = Score()

        score.name = self.request.get('name')
        score.points = int(self.request.get('points'))
        score.put()

        self.response.out.write('ok')

This last snippet is going to get the variables name and points from a urlrequest and save them on Score database. For more explanations on this check the original example.

And finally, we need to list the first nth players (in this case, 15):
class MainHandler(webapp.RequestHandler):
    def get(self):
        #self.response.out.write('Hello world 2!')

        scores = db.GqlQuery("SELECT * FROM Score ORDER BY points DESC LIMIT 15")

        for score in scores:
            self.response.out.write(score.name)
            self.response.out.write('\t')
            self.response.out.write(str(score.points))
            self.response.out.write('\n')

def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          ('/sendscore', SendScore)],
                                         debug=True)
    run_wsgi_app(application)


if __name__ == '__main__':
    main()
It's very straightforward if you know some SQL and Python. The characters \t and \n are used to parse the data in as3. It's done. In the next part we'll learn how to communicate with this server through as3 using the flash.net API.

Nenhum comentário:

Postar um comentário