The following files have been attached to this tutorial:
This tutorial hasn't been translated.
This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.
As I've read quite a few tutorials here, I saw that cross-domain calls were held as something difficult. So I decided to give it a shot and now I'm providing a working solution for you to evaluate yourself.
So, what functionality of web2py do we use here?
Well we use "run" service and DAL(database abstraction layer).
In the default.py file you'll find a function named call(), it allows to decorate other functions with a @service.(run/xml/json/csv... and others) in my example I use the @service.run decorator.
Which means if I define a function say_hello, which takes a name you provide and returns it with a hello:
return "hello "+name
I'll be able to access it in a few ways:
If the function takes in more variables it would be expanded like:
I chose the first option as it seems more simple to work with, though you could potentially mistake the order of the variables if working with many. For example by sending ID,Name,Move when the function takes ID,Move,Name. So it's up to you, to choose how you want to formulate your request.
Now the DAL is a nice thing, cause if you use SQLite on your machine, but you want to upload it to some website which only has MySQL or say PostgreSQL databases. All you have to change is the connection string to the database, all your other code stays the same.
AS of now, our database looks like:
Field('TS1','integer',default=0), #Tack 1 state
The database "rooms" contains the information:
1) number of players in the room
2) p1 id
3) p1 name
6) the valiue of the first position, taking that the 9 rock field is represented in the fashion
1 2 3
4 5 6
7 8 9
15) The position which was changed on the last move, so we wouldn't have to send all of the 9 variables to see which one changed
16) Which players turn is it now
17) Is the game over
The "room_count" database, just keeps score of how many rooms exist in the database at the moment. When it hits 10000 the databases are wiped and if you were playing in room 9999 at that time, I'm sorry, it's just a fail-safe :)
The server-side of the game consists of 4 main functions:
And 2 helper functions:
The function names probably describe them enough. And I'll go into more detail of the python code in a revision of the tutorial if requested. For now I'll leave it at that.
For cross-domain access every function has a:
response.headers["Access-Control-Allow-Origin"] = client_address
The "client address" variable is located in the top of the default.py file text, so you wouldn't have to rewrite the access header value in every function:
client_address='*' - means requests from any domain will be accepted.
If set it to an address like:
client_address='http://zerostatesociety.com' - requests will only accepted from this domain