home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Acao / the_beard.swf / scripts / __Packages / NewgroundsAPI.as
Encoding:
Text File  |  2008-09-23  |  14.7 KB  |  365 lines

  1. class NewgroundsAPI
  2. {
  3.    var firstChild;
  4.    static var tracker_id;
  5.    static var host;
  6.    static var version;
  7.    static var debug;
  8.    static var error_format;
  9.    static var header_format;
  10.    static var normal_format;
  11.    static var link_format;
  12.    static var movie_options = new Object();
  13.    static var custom_events = new Object();
  14.    static var custom_links = new Object();
  15.    static var MOVIE_VIEWS = 1;
  16.    static var AUTHOR_SITE = 2;
  17.    static var NEWGROUNDS = 3;
  18.    static var NEW_VERSION = 4;
  19.    static var CUSTOM_STATS = 50;
  20.    static var GATEWAY_URL = "http://www.ngads.com/gateway.php";
  21.    function NewgroundsAPI()
  22.    {
  23.    }
  24.    static function connectMovie(id)
  25.    {
  26.       if(!id)
  27.       {
  28.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.connectMovie(id:Number)");
  29.       }
  30.       else if(!NewgroundsAPI.tracker_id)
  31.       {
  32.          NewgroundsAPI.SendMessage("Connecting to API gateway...");
  33.          NewgroundsAPI.tracker_id = id;
  34.          NewgroundsAPI.host = _url.split("/")[2].toLowerCase();
  35.          if(NewgroundsAPI.host.length < 1)
  36.          {
  37.             NewgroundsAPI.host = "localhost";
  38.          }
  39.          var _loc2_ = new Object();
  40.          NewgroundsAPI.SendEvent(NewgroundsAPI.MOVIE_VIEWS);
  41.       }
  42.    }
  43.    static function setMovieVersion(movie_version)
  44.    {
  45.       if(!movie_version)
  46.       {
  47.          NewgroundsAPI.SendError("Missing required \'version\' in NewgroundsAPI.setMovieVersion(version:String)");
  48.       }
  49.       else
  50.       {
  51.          NewgroundsAPI.version = movie_version;
  52.       }
  53.    }
  54.    static function debugMode()
  55.    {
  56.       NewgroundsAPI.debug = true;
  57.    }
  58.    static function addCustomEvent(stat_id, stat_name)
  59.    {
  60.       if(!stat_id)
  61.       {
  62.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.AddCustomEvent(id:Number, event_name:String)");
  63.       }
  64.       else if(!stat_name)
  65.       {
  66.          NewgroundsAPI.SendError("Missing required \'event_name\' parameter in NewgroundsAPI.AddCustomEvent(id:Number, event_name:String)");
  67.       }
  68.       else
  69.       {
  70.          NewgroundsAPI.custom_events[stat_name] = NewgroundsAPI.CUSTOM_STATS + stat_id;
  71.          NewgroundsAPI.SendMessage("Created custom event: " + stat_name);
  72.       }
  73.    }
  74.    static function addCustomLink(stat_id, stat_name)
  75.    {
  76.       if(!stat_id)
  77.       {
  78.          NewgroundsAPI.SendError("Missing required \'id\' parameter in NewgroundsAPI.AddCustomLink(id:Number, link_name:String)");
  79.       }
  80.       else if(!stat_name)
  81.       {
  82.          NewgroundsAPI.SendError("Missing required \'link_name\' parameter in NewgroundsAPI.AddCustomLink(id:Number, link_name:String)");
  83.       }
  84.       else
  85.       {
  86.          NewgroundsAPI.custom_links[stat_name] = NewgroundsAPI.CUSTOM_STATS + stat_id;
  87.          NewgroundsAPI.SendMessage("Created custom link " + stat_id + ": " + stat_name);
  88.       }
  89.    }
  90.    static function loadMySite()
  91.    {
  92.       NewgroundsAPI.SendLink(NewgroundsAPI.AUTHOR_SITE);
  93.    }
  94.    static function loadNewgrounds()
  95.    {
  96.       NewgroundsAPI.SendLink(NewgroundsAPI.NEWGROUNDS);
  97.    }
  98.    static function logCustomEvent(event_name)
  99.    {
  100.       if(!event_name)
  101.       {
  102.          NewgroundsAPI.SendError("Missing required \'event_name\' parameter in NewgroundsAPI.loadCustomLink(event_name:String)");
  103.       }
  104.       else if(!NewgroundsAPI.custom_links[event_name])
  105.       {
  106.          NewgroundsAPI.SendError("Attempted to open undefined custom link: " + event_name);
  107.       }
  108.       else
  109.       {
  110.          NewgroundsAPI.SendEvent(NewgroundsAPI.custom_links[event_name]);
  111.       }
  112.    }
  113.    static function loadCustomLink(link_name)
  114.    {
  115.       if(!link_name)
  116.       {
  117.          NewgroundsAPI.SendError("Missing required \'link_name\' parameter in NewgroundsAPI.loadCustomLink(link_name:String)");
  118.       }
  119.       else if(!NewgroundsAPI.custom_links[link_name])
  120.       {
  121.          NewgroundsAPI.SendError("Attempted to open undefined custom link: " + link_name);
  122.       }
  123.       else
  124.       {
  125.          NewgroundsAPI.SendLink(NewgroundsAPI.custom_links[link_name]);
  126.       }
  127.    }
  128.    static function getAdURL()
  129.    {
  130.       return NewgroundsAPI.movie_options.ad_url;
  131.    }
  132.    static function getMovieURL()
  133.    {
  134.       if(NewgroundsAPI.movie_options.movie_url)
  135.       {
  136.          return NewgroundsAPI.movie_options.movie_url;
  137.       }
  138.       return "Newgrounds.com";
  139.    }
  140.    static function getNewVersionURL()
  141.    {
  142.       return NewgroundsAPI.GATEWAY_URL + "?&id=" + NewgroundsAPI.tracker_id + "&host=" + escape(NewgroundsAPI.host) + "&stat=" + NewgroundsAPI.NEW_VERSION;
  143.    }
  144.    static function SendEvent(id)
  145.    {
  146.       NewgroundsAPI.SendStat(id,false);
  147.    }
  148.    static function SendLink(id)
  149.    {
  150.       NewgroundsAPI.SendStat(id,true);
  151.    }
  152.    static function ReadGatewayData(params)
  153.    {
  154.       for(var _loc2_ in params)
  155.       {
  156.          NewgroundsAPI.movie_options[_loc2_] = unescape(params[_loc2_]);
  157.       }
  158.       if(NewgroundsAPI.movie_options.settings_loaded)
  159.       {
  160.          NewgroundsAPI.SendMessage("Connection to the API gateway was successful!");
  161.          if(NewgroundsAPI.movie_options.message)
  162.          {
  163.             NewgroundsAPI.SendMessage(NewgroundsAPI.movie_options.message);
  164.          }
  165.          if(NewgroundsAPI.movie_options.ad_url)
  166.          {
  167.             NewgroundsAPI.SendMessage("Your movie has been approved to run Flash Ads");
  168.             NewgroundsAPI.onAdsApproved(NewgroundsAPI.movie_options.ad_url);
  169.          }
  170.          if(NewgroundsAPI.movie_options.movie_version.toString() != NewgroundsAPI.version.toString())
  171.          {
  172.             NewgroundsAPI.SendMessage("WARNING: The movie version configured in your API settings does not match this movie\'s version!");
  173.             NewgroundsAPI.onNewVersionAvailable(NewgroundsAPI.movie_options.movie_version,NewgroundsAPI.getMovieURL(),NewgroundsAPI.getNewVersionURL());
  174.          }
  175.          if(NewgroundsAPI.movie_options.deny_host)
  176.          {
  177.             NewgroundsAPI.SendMessage("You have blocked \'localHost\' in your API settings.");
  178.             NewgroundsAPI.SendMessage("If you wish to test your movie you will need to remove this block.");
  179.             NewgroundsAPI.onDenyHost(NewgroundsAPI.host,NewgroundsAPI.getMovieURL(),NewgroundsAPI.getNewVersionURL());
  180.          }
  181.       }
  182.       else
  183.       {
  184.          NewgroundsAPI.SendError("Could not establish connection to the API gateway.");
  185.       }
  186.    }
  187.    static function SendStat(stat_id, open_in_browser)
  188.    {
  189.       if(!NewgroundsAPI.tracker_id)
  190.       {
  191.          NewgroundsAPI.SendError("API calls cannot be made without a valid movie id");
  192.       }
  193.       else
  194.       {
  195.          var _loc7_ = NewgroundsAPI.GATEWAY_URL + "?&id=" + NewgroundsAPI.tracker_id + "&host=" + escape(NewgroundsAPI.host) + "&stat=" + stat_id;
  196.          trace(_loc7_);
  197.          if(NewgroundsAPI.debug)
  198.          {
  199.             _loc7_ += "&debug=1";
  200.          }
  201.          if(open_in_browser)
  202.          {
  203.             getURL(_loc7_,"_blank");
  204.          }
  205.          else
  206.          {
  207.             var _loc8_ = new XML();
  208.             _loc8_.ignoreWhite = true;
  209.             _loc8_.onLoad = function(success)
  210.             {
  211.                var _loc6_ = new Object();
  212.                var _loc3_ = 0;
  213.                while(_loc3_ < this.firstChild.childNodes.length)
  214.                {
  215.                   var _loc4_ = this.firstChild.childNodes[_loc3_];
  216.                   var _loc5_ = _loc4_.nodeName;
  217.                   var _loc2_ = _loc4_.attributes.value;
  218.                   if(_loc2_ == Number(_loc2_))
  219.                   {
  220.                      _loc2_ = Number(_loc2_);
  221.                   }
  222.                   _loc6_[_loc5_] = _loc2_;
  223.                   _loc3_ = _loc3_ + 1;
  224.                }
  225.                NewgroundsAPI.ReadGatewayData(_loc6_);
  226.             };
  227.             _loc8_.load(_loc7_);
  228.          }
  229.       }
  230.    }
  231.    static function SendError(msg)
  232.    {
  233.       trace("[NEWGROUNDS API ERROR] :: " + msg);
  234.    }
  235.    static function SendMessage(msg)
  236.    {
  237.       trace("[NEWGROUNDS API] :: " + msg);
  238.    }
  239.    static function InitTextFormats()
  240.    {
  241.       if(!NewgroundsAPI.error_format)
  242.       {
  243.          NewgroundsAPI.error_format = new TextFormat();
  244.          NewgroundsAPI.error_format.font = "Arial Black";
  245.          NewgroundsAPI.error_format.size = 48;
  246.          NewgroundsAPI.error_format.color = 16711680;
  247.       }
  248.       if(!NewgroundsAPI.header_format)
  249.       {
  250.          NewgroundsAPI.header_format = new TextFormat();
  251.          NewgroundsAPI.header_format.font = "Arial Black";
  252.          NewgroundsAPI.header_format.size = 24;
  253.          NewgroundsAPI.header_format.color = 16777215;
  254.       }
  255.       if(!NewgroundsAPI.normal_format)
  256.       {
  257.          NewgroundsAPI.normal_format = new TextFormat();
  258.          NewgroundsAPI.normal_format.font = "Arial";
  259.          NewgroundsAPI.normal_format.bold = true;
  260.          NewgroundsAPI.normal_format.size = 12;
  261.          NewgroundsAPI.normal_format.color = 16777215;
  262.       }
  263.       if(!NewgroundsAPI.link_format)
  264.       {
  265.          NewgroundsAPI.link_format = new TextFormat();
  266.          NewgroundsAPI.link_format.color = 16776960;
  267.          NewgroundsAPI.link_format.underline = true;
  268.       }
  269.    }
  270.    static function onNewVersionAvailable(version, movie_url, redirect_url)
  271.    {
  272.       NewgroundsAPI.InitTextFormats();
  273.       var _loc2_ = new Object();
  274.       _loc2_.x = Stage.width / 2;
  275.       _loc2_.y = Stage.height / 2;
  276.       _root.createEmptyMovieClip("NGAPI_new_version_overlay",_root.getNextHighestDepth());
  277.       _root.NGAPI_new_version_overlay.lineStyle(1,0,100);
  278.       _root.NGAPI_new_version_overlay.beginFill(0,70);
  279.       _root.NGAPI_new_version_overlay.moveTo(-10,-10);
  280.       _root.NGAPI_new_version_overlay.lineTo(-10,1000);
  281.       _root.NGAPI_new_version_overlay.lineTo(1000,1000);
  282.       _root.NGAPI_new_version_overlay.lineTo(1000,-10);
  283.       _root.NGAPI_new_version_overlay.lineTo(-10,-10);
  284.       _root.NGAPI_new_version_overlay.endFill();
  285.       _root.NGAPI_new_version_overlay.lineStyle(10,0,100);
  286.       _root.NGAPI_new_version_overlay.beginFill(51);
  287.       _root.NGAPI_new_version_overlay.moveTo(_loc2_.x - 240,_loc2_.y - 120);
  288.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x + 240,_loc2_.y - 120);
  289.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x + 240,_loc2_.y + 80);
  290.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x - 240,_loc2_.y + 80);
  291.       _root.NGAPI_new_version_overlay.lineTo(_loc2_.x - 240,_loc2_.y - 120);
  292.       _root.NGAPI_new_version_overlay.endFill();
  293.       _root.NGAPI_new_version_overlay.createEmptyMovieClip("exit",1000);
  294.       _root.NGAPI_new_version_overlay.exit.lineStyle(2,39423,100);
  295.       _root.NGAPI_new_version_overlay.exit.beginFill(0,50);
  296.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 210,_loc2_.y - 110);
  297.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 230,_loc2_.y - 110);
  298.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 230,_loc2_.y - 90);
  299.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 210,_loc2_.y - 90);
  300.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 210,_loc2_.y - 110);
  301.       _root.NGAPI_new_version_overlay.exit.endFill();
  302.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 214,_loc2_.y - 106);
  303.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 226,_loc2_.y - 94);
  304.       _root.NGAPI_new_version_overlay.exit.moveTo(_loc2_.x + 226,_loc2_.y - 106);
  305.       _root.NGAPI_new_version_overlay.exit.lineTo(_loc2_.x + 214,_loc2_.y - 94);
  306.       _root.NGAPI_new_version_overlay.exit.onMouseUp = function()
  307.       {
  308.          if(_root.NGAPI_new_version_overlay.exit.hitTest(_root._xmouse,_root._ymouse))
  309.          {
  310.             _root.NGAPI_new_version_overlay.removeMovieClip();
  311.          }
  312.       };
  313.       var _loc3_ = "Version " + version + " is now available at:" + "\n";
  314.       var _loc5_ = _loc3_.length;
  315.       _loc3_ += movie_url;
  316.       var _loc4_ = _loc3_.length;
  317.       _root.NGAPI_new_version_overlay.createTextField("mouseblocker",99,-10,-10,1000,1000);
  318.       _root.NGAPI_new_version_overlay.createTextField("newversion",100,_loc2_.x - 210,_loc2_.y - 90,400,80);
  319.       _root.NGAPI_new_version_overlay.newversion.text = "New Version Available!";
  320.       _root.NGAPI_new_version_overlay.newversion.setTextFormat(NewgroundsAPI.header_format);
  321.       _root.NGAPI_new_version_overlay.createTextField("message",101,(Stage.width - 400) / 2,Stage.height / 2,400,40);
  322.       _root.NGAPI_new_version_overlay.message.text = _loc3_;
  323.       _root.NGAPI_new_version_overlay.message.multiline = true;
  324.       _root.NGAPI_new_version_overlay.message.wordWrap = true;
  325.       _root.NGAPI_new_version_overlay.message.html = true;
  326.       _root.NGAPI_new_version_overlay.message.setTextFormat(NewgroundsAPI.normal_format);
  327.       NewgroundsAPI.link_format.url = redirect_url;
  328.       _root.NGAPI_new_version_overlay.message.setTextFormat(_loc5_,_loc4_,NewgroundsAPI.link_format);
  329.    }
  330.    static function onDenyHost(hostname, movie_url, redirect_url)
  331.    {
  332.       NewgroundsAPI.InitTextFormats();
  333.       _root.createEmptyMovieClip("NGAPI_deny_host_overlay",_root.getNextHighestDepth());
  334.       _root.NGAPI_deny_host_overlay.lineStyle(20,0,100);
  335.       _root.NGAPI_deny_host_overlay.beginFill(6684672);
  336.       _root.NGAPI_deny_host_overlay.moveTo(0,0);
  337.       _root.NGAPI_deny_host_overlay.lineTo(Stage.width,0);
  338.       _root.NGAPI_deny_host_overlay.lineTo(Stage.width,Stage.height);
  339.       _root.NGAPI_deny_host_overlay.lineTo(0,Stage.height);
  340.       _root.NGAPI_deny_host_overlay.lineTo(0,0);
  341.       _root.NGAPI_deny_host_overlay.endFill();
  342.       var _loc2_ = "This movie has not been approved for use on " + hostname + ".";
  343.       _loc2_ += "\r\rFor an aproved copy, please visit:\r";
  344.       var _loc4_ = _loc2_.length;
  345.       _loc2_ += movie_url;
  346.       var _loc3_ = _loc2_.length;
  347.       _root.NGAPI_deny_host_overlay.createTextField("mousekill",100,0,0,Stage.width,Stage.height);
  348.       _root.NGAPI_deny_host_overlay.createTextField("error",101,(Stage.width - 400) / 2,Stage.height / 2 - 100,400,200);
  349.       _root.NGAPI_deny_host_overlay.error.text = "ERROR!";
  350.       _root.NGAPI_deny_host_overlay.error.setTextFormat(NewgroundsAPI.error_format);
  351.       _root.NGAPI_deny_host_overlay.createTextField("message",102,(Stage.width - 400) / 2,Stage.height / 2,400,200);
  352.       _root.NGAPI_deny_host_overlay.message.text = _loc2_;
  353.       _root.NGAPI_deny_host_overlay.message.multiline = true;
  354.       _root.NGAPI_deny_host_overlay.message.wordWrap = true;
  355.       _root.NGAPI_deny_host_overlay.message.html = true;
  356.       _root.NGAPI_deny_host_overlay.message.setTextFormat(NewgroundsAPI.normal_format);
  357.       NewgroundsAPI.link_format.url = redirect_url;
  358.       _root.NGAPI_deny_host_overlay.message.setTextFormat(_loc4_,_loc3_,NewgroundsAPI.link_format);
  359.    }
  360.    static function onAdsApproved(ad_url)
  361.    {
  362.       trace("load ad movielip from " + NewgroundsAPI.getAdURL());
  363.    }
  364. }
  365.