Advertisement
Guest User

GameJoltConnect.as

a guest
Feb 7th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import com.gamejolt.API;
  4.     /**
  5.      * ...
  6.      * @author Faisal
  7.      */
  8.     public class GameJoltConnect
  9.     {
  10.         private static var g_instance:GameJoltConnect;
  11.         public var gameJoltApi:API;
  12.        
  13.         private var userVerified:Boolean = false;
  14.        
  15.         private var _gameId:int = 22261;
  16.        
  17.         private var _privateKey:String = "1343248e9b2ca81c51d6a45574cc5f04";
  18.        
  19.         public var userName:String = "Guest";
  20.         public var userToken:String = "000000";
  21.        
  22.         private var firstTrophyId:int = 6100;
  23.         private var secondTrophyId:int = 6101;
  24.         private var thirdTrophyId:int = 6102;
  25.        
  26.         public function GameJoltConnect()
  27.         {
  28.             trace("Gamejolt API initialized");
  29.             gameJoltApi = new API();
  30.         }
  31.        
  32.         public static function get instance():GameJoltConnect
  33.         {
  34.             if (g_instance == null)
  35.             {
  36.                 g_instance = new GameJoltConnect();
  37.             }
  38.             return g_instance;
  39.         }
  40.        
  41.         public function authUser(_uName:String, _uToken:String):void
  42.         {
  43.             Main.printLog("authenticating user...");
  44.             trace("authenticating user...");
  45.             userName = _uName;
  46.             userToken = _uToken;
  47.             gameJoltApi.authUser(_gameId, _privateKey, _uName, _uToken, onUserAuthCallback);
  48.         }
  49.        
  50.         private function onUserAuthCallback(success:Boolean):void
  51.         {
  52.             if (success)
  53.             {
  54.                 Main.printLog("user verified Success");
  55.                 trace("user verified Success");
  56.                 userVerified = true;
  57.                 Main.userNameLabel.text = "Username:"+userName;
  58.                 Main.tokenLabel.text = "Token:"+userToken;
  59.                 //gameJoltApi.getKeyData(_gameId, _privateKey, "BRONZE", onBronzeDataCallback, userName, userToken);
  60.                 //gameJoltApi.getKeyData(_gameId, _privateKey, "SILVER", onSilverDataCallback, userName, userToken);
  61.             }
  62.             else
  63.             {
  64.                 Main.printLog("user verified Failed. Guest Login");
  65.                 //trace("user authentication Failed");
  66.                 userName = "Guest";
  67.                 Main.userNameLabel.text = "Username:"+userName;
  68.                 Main.tokenLabel.text = "Token:NA";
  69.                 userVerified = false;
  70.             }
  71.         }
  72.        
  73.         //private function onSilverDataCallback(data:String):void
  74.         //{
  75.             //trace("Checking Silver Status");
  76.             //if (data != null)
  77.             //{
  78.                 //trace("Silver Data:" + data);
  79.                 //isSilverAchieved = data;
  80.             //}
  81.         //}
  82.         //
  83.         //private function onBronzeDataCallback(data:String):void
  84.         //{
  85.             //trace("Checking Bronze Status");
  86.             //if (data != null)
  87.             //{
  88.                 //trace("Bronze Data:" + data);
  89.                 //isBronzeAchieved = data;
  90.             //}
  91.         //}
  92.        
  93.         public function setKeyData(key:String, data:String):void
  94.         {
  95.             if (userVerified)
  96.             {
  97.                 Main.printLog("SetData "+data+" with Key: "+key);
  98.                 gameJoltApi.setKeyData(_gameId, _privateKey, key, data, userName, userToken);
  99.             }
  100.             else
  101.                 Main.printLog("Not GameJolt User");
  102.         }
  103.        
  104.         public function getKeyData(key:String):void
  105.         {
  106.             if (userVerified)
  107.             {
  108.                 Main.printLog("getData with Key: "+key);
  109.                 gameJoltApi.getKeyData(_gameId, _privateKey, key, onGetDataCallback, userName, userToken);
  110.             }
  111.             else
  112.                 Main.printLog("Not GameJolt User");
  113.         }
  114.        
  115.         private function onGetDataCallback(data:String):void
  116.         {
  117.             //trace("Checking Silver Status");
  118.             Main.printLog("GetData Callback recieved");
  119.             if (data != null)
  120.             {
  121.                 Main.printLog("Recieved Data:"+data);
  122.             }
  123.         }
  124.        
  125.         public function addHighScore(score:int = 0):void
  126.         {
  127.             if (userVerified)
  128.             {
  129.                 trace("HighScore Send");
  130.                 Main.printLog("HighScore Send");
  131.                 gameJoltApi.setHighscore(_gameId, _privateKey, String(score), Number(score), userName, userToken);
  132.             }
  133.             else
  134.             {
  135.                 trace("HS Set Guest");
  136.                 Main.printLog("HighScore Send - Guest");
  137.                 gameJoltApi.setHighscore(_gameId, _privateKey, String(score), Number(score), userName, userToken, userName);
  138.             }
  139.         }
  140.        
  141.         public function addTrophy(id:int = 0):void
  142.         {
  143.             if (!userVerified)
  144.             {
  145.                 trace("Trophy adding failed. invalid user data");
  146.                 Main.printLog("Trophy adding failed. invalid user data");
  147.                 return;
  148.             }
  149.             if (id == 0)
  150.             {
  151.                 Main.printLog("Trophy added. 20 Clicks.");
  152.                 gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, firstTrophyId);
  153.                 //if (isBronzeAchieved != "Y")
  154.                 //{
  155.                     //trace("Got Bronze Trophy");
  156.                     //isBronzeAchieved = "Y";
  157.                     //gameJoltApi.setKeyData(_gameId, _privateKey, "BRONZE", "Y", userName, userToken);
  158.                     //gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, bronzeTrophyId);
  159.                 //}
  160.                 //else
  161.                 //{
  162.                     //trace("Bronze Trophy achieved already");
  163.                 //}
  164.             }
  165.             else if (id == 1)
  166.             {  
  167.                 Main.printLog("Trophy added. 40 Clicks.");
  168.                 gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, secondTrophyId);
  169.                 //if (isSilverAchieved != "Y")
  170.                 //{
  171.                     //trace("Got Silver Trophy too.Cool");
  172.                     //isSilverAchieved = "Y";
  173.                     //gameJoltApi.setKeyData(_gameId, _privateKey, "SILVER", "Y", userName, userToken);
  174.                     //gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, silverTrophyId);
  175.                 //}
  176.                 //else
  177.                 //{
  178.                     //trace("Silver Trophy achieved already");
  179.                 //}
  180.             }
  181.             else if (id == 2)
  182.             {  
  183.                 Main.printLog("Trophy added. 5 Breakout Points.");
  184.                 gameJoltApi.addTrophyAchieved(_gameId, _privateKey, userName, userToken, thirdTrophyId);
  185.             }
  186.         }
  187.        
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement