PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Assets/_API/Classes/ConnectionManager.cs

https://bitbucket.org/jamvillones/rnc
C# | 1156 lines | 0 code | 206 blank | 950 comment | 0 complexity | 651d7bb976154d6214e38c6d8dad3bdf MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0
  1. //using FullInspector;
  2. //using FullSerializer;
  3. //using Newtonsoft.Json;
  4. //using Newtonsoft.Json.Linq;
  5. //using System;
  6. //using System.Linq;
  7. //using UnityEngine;
  8. //using UnityEssentials;
  9. //using UnityEssentials.ExtensionMethods;
  10. //using System.Collections.Generic;
  11. //public class ConnectionManager : FSSingleton<ConnectionManager>
  12. //{
  13. // #region Fields
  14. // [InspectorHeader("Game Settings")]
  15. // [fsProperty] private User _user;
  16. // [fsProperty] private string _matchId;
  17. // [InspectorHeader("API Settings")]
  18. // [fsProperty] private ISabongApiHandler _apiHandler;
  19. // [fsProperty] private string _apiUrl = "http://api.legendsofthepit.com/api/v1";
  20. // [InspectorHeader("Socket.IO Settings")]
  21. // [fsProperty] private BaseSocketIoClient _socketHandler;
  22. // [fsProperty] private string _socketUrl = "ws://128.199.215.246:3000";
  23. // [fsProperty] private bool _socketConnected = false;
  24. // [fsProperty] private bool _socketInMatch = false;
  25. // [fsProperty] private string _socketInMatchId = string.Empty;
  26. // public MockSocketDataObject SocketDataObject;
  27. // private SocketMatch _socketMatch = null;
  28. // private bool _initialized = false;
  29. // private Action<bool> _socket_On_JoinGameFinish;
  30. // private Action<bool> _socket_On_JoinMatchFinish;
  31. // private int _socket_blueBet = 0;
  32. // private int _socket_redBet = 0;
  33. // #endregion
  34. // int tempBet;
  35. // #region Properties
  36. // public bool Initialized
  37. // {
  38. // get { return _initialized; }
  39. // }
  40. // public User User
  41. // {
  42. // get { return _user; }
  43. // }
  44. // public bool SocketConnected
  45. // {
  46. // get { return _socketConnected; }
  47. // }
  48. // public bool SocketInMatch
  49. // {
  50. // get { return _socketInMatch; }
  51. // }
  52. // #endregion
  53. // #region Methods
  54. // private void Initialize ()
  55. // {
  56. // if (_initialized)
  57. // return;
  58. // // Temporary
  59. // //RandomizeUser();
  60. // //MenuController.Instance.Datas.CheckPlayerPrefs();
  61. // _matchId = UERandom.Range(1, 1000001).ToString();
  62. // // END
  63. // // Initialize API Handler.
  64. // if (_apiHandler != null)
  65. // {
  66. // if (_apiHandler is MarkSabongApiHandler)
  67. // {
  68. // MarkSabongApiHandler msah = (_apiHandler as MarkSabongApiHandler);
  69. // msah.BaseUrl = _apiUrl;
  70. // msah.UserId = _user.UserId;
  71. // msah.AccessToken = "helloworld";
  72. // msah.MonoBehaviour = this;
  73. // }
  74. // //else if (_apiHandler is MockSabongApiHandler)
  75. // //{
  76. // // MockSabongApiHandler msah = (_apiHandler as MockSabongApiHandler);
  77. // // msah.MockApiDataObject = Resources.Load("MockApiDataObject") as MockApiDataObject;
  78. // //}
  79. // }
  80. // else
  81. // {
  82. // Debug.Log("Api Handler is null. Setting value to MarkSabongApiHandler.");
  83. // MarkSabongApiHandler msah = new MarkSabongApiHandler();
  84. // msah.BaseUrl = _apiUrl;
  85. // msah.UserId = _user.UserId;
  86. // msah.AccessToken = "helloworld";
  87. // msah.MonoBehaviour = this;
  88. // _apiHandler = msah;
  89. // }
  90. // // Initialize Socket Handler.
  91. // if (_socketHandler != null &&
  92. // !_socketUrl.IsNullOrEmpty())
  93. // {
  94. // _socketHandler.BaseUrl = _socketUrl;
  95. // _socketHandler.On_Connect = Socket_SubscribeSabongEvents;
  96. // _socketHandler.On_Disconnect = Socket_UnsubscribeSabongEvents;
  97. // _socketHandler.Connect();
  98. // }
  99. // else
  100. // {
  101. // Debug.Log("Socket Handler is null. Setting value to QuobjectSocketIoClient.");
  102. // _socketHandler = new QuobjectSocketIoClient (_socketUrl)
  103. // {
  104. // On_Connect = Socket_SubscribeSabongEvents,
  105. // On_Disconnect = Socket_UnsubscribeSabongEvents
  106. // };
  107. // _socketHandler.Connect();
  108. // }
  109. // _initialized = true;
  110. // }
  111. // // Temporary
  112. // public void RandomizeUser ()
  113. // {
  114. // _user.UserId = UERandom.Range(1, 1000001).ToString();
  115. // _user.FirstName = UERandom.Choice("Andrew", "Bobson", "Charlie", "Dean", "Eric",
  116. // "Alice", "Betty", "Christine", "Dianne", "Elaine");
  117. // _user.LastName = UERandom.Choice("Atlas", "Butcher", "Cook", "Diner", "Enger",
  118. // "Filibuster", "Garfield", "Holland", "Il", "Jackson");
  119. // _user.Money = 3000;
  120. // }
  121. // #endregion
  122. // #region ISabongApiHandler Methods
  123. // public void GetAllArenas (Action<bool, Arena[]> callback)
  124. // {
  125. // if (_apiHandler == null)
  126. // {
  127. // Debug.LogError("Api not handled.");
  128. // return;
  129. // }
  130. // _apiHandler.GetAllArenas(callback);
  131. // }
  132. // public void GetAllDerbies (Action<bool, Derby[]> callback)
  133. // {
  134. // if (_apiHandler == null)
  135. // {
  136. // Debug.LogError("Api not handled.");
  137. // return;
  138. // }
  139. // _apiHandler.GetAllDerbies(callback);
  140. // }
  141. // public void GetArena (string arenaId, Action<bool, Arena> callback)
  142. // {
  143. // if (_apiHandler == null)
  144. // {
  145. // Debug.LogError("Api not handled.");
  146. // return;
  147. // }
  148. // _apiHandler.GetArena(arenaId, callback);
  149. // }
  150. // public void GetDerby (string derbyId, Action<bool, Derby> callback)
  151. // {
  152. // if (_apiHandler == null)
  153. // {
  154. // Debug.LogError("Api not handled.");
  155. // return;
  156. // }
  157. // _apiHandler.GetDerby(derbyId, callback);
  158. // }
  159. // public void GetFight (string fightId, Action<bool, GCFight> callback)
  160. // {
  161. // if (_apiHandler == null)
  162. // {
  163. // Debug.LogError("Api not handled.");
  164. // return;
  165. // }
  166. // _apiHandler.GetFight(fightId, callback);
  167. // }
  168. // public void GetFightResult (string fightId, Action<bool, GCFightData> callback)
  169. // {
  170. // if (_apiHandler == null)
  171. // {
  172. // Debug.LogError("Api not handled.");
  173. // return;
  174. // }
  175. // _apiHandler.GetFightResult(fightId, callback);
  176. // }
  177. // public void GetActiveDerby (string arenaId, Action<bool, Derby> callback)
  178. // {
  179. // if (_apiHandler == null)
  180. // {
  181. // Debug.LogError("Api not handled.");
  182. // return;
  183. // }
  184. // _apiHandler.GetActiveDerby(arenaId, callback);
  185. // }
  186. // public void GetDerbyFights (string derbyId, Action<bool, GCFight[]> callback)
  187. // {
  188. // if (_apiHandler == null)
  189. // {
  190. // Debug.LogError("Api not handled.");
  191. // return;
  192. // }
  193. // _apiHandler.GetDerbyFights(derbyId, callback);
  194. // }
  195. // public void PostFightBet (string fightId, int gamecock1Bet, int gamecock2Bet, Action<bool> callback)
  196. // {
  197. // if (_apiHandler == null)
  198. // {
  199. // Debug.LogError("Api not handled.");
  200. // return;
  201. // }
  202. // _apiHandler.PostFightBet(fightId, gamecock1Bet, gamecock2Bet, callback);
  203. // }
  204. // #endregion
  205. // #region BaseSocketIoClient Subscription Methods
  206. // private void Socket_SubscribeSabongEvents (object data)
  207. // {
  208. // if (_socketHandler != null)
  209. // {
  210. // _socketHandler.On("join_game", Socket_On_JoinGame);
  211. // _socketHandler.On("authSuccess", Socket_On_AuthSuccess);
  212. // _socketHandler.On("authFailed", Socket_On_AuthFailed);
  213. // _socketHandler.On("join_match", Socket_On_JoinMatch);
  214. // _socketHandler.On("joinMatchSuccess", Socket_On_JoinMatchSuccess);
  215. // _socketHandler.On("alreadyInMatch", Socket_On_AlreadyInMatch);
  216. // _socketHandler.On("new_bet", Socket_On_NewBet);
  217. // Socket_Emit_JoinGame(_user.UserId, (b) => _socketConnected = b);
  218. // }
  219. // }
  220. // private void Socket_UnsubscribeSabongEvents (object data)
  221. // {
  222. // if (_socketHandler != null)
  223. // {
  224. // _socketHandler.Off("join_game");
  225. // _socketHandler.Off("authSuccess");
  226. // _socketHandler.Off("authFailed");
  227. // _socketHandler.Off("join_match");
  228. // _socketHandler.Off("joinMatchSuccess");
  229. // _socketHandler.Off("alreadyInMatch");
  230. // _socketHandler.Off("new_bet");
  231. // }
  232. // }
  233. // private void Socket_On_JoinGame ()
  234. // {
  235. // Debug.Log("join_game");
  236. // }
  237. // private void Socket_On_AuthSuccess ()
  238. // {
  239. // Debug.Log("authSuccess");
  240. // if (_socket_On_JoinGameFinish != null)
  241. // {
  242. // _socket_On_JoinGameFinish(true);
  243. // _socket_On_JoinGameFinish = null;
  244. // }
  245. // }
  246. // private void Socket_On_AuthFailed ()
  247. // {
  248. // Debug.Log("authFailed");
  249. // if (_socket_On_JoinGameFinish != null)
  250. // {
  251. // _socket_On_JoinGameFinish(false);
  252. // _socket_On_JoinGameFinish = null;
  253. // }
  254. // }
  255. // private void Socket_On_JoinMatch ()
  256. // {
  257. // Debug.Log("join_match");
  258. // }
  259. // private void Socket_On_JoinMatchSuccess ()
  260. // {
  261. // Debug.Log("joinMatchSuccess");
  262. // _socketInMatch = true;
  263. // // TODO
  264. // MockSocket_AddMatch(_user.UserId, _socketMatch);
  265. // if (_socket_On_JoinMatchFinish != null)
  266. // {
  267. // _socket_On_JoinMatchFinish(true);
  268. // _socket_On_JoinMatchFinish = null;
  269. // }
  270. // }
  271. // private void Socket_On_AlreadyInMatch ()
  272. // {
  273. // Debug.Log("alreadyInMatch");
  274. // _socketInMatch = true;
  275. // if (_socket_On_JoinMatchFinish != null)
  276. // {
  277. // _socket_On_JoinMatchFinish(true);
  278. // _socket_On_JoinMatchFinish = null;
  279. // }
  280. // }
  281. // private void Socket_On_NewBet (object data)
  282. // {
  283. // SocketBet bet = JsonConvert.DeserializeObject<SocketBet>(data.ToString());
  284. // // TODO
  285. // MockSocket_UpdateBet(_socketInMatchId, bet.username, bet.betAmount, bet.cockId);
  286. // Debug.LogFormat("'{0}' betted ${1} on Cock #{2}.",
  287. // bet.username,
  288. // bet.betAmount,
  289. // bet.cockId);
  290. // }
  291. // public bool Socket_Emit_JoinGame (string username, Action<bool> callback = null)
  292. // {
  293. // if (_socketHandler == null)
  294. // return false;
  295. // bool result = _socketHandler.Emit("join_game",
  296. // new JObject(
  297. // new JProperty("username", username)));
  298. // if (!result)
  299. // {
  300. // if (callback != null)
  301. // callback(false);
  302. // return false;
  303. // }
  304. // if (callback != null)
  305. // _socket_On_JoinGameFinish = callback;
  306. // return true;
  307. // }
  308. // public bool Socket_Emit_JoinMatch (string eventMatchId, string eventId, string blueId, string redId, string startTime, string endTime, string duration, Action<bool> callback = null)
  309. // {
  310. // if (_socketHandler == null)
  311. // return false;
  312. // bool result = _socketHandler.Emit("join_match",
  313. // new JObject(
  314. // new JProperty("EventMatchID", eventMatchId),
  315. // new JProperty("EventID", eventId),
  316. // new JProperty("StartTime", startTime),
  317. // new JProperty("EndTime", endTime),
  318. // new JProperty("Duration", duration)));
  319. // if (!result)
  320. // {
  321. // if (callback != null)
  322. // callback(false);
  323. // return false;
  324. // }
  325. // _socketInMatchId = eventMatchId;
  326. // _socket_blueBet = UERandom.Range(0, 100001);
  327. // _socket_redBet = _socket_blueBet + UERandom.Range(-5000, 5001);
  328. // _socketMatch = new SocketMatch()
  329. // {
  330. // EventMatchID = eventMatchId,
  331. // EventID = eventId,
  332. // BlueID = blueId,
  333. // RedID = redId,
  334. // StartTime = startTime,
  335. // EndTime = endTime,
  336. // Duration = duration
  337. // };
  338. // if (callback != null)
  339. // _socket_On_JoinMatchFinish = callback;
  340. // return true;
  341. // }
  342. // public bool Socket_Emit_LeaveMatch (string eventMatchId)
  343. // {
  344. // _socketInMatch = false;
  345. // _socketInMatchId = string.Empty;
  346. // Debug.LogFormat("'{0}' left the match #{1}.",
  347. // _user.FullName,
  348. // _socketInMatchId);
  349. // return true;
  350. // }
  351. // public bool Socket_Emit_NewBet (string eventMatchId, string amount, string cockId)
  352. // {
  353. // if (_socketHandler == null)
  354. // return false;
  355. // // TODO
  356. // if (SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId) != null)
  357. // {
  358. // if (cockId == SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BlueID)
  359. // tempBet = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BettingUsers.First(x => x.user.UserId == User.UserId).blueBet;
  360. // else if (cockId == SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).RedID)
  361. // tempBet = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BettingUsers.First(x => x.user.UserId == User.UserId).redBet;
  362. // }
  363. // _user.Money = _user.Money - Mathf.Abs(tempBet - int.Parse(amount));
  364. // //Debug.Log("User Money: " + _user.Money);
  365. // MockSocket_UpdateBet(eventMatchId, _user.UserId, amount, cockId);
  366. // Debug.LogFormat("'{0}' betted ${1} on Cock #{2}.",
  367. // "You",
  368. // amount,
  369. // cockId);
  370. // return _socketHandler.Emit("new_bet",
  371. // new JObject(
  372. // new JProperty("EventMatchID", eventMatchId),
  373. // new JProperty("amount", amount),
  374. // new JProperty("cockId", cockId)));
  375. // }
  376. // public void MockSocket_AddMatch (string userId, SocketMatch newSocketMatch)
  377. // {
  378. // if (SocketDataObject != null)
  379. // {
  380. // if (SocketDataObject.Matches == null)
  381. // SocketDataObject.Matches = new List<SocketMatch>();
  382. // if (SocketDataObject.Matches.Count == 0 ||
  383. // !SocketDataObject.Matches.Any(x => x.EventMatchID == newSocketMatch.EventMatchID))
  384. // {
  385. // SocketDataObject.Matches.Add(new SocketMatch()
  386. // {
  387. // EventMatchID = newSocketMatch.EventMatchID,
  388. // EventID = newSocketMatch.EventID,
  389. // BlueID = newSocketMatch.BlueID,
  390. // RedID = newSocketMatch.RedID,
  391. // StartTime = newSocketMatch.StartTime,
  392. // EndTime = newSocketMatch.EndTime,
  393. // Duration = newSocketMatch.Duration
  394. // });
  395. // }
  396. // SocketMatch socketMatch = SocketDataObject.Matches.First(x => x.EventMatchID == newSocketMatch.EventMatchID);
  397. // if (socketMatch != null)
  398. // {
  399. // socketMatch.BettingUsers.Add(new SocketBettingUser()
  400. // {
  401. // blueBet = _socket_blueBet,
  402. // redBet = _socket_redBet,
  403. // user = new User() { UserId = "All" }
  404. // });
  405. // socketMatch.BettingUsers.Add(new SocketBettingUser() { blueBet = 0, redBet = 0,
  406. // user = new User() { UserId = userId }
  407. // });
  408. // }
  409. // }
  410. // }
  411. // public void MockSocket_UpdateBet (string eventMatchId, string userId, string amount, string cockId)
  412. // {
  413. // if (SocketDataObject != null)
  414. // {
  415. // SocketMatch socketMatch = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId);
  416. // if (socketMatch != null)
  417. // {
  418. // if (socketMatch.BettingUsers == null)
  419. // socketMatch.BettingUsers = new List<SocketBettingUser>();
  420. // if (socketMatch.BettingUsers.Count == 0 ||
  421. // !socketMatch.BettingUsers.Any(x => x.user.UserId == userId))
  422. // {
  423. // socketMatch.BettingUsers.Add(new SocketBettingUser()
  424. // {
  425. // blueBet = (socketMatch.BlueID == cockId ? int.Parse(amount) : 0),
  426. // redBet = (socketMatch.RedID == cockId ? int.Parse(amount) : 0),
  427. // user = new User()
  428. // {
  429. // UserId = userId
  430. // }
  431. // });
  432. // }
  433. // else
  434. // {
  435. // SocketBettingUser socketBettingUser = socketMatch.BettingUsers.First(x => x.user.UserId == userId);
  436. // if (socketBettingUser != null)
  437. // {
  438. // if (socketMatch.BlueID == cockId)
  439. // socketBettingUser.blueBet = int.Parse(amount);
  440. // else if (socketMatch.RedID == cockId)
  441. // socketBettingUser.redBet = int.Parse(amount);
  442. // }
  443. // }
  444. // }
  445. // }
  446. // }
  447. // #endregion
  448. // #region MonoBehaviour Methods
  449. // protected override void Awake ()
  450. // {
  451. // base.Awake();
  452. // Initialize();
  453. // }
  454. // private void Update ()
  455. // {
  456. // if (Input.GetKeyDown(KeyCode.Q))
  457. // Socket_Emit_JoinGame(_user.UserId);
  458. // if (Input.GetKeyDown(KeyCode.W))
  459. // Socket_Emit_JoinMatch(_matchId, _matchId, "1", "2", "0", "10", "10");
  460. // if (Input.GetKeyDown(KeyCode.E))
  461. // Socket_Emit_NewBet(_matchId, UERandom.Range(1, 101).ToString(), UERandom.Choice("1", "2"));
  462. // if (Input.GetKeyDown(KeyCode.R))
  463. // Socket_Emit_LeaveMatch(_matchId);
  464. // if (Input.GetKeyDown(KeyCode.Escape))
  465. // if (_socketHandler != null)
  466. // _socketHandler.Disconnect();
  467. // }
  468. // protected override void OnDestroy ()
  469. // {
  470. // base.OnDestroy();
  471. // if (_socketHandler != null)
  472. // _socketHandler.Disconnect();
  473. // }
  474. // #endregion
  475. //}
  476. ////using FullInspector;
  477. ////using FullSerializer;
  478. ////using Newtonsoft.Json;
  479. ////using Newtonsoft.Json.Linq;
  480. ////using System;
  481. ////using System.Linq;
  482. ////using UnityEngine;
  483. ////using UnityEssentials;
  484. ////using UnityEssentials.ExtensionMethods;
  485. ////using System.Collections.Generic;
  486. ////public class ConnectionManager : FSSingleton<ConnectionManager>
  487. ////{
  488. //// #region Fields
  489. //// [InspectorHeader("Game Settings")]
  490. //// [fsProperty] private User _user;
  491. //// [fsProperty] private string _matchId;
  492. //// [InspectorHeader("API Settings")]
  493. //// [fsProperty] private ISabongApiHandler _apiHandler;
  494. //// [fsProperty] private string _apiUrl = "http://api.legendsofthepit.com/api/v1";
  495. //// [InspectorHeader("Socket.IO Settings")]
  496. //// [fsProperty] private BaseSocketIoClient _socketHandler;
  497. //// [fsProperty] private string _socketUrl = "ws://128.199.215.246:3000";
  498. //// [fsProperty] private bool _socketConnected = false;
  499. //// [fsProperty] private bool _socketInMatch = false;
  500. //// [fsProperty] private string _socketInMatchId = string.Empty;
  501. //// public MockSocketDataObject SocketDataObject;
  502. //// private SocketMatch _socketMatch = null;
  503. //// private bool _initialized = false;
  504. //// private Action<bool> _socket_On_JoinGameFinish;
  505. //// private Action<bool> _socket_On_JoinMatchFinish;
  506. //// private int _socket_blueBet = 0;
  507. //// private int _socket_redBet = 0;
  508. //// #endregion
  509. //// int tempBet;
  510. //// #region Properties
  511. //// public bool Initialized
  512. //// {
  513. //// get { return _initialized; }
  514. //// }
  515. //// public User User
  516. //// {
  517. //// get { return _user; }
  518. //// }
  519. //// public bool SocketConnected
  520. //// {
  521. //// get { return _socketConnected; }
  522. //// }
  523. //// public bool SocketInMatch
  524. //// {
  525. //// get { return _socketInMatch; }
  526. //// }
  527. //// #endregion
  528. //// #region Methods
  529. //// private void Initialize ()
  530. //// {
  531. //// if (_initialized)
  532. //// return;
  533. //// // Temporary
  534. //// //RandomizeUser();
  535. //// //MenuController.Instance.Datas.CheckPlayerPrefs();
  536. //// _matchId = UERandom.Range(1, 1000001).ToString();
  537. //// // END
  538. //// // Initialize API Handler.
  539. //// if (_apiHandler != null)
  540. //// {
  541. //// if (_apiHandler is MarkSabongApiHandler)
  542. //// {
  543. //// MarkSabongApiHandler msah = (_apiHandler as MarkSabongApiHandler);
  544. //// msah.BaseUrl = _apiUrl;
  545. //// msah.UserId = _user.UserId;
  546. //// msah.AccessToken = "helloworld";
  547. //// msah.MonoBehaviour = this;
  548. //// }
  549. //// else if (_apiHandler is MockSabongApiHandler)
  550. //// {
  551. //// MockSabongApiHandler msah = (_apiHandler as MockSabongApiHandler);
  552. //// msah.MockApiDataObject = Resources.Load("MockApiDataObject") as MockApiDataObject;
  553. //// }
  554. //// }
  555. //// else
  556. //// {
  557. //// Debug.Log("Api Handler is null. Setting value to MarkSabongApiHandler.");
  558. //// MarkSabongApiHandler msah = new MarkSabongApiHandler();
  559. //// msah.BaseUrl = _apiUrl;
  560. //// msah.UserId = _user.UserId;
  561. //// msah.AccessToken = "helloworld";
  562. //// msah.MonoBehaviour = this;
  563. //// _apiHandler = msah;
  564. //// }
  565. //// // Initialize Socket Handler.
  566. //// if (_socketHandler != null &&
  567. //// !_socketUrl.IsNullOrEmpty())
  568. //// {
  569. //// _socketHandler.BaseUrl = _socketUrl;
  570. //// _socketHandler.On_Connect = Socket_SubscribeSabongEvents;
  571. //// _socketHandler.On_Disconnect = Socket_UnsubscribeSabongEvents;
  572. //// _socketHandler.Connect();
  573. //// }
  574. //// else
  575. //// {
  576. //// Debug.Log("Socket Handler is null. Setting value to QuobjectSocketIoClient.");
  577. //// _socketHandler = new QuobjectSocketIoClient (_socketUrl)
  578. //// {
  579. //// On_Connect = Socket_SubscribeSabongEvents,
  580. //// On_Disconnect = Socket_UnsubscribeSabongEvents
  581. //// };
  582. //// _socketHandler.Connect();
  583. //// }
  584. //// _initialized = true;
  585. //// }
  586. //// // Temporary
  587. //// public void RandomizeUser ()
  588. //// {
  589. //// _user.UserId = UERandom.Range(1, 1000001).ToString();
  590. //// _user.FirstName = UERandom.Choice("Andrew", "Bobson", "Charlie", "Dean", "Eric",
  591. //// "Alice", "Betty", "Christine", "Dianne", "Elaine");
  592. //// _user.LastName = UERandom.Choice("Atlas", "Butcher", "Cook", "Diner", "Enger",
  593. //// "Filibuster", "Garfield", "Holland", "Il", "Jackson");
  594. //// _user.Money = 3000;
  595. //// }
  596. //// #endregion
  597. //// #region ISabongApiHandler Methods
  598. //// public void GetAllArenas (Action<bool, Arena[]> callback)
  599. //// {
  600. //// if (_apiHandler == null)
  601. //// {
  602. //// Debug.LogError("Api not handled.");
  603. //// return;
  604. //// }
  605. //// _apiHandler.GetAllArenas(callback);
  606. //// }
  607. //// public void GetAllDerbies (Action<bool, Derby[]> callback)
  608. //// {
  609. //// if (_apiHandler == null)
  610. //// {
  611. //// Debug.LogError("Api not handled.");
  612. //// return;
  613. //// }
  614. //// _apiHandler.GetAllDerbies(callback);
  615. //// }
  616. //// public void GetArena (string arenaId, Action<bool, Arena> callback)
  617. //// {
  618. //// if (_apiHandler == null)
  619. //// {
  620. //// Debug.LogError("Api not handled.");
  621. //// return;
  622. //// }
  623. //// _apiHandler.GetArena(arenaId, callback);
  624. //// }
  625. //// public void GetDerby (string derbyId, Action<bool, Derby> callback)
  626. //// {
  627. //// if (_apiHandler == null)
  628. //// {
  629. //// Debug.LogError("Api not handled.");
  630. //// return;
  631. //// }
  632. //// _apiHandler.GetDerby(derbyId, callback);
  633. //// }
  634. //// public void GetFight (string fightId, Action<bool, GCFight> callback)
  635. //// {
  636. //// if (_apiHandler == null)
  637. //// {
  638. //// Debug.LogError("Api not handled.");
  639. //// return;
  640. //// }
  641. //// _apiHandler.GetFight(fightId, callback);
  642. //// }
  643. //// public void GetFightResult (string fightId, Action<bool, GCFightData> callback)
  644. //// {
  645. //// if (_apiHandler == null)
  646. //// {
  647. //// Debug.LogError("Api not handled.");
  648. //// return;
  649. //// }
  650. //// _apiHandler.GetFightResult(fightId, callback);
  651. //// }
  652. //// public void GetActiveDerby (string arenaId, Action<bool, Derby> callback)
  653. //// {
  654. //// if (_apiHandler == null)
  655. //// {
  656. //// Debug.LogError("Api not handled.");
  657. //// return;
  658. //// }
  659. //// _apiHandler.GetActiveDerby(arenaId, callback);
  660. //// }
  661. //// public void GetDerbyFights (string derbyId, Action<bool, GCFight[]> callback)
  662. //// {
  663. //// if (_apiHandler == null)
  664. //// {
  665. //// Debug.LogError("Api not handled.");
  666. //// return;
  667. //// }
  668. //// _apiHandler.GetDerbyFights(derbyId, callback);
  669. //// }
  670. //// public void PostFightBet (string fightId, int gamecock1Bet, int gamecock2Bet, Action<bool> callback)
  671. //// {
  672. //// if (_apiHandler == null)
  673. //// {
  674. //// Debug.LogError("Api not handled.");
  675. //// return;
  676. //// }
  677. //// _apiHandler.PostFightBet(fightId, gamecock1Bet, gamecock2Bet, callback);
  678. //// }
  679. //// #endregion
  680. //// #region BaseSocketIoClient Subscription Methods
  681. //// private void Socket_SubscribeSabongEvents (object data)
  682. //// {
  683. //// if (_socketHandler != null)
  684. //// {
  685. //// _socketHandler.On("join_game", Socket_On_JoinGame);
  686. //// _socketHandler.On("authSuccess", Socket_On_AuthSuccess);
  687. //// _socketHandler.On("authFailed", Socket_On_AuthFailed);
  688. //// _socketHandler.On("join_match", Socket_On_JoinMatch);
  689. //// _socketHandler.On("joinMatchSuccess", Socket_On_JoinMatchSuccess);
  690. //// _socketHandler.On("alreadyInMatch", Socket_On_AlreadyInMatch);
  691. //// _socketHandler.On("new_bet", Socket_On_NewBet);
  692. //// Socket_Emit_JoinGame(_user.UserId, (b) => _socketConnected = b);
  693. //// }
  694. //// }
  695. //// private void Socket_UnsubscribeSabongEvents (object data)
  696. //// {
  697. //// if (_socketHandler != null)
  698. //// {
  699. //// _socketHandler.Off("join_game");
  700. //// _socketHandler.Off("authSuccess");
  701. //// _socketHandler.Off("authFailed");
  702. //// _socketHandler.Off("join_match");
  703. //// _socketHandler.Off("joinMatchSuccess");
  704. //// _socketHandler.Off("alreadyInMatch");
  705. //// _socketHandler.Off("new_bet");
  706. //// }
  707. //// }
  708. //// private void Socket_On_JoinGame ()
  709. //// {
  710. //// Debug.Log("join_game");
  711. //// }
  712. //// private void Socket_On_AuthSuccess ()
  713. //// {
  714. //// Debug.Log("authSuccess");
  715. //// if (_socket_On_JoinGameFinish != null)
  716. //// {
  717. //// _socket_On_JoinGameFinish(true);
  718. //// _socket_On_JoinGameFinish = null;
  719. //// }
  720. //// }
  721. //// private void Socket_On_AuthFailed ()
  722. //// {
  723. //// Debug.Log("authFailed");
  724. //// if (_socket_On_JoinGameFinish != null)
  725. //// {
  726. //// _socket_On_JoinGameFinish(false);
  727. //// _socket_On_JoinGameFinish = null;
  728. //// }
  729. //// }
  730. //// private void Socket_On_JoinMatch ()
  731. //// {
  732. //// Debug.Log("join_match");
  733. //// }
  734. //// private void Socket_On_JoinMatchSuccess ()
  735. //// {
  736. //// Debug.Log("joinMatchSuccess");
  737. //// _socketInMatch = true;
  738. //// // TODO
  739. //// MockSocket_AddMatch(_user.UserId, _socketMatch);
  740. //// if (_socket_On_JoinMatchFinish != null)
  741. //// {
  742. //// _socket_On_JoinMatchFinish(true);
  743. //// _socket_On_JoinMatchFinish = null;
  744. //// }
  745. //// }
  746. //// private void Socket_On_AlreadyInMatch ()
  747. //// {
  748. //// Debug.Log("alreadyInMatch");
  749. //// _socketInMatch = true;
  750. //// if (_socket_On_JoinMatchFinish != null)
  751. //// {
  752. //// _socket_On_JoinMatchFinish(true);
  753. //// _socket_On_JoinMatchFinish = null;
  754. //// }
  755. //// }
  756. //// private void Socket_On_NewBet (object data)
  757. //// {
  758. //// SocketBet bet = JsonConvert.DeserializeObject<SocketBet>(data.ToString());
  759. //// // TODO
  760. //// MockSocket_UpdateBet(_socketInMatchId, bet.username, bet.betAmount, bet.cockId);
  761. //// Debug.LogFormat("'{0}' betted ${1} on Cock #{2}.",
  762. //// bet.username,
  763. //// bet.betAmount,
  764. //// bet.cockId);
  765. //// }
  766. //// public bool Socket_Emit_JoinGame (string username, Action<bool> callback = null)
  767. //// {
  768. //// if (_socketHandler == null)
  769. //// return false;
  770. //// bool result = _socketHandler.Emit("join_game",
  771. //// new JObject(
  772. //// new JProperty("username", username)));
  773. //// if (!result)
  774. //// {
  775. //// if (callback != null)
  776. //// callback(false);
  777. //// return false;
  778. //// }
  779. //// if (callback != null)
  780. //// _socket_On_JoinGameFinish = callback;
  781. //// return true;
  782. //// }
  783. //// public bool Socket_Emit_JoinMatch (string eventMatchId, string eventId, string blueId, string redId, string startTime, string endTime, string duration, Action<bool> callback = null)
  784. //// {
  785. //// if (_socketHandler == null)
  786. //// return false;
  787. //// bool result = _socketHandler.Emit("join_match",
  788. //// new JObject(
  789. //// new JProperty("EventMatchID", eventMatchId),
  790. //// new JProperty("EventID", eventId),
  791. //// new JProperty("StartTime", startTime),
  792. //// new JProperty("EndTime", endTime),
  793. //// new JProperty("Duration", duration)));
  794. //// if (!result)
  795. //// {
  796. //// if (callback != null)
  797. //// callback(false);
  798. //// return false;
  799. //// }
  800. //// _socketInMatchId = eventMatchId;
  801. //// _socket_blueBet = UERandom.Range(0, 100001);
  802. //// _socket_redBet = _socket_blueBet + UERandom.Range(-5000, 5001);
  803. //// _socketMatch = new SocketMatch()
  804. //// {
  805. //// EventMatchID = eventMatchId,
  806. //// EventID = eventId,
  807. //// BlueID = blueId,
  808. //// RedID = redId,
  809. //// StartTime = startTime,
  810. //// EndTime = endTime,
  811. //// Duration = duration
  812. //// };
  813. //// if (callback != null)
  814. //// _socket_On_JoinMatchFinish = callback;
  815. //// return true;
  816. //// }
  817. //// public bool Socket_Emit_LeaveMatch (string eventMatchId)
  818. //// {
  819. //// _socketInMatch = false;
  820. //// _socketInMatchId = string.Empty;
  821. //// Debug.LogFormat("'{0}' left the match #{1}.",
  822. //// _user.FullName,
  823. //// _socketInMatchId);
  824. //// return true;
  825. //// }
  826. //// public bool Socket_Emit_NewBet (string eventMatchId, string amount, string cockId)
  827. //// {
  828. //// if (_socketHandler == null)
  829. //// return false;
  830. //// // TODO
  831. //// if (SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId) != null)
  832. //// {
  833. //// if (cockId == SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BlueID)
  834. //// tempBet = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BettingUsers.First(x => x.user.UserId == User.UserId).blueBet;
  835. //// else if (cockId == SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).RedID)
  836. //// tempBet = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId).BettingUsers.First(x => x.user.UserId == User.UserId).redBet;
  837. //// }
  838. //// _user.Money = _user.Money - Mathf.Abs(tempBet - int.Parse(amount));
  839. //// //Debug.Log("User Money: " + _user.Money);
  840. //// MockSocket_UpdateBet(eventMatchId, _user.UserId, amount, cockId);
  841. //// Debug.LogFormat("'{0}' betted ${1} on Cock #{2}.",
  842. //// "You",
  843. //// amount,
  844. //// cockId);
  845. //// return _socketHandler.Emit("new_bet",
  846. //// new JObject(
  847. //// new JProperty("EventMatchID", eventMatchId),
  848. //// new JProperty("amount", amount),
  849. //// new JProperty("cockId", cockId)));
  850. //// }
  851. //// public void MockSocket_AddMatch (string userId, SocketMatch newSocketMatch)
  852. //// {
  853. //// if (SocketDataObject != null)
  854. //// {
  855. //// if (SocketDataObject.Matches == null)
  856. //// SocketDataObject.Matches = new List<SocketMatch>();
  857. //// if (SocketDataObject.Matches.Count == 0 ||
  858. //// !SocketDataObject.Matches.Any(x => x.EventMatchID == newSocketMatch.EventMatchID))
  859. //// {
  860. //// SocketDataObject.Matches.Add(new SocketMatch()
  861. //// {
  862. //// EventMatchID = newSocketMatch.EventMatchID,
  863. //// EventID = newSocketMatch.EventID,
  864. //// BlueID = newSocketMatch.BlueID,
  865. //// RedID = newSocketMatch.RedID,
  866. //// StartTime = newSocketMatch.StartTime,
  867. //// EndTime = newSocketMatch.EndTime,
  868. //// Duration = newSocketMatch.Duration
  869. //// });
  870. //// }
  871. //// SocketMatch socketMatch = SocketDataObject.Matches.First(x => x.EventMatchID == newSocketMatch.EventMatchID);
  872. //// if (socketMatch != null)
  873. //// {
  874. //// socketMatch.BettingUsers.Add(new SocketBettingUser()
  875. //// {
  876. //// blueBet = _socket_blueBet,
  877. //// redBet = _socket_redBet,
  878. //// user = new User() { UserId = "All" }
  879. //// });
  880. //// socketMatch.BettingUsers.Add(new SocketBettingUser() { blueBet = 0, redBet = 0,
  881. //// user = new User() { UserId = userId }
  882. //// });
  883. //// }
  884. //// }
  885. //// }
  886. //// public void MockSocket_UpdateBet (string eventMatchId, string userId, string amount, string cockId)
  887. //// {
  888. //// if (SocketDataObject != null)
  889. //// {
  890. //// SocketMatch socketMatch = SocketDataObject.Matches.First(x => x.EventMatchID == eventMatchId);
  891. //// if (socketMatch != null)
  892. //// {
  893. //// if (socketMatch.BettingUsers == null)
  894. //// socketMatch.BettingUsers = new List<SocketBettingUser>();
  895. //// if (socketMatch.BettingUsers.Count == 0 ||
  896. //// !socketMatch.BettingUsers.Any(x => x.user.UserId == userId))
  897. //// {
  898. //// socketMatch.BettingUsers.Add(new SocketBettingUser()
  899. //// {
  900. //// blueBet = (socketMatch.BlueID == cockId ? int.Parse(amount) : 0),
  901. //// redBet = (socketMatch.RedID == cockId ? int.Parse(amount) : 0),
  902. //// user = new User()
  903. //// {
  904. //// UserId = userId
  905. //// }
  906. //// });
  907. //// }
  908. //// else
  909. //// {
  910. //// SocketBettingUser socketBettingUser = socketMatch.BettingUsers.First(x => x.user.UserId == userId);
  911. //// if (socketBettingUser != null)
  912. //// {
  913. //// if (socketMatch.BlueID == cockId)
  914. //// socketBettingUser.blueBet = int.Parse(amount);
  915. //// else if (socketMatch.RedID == cockId)
  916. //// socketBettingUser.redBet = int.Parse(amount);
  917. //// }
  918. //// }
  919. //// }
  920. //// }
  921. //// }
  922. //// #endregion
  923. //// #region MonoBehaviour Methods
  924. //// protected override void Awake ()
  925. //// {
  926. //// base.Awake();
  927. //// Initialize();
  928. //// }
  929. //// private void Update ()
  930. //// {
  931. //// if (Input.GetKeyDown(KeyCode.Q))
  932. //// Socket_Emit_JoinGame(_user.UserId);
  933. //// if (Input.GetKeyDown(KeyCode.W))
  934. //// Socket_Emit_JoinMatch(_matchId, _matchId, "1", "2", "0", "10", "10");
  935. //// if (Input.GetKeyDown(KeyCode.E))
  936. //// Socket_Emit_NewBet(_matchId, UERandom.Range(1, 101).ToString(), UERandom.Choice("1", "2"));
  937. //// if (Input.GetKeyDown(KeyCode.R))
  938. //// Socket_Emit_LeaveMatch(_matchId);
  939. //// if (Input.GetKeyDown(KeyCode.Escape))
  940. //// if (_socketHandler != null)
  941. //// _socketHandler.Disconnect();
  942. //// }
  943. //// protected override void OnDestroy ()
  944. //// {
  945. //// base.OnDestroy();
  946. //// if (_socketHandler != null)
  947. //// _socketHandler.Disconnect();
  948. //// }
  949. //// #endregion
  950. ////}