Exemplo n.º 1
0
    // if we are looking for npcs, mailing lists are not included
    public void GetLatest(LatestReceivedCallback success, LatestFailedCallback failure, bool npcs = false)
    {
        List <string> rolenames = npcs ? m_UserManager.GetNPCRoles() : m_UserManager.GetCharacterRoles();

        m_LatestResultCount = 0;
        m_LatestResults.Clear();

        for (int i = 0; i < rolenames.Count; ++i)
        {
            StartCoroutine(GetLatestCoroutine(success, failure, "role/" + rolenames[i], npcs ? rolenames.Count : rolenames.Count + 1, false));
        }

        if (!npcs)
        {
            StartCoroutine(GetLatestCoroutine(success, failure, "mailinglist", rolenames.Count + 1, true));
        }
    }
Exemplo n.º 2
0
    private IEnumerator GetLatestCoroutine(LatestReceivedCallback success, LatestFailedCallback failure, string path, int totalCount, bool mailinglist)
    {
        string url = "/api/messages/latest/" + path;

        UnityWebRequest request = UnityWebRequest.Get(Constants.serverAddress + url);

        request.chunkedTransfer = false;

        yield return(request.SendWebRequest());

        while (!request.isDone)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot get latest: " + request.error + ", Code = " + request.responseCode);
            if (failure != null)
            {
                failure();
            }
        }
        else if (request.isHttpError)
        {
            Debug.Log("Http error: Cannot get latest: " + request.error + ", Code = " + request.responseCode);
            if (failure != null)
            {
                failure();
            }
        }
        else
        {
            List <LatestInfo> latest = null;
            try
            {
                latest = new List <LatestInfo>();
                // Reprocess the info to a common format to make it unified for both cases
                if (mailinglist)
                {
                    List <LatestMailingListInfo> tmp = JsonConvert.DeserializeObject <List <LatestMailingListInfo> >(request.downloadHandler.text);
                    for (int i = 0; i < tmp.Count; ++i)
                    {
                        LatestInfo info = new LatestInfo();
                        info._id       = tmp[i].recipientList._id;
                        info.createdAt = tmp[i].createdAt;
                        latest.Add(info);
                    }
                }
                else
                {
                    List <LatestMessageInfo> tmp = JsonConvert.DeserializeObject <List <LatestMessageInfo> >(request.downloadHandler.text);
                    for (int i = 0; i < tmp.Count; ++i)
                    {
                        LatestInfo info = new LatestInfo();
                        info._id       = tmp[i].recipient._id;
                        info.createdAt = tmp[i].createdAt;
                        latest.Add(info);
                    }
                }
            } catch (Exception)
            {
                Debug.LogError("Warning: cannot get latest messages.");
            }
            if (latest != null)
            {
                m_LatestResults.AddRange(latest);

                m_LatestResultCount++;
                if (m_LatestResultCount == totalCount)
                {
                    m_LatestResults.Sort((m1, m2) => { return(MessageManager.ParseTimeStamp(m2.createdAt).CompareTo(MessageManager.ParseTimeStamp(m1.createdAt))); });
                    m_LatestResults = m_LatestResults.GetRange(0, Mathf.Min(m_LatestResults.Count, 10));

                    if (success != null)
                    {
                        success(m_LatestResults);
                    }
                }
            }
        }
    }