Page 1 of 2

debugging php

Posted: 22 June 2020, 20:05
by developgame
Hi Everyone:
I am very new to trying to create games on Board Game Arena

I am having problems with debugging PHP code.

I do not know how to debug the game when the game does not even start. When I cannot even get into the game interface to access the logs.

How do you debug when the problem is with setup?

How are you debugging PHP code?
Which tools are you using?

Any suggestions on what has worked and what to stay away from?

Thanks
DG

Re: debugging php

Posted: 22 June 2020, 21:34
by Brainchild
Take a look at this page for some debugging techniques:

http://en.doc.boardgamearena.com/Practical_debugging

Re: debugging php

Posted: 22 June 2020, 22:32
by developgame
Thanks Brainchild, Really appreciate your help. The page really helps.


I do not know how to debug the game when the game does not even start. When I cannot even get into the game interface to access the logs.

How do you debug when the problem is with setup?


I added this reply to my original post as it was not clear.
Thanks again

DG

Re: debugging php

Posted: 22 June 2020, 22:47
by Fusionpro
I'm also a new developer here, developgame.

Some of the better places to start definitely include the page below, and all the associated materials in the right menu bar on that page:
https://boardgamearena.com/doc/Troubleshooting

Because of the scope of changes being made to all areas of a project, my experience has been to test early and test often. Using a repository system to check-in changes and keep track of code history is invaluable working with BGA to make sure you can roll-back to a happier time when your code worked as expected.

That all said, I would also LOVE to hear a veteran's tips and tricks for debugging PHP.

Re: debugging php

Posted: 23 June 2020, 10:27
by joezg
Here are a few tips I find useful for debugging a php code:
  • Use var_dump and die statements - you can always see output of these statements in javascript console in browser. Die statements are particularly useful when debugging some user action as their use will throw an error and rollback all the changes so you can repeatedly test same action. In your case you can put it at the beginning of setupNewGame method to see if it gets hit.
  • For debugging algorithms above wouldn't usually work good. I tend to encapsulate all game logic in php modules (you can put them in modules directory). As they are not coupled with BGA system and database I can debug them with local instance of php with xdebug enabled. You can see examples of it in my railroadink project. I have a couple of modules in modules directory and I test them with testBoard.php script in misc directory.
With this two techniques I tend to work smoothly on php code.

For your error, is there anything useful in error message you get? Remember that you can see php errors in javascript console when they disappear from the screen. Maybe there is some error in dbmodel.sql. Try to simplify code by removing most of it and then you can put it back chunk by chunk until you find the problematic code.

Re: debugging php

Posted: 23 June 2020, 10:42
by RicardoRix
Yes, mainly var_dump and die as already suggested.

You can also try echo, if you want to format some debug output better:
//echo '<pre>', $state, '::', $bco, ':C:', $bridgecolour, ':TS:', $token['state'], '</pre>';

You can also directly run php game functions from the chat. myTestFunc()

Re: debugging php

Posted: 23 June 2020, 21:48
by developgame
Fusionpro wrote: 22 June 2020, 22:47 I'm also a new developer here, developgame.

Some of the better places to start definitely include the page below, and all the associated materials in the right menu bar on that page:
https://boardgamearena.com/doc/Troubleshooting

Because of the scope of changes being made to all areas of a project, my experience has been to test early and test often. Using a repository system to check-in changes and keep track of code history is invaluable working with BGA to make sure you can roll-back to a happier time when your code worked as expected.

That all said, I would also LOVE to hear a veteran's tips and tricks for debugging PHP.
Thank you Fusionpro! Your troubleshooting page is really helpful. I was able to use it to get the the game to finally load properly!

Thanks for your tips on testing frequently and early and a repository so rolling back to when the code worked properly. Smart ideas!

Check out the tips I got from other developers. I am going to try each one out. Probably going to learn a whole lot.

Re: debugging php

Posted: 23 June 2020, 21:51
by developgame
joezg wrote: 23 June 2020, 10:27 Here are a few tips I find useful for debugging a php code:
  • Use var_dump and die statements - you can always see output of these statements in javascript console in browser. Die statements are particularly useful when debugging some user action as their use will throw an error and rollback all the changes so you can repeatedly test same action. In your case you can put it at the beginning of setupNewGame method to see if it gets hit.
  • For debugging algorithms above wouldn't usually work good. I tend to encapsulate all game logic in php modules (you can put them in modules directory). As they are not coupled with BGA system and database I can debug them with local instance of php with xdebug enabled. You can see examples of it in my railroadink project. I have a couple of modules in modules directory and I test them with testBoard.php script in misc directory.
With this two techniques I tend to work smoothly on php code.

For your error, is there anything useful in error message you get? Remember that you can see php errors in javascript console when they disappear from the screen. Maybe there is some error in dbmodel.sql. Try to simplify code by removing most of it and then you can put it back chunk by chunk until you find the problematic code.
Thank you Joezg! I did not know that var_dump and die statements puts output on the javascript console in the browser. That knowledge is going to make programming on BGA so much easier as I am having problems seeing my php errors. Javascript is much easier because of the javascript console.

Thanks for explaining how you are coding your railroadink project! Going to check that out tomorrow and probably going to learn a few things. Thanks!
Yes, you are correct, the error message was in the javascript console. I also did not put in the table changes into dbmodel.sql. My game start now, at least the setup sort of looks okay, yay! and Thanks!

Re: debugging php

Posted: 23 June 2020, 22:03
by developgame
RicardoRix wrote: 23 June 2020, 10:42 Yes, mainly var_dump and die as already suggested.

You can also try echo, if you want to format some debug output better:
//echo '<pre>', $state, '::', $bco, ':C:', $bridgecolour, ':TS:', $token['state'], '</pre>';

You can also directly run php game functions from the chat. myTestFunc()
Thanks RicardoRix ! I have always used echo for debugging, but did not know about <pre>', $state, '::', $bco, ':C:', $bridgecolour, ':TS:', $token['state'], . Thanks for the tip!

Wow, did not know about running php functions from chat. Nice information! Thanks!

Re: debugging php

Posted: 26 June 2020, 01:11
by Fusionpro
Not sure if I'm doing something wrong, but in the game.php file I was attempting to debug using:

var_dump('variable');

Turns out I was getting errors only when attempting to call var_dump in the setupNewGame() function. In order to check the results I had to move the call to getAllDatas() in order for the game to start - otherwise I would get halted with a

Unexpected error: Wrong formatted data from BGA gameserver 1 (method: createGame): ...

Just one example of things that have slowed my efforts at debugging PHP down. :?