UILeaderBoard.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections;
  5. using UnityEngine.SocialPlatforms.GameCenter;
  6. public class UILeaderBoard : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private UIButton Home;
  10. [SerializeField]
  11. private UIAtlas _atlas;
  12. [SerializeField]
  13. private string _spriteName;
  14. [SerializeField]
  15. private GameObject _scrollRoot;
  16. [SerializeField]
  17. private float Offset;
  18. [SerializeField]
  19. private UIFont _font;
  20. private int _maxScroll;
  21. private List<GameObject> _items = new List<GameObject>();
  22. //private List<GPGScore> _scores = new List<GPGScore>();
  23. public List<UserTop> scores = new List<UserTop>();
  24. private WaitForSeconds wait = new WaitForSeconds(0.5f);
  25. //public bool _isScoresCreated = false;
  26. public UIButton Up;
  27. public UIButton Down;
  28. private UIGrid _grid;
  29. public static UILeaderBoard Instance;
  30. #if UNITY_ANDROID
  31. private const string _leaderBoardID = "CgkIl5Kk5v8bEAIQBw";
  32. #endif
  33. // Use this for initialization
  34. void Start ()
  35. {
  36. Instance = this;
  37. Home.onClick.Add(new EventDelegate(OnHome));
  38. #if UNITY_ANDROID
  39. Init();
  40. #endif
  41. #if UNITY_IPHONE
  42. //gameObject.AddComponent<GCLeaderBoard>();
  43. Debug.Log("UILeaderBoard::Started");
  44. #endif
  45. _grid = this.gameObject.GetComponentInChildren<UIGrid>();
  46. }
  47. void Update()
  48. {
  49. }
  50. //
  51. public void Reset()
  52. {
  53. //return;
  54. for (int i = 0; i < _items.Count; i++)
  55. {
  56. Destroy(_items[i]);
  57. }
  58. _items.Clear();
  59. StartLoadScores();
  60. }
  61. public void StartLoadScores()
  62. {
  63. StartCoroutine(LoadScores());
  64. }
  65. void OnEnable()
  66. {
  67. ServerGiftManager.Instance.RequestWeeksUpdate(week =>
  68. {
  69. if (string.IsNullOrEmpty(week))
  70. {
  71. Debug.Log("Error");
  72. //todo: server error, or wrong number filled
  73. }
  74. else
  75. {
  76. Label.text = string.Format(week.ToString());
  77. }
  78. });
  79. }
  80. public IEnumerator LoadScores()
  81. {
  82. if (scores == null)
  83. {
  84. yield break;
  85. }
  86. for (int i = 0; i < scores.Count; i++)
  87. {
  88. if (i % 10 == 0)
  89. {
  90. yield return wait;
  91. }
  92. // ---- CREATE SPRITE
  93. var panel = NGUITools.AddChild<UIPanel>(_scrollRoot);//.AddSprite(_scrollRoot, _atlas, _spriteName);
  94. var go = panel.transform;
  95. Destroy(panel);
  96. //go.MakePixelPerfect();
  97. //go.transform.localPosition = new Vector3(0, -i * (go.height + 5) + Offset, 0);
  98. go.gameObject.AddComponent<UIDragPanelContents>();
  99. //go.gameObject.AddComponent<BoxCollider>();
  100. //go.gameObject.GetComponent<BoxCollider>().size = new Vector3(400, 110, 0);
  101. //go.type = UISprite.Type.Simple;
  102. //go.width = 450;
  103. //go.height = 110;
  104. // ---- ADD USER_PLACE TO SPRITE
  105. var place = NGUITools.AddChild<UILabel>(go.gameObject);
  106. place.name = "Place";
  107. place.pivot = UIWidget.Pivot.Left;
  108. place.font = _font;
  109. place.text = (i + 1).ToString() + ".";
  110. place.depth = 0;
  111. place.transform.localPosition = new Vector3(-180, 10, -1);
  112. place.transform.localScale = new Vector3(0.3f, 0.3f, 1);
  113. // ---- ADD USER_NAME TO SPRITE
  114. var pos = NGUITools.AddChild<UILabel>(go.gameObject);
  115. var pos2 = NGUITools.AddChild<UILabel>(go.gameObject);
  116. pos.name = "FacebookUserName";
  117. pos2.name = "FacebookSecondName";
  118. pos2.pivot =
  119. pos.pivot = UIWidget.Pivot.Left;
  120. pos.font = _font;
  121. pos.text = scores[i].User.FirstName;// +" " + _scores[i].User.LastName;
  122. pos.depth = 0;
  123. pos.transform.localPosition = new Vector3(-140, 10, -1);
  124. pos.transform.localScale = new Vector3(0.3f, 0.3f, 1);
  125. // ---- ADD USER_SCORE TO SPRITE
  126. var lname = NGUITools.AddChild<UILabel>(go.gameObject);
  127. lname.name = "Distance";
  128. lname.font = _font;
  129. lname.text = scores[i].User.Score.ToString() + " point";
  130. lname.depth = 0;
  131. lname.transform.localPosition = new Vector3(80, 10, 0);
  132. lname.transform.localScale = new Vector3(0.3f, 0.3f, 1);
  133. // ---- ADD USER_gifts TO SPRITE
  134. //var gift = NGUITools.AddChild<UILabel>(go.gameObject);
  135. //gift.name = "Gifts";
  136. //gift.font = _font;
  137. //gift.text = _scores[i].User.Gifts.ToString();
  138. //gift.depth = 0;
  139. //gift.transform.localPosition = new Vector3(160, -22, 0);
  140. //gift.transform.localScale = new Vector3(0.6f, 0.6f, 1);
  141. if (i < 3)
  142. {
  143. // ---- ADD SANTA_ICONS_TOP_3 TO SPRITE
  144. string spriteName = "";
  145. //int idBadge = _scores[i].User.SantaBadge;
  146. switch (i)
  147. {
  148. case 0: spriteName = "medal_gold"; break;
  149. case 1: spriteName = "medal_silver"; break;
  150. case 2: spriteName = "medal_bronze"; break;
  151. }
  152. var santa = NGUITools.AddSprite(go.gameObject, _atlas, spriteName);
  153. santa.name = "santaBadge";
  154. santa.transform.localPosition = new Vector3(160, 10, -1);
  155. santa.MakePixelPerfect();
  156. santa.width = 40;
  157. santa.height = 50;
  158. santa.depth = 0;
  159. }
  160. _items.Add(go.gameObject);
  161. _grid.Reposition();
  162. }
  163. //_isScoresCreated = true;
  164. yield break;
  165. }
  166. //
  167. private bool _initDone;
  168. public UILabel Label;
  169. #if UNITY_ANDROID
  170. private void Init()
  171. {
  172. if (_initDone)
  173. {
  174. return;
  175. }
  176. _initDone = true;
  177. /* GPGManager.authenticationSucceededEvent += OnAuthDone;
  178. GPGManager.authenticationFailedEvent += OnAuthFail;
  179. GPGManager.submitScoreFailedEvent += OnSubmitScoreFail;
  180. GPGManager.submitScoreSucceededEvent += OnSubmitScoreDone;
  181. GPGManager.loadScoresFailedEvent += OnGetLeadeboardFail;
  182. GPGManager.loadScoresSucceededEvent += OnGetLeadeboardDone;*/
  183. //FileLog.Init(Application.persistentDataPath,"leaderboard",true,true,true,FileLog.LogLevels.Log);
  184. LoginProcess();
  185. Debug.Log("Init");
  186. }
  187. #endif
  188. //private List<Action> _postAuthActions = new List<Action>();
  189. //private List<Action> _postRequestActions = new List<Action>();
  190. #if UNITY_ANDROID
  191. private bool LoginProcess()
  192. {
  193. // if (!PlayGameServices.isSignedIn())
  194. {
  195. //#if UNITY_IPHONE
  196. // if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", "")))
  197. // {
  198. // PlayGameServices.init(PlayerPrefs.GetString("user_id"), true);
  199. // }
  200. // else
  201. // {
  202. // PlayGameServices.authenticate();
  203. // }
  204. //#else
  205. if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", "")))
  206. {
  207. //PlayGameServices.attemptSilentAuthentication();
  208. }
  209. else
  210. {
  211. // PlayGameServices.authenticate();
  212. Debug.Log("LoginProcess:authenticate");
  213. }
  214. //#endif
  215. return false;
  216. }
  217. return true;
  218. }
  219. private void OnAuthDone(string userId)
  220. {
  221. Debug.Log("OnAuthDone " + userId);
  222. PlayerPrefs.SetString("user_id",userId);
  223. if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard)
  224. {
  225. RequestLeaderBoard();
  226. }
  227. }
  228. private void OnAuthFail(string fail)
  229. {
  230. Debug.LogError("OnAuthFail " + fail);
  231. //PlayGameServices.authenticate();
  232. }
  233. private void OnSubmitScoreDone(string s, Dictionary<string, object> dictionary)
  234. {
  235. /* Debug.Log("OnSubmitScoreDone " + s);
  236. if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard)
  237. {
  238. RequestLeaderBoard();
  239. }*/
  240. }
  241. private void OnSubmitScoreFail(string a, string b)
  242. {
  243. //Debug.Log("OnSubmitScoreFail " + a + " : " + b);
  244. }
  245. private void OnGetLeadeboardDone(/*List<GPGScore> gpgScores*/)
  246. {
  247. /*Debug.Log("OnGetLeadeboardDone " + gpgScores.Count);
  248. _scores = gpgScores;
  249. Reset();*/
  250. }
  251. private void OnGetLeadeboardFail(string a, string b)
  252. {
  253. //Debug.Log("OnGetLeadeboardFail " + a + " : " + b);
  254. }
  255. #endif
  256. private void OnHome()
  257. {
  258. if (NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.Popup ||
  259. NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.GameOver)
  260. {
  261. //NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew);
  262. //NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Banner);
  263. InGameScriptCS.Instance.LoadStartSanta();
  264. return;
  265. }
  266. NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew);
  267. NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Menu);
  268. }
  269. /*
  270. private void OnDrag(Vector2 drag)
  271. {
  272. _scrollRoot.transform.localPosition+= Vector3.up * drag.y;
  273. if (_scrollRoot.transform.localPosition.y < 0)
  274. {
  275. _scrollRoot.transform.localPosition = Vector3.zero;
  276. }
  277. if (_scrollRoot.transform.localPosition.y > _maxScroll)
  278. {
  279. _scrollRoot.transform.localPosition = new Vector3(0, _maxScroll,0);
  280. }
  281. }*/
  282. #if UNITY_ANDROID
  283. public void RequestLeaderBoard()
  284. {
  285. //PlayGameServices.showLeaderboard(_leaderBoardID, GPGLeaderboardTimeScope.AllTime);
  286. }
  287. #endif
  288. public void OnShow()
  289. {
  290. #if UNITY_IPHONE
  291. //GCLeaderBoard.Show();
  292. return;
  293. #endif
  294. #if UNITY_ANDROID
  295. Init();
  296. if (LoginProcess())
  297. {
  298. RequestLeaderBoard();
  299. Debug.Log("OnShow:LoginProcess:true");
  300. return;
  301. }
  302. Debug.Log("OnShow:LoginProcess:false");
  303. #endif
  304. }
  305. public static void SummitScore(long score)
  306. {
  307. #if UNITY_IPHONE
  308. GCLeaderBoard.SummitScore(score);
  309. return;
  310. #endif
  311. #if UNITY_ANDROID
  312. // PlayGameServices.submitScore(_leaderBoardID, score);
  313. #endif
  314. }
  315. public void SetScores(List<UserTop> list)
  316. {
  317. scores = list;
  318. }
  319. public List<GameObject> GetItems()
  320. {
  321. return _items;
  322. }
  323. public void SetItems(List<GameObject> list)
  324. {
  325. _items = list;
  326. }
  327. }