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

        SceneManager.LoadScene(0);
    }
Example #2
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);
        }
    }