API.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using Newgrounds.Services;
  5. namespace Newgrounds {
  6. public class API:MonoBehaviour {
  7. // Properties
  8. private static string _apiId;
  9. private static string _encryptionKey;
  10. private static bool _isConnected;
  11. private static GameObjectSurrogate _surrogate;
  12. // Passed user data
  13. private static string _connectionUserName;
  14. private static string _connectionUserId;
  15. private static string _connectionPublisherId;
  16. private static string _connectionUserpageFormat;
  17. private static string _connectionSessionId;
  18. private static string _connectionSaveGroupId;
  19. private static string _connectionSaveFileId;
  20. // ================================================================================================================
  21. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  22. public static void Connect(string __apiId, string __encryptionKey = null, string __version = "") {
  23. _apiId = __apiId;
  24. _encryptionKey = __encryptionKey;
  25. // This is an odd combination: the static class creates a GameObject that has an instance of the class.
  26. // The instance just forwards everything to the static class.
  27. GameObject newGameObject = new GameObject("NewgroundsAPISurrogate-" + (int)(Time.realtimeSinceStartup * 1000f));
  28. _surrogate = newGameObject.AddComponent<GameObjectSurrogate>();
  29. Debug.Log("Attempting to connect to newgrounds API");
  30. if (Application.isEditor) {
  31. // Running in editor: use fake data
  32. // REPLACE THIS WITH THE URL OF YOUR GAME WHEN TESTING! It needs the session id!
  33. setContainerURLStatic("http://uploads.ungrounded.net/alternate/999999/999999_alternate_9999.zip/?NewgroundsAPI_PublisherID=9&NewgroundsAPI_SandboxID=Abc999&NewgroundsAPI_SessionID=Abc999&NewgroundsAPI_UserName=john&NewgroundsAPI_UserID=999999&ng_username=john");
  34. } else {
  35. // Need the container URL first (for user parameters)
  36. Application.ExternalEval(
  37. "document.getElementById('unityPlayer').children[0].SendMessage('" + newGameObject.name + "', 'setContainerURL', document.location.toString());"
  38. );
  39. }
  40. }
  41. internal static void setContainerURLStatic(string __url) {
  42. // Now that the container is known, continue connecting
  43. // Example URL passed: http://uploads.ungrounded.net/alternate/999999/999999_alternate_9999.zip/?NewgroundsAPI_PublisherID=9&NewgroundsAPI_SandboxID=Abc999&NewgroundsAPI_SessionID=Abc999&NewgroundsAPI_UserName=john&NewgroundsAPI_UserID=999999&ng_username=john
  44. Debug.Log("Container URL is " + __url);
  45. // Dirty querystring parsing (no access to C#'s System.Web)
  46. string[] pairs = __url.Substring(__url.IndexOf("?") + 1).Split('&');
  47. string[] pair;
  48. for (int i = 0; i < pairs.Length; i++) {
  49. pair = pairs[i].Split('=');
  50. switch (pair[0]) {
  51. case "NewgroundsAPI_UserName":
  52. _connectionUserName = pair[1];
  53. break;
  54. case "NewgroundsAPI_UserID":
  55. _connectionUserId = pair[1];
  56. break;
  57. case "NewgroundsAPI_PublisherID":
  58. _connectionPublisherId = pair[1];
  59. break;
  60. case "NewgroundsAPI_UserpageFormat":
  61. _connectionUserpageFormat = pair[1];
  62. break;
  63. case "NewgroundsAPI_SessionID":
  64. _connectionSessionId = pair[1];
  65. break;
  66. case "NewgroundsAPI_SaveGroupID":
  67. _connectionSaveGroupId = pair[1];
  68. break;
  69. case "NewgroundsAPI_SaveFileID":
  70. _connectionSaveFileId = pair[1];
  71. break;
  72. }
  73. }
  74. Debug.Log("connectionUserName => " + _connectionUserName); // 'zehfernando'
  75. Debug.Log("connectionUserId => " + _connectionUserId); // int
  76. Debug.Log("connectionPublisherId => " + _connectionPublisherId); // 1
  77. Debug.Log("connectionUserpageFormat => " + _connectionUserpageFormat); // Empty
  78. Debug.Log("connectionSessionId => " + _connectionSessionId); // Long hash
  79. Debug.Log("connectionSaveGroupId => " + _connectionSaveGroupId); // Empty
  80. Debug.Log("connectionSaveFileId => " + _connectionSaveFileId); // Empty
  81. _isConnected = true;
  82. }
  83. public static void UnlockMedal(string __medalName) {
  84. }
  85. public static void PostScore(string __scoreBoardName, int __numericScore, string __tag = null) {
  86. PostScoreService service = new PostScoreService(eResult);
  87. service.setData(__scoreBoardName, __numericScore, __tag);
  88. service.execute();
  89. }
  90. private static void eResult(BasicService __service = null) {
  91. if (__service.resultSuccess) {
  92. Debug.Log("==============> success posting " + __service);
  93. } else {
  94. Debug.Log("==============> error posting: " + __service.resultErrorMessage);
  95. }
  96. }
  97. // ================================================================================================================
  98. // ACCESSOR INTERFACE ---------------------------------------------------------------------------------------------
  99. public static string apiId {
  100. get { return _apiId; }
  101. }
  102. public static string encryptionKey {
  103. get { return _encryptionKey; }
  104. }
  105. public static bool isConnected {
  106. get { return _isConnected; }
  107. }
  108. public static GameObjectSurrogate surrogate {
  109. get { return _surrogate; }
  110. }
  111. public static string connectionUserName {
  112. get { return _connectionUserName; }
  113. }
  114. public static string connectionUserId {
  115. get { return _connectionUserId; }
  116. }
  117. public static string connectionPublisherId {
  118. get { return _connectionPublisherId; }
  119. }
  120. public static string connectionUserpageFormat {
  121. get { return _connectionUserpageFormat; }
  122. }
  123. public static string connectionSessionId {
  124. get { return _connectionSessionId; }
  125. }
  126. public static string connectionSaveGroupId {
  127. get { return _connectionSaveGroupId; }
  128. }
  129. public static string connectionSaveFileId {
  130. get { return _connectionSaveFileId; }
  131. }
  132. /*
  133. Return Object
  134. command_id = getMedals
  135. medals - An array of medal objects (if the game uses them) with the following properties:
  136. medal_id - The numeric id of the medal
  137. medal_name - The name of the medal
  138. medal_value - The point value of the medal
  139. medal_difficulty - 1=easy, 2=moderate, 3=challenging, 4=difficult, 5=brutal
  140. medal_unlocked - true/false (if publisher_id and user_id were passed)
  141. medal_icon - The URL of the icon for this medal
  142. secret - 1 if this is a secret medal
  143. medal_description - The description of this medal
  144. success = 1
  145. {"command_id":"getMedals","medals":
  146. [
  147. {"medal_id":-32827,"medal_name":"Completed Level 1 (Ambush)","medal_value":0,"medal_difficulty":1,"medal_unlocked":false,"medal_icon":"http:\/\/apifiles.ngfiles.com\/medals\/36000\/36957\/32827_completedlevel1ambush.jpg","secret":0,"medal_description":"Completed Level 1 (Ambush)"}
  148. ],"success":1}
  149. UnityEngine.Debug:Log(Object)
  150. Debug.Log(Object) (at Assets/Scripts/Libraries/logging/D.cs:25)
  151. <doPostWithYield>c__Iterator9:MoveNext() (at Assets/Scripts/Libraries/net/newgrounds/NewgroundsAPI.cs:186)
  152. */
  153. }
  154. }