Example - storing data in the coud

There may be a time when you want to store some data on web servers, to create multiplayer and such, or maybe to store the results of data gathering. Here is a little example of how to do it using Google App Engine, which is a pretty nice cloud processing and storage service. You can set up a Google App Engine account and submit apps for free as long as they don’t have a ton of traffic. And if your game becomes a best seller, Google can easily handle the traffic and the prices are pretty cheap.

This is example is an online highscore handler. You can submit a highscore and store it on the web server, and retrieve a list of the top scores. Here is a Codea program that connects to the service:


function setup()
    gameName = "vegagame" --change this to make a new list
    playerName = "vega" -- This is the name sent with the highscore.
    http.get("https://vega-highscore.appspot.com/listscores?game=" .. gameName,gotScores, gotError)
    --notice I use the SSL address so no one can intercept the password
    state = 1
end

function draw()
    background(0, 0, 0, 255)
    if state == 1 then
        text("touch screen to submit a random highscore.", WIDTH/2, HEIGHT/2)
    elseif state == 2 then
        text("Submitting highscore to cloud server.", WIDTH/2, HEIGHT/2)
    else
        text("Touch screen to download highscore list.", WIDTH/2, HEIGHT/2)
    end
end

function touched(touch)
    if touch.state == ENDED then
        if state == 1 then
            dataStr = "name=" .. playerName .. "&score=" .. math.random(20000) 
            dataStr = dataStr .. "&game=" .. gameName
            dataStr = dataStr .. "&secret=abcdef"
            local tbl={["method"]="POST",["data"]=dataStr}
            http.get("https://vega-highscore.appspot.com/addscore",sentScore,gotError,tbl)
            state = 2
        elseif state == 3 then
            http.get("https://vega-highscore.appspot.com/listscores?game=" .. gameName,gotScores, gotError)
            state = 1
        end
    end
end

function gotScores(data,status,headers)
    print("Current top 10 scores")
    print(data)
end

function gotError(error)
    print("Error:"..error)
end

function sentScore(data,status,headers)
    print(data)
    state = 3
end

You can download and test that as-is, or change the gameName to create a new list. It was set up that way so you can deploy the web app once and run all of your games off of it.

If you are using this, of course, you will want to deploy your own version of the web app that you are in control of rather than using mine. So here is a little explanation of how to deploy it and then the source code will follow.

  1. Go to appengine.google.com and either setup a new google account or register yours for appengine.
  2. After registering your can hit the button that says “Create an application” and you may be required to verify your identity using a text message on a cell phone.
  3. When you finally get to the create app form, choose a unique app identifier, mine is vega-highscore, yours can be anything that isn’t already taken. After you get that chosen write that down, because your app will be accessed at “yourIdentifier.appspot.com
  4. Leave it open to all google users, and choose “High Replication.”
  5. Go to https://developers.google.com/appengine/downloads and download the SDK for Python. There are versions for Windows, Linux, and Mac here.
  6. Run the appEngine and proceed to the next post.
  1. Alright, so now we need to create our App Engine App. In the appengine launcher, choose Create New Application. Now, where it says “Application Name,” you need to enter the unique identifier you created back in step 3. So for me it is vega-highscore. Then choose a directory to put it in.
  2. Navigate to that directory on your computer and you should see a few files, such as app.yaml, index.yaml and main.py. Edit main.py to look like the following:
#!/usr/bin/env python
#Google App Engine High Score Handler
#Made for Codea Projects by Kyle Howard (Vega), 2012
#Edit it, use it, sell it, whatever. Just don't sue me.  No warranty.

import cgi
import datetime
import urllib
import webapp2
from google.appengine.ext import db

#You can edit this secret code, then you pass it in from your app along with the score
#This adds very basic security to try and prevent people from hacking the highscore list
secretCode = "abcdef"

class Score(db.Model):
	game = db.StringProperty()
	name = db.StringProperty()
	score = db.IntegerProperty()
	date = db.DateTimeProperty(auto_now_add=True)
  
class MainHandler(webapp2.RequestHandler):
	def get(self):
		self.response.out.write('<html><head><title>High Score Handler</title></head><body><h1>High Score Handler</h1></br><h2>By Kyle Howard</h2></body></html>')

class AddScore(webapp2.RequestHandler):
	def post(self):
		secret = self.request.get('secret')
		if secret == secretCode:
			score = Score()
			score.game = self.request.get('game')
			score.score = int(self.request.get('score'))
			score.name = self.request.get('name')
			score.put()
					
class ListScores(webapp2.RequestHandler):
	def get(self):
		game=self.request.get('game')
		scores = db.GqlQuery("SELECT * FROM Score WHERE game = :1 ORDER BY score DESC LIMIT 10", game)
		for score in scores:
			self.response.out.write("{0},{1}\
".format(score.name, score.score))

app = webapp2.WSGIApplication([('/', MainHandler),('/addscore', AddScore),('/listscores', ListScores)], debug=False)
  1. Now edit app.yaml to look like this, but make sure to change the top line to YOUR identifier:
application: vega-highscore
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\\.ico
  static_files: favicon.ico
  upload: favicon\\.ico  
  
- url: .*
  script: main.app
  secure: optional

libraries:
- name: webapp2
  version: "2.5.1"
  1. Very important: run the app on your local machine. You should be able to do this on the app engine launcher by hitting the run button. This automatically generates a proper index.yaml. If you want you can use a browser to navigate to http://localhost:8080/ to ensure it is working.
  2. Deploy the app. You can use the button on the App Engine Launcher that says DEPLOY (easiest) or you can use a command line tool, instructions here: https://developers.google.com/appengine/docs/python/gettingstartedpython27/uploading
  3. Now, in the Codea application, change the 3 places that have “vega-highscore” to your application id.

Hopefully some of you guys will use this as a starting point for creating your own cloud connected apps. Though Python is different than Lua, it is a very easy language to learn and Google App Engine works pretty well with Codea. Good luck.

Hi @Vega,

Interesting !!!

I am close to finishing the Flood-it app I started in a previous thread (probably in line with @veeerlap) and was wondering about adding a high score table.

I saw this in two levels - one to keep the Hi-score data in the local data storage, then - two to upload it to my own website. I’m not that sure I’d like to give Google access to my PC as it looks like your setting up a local server access on your own PC.

I was hoping I could transfer data using the HTTP facilities added to version 1.41 of Codea, but haven’t started to look yet - still ironing out bugs in the app code.

Have you tried any other options for storage?

Great work by the way.

Thanks,

Bri_G

:slight_smile:

Google App Engine does not give Google access to your PC. Rather you download a test server to test items on your computer, then when it is fully working you upload to Google’s servers and it runs from there. After that your computer never needs to be connected and you could even uninstall the test server from your PC and the app would continue to run on the server.

Another option would be Amazon Web Services, which is nearly identical to Google App Engine, or you use PHP and MySQL to run it on your own web server. The problem with running on your own web server is scalability. If you only have a few hundred users using your app concurrently, any web server can handle the traffic. But if you suddenly had 100k people using your app concurrently, your web server crashes. This Google App Engine app for highscores could theoretically handle millions of users and infinite database size, which is why I like it.

Hi @Vega,

Thanks for the feedback. I already have Apache running on my system, and have used Microsoft web servers; but only up to a level in getting a certificate for web design. So my knowledge is limited.

From a personal point of view I think php and MySQL would meet my needs on my own website. I anticipate access demand will be limited and aimed at1 page only which would be a static web page with limited graphics so tiny.

I’ve seen your coding and I can understand your needs for bigger traffic. Can’t see many people likely to use Flood-it, too many versions on the net. But it would probably be a free app, so there may be a few users. I only intend to hold the top 100 users data anyway - not a big database.

Thanks,

Bri_G

:slight_smile:

@Vega very nice!

@Vega - most excellent!

It is amazing how these things converge - i’m working on a simple high score tute using the MineSweeper demo. This will make a great addendum for the more advanced folks, assuming I can work out how to draw text in columns (see earlier post http://twolivesleft.com/Codea/Talk/discussion/1381/print-text-in-columns#Item_1)

That’s great, @Reefwing. You are quickly becoming a vital member of this community; I see a lot of posts talking about your tutorials. Let me know if you ever want a guest contributor to your tutorial site.

The real weakness of this online high score system is: as long as the code is being given out, anyone can easily changing the scoring and cheat. Once you release it as an app, though, it should be much more secure.

I’ll create one specially for your site, @Reefwing. If you think of something you’d like a tutorial for give me a shout, or I will have an idea sometime and do it.

@Vega - thanks muchly. Would love to have you (or anyone else for that matter) as a guest contributor. Especially since I already use a lot of your code as examples. I figure different people learn different ways and the more channels we have the better. I can’t believe it took me this long to discover Codea, it would probably benefit from some more marketing.

Do you want to write something specifically or use one of your existing posts (this or the mesh tute for example)?

Excellent - I’m basically working my way through the various game genres and as I need to do something I learn it and do a tute on it. So the content is a bit ad hoc. Probably best for you to write on something you’re interested in. Let me know when you get it done.

Th problem with this is that you can’t send a score if you are not connected to wifi.

Could this method be used to store persistent app data between App Store updates, or is there a better method?

Very cool. I’ll have to look into it a bit more later.

Hi Vega, I’m new to codea but this is a great little app, I’m writing some code and need to access common data across several iPads, so this looks ideal. Sorry I had to “cheat” to get a hi score so that I could test it was working. Many thanks for sharing this with us!