Page 1 of 1

Arguments of notification message disappearing after a reload

Posted: 06 December 2020, 01:25
by Lymon Flowers
Dear all,

I have encountered the following, what do you think, is that a bug in the platform or some technical limitation ?

I have created a notification to say that someone played a piece somewhere or transfers pieces:

Code: Select all

            $this -> notifyAllPlayers ( 'piecePlaced', clienttranslate ( '${player_name} places ${piece} at position ${x},${y}' ),
                                        [ 'player_name' => $this -> getActivePlayerName ( ),
                                          'player_id' => $this -> getActivePlayerId ( ),
                                          'x' => $x,
                                          'y' => $y,
                                          'piece' => $type,
                                          'piece_type' => $type,
                                          'piece_color' => $color,
                                          'possible_moves' => $piece->possiblePlaces() ] );
I have setup a format_string_recursive override:

Code: Select all

        format_string_recursive : function(log, args) {
            try {
                if (log && args && !args.processed) {
                    args.processed = true;
                    
                    if (!this.isSpectator)
                        args.You = this.divYou(); // will replace ${You} with colored version

                    // list of other known variables
                    var keys = [ 'piece' ];
                    
                    for (var k in keys) {
                        if (typeof args[keys[k]] == 'string') {
			    if ( keys[k] == 'piece' )
				args[keys[k]] = this.getPieceDiv(keys[k], args);
                        }
                    }
                }
            } catch (e) {
                console.error(log,args,"Exception thrown", e.stack);
            }
            return this.inherited(arguments);
        },
So that tokens are replaced with nice images. It works perfectly when playing. Here are for example the arguments passed to getPieceDiv:

Code: Select all

You: "<span style=\"font-weight:bold;color:#ff0000;\">You</span>"
​piece: "<span id=\"tamere\" class=\"medina-block medina-block-building medina-block-building-maroon \" data-palace-id=\"0\" data-x=\"0\" data-y=\"0\" data-type=\"building\" data-color=\"maroon\" style=\"left:0px;top:0px;z-index:99\"></span>"
​piece_color: "maroon"
​piece_type: "building"
​player_id: "2317321"
​player_name: "<!--PNS--><span class=\"playername\"><!--PNS--><span class=\"playername\" style=\"color:#ff0000;\">benj2</span><!--PNE--></span><!--PNE-->"
​possible_moves: Array(98) [ (2) […], (2) […], (2) […], … ]
​processed: true
​x: "9"
​y: "2"
​
However, when I reload the page, the same code gets called supposedly on the same notification. But only parts of the arguments are kept and sent to the JS code:

Code: Select all

You: "<span style=\"font-weight:bold;color:#ff0000;\">You</span>"
​piece: "building"
​player_id: "2317321"
​player_name: "<!--PNS--><span class=\"playername\"><!--PNS--><span class=\"playername\" style=\"color:#ff0000;\">benj2</span><!--PNE--></span><!--PNE-->"
​processed: true
​x: "9"
​y: "2"
​
As we can see, the "piece_type" and "piece_color" as well as other variables disappear. My best bet is that only the arguments that are actually used in the notification message get stored in the DB. So everything that is not displayed to the log message is lost.

Which means that I cannot use such arguments to display nice sprites. :mrgreen:

Is that an expected behavior or just some side effect ?

I think that I can still pack every information I need in a special argument and unpack them using JS, but that would do no good because this would cause unreadable text when the JS is not called (like in the "replay game" page).

Re: Arguments of notification message disappearing after a reload

Posted: 06 December 2020, 02:16
by Victoria_La
Yes this is platform limitation, when you reload game it does not actually plays your notification it plays some
sort of 'history' notification. The reason it works in my code is because I use same id for placement, it does not remove arguments
references in the message, i.e. if you do this
Bla bla ${token_name}

And pass both

'token_name'=>$token_id,
'token_id'=>$token_id

When realoding you will only get 'token_name', because it only one that is referenced, but in my case it is what I need to show right div

There are some hacks to keep them, for example you list args you need to keep for display after some separator, i.e. |, and you remove yours separator using format_log_recursive fcuntion, i..e

Code: Select all

			format_string_recursive: function(log, args) {
				try {
                                       ...
					if (log) {
						var k = log.indexOf('|');
						if (k > 0) log = log.substring(0, k);
					}
Your notif will look like this (dot to concate strings, clienttranslate should not include this part)
clienttranslate ( '${player_name} places ${piece} at position ${x},${y}' ) . "|${piece_color}${piece_type}"

The other method is similar but you pass all arguments required to make div as object (duplicating these args )

Code: Select all

'piece_div' => [ log=>'${piece}${piece_color}${piece_type}', 
                        args=>[
                                          'piece' => $type,
                                          'piece_type' => $type,
                                          'piece_color' => $color ]
]
And code that process it should replace piece_div with actual div using its own args, before going into recursion

Edit: I went to edit wiki but this was actually already there, this method and the problem and solution
https://en.doc.boardgamearena.com/BGA_S ... in_the_log

Re: Arguments of notification message disappearing after a reload

Posted: 08 December 2020, 11:57
by Lymon Flowers
The recursive arguments approach looks good, thanks!