It looks like you're new here. If you want to get involved, click one of these buttons!
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.
Comments
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
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
@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.
@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)?
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.
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!