Can i use a seperated gamelogic instance in match handle?

i am writing a porker game in typescript.
and i create a GameLogic class which have some methods to handle game flow.
eg. sendCards, shuffleCards …etc.
and also have some property eg, cards, currentPlayer …etc.

so i save this GameLogic instance in the match state like this.

state={
gameLogic: new GameLogic()
}

and access it in whole match lifecycle handles

however, it is not support, it looks the state object must be a simple object that can be serialized by JSON。
so what can i do, the GameLogic class is copied from my old project that use socket.js. i perfer to use it directly, otherthan hardcore translate and inject into match handles.

or can i just save the GameLogic as a constant on the top of all handles, but i wonder how the handlers work. is it seperated by each match? what i want is one match with one GameLogic instance to manage.

any hint? thanks

at the end of the match loop serialize the GameLogic and at the start of the loop deserialize it and process it. that’s what I am doing.

thanks for reply.
yes, that’s what i did so far too.
i made my GameLogic class be stateless too, and re-create it in each match handler hooks, and serialize it into plain object and store in state.

i also defined a tick method in gameLogic, and in each loop hooks, it would be called.

so far it looks fine, however, i have no idea about how to log out some debug infomation. as the console.log can not be used, and i try to set the logger object form the paramater of match handler to gameLogic, however it is not work too.

do u have any bette idea to print out logs in gameLogic ?

when creating the instance pass the logger from handler.
gameLogic: new GameLogic(logger: nkruntime.Logger)

public constructor(logger: nkruntime.Logger)
{
this._logger = logger;
}

what i did is like this

gameLogic: new GameLogic(logger.debug)

and inside GameLogic is use like this

constructor(log)
{
this.log = log;
}

test()
{
this.log('hello','world');
}

works as expected? Facing any issue with this logic?

yes.it works fine. i asked here and got response from heroiclabs guy

1 Like