adrfoong / t3

MIT License
0 stars 1 forks source link

Evaluate custom player code #2

Open adrfoong opened 3 years ago

adrfoong commented 3 years ago

This code creates the bot function from a player's gist or custom input:

https://github.com/adrfoong/t3/blob/a1874cd50b043b1b6ae4e28796f4c3c62e953f1d/src/GameManager.ts#L40-L56

In a previous version of this project, the app makes a request to the player's server (or serverless function) to get the next move. In this version, I've tried to make this part more accessible by turning a plain text input into a function using eval() (using the Function() constructor works as well). There are concerns with eval() but I don't think it's an issue with how it's being used here.

How else can we allow users to give us their custom functions without using eval() or Function()?

karkipy commented 3 years ago

serialisation could be implemented to tackle this, though why would you want to get it from the player, wouldn't it make sense to have player hit a server then that will trigger the change ?

adrfoong commented 3 years ago

@karkipy Can you explain with an example how you would tackle this with serialization? Also, there is no server for the players to hit with this app. It essentially just runs the players' provided functions.

karkipy commented 3 years ago

@adrfoong we had similar issue as yours, so we decided to serialize our Classes which contained function to be called in individual player side. in your case i think you can standardise the function call with change in payload, so you can register function under certain standard name and the payload could differ

adrfoong commented 3 years ago

@karkipy this sounds interesting, do you have an example I can look at?

karkipy commented 3 years ago

@adrfoong sure man, sorry for the late response, so we have a library for such cases of serialization, https://github.com/Bhoos/serialization we register our classes and serialize them using said library, the other way around it would be standardizing the function name similar concept say all the players have same instances of function call like

 function doMove(payload) {
  // function does work gigo
 }

 class functionsCalls {
   this.fns = {};
    register(name, fn){
     this.fns[name] = fn;
    }

    execute(name, payload) {
      this.fns[name](payload);
    }

    getFn(name){ return this.fns[name]}
 }

 functionsCalls.register('doMove', doMove)

now you can store payload with function name rather than having an eval