I built a multiplayer game (more precisely, 4 players) using the erlang messaging construct. As an example, I followed the tictactoe game at the following link, but what really seems to be a message passing construct, as shown in the game:
Then I decided to launch this game on ejabberd Multi User Chatroom, I wrote an ejabberd hook for this. But if you look at NewGameState in the tictactoe.erl file from the link above, you will find that there is no way to get it in a variable.
So, I used mnesia and wrote every new gameplay generated in this mnesia table. Now inside my ejabberd hook I call my game function (that is, a series of modules is executed for each call β "gen_server, game_modules, mnesia_modules") and inside the hook just below the game function call, which I read from the mnesia table for gamestate as follows (here the myMessage function is a function inside the ejabberd hook):
myMessage({
Now my problem is that the read operation gives me an empty table when the execution order
some_other_module:game_func(Args), GameState=mnesia_module:read(key),
and when I insert a delay between these two lines as timer:sleep/1 , as shown below (a value of 200 is randomly selected after some testing with different values):
some_other_module:game_func(Args), timer:sleep(200) GameState=mnesia_module:read(key),
I get the correct GameState value, so I suggest that the read operation in the line
GameState=mnesia_module:read(key),
executed / executed before the string some_other_module:game_func(Args) (which is a series of modules β "gen_server, game_modules, mnesia_modules") can execute mnesia modules and write GameState to the mnesia table.
How to solve this problem, because I do not want to use timer:sleep/1 , since this is not a reliable solution.
Can anyone suggest me a job here. What do I mean, can anyone suggest me a way to get the GameState inside the hook in any other ways than mnesia, so I donβt have a race condition at all.
Or is there some way that ejabberd provides some functions that I can use here?
Thanks in advance.