This is a documentation for Board Game Arena: play board games online !

User talk:SwHawk/Create Modular Code: Difference between revisions

From Board Game Arena
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Please sign your comments using this syntax <code><nowiki>-- ~~~~</nowiki></code> so that I may credit your additions to the article.
==User names==
==User names==
"Tisaac Human Expert" -> Tisaac
"Tisaac Human Expert" -> Tisaac
Line 4: Line 6:
"VictoriaLia" -> VictoriaLa
"VictoriaLia" -> VictoriaLa


-- [[User:Mogri|Mogri]] ([[User_talk:Mogri|talk]]) 18:08, 14 June 2022 (CEST)
: Thanks for the corrections -- [[User:SwHawk|SwHawk]] ([[User talk:SwHawk|talk]]) 19:45, 14 June 2022 (CEST)
==A non-dojo JS approach==
==A non-dojo JS approach==


Line 34: Line 39:


Notes on this approach:
Notes on this approach:
- Aside from setup and any framework-override functions, you should be using arrow functions
*Aside from setup and any framework-override functions, you should be using arrow functions
- Always use X instead of this
*Always use X instead of this
- As usual, you can use any number of modules this way
*As usual, you can use any number of modules this way
 
-- [[User:Mogri|Mogri]] ([[User_talk:Mogri|talk]]) 18:08, 14 June 2022 (CEST)
 
: While I agree that most of the components of the framework (be it PHP or JS) are old, there are some advantages to using its loader: dependencies management for instance. Also I'm not sure that changing every function definition to an arrow function offers better readability but that's a personal opinion, as I'm more used to languages where function keywords exists. -- [[User:SwHawk|SwHawk]] ([[User talk:SwHawk|talk]]) 19:45, 14 June 2022 (CEST)
 
== Highlight lines added in code blocks ==
 
The code blocks are rather long right now with all of the comments, however I do think it is worth keeping them so they look like the starter files.
 
You could consider coloring or emphasising the lines added in some way.
 
e.g.
<nowiki>[[X_game.php]]</nowiki>
<?php
/**
  *------
  * BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
  * X implementation : © <Your name here> <Your email address here>
  *
  * This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
  * See http://en.boardgamearena.com/#!doc/Studio for more information.
  * -----
  *
  * X.game.php
  *
  * This is the main file for your game logic.
  *
  * In this PHP file, you are going to defines the rules of the game.
  *
  */
'''require_once 'modules/UtilityFunctionsTrait.php';''' //Here we require the trait to be loaded when the class is loaded
<span style="color:green">use YourProjectNamespace\Modules\UtilityFunctionsTrait;</span> //We import the trait to be used directly as UtilityFunctionsTraits, otherwise we would have to use the Fully qualified  class name (YourProjectNamespace\Modules\UtilityFunctionsTrait)
class YourProjectName extends Table
{
//...
//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////   
    /*
        In this space, you can put any utility methods useful for your game logic
    */
    <span style="color:green">'''use UtilityFunctionsTrait;'''</span> //Here we actually include the trait inside the class, making all the trait's methods available as class methods, keeping their visibility scope (so a private method is still private)
}
-- [[User:Thenmal|Thenmal]] ([[User_talk:Thenmal|Thenmal]]) 19:04, 14 June 2022 (CEST)
 
: Well thanks for that, I looked into the formatting guide, but I must have overlooked that part, thanks for showing me how to do it! -- [[User:SwHawk|SwHawk]] ([[User talk:SwHawk|talk]]) 19:45, 14 June 2022 (CEST)

Latest revision as of 19:01, 14 June 2022

Please sign your comments using this syntax -- ~~~~ so that I may credit your additions to the article.

User names

"Tisaac Human Expert" -> Tisaac

"VictoriaLia" -> VictoriaLa

-- Mogri (talk) 18:08, 14 June 2022 (CEST)

Thanks for the corrections -- SwHawk (talk) 19:45, 14 June 2022 (CEST)

A non-dojo JS approach

Dojo is old and kinda bad. This approach lets you avoid Dojo and this and allows more modern JS.

[X.js]
window.X = {}; // X = your game, or you can use something more generic

...

setup: function (gameData) { // this cannot be an arrow function due to dojo
   X = Object.assign(this, X);
   // the rest of your setup goes here
},
[modules/whatever.js]
X = Object.assign(X, {
  MY_CONSTANT: 1,

  myFunction: (arg1, arg2) => {
    X.someFunction(X.MY_CONSTANT);
    return arg1 + arg2;
  },

  someFunction: (x) => _('The answer is: ') + x,
}

Notes on this approach:

  • Aside from setup and any framework-override functions, you should be using arrow functions
  • Always use X instead of this
  • As usual, you can use any number of modules this way

-- Mogri (talk) 18:08, 14 June 2022 (CEST)

While I agree that most of the components of the framework (be it PHP or JS) are old, there are some advantages to using its loader: dependencies management for instance. Also I'm not sure that changing every function definition to an arrow function offers better readability but that's a personal opinion, as I'm more used to languages where function keywords exists. -- SwHawk (talk) 19:45, 14 June 2022 (CEST)

Highlight lines added in code blocks

The code blocks are rather long right now with all of the comments, however I do think it is worth keeping them so they look like the starter files.

You could consider coloring or emphasising the lines added in some way.

e.g.

[[X_game.php]]
<?php
/**
 *------
 * BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
 * X implementation : © <Your name here> <Your email address here>
 * 
 * This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
 * See http://en.boardgamearena.com/#!doc/Studio for more information.
 * -----
 * 
 * X.game.php
 *
 * This is the main file for your game logic.
 *
 * In this PHP file, you are going to defines the rules of the game.
 *
 */

require_once 'modules/UtilityFunctionsTrait.php'; //Here we require the trait to be loaded when the class is loaded

use YourProjectNamespace\Modules\UtilityFunctionsTrait; //We import the trait to be used directly as UtilityFunctionsTraits, otherwise we would have to use the Fully qualified  class name (YourProjectNamespace\Modules\UtilityFunctionsTrait)

class YourProjectName extends Table
{
//...

//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////    

   /*
       In this space, you can put any utility methods useful for your game logic
   */

   use UtilityFunctionsTrait; //Here we actually include the trait inside the class, making all the trait's methods available as class methods, keeping their visibility scope (so a private method is still private)
}

-- Thenmal (Thenmal) 19:04, 14 June 2022 (CEST)

Well thanks for that, I looked into the formatting guide, but I must have overlooked that part, thanks for showing me how to do it! -- SwHawk (talk) 19:45, 14 June 2022 (CEST)