PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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