davidje13 / koth-webplayer

Framework for King-of-the-Hill Javascript competitions
MIT License
7 stars 7 forks source link

Support running in NodeJS #20

Open davidje13 opened 6 years ago

davidje13 commented 6 years ago

For larger competitions, running on a server can be beneficial. To support this, the game running logic should be made reusable in a NodeJS environment.

Initially only the scores (and possibly game seeds) need be stored / returned, but a later enhancement could be to record game states, allowing later replaying of games in a browser (or even live-streaming of games).

This will also require additional thoughts on security, as the HTML sandbox will not be available, and all submitted code will be able to access the filesystem, etc.

eaglgenes101 commented 6 years ago

So where should I start contributing? Can NodeJS make heads or tails of the framework code as-is?

davidje13 commented 6 years ago

Honestly I don't know what the issues will be. I imagine the bulk of the code will be fine, but the requirejs-esque parts will probably not work (i.e. the define() blocks wrapping each file may not be compatible; I think NodeJS uses module.exports instead, but maybe it supports define too). It certainly doesn't support web-workers (doesn't really have any threading to speak of), but maybe there's a community-made library to shim them.

Sadly I don't really have time to look at this myself right now, but I think it would be a great capability.

A simpler alternative which would also allow you to run this on a server is to experiment with PhantomJS. It won't be as efficient, but because it's basically an old version of Google Chrome that happens to run without a UI it should just work (and its built-in sandboxing might offer better security, but I still wouldn't bet a server I care about on it). You'll need to figure out some way to trigger games and get the results (which might involve writing a separate NodeJS service, or Java or something else if you prefer, just to invoke the necessary functions in PhantomJS and handle the results). There are lots of PhantomJS communication libraries around (most famously Selenium, usually used for tests but could also do this).

If you want to try running it in NodeJS, my suggestion would be for you to get a feel for running Javascript code on a server (e.g. a simple hello-world service). You might need to get up-to-speed with NodeJS / PhantomJS / NPM if you don't know them already. Next step might be to get some test files (e.g. in core) working under NodeJS.

eaglgenes101 commented 6 years ago

I've done quite a bit of hacking over the past few weeks to help this, and I'm starting to get somewhere, with rudimentary batch running currently implemented (with obvious caveats due to untested, messy code). You can check my fork if you want to see.

eaglgenes101 commented 6 years ago

I think we can mark this as mostly resolved, with only a number of document references to update as a result of this change. I see that my changes were pulled into a separate branch, so that has to be merged in too.

davidje13 commented 6 years ago

Yup, I think this can be resolved once the branch is merged in. Good call on the documentation; that can be handled after the merge. (see my comments on the closed PR for detail on where I'm up to with merging that branch)