Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * FortBattleInterface
  3.  *
  4.  * Can be used for advanced statistics for fort battles' rounds.
  5.  *
  6.  * Example:
  7.  *      FortBattleInterface.RegisterHandler(123);
  8.  *
  9.  * Greasemonkey Example:
  10.  *      var BattleInterfaceScript  = document.createElement('script');
  11.  *      BattleInterfaceScript.type = 'text/javascript';
  12.  *      BattleInterfaceScript.text = 'FortBattleInterface.RegisterHandler(123);';
  13.  *      document.body.appendChild(BattleInterfaceScript);
  14.  */
  15. var FortBattleInterface = {
  16.     /**
  17.     * Stores all registered handlers
  18.     */
  19.     Handlers:           [],
  20.  
  21.     /**
  22.     * Registers new handler
  23.     * @param    int         FortID      ID of the fort
  24.     * @param    function    Func        Function that will be called
  25.     */
  26.     RegisterHandler:    function(FortID, Func){
  27.         // Only one handler per fort battle
  28.         if (FortID in this.Handlers){
  29.             alert('Only one handler per fort battle is allowed.');
  30.             return;
  31.         }
  32.  
  33.         // Add to hadlers list
  34.         this.Handlers[FortID]   = Func;
  35.  
  36.         // Notify flash that we want to get logs of battle in this fort
  37.         var obj = this.GetFlashObject(FortID);
  38.         var OldTxt = obj.GetVariable('jsvar_ObservedBattles');
  39.         var NewTxt = ((OldTxt == '') ? FortID : ',' + FortID)
  40.         obj.SetVariable('jsvar_ObservedBattles',NewTxt);
  41.     },
  42.  
  43.     /**
  44.     * Called by flash, calls correct handler
  45.     * @param    int         FortID      ID of the fort
  46.     * @param    string      Log         JSON Log
  47.     */
  48.     RoundStarted:       function(FortID, Log){
  49.         // Check whether handler was registered
  50.         if (FortID in this.Handlers){
  51.             // Call registered handler
  52.             this.Handlers[FortID](Log);
  53.         }
  54.     },
  55.  
  56.     /**
  57.     * Returns embed object for given name
  58.     * @param    int         FortID      ID of the fort
  59.     */
  60.     GetFlashObject:     function(FortID){
  61.         var BattleName  = 'flash_battle_' + FortID;
  62.         if (window.document[BattleName]){
  63.             return window.document[BattleName];
  64.         }
  65.         if (navigator.appName.indexOf("Microsoft Internet")==-1){
  66.             if (document.embeds && document.embeds[BattleName]) { return document.embeds[BattleName]; }
  67.         }
  68.         else { return document.getElementById(BattleName); }
  69.     }
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement