0

Scenario:

I am developing a text-based RPG game in Django and I am wondering what the best way would be to run an event (such as a fight between two characters) server side, and instead of simply passing the result back to the client via a template, such as who won, I want to re-run this 'fight' but in a slower speed using JavaScript. This would show each characters 'turn' and the damage they dealt, although this would be processed instantly on the server.

The methods I thought about using were as follows:

  • Pass the combat log/fight through to the template via context using some dictionary or JSON and access the template variable via JavaScript
  • Send this data to an API which the JavaScript code in the template will fetch and modify to show at a reduced speed

Do anyone have any better solutions? I don't think I should be posting the outcome of each fight using an API just to transfer this data to client side for use with JS, it's a waste of storage if this is the only use.

Thanks!

lstev
  • 1
  • Have you thought about using WebSockets? – Michael Curry Mar 05 '18 at 12:15
  • @MichaelCurry No, I don't have experience in WebSockets but I think it would be overkill? I think all I am wanting to do is pass data from Python (in a view for example) to a template to be accessed by JavaScript. I know you can do this via a context variable but thought this is a bit weird to do and must be a simpler/easier/better way. – lstev Mar 05 '18 at 12:53
  • Instead of including the JS in your template, just pass a JSON object with a 'log' of the fight. Keep all your JS in a separate file, when you want to 'run' the fight, just make an AJAX request using JS to your Django server. In the success function of your AJAX you'll have your JSON for the fight, you can then use an interval in JS to replay it slowly. See this question: https://stackoverflow.com/questions/1445989/passing-python-data-to-javascript-via-django – Michael Curry Mar 05 '18 at 13:53

0 Answers0