@Laurent : good to know :( I am not alone @Oosh : yes, it is already part of the things tried already, I'll try to paste some code extracts @Scott : For me it is better experience if the user doesn't have to know that he has a manual setting to take care of... but the manual toggle and the option in the sheet.json is the current we have before trying to have this function... OK, some code In the Sheetworker, on sheet opened, set the API toggle to 2 (I have pinged the API and waiting for a pong), and API_max to 12 or 2 (this ensures that there is some kind of heartbeat so that there is no lack of detection by the API due to having missed the changed value on API // Update the API status according to watchdog from the API
var checkWatchdog = function fncheckWatchdog(){
'use strict';
try{
getAttrs(["edition_max", "NPC", "API", "API_max"], function gacheckWatchdog(values) {
'use strict';
logvalues( values);
log("API Watchdog " +values["API"] + " max "+ values["API_max"])
//API Watchdog - Set to NoAPI no matter what, if the API is active it will reset to API.
//Temporarily disabled until API is released
let vals={};
switch( values[ "API" ] ) {
case undefined :
vals["API"]="0";
case "0": case 0:
if( values[ "API_max" ] == "10" )
vals[ "API_max" ] = "0";
else
vals[ "API_max" ] = "10";
break;
case "1": case 1:
vals[ "API" ] = "2";
vals[ "API_max" ] = "2";
break;
case "2": case 2:
if( values[ "API_max" ] == "2" )
vals[ "API_max" ] = "12";
else
vals[ "API_max" ] = "2";
}
setAttrsLog( vals );
});
} catch(err) {log("Earthdawn:CheckWatchdog() error caught: " + err );}
};//end CheckWatchdog/ // When a sheet is first opened, see if it needs to be updated from a previous version.
on('sheet:opened',function fnOnSheetOpened(){
'use strict';
try{
// edition_max is character sheet version
getAttrs(["edition_max", "NPC"], function gaSheetOpened(values) {
'use strict';
logvalues( values);
checkWatchdog();
var newSheetVersion = SheetVersion; // The Version number is set as a global variable at the beginning of the sheet-worker. It is changed with every revision of the sheet that needs to call this routine. Also updated twice above and in .js file.
var oldSheetVersion = parseFloat( values[ "edition_max" ] );
if( oldSheetVersion != newSheetVersion ) {
updateCharactersheet( newSheetVersion, oldSheetVersion, oldSheetVersion, 0, values, 0, "" );
} // End there are updates to perform
});
recalc(); // recalc on sheet opened whether updated anything or not.
} catch(err) {log("Earthdawn:OnSheetOpened() error caught: " + err );}
});//end OnSheetOpened In the API, a routine checks for changes /addition of attributes, and triggers a response depending on which attribute it is... If it is API (i.e. the watchdog coming from the sheetworker), respond to it on("add:attribute", function (attr) {
'use strict';
Earthdawn.attribute( attr );
});
// change attribute. See if it needs some special processing.
on("change:attribute", function (attr, prev) {
'use strict';
Earthdawn.attribute( attr, prev );
}); // end on("change:attribute" // An attribute for some character has changed. See if it is one that needs special processing and do it.
Earthdawn.attribute = function ( attr, prev ) {
'use strict';
try {
log( attr); // use attr.get("name") and attr.get("current"). // {"name":"Wounds","current":"1","max":8,"_id":"-MlqexKD2f4f744TgzlK","_type":"attribute","_characterid":"-MlqeuXlNO51-RYxmJv8"}
//log( prev); // use prev["name"] and prev["current"] // {"name":"Wounds","current":0,"max":8,"_id":"-MlqexKD2f4f744TgzlK","_type":"attribute","_characterid":"-MlqeuXlNO51-RYxmJv8"}
let sa = attr.get( "name" ),
cID = attr.get( "_characterid" );
if( sa === "API" ) { // API or API_max has changed. If they have not changed to "1", set them to "1".
if( attr.get( "current" ) !== "1" ) {
Earthdawn.setWithWorker( attr, "current", "1", "1" );
Earthdawn.setWithWorker( attr, "max", "1", "1" );
log("set API watchdog from API change")
} }
Another routine checks for new characters, and after a timeout (to let the sheetworker do its stuff) sets some initializations on("add:character", function( obj ) { // Brand new character. Make sure that certain important attributes fully exist. 'use strict'; let ED = new Earthdawn.EDclass(); ED.newCharacter( obj.get( "_id" ) ); }); // This routine runs when a new character is added. // It can be called from events "on character add" when one is created manually, or // It can be called from WelcomePackage when it makes one. // Note, it also seem to be triggered on character add, when a character is imported from the character vault (in which case it is not truely a new character!). this.newCharacter = function( cID ) { 'use strict'; let edc = this; try { setTimeout(function() { // When a character is imported from the character vault, the recalc caused by edition has been observed to cause a race condition. Delay this processig long enough for the import to have been done. try { Earthdawn.errorLog ( "newchar test ", this); let npc = ( typeof WelcomePackage === 'undefined' ) ? Earthdawn.charType.pc : Earthdawn.charType.npc; let plr = findObjs({ _type: "player", _online: true }); // If there is only one person on-line, that is the player. if( plr && plr.length === 1 ) npc = playerIsGM( plr[0].get( "_id")) ? Earthdawn.charType.npc : Earthdawn.charType.pc; let CharObj = getObj( "character", cID); // See if we can put a default value in the player name. if ( CharObj ) { let lst = CharObj.get( "controlledby" ); let arr = _.without( lst.split( "," ), "all" ); if( arr && arr.length === 1 && arr[ 0 ] !== "" ) { // If there is only one person who can control the character, use their name. let pObj = getObj( "player", arr[ 0 ]); if( pObj ) { Earthdawn.SetDefaultAttribute( cID, "player-name", pObj.get( "_displayname" )); let attribute = Earthdawn.findOrMakeObj({_type: 'attribute', _characterid: cID, name: "playerWho"}); Earthdawn.set( attribute, "current", pObj.get( "_displayname" )); Earthdawn.set( attribute, "max", pObj.get( "_id" )); } npc = playerIsGM( arr[ 0 ] ) ? Earthdawn.charType.npc : Earthdawn.charType.pc; // character was created by welcome package, if for GM, make it an NPC else PC. } } let attr = Earthdawn.findOrMakeObj({_type: 'attribute', _characterid: cID, name: "API"}); attr.set( "current", "1"); //set the API running attr.set( "max", "1"); log("set API watchdog from newChar "+attr.get("current")); // If a character was created by Welcome package for a Player, or if Welcome Package is not installed, default to PC. // If a character was created by Welcome package for a GM, or if Welcome Package is installed, default to NPC. Earthdawn.SetDefaultAttribute( cID, "NPC", npc ? npc : Earthdawn.charType.pc ); Earthdawn.SetDefaultAttribute( cID, "RollType", (state.Earthdawn.defRolltype & (( npc === Earthdawn.charType.pc ) ? 0x04 : 0x01)) ? "/w gm" : " " ); } catch(err) { Earthdawn.errorLog( "ED.newCharacter setTimeout() error caught: " + err, edc ); } }, 10000); // end delay 10 seconds. } catch(err) { Earthdawn.errorLog( "newCharacter error caught: " + err, edc ); } }; // End newCharacter; As you can see in this test code, I have added logs to try to understand, and I can actually see when creating a new character that the very last log is set API watchdog from newChar 1 But still the value of the API is 2. I also have a manual toggle, just to see what the actual status of API is... When all works correctly, I can change this toggle to 2, and see instantaneously the API triggering and setting it back to 1... BUT when it is a brand new sheet (until I close and reopen it), I can set it to 2 and nothing happens. BUT I can see from the same Set API watchdog from newChar 1 log that not only was the event seen, not only did it try to set the attribute, but even when it reads back the attribute, with attr.get("current") it sees back the 1 it just wrote... But still on the sheet itself, the Attribute is 2, as if the communication between the API (which to my understanding runs on the servers) and the HTML (that to my understanding runs on my computer client) was somehow broken... Ideas ?