thadarawgs.js - hadarawgs - Hadara adaptation for boardgamearena.com
 (HTM) git clone git://git.z3bra.org/hadarawgs.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       thadarawgs.js (7771B)
       ---
            1 /**
            2  *------
            3  * BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
            4  * hadarawgs implementation : © Willy Goiffon <dev@z3bra.org>
            5  *
            6  * This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
            7  * See http://en.boardgamearena.com/#!doc/Studio for more information.
            8  * -----
            9  *
           10  * hadarawgs.js
           11  *
           12  * hadarawgs user interface script
           13  *
           14  * In this file, you are describing the logic of your user interface, in Javascript language.
           15  *
           16  */
           17 
           18 define([
           19         "dojo","dojo/_base/declare",
           20         "ebg/core/gamegui",
           21         "ebg/counter",
           22         "ebg/stock"
           23 ],
           24 
           25 function (dojo, declare) {
           26         return declare("bgagame.hadarawgs", ebg.core.gamegui, {
           27                 constructor: function() {
           28                         console.log('hadarawgs constructor');
           29 
           30                         // global variables
           31                         this.cardwidth  = 64;
           32                         this.cardheight = 96;
           33                 },
           34 
           35                 /*
           36                  * setup:
           37                  *
           38                  * This method must set up the game user interface according to current game situation specified
           39                  * in parameters.
           40                  *
           41                  * The method is called each time the game interface is displayed to a player, ie:
           42                  * _ when the game starts
           43                  * _ when a player refreshes the game page (F5)
           44                  *
           45                  * "gamedatas" argument contains all datas retrieved by your "getAllDatas" PHP method.
           46                 */
           47 
           48                 setup: function( gamedatas ) {
           49                         console.log( "Starting game setup" );
           50 
           51                         // Setting up player boards
           52                         // TODO: Set up your game interface here, according to "gamedatas"
           53                         for (var player_id in gamedatas.players) {
           54                                 var player = gamedatas.players[player_id];
           55                                 var board  = gamedatas.boards[player_id];
           56                                 board['ncard'] = 0;
           57 
           58                                 // Setup player panel values
           59                                 var panel = $('player_board_' + player_id);
           60                                 dojo.place(this.format_block('jstpl_player_board', board), panel);
           61 
           62                                 // Make card numbers stand out if food level is too low
           63                                 if (board['ncard'] > board['food'])
           64                                         document.getElementById('pp_ncard_p'+ player_id).classList.add('standout');
           65 
           66                                 // Set token values on each player board
           67                                 var tokens = ["income", "military", "culture", "food"];
           68                                 tokens.forEach(function (token) {
           69                                         var cur = $(token +'_p' + player_id);
           70                                         cur.innerHTML = board[token];
           71                                 });
           72                         }
           73 
           74                         this.deck = new ebg.stock();
           75                         this.deck.create(this, $('deck'), this.cardwidth, this.cardheight);
           76                         this.deck.image_items_per_row = 10;
           77                         this.deck.centerItems = true;
           78 
           79                         for (var epoch = 0; epoch < 3; epoch++) {
           80                                 for (cardtype = 0; cardtype < 5; cardtype++) {
           81                                         for (var cardid = 0; cardid < 10; cardid++) {
           82                                                 var cardref = cardtype * 10 + epoch * 50 + cardid;
           83                                                 this.deck.addItemType(cardref, cardtype, g_gamethemeurl + 'img/cards.png', cardref);
           84                                                 this.deck.addToStock(cardref, 'deck');
           85                                         }
           86                                 }
           87                         }
           88 
           89                         // Setup game notifications to handle (see "setupNotifications" method below)
           90                         this.setupNotifications();
           91 
           92                         console.log( "Ending game setup" );
           93                 },
           94 
           95 
           96                 ///////////////////////////////////////////////////
           97                 //// Game & client states
           98 
           99                 // onEnteringState: this method is called each time we are entering into a new game state.
          100                 //                  You can use this method to perform some user interface changes at this moment.
          101                 //
          102                 onEnteringState: function( stateName, args )
          103                 {
          104                         console.log( 'Entering state: '+stateName );
          105 
          106                         switch( stateName )
          107                         {
          108 
          109                         /* Example:
          110 
          111                         case 'myGameState':
          112 
          113                                 // Show some HTML block at this game state
          114                                 dojo.style( 'my_html_block_id', 'display', 'block' );
          115 
          116                                 break;
          117                    */
          118 
          119 
          120                         case 'dummmy':
          121                                 break;
          122                         }
          123                 },
          124 
          125                 // onLeavingState: this method is called each time we are leaving a game state.
          126                 //                 You can use this method to perform some user interface changes at this moment.
          127                 //
          128                 onLeavingState: function( stateName )
          129                 {
          130                         console.log( 'Leaving state: '+stateName );
          131 
          132                         switch( stateName )
          133                         {
          134 
          135                         /* Example:
          136 
          137                         case 'myGameState':
          138 
          139                                 // Hide the HTML block we are displaying only during this game state
          140                                 dojo.style( 'my_html_block_id', 'display', 'none' );
          141 
          142                                 break;
          143                    */
          144 
          145 
          146                         case 'dummmy':
          147                                 break;
          148                         }
          149                 },
          150 
          151                 // onUpdateActionButtons: in this method you can manage "action buttons" that are displayed in the
          152                 //                        action status bar (ie: the HTML links in the status bar).
          153                 //
          154                 onUpdateActionButtons: function( stateName, args )
          155                 {
          156                         console.log( 'onUpdateActionButtons: '+stateName );
          157 
          158                         if( this.isCurrentPlayerActive() )
          159                         {
          160                                 switch( stateName )
          161                                 {
          162 /*
          163                                 Example:
          164 
          165                                 case 'myGameState':
          166 
          167                                    // Add 3 action buttons in the action status bar:
          168 
          169                                 this.addActionButton( 'button_1_id', _('Button 1 label'), 'onMyMethodToCall1' );
          170                                 this.addActionButton( 'button_2_id', _('Button 2 label'), 'onMyMethodToCall2' );
          171                                 this.addActionButton( 'button_3_id', _('Button 3 label'), 'onMyMethodToCall3' );
          172                                 break;
          173 */
          174                                 }
          175                         }
          176                 },
          177 
          178                 ///////////////////////////////////////////////////
          179                 //// Utility methods
          180 
          181                 /*
          182 
          183                         Here, you can defines some utility methods that you can use everywhere in your javascript
          184                         script.
          185 
          186                 */
          187 
          188 
          189                 ///////////////////////////////////////////////////
          190                 //// Player's action
          191 
          192                 /*
          193 
          194                         Here, you are defining methods to handle player's action (ex: results of mouse click on
          195                         game objects).
          196 
          197                         Most of the time, these methods:
          198                         _ check the action is possible at this game state.
          199                         _ make a call to the game server
          200 
          201                 */
          202 
          203                 /* Example:
          204 
          205                 onMyMethodToCall1: function( evt )
          206                 {
          207                         console.log( 'onMyMethodToCall1' );
          208 
          209                         // Preventing default browser reaction
          210                         dojo.stopEvent( evt );
          211 
          212                         // Check that this action is possible (see "possibleactions" in states.inc.php)
          213                         if( ! this.checkAction( 'myAction' ) )
          214                         {   return; }
          215 
          216                         this.ajaxcall( "/hadarawgs/hadarawgs/myAction.html", {
          217                                                                                     lock: true,
          218                                                                                     myArgument1: arg1,
          219                                                                                     myArgument2: arg2,
          220                                                                                     ...
          221                                                                                  },
          222                                          this, function( result ) {
          223 
          224                                             // What to do after the server call if it succeeded
          225                                             // (most of the time: nothing)
          226 
          227                                          }, function( is_error) {
          228 
          229                                             // What to do after the server call in anyway (success or failure)
          230                                             // (most of the time: nothing)
          231 
          232                                          } );
          233                 },
          234 
          235                 */
          236 
          237 
          238                 ///////////////////////////////////////////////////
          239                 //// Reaction to cometD notifications
          240 
          241                 /*
          242                         setupNotifications:
          243 
          244                         In this method, you associate each of your game notifications with your local method to handle it.
          245 
          246                         Note: game notification names correspond to "notifyAllPlayers" and "notifyPlayer" calls in
          247                                   your hadarawgs.game.php file.
          248 
          249                 */
          250                 setupNotifications: function()
          251                 {
          252                         console.log( 'notifications subscriptions setup' );
          253 
          254                         // TODO: here, associate your game notifications with local methods
          255 
          256                         // Example 1: standard notification handling
          257                         // dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
          258 
          259                         // Example 2: standard notification handling + tell the user interface to wait
          260                         //            during 3 seconds after calling the method in order to let the players
          261                         //            see what is happening in the game.
          262                         // dojo.subscribe( 'cardPlayed', this, "notif_cardPlayed" );
          263                         // this.notifqueue.setSynchronous( 'cardPlayed', 3000 );
          264                         //
          265                 },
          266 
          267                 // TODO: from this point and below, you can write your game notifications handling methods
          268 
          269                 /*
          270                 Example:
          271 
          272                 notif_cardPlayed: function( notif )
          273                 {
          274                         console.log( 'notif_cardPlayed' );
          275                         console.log( notif );
          276 
          277                         // Note: notif.args contains the arguments specified during you "notifyAllPlayers" / "notifyPlayer" PHP call
          278 
          279                         // TODO: play the card in the user interface.
          280                 },
          281 
          282                 */
          283    });
          284 });