I'm in the process of porting a command-line game to the web. I will be using Ajax to glue everything together.
开发者_如何学GoNow after the first few proof-of-concept lines I wrote I'm facing with architecture doubts. How would you normally structure such a project?
My game engine is written in object-oriented style in Python so I have for example a Game class, a Player class, etc...
As far as I can tell I have basically 2 options:
a) use Javascript as the glue b) reproduce the bare minimum of my object model in Javascript
Since this is a card game I would have for example a function to "play the card" and in option A it would look a lot like this:
function playCard(idx, player){
$.ajax({
type: 'POST',
url: '/play_card/' + idx + '/',
success: function(){//do something}
});
}
In option B this playCard would be a method of a Player object that has been instanced at the start of the game.
Also, in case you would go for option B how would you connect the Javascript instance with its server-side counterpart? For example when PlayerA.playCard(1) plays a card how could I make it aware of which Player's cards pick that card from?
Thanks in advance.
I work on a site where we use Django and Ajax. The only tricky part is form validation. We don't push the generated form html to JavaScript, instead we build our own UI forms and created a REST API that uses the Django models and form api manually in our python ajax handlers. The other tricky thing is the csrf token which you'll have to tell your JavaScript about.
精彩评论