Beispiel #1
0
    // restart
    public static void Restart()
    {
        UDP.Clear();
        UIPlayers.Clear();
        Match.Clear();
        Account.Clear();

        SceneManager.LoadScene(0);
    }
Beispiel #2
0
    /// <summary>
    /// Refreshes player list
    /// </summary>
    /// <param name="r"></param>
    /// <returns></returns>
    public bool Players(UDPPacket r = null)
    {
        if (r == null)
        {
            // if the user is not logged in or there is no event, skip
            if (!Account.IsLoggedIn() || activeEvent == null)
            {
                return(false);
            }

            Event e = new Event
            {
                User = Account.GetUser(),
                M    = new Game
                {
                    ID  = activeEvent.M.ID,
                    Key = activeEvent.M.Key
                }
            };

            // request
            string j = JsonConvert.SerializeObject(e);
            UDP.Request(new UDPPacket
            {
                Service = UDPService.Match,
                Action  = UDPAction.Players,
                Request = j,
            });
            return(false);
        }
        else
        {
            if (r.Error != 0)
            {
                Debug.LogWarning(r.ToString());
                return(false);
            }

            Event e = JsonConvert.DeserializeObject <Event>(r.Response);
            activeEvent.M.Status = e.M.Status;
            activeEvent.Players  = e.Players;
            UIPlayers.loaded     = false;
            UIPlayers.Load();

            if (e.M.Status == MatchStatus.Ready ||
                e.M.Status == MatchStatus.Play)
            {
                CancelInvoke();
                StartCoroutine(UIPlayers.self.Ready());
            }

            return(true);
        }
    }
Beispiel #3
0
    /// <summary>
    /// Chooses a character for the match
    /// </summary>
    /// <param name="cid"></param>
    /// <param name="r"></param>
    /// <returns></returns>
    public bool Choose(int cid, UDPPacket r = null)
    {
        if (r == null)
        {
            // if the user is not logged in or match type is none, restart
            if (!Account.IsLoggedIn() || cid == 0)
            {
                Kill2Live.Restart();
                return(false);
            }
            Debug.Log("Choosing a character: " + cid.ToString() + " ...");

            Event e = Match.self.activeEvent;
            e.User = Account.GetUser();
            e.Me   = new Player
            {
                Character = cid
            };

            // request
            string j = JsonConvert.SerializeObject(e);
            UDP.Request(new UDPPacket
            {
                Service = UDPService.Match,
                Action  = UDPAction.Choose,
                Request = j,
            });
            return(false);
        }
        else
        {
            if (r.Error != 0 || r.Response == null)
            {
                UIMessageGlobal.Open(Language.v["choosefailed"],
                                     Language.v["choosefaileddesc"]);
                Debug.LogWarning(r.ToString());
                return(false);
            }
            Debug.Log("Chosen: " + r.Response);

            Event e = JsonConvert.DeserializeObject <Event>(r.Response);
            Match.self.activeEvent.Me.Character = e.Me.Character;

            UIPlayers.Set(Account.GetUser().Nickname, e.Me.Character);

            // TODO update ui
            uiChoose.SetActive(false);

            return(true);
        }
    }
Beispiel #4
0
    /// <summary>
    /// Find and join into a game.
    /// </summary>
    /// <param name="type"></param>
    /// <param name="r"></param>
    public bool Join(MatchType typ = MatchType.None, UDPPacket r = null)
    {
        if (r == null)
        {
            // if the user is not logged in or match type is none, restart
            if (!Account.IsLoggedIn())
            {
                Kill2Live.Restart();
                return(false);
            }
            Debug.Log("Finding a match for " + typ.ToString() + " ...");

            Event e = new Event
            {
                User = Account.GetUser(),
                M    = new Game
                {
                    Typ = typ,
                }
            };

            // request
            string j = JsonConvert.SerializeObject(e);
            UDP.Request(new UDPPacket
            {
                Service = UDPService.Match,
                Action  = UDPAction.Join,
                Request = j,
            });
            return(false);
        }
        else
        {
            if (r.Error != 0)
            {
                if (r.Error == (int)ServiceError.AlreadyJoined)
                {
                    // continue
                    // no return here because of this situation
                }
                else if (r.Error >= (int)ServiceError.MatchUnknown)
                {
                    UIMessageGlobal.Open(Language.v["joinfailed"],
                                         Language.v["joinfaileddesc"]);
                    Debug.LogWarning(r.ToString());
                    return(false);
                }
            }
            Debug.Log("Joining: " + r.Response);

            Event e = JsonConvert.DeserializeObject <Event>(r.Response);
            if (e.M.Typ == MatchType.None)
            {
                return(false);
            }
            activeEvent = e;

            if (e.M.Status == MatchStatus.Reserved) // still creating a match
            {
                if (!refreshing)
                {
                    refreshing = true;
                    InvokeRepeating("PlayersLoop", 0f, refresh);
                }
            }
            else
            {
                if (e.M.Status == MatchStatus.Ready ||
                    e.M.Status == MatchStatus.Play)
                {
                    Players();
                }
                else if (e.M.Status == MatchStatus.End ||
                         e.M.Status == MatchStatus.Released)
                {
                    activeEvent = null;
                    UIPlayers.Clear();
                    return(false);
                }
            }
            return(true);
        }
    }
Beispiel #5
0
 private void Awake()
 {
     self = FindObjectOfType <UIPlayers>();
 }