Ejemplo n.º 1
0
    private IEnumerator GetMailingListsCoroutine(ListsReadyCallback success, NoConnectionCallback noConnection)
    {
        UnityWebRequest request = UnityWebRequest.Get(Constants.serverAddress + "/api/mailinglists");

        request.chunkedTransfer = false;

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot get lists: " + request.error + ", Code = " + request.responseCode);
            if (noConnection != null)
            {
                noConnection();
            }
        }
        else if (request.isHttpError)
        {
            Debug.Log("Http error: Cannot get lists: " + request.error + ", Code = " + request.responseCode);
            if (noConnection != null)
            {
                noConnection();
            }
        }
        else
        {
            try
            {
                Dictionary <string, ListInfo> lists = JsonConvert.DeserializeObject <Dictionary <string, ListInfo> >(request.downloadHandler.text);
                m_CachedListNames = new List <string>();
                m_CachedLists     = new Dictionary <string, ListInfo>();

                foreach (var list in lists)
                {
                    //Debug.Log(list.Value.name + ": " + list.Value._id);
                    m_CachedListNames.Add(list.Value.name);
                    m_CachedLists[list.Value._id] = list.Value;
                }

                if (success != null)
                {
                    success();
                }
            } catch (Exception)
            {
                Debug.LogWarning("Warning: Getting mailing lists failed.");
                m_CachedListNames = new List <string>();
                m_CachedLists     = new Dictionary <string, ListInfo>();
            }
        }
    }
Ejemplo n.º 2
0
 public void GetNews(bool forceReload, NewsReceivedCallback success, NoConnectionCallback noconnection, NewsReceivingError failure)
 {
     if (m_CachedNews == null || forceReload || IsTimeToReloadNews())
     {
         m_LastRefresh = Time.time;
         StartCoroutine(GetNewsCoroutine(success, noconnection, failure));
     }
     else
     {
         if (success != null)
         {
             success(m_CachedNews);
         }
     }
 }
Ejemplo n.º 3
0
 public void GetMessages(bool forceReload, MessagesReceivedCallback success, NoConnectionCallback noconnection, MessageReceivingError failure)
 {
     if (m_CachedMessages == null || forceReload || IsTimeToReloadMessages() || !GetUserToShow().Equals(m_CachedMessageUser))
     {
         m_LastRefresh = Time.time;
         StartCoroutine(GetMessagesCoroutine(success, noconnection, failure));
     }
     else
     {
         if (success != null)
         {
             success();
         }
     }
 }
Ejemplo n.º 4
0
 public void GetCurrentUserInfo(UserProfileReceivedCallback callback, NoConnectionCallback failCallback)
 {
     if (m_CachedUsers != null)
     {
         if (callback != null)
         {
             callback(InternalGetCurrentUserProfile());
         }
     }
     else
     {
         StartCoroutine(GetRolesCoroutine());
         StartCoroutine(GetMailingListsCoroutine(null, failCallback));
         StartCoroutine(GetUsersCoroutine(() => { callback(InternalGetCurrentUserProfile()); }, failCallback));
     }
 }
Ejemplo n.º 5
0
 public void Login(string username, string pin, LoginSuccessfulCallback successCallback, LoginFailedCallback loginFailCallback, NoConnectionCallback failCallback)
 {
     StartCoroutine(TryLoginCoroutine(username, pin, successCallback, loginFailCallback, failCallback));
 }
Ejemplo n.º 6
0
    private IEnumerator TryLoginCoroutine(string username, string password, LoginSuccessfulCallback success, LoginFailedCallback failure, NoConnectionCallback noconnection)
    {
        WWWForm form = new WWWForm();

        form.AddField("username", username);
        form.AddField("password", password);
        UnityWebRequest request = UnityWebRequest.Post(Constants.serverAddress + "api/login", form);

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot login: "******", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 404)
            {
                Debug.Log("Http error: User not found: " + request.error + ", Code = " + request.responseCode);
            }
            else if (request.responseCode == 401)
            {
                Debug.Log("Http error: Wrong password: "******", Code = " + request.responseCode);
            }

            if (failure != null)
            {
                failure();
            }
        }
        else
        {
            LoginData logindata = default(LoginData);

            try
            {
                logindata = JsonConvert.DeserializeObject <LoginData>(request.downloadHandler.text);
            } catch (Exception)
            {
                Debug.LogError("Error: Cannot deserialize login data");
            }

            if (logindata.token != null)
            {
                //Debug.Log(logindata.user.username + ": " + logindata.token);

                CurrentUser        = logindata.user._id;
                CurrentUserName    = logindata.user.username;
                CurrentUserClass   = logindata.user.profile.role + " " + logindata.user.profile.group;
                CurrentUserBalance = logindata.user.profile.balance != null ? logindata.user.profile.balance : "0";
                CurrentUserRole    = logindata.user.profile.role;
                m_UserToken        = logindata.token;

                if (success != null)
                {
                    success();
                }
            }
            else
            {
                if (failure != null)
                {
                    failure();
                }
            }
        }
    }
Ejemplo n.º 7
0
 public void GetUsers(UsersReceivedCallback callback, ListsReadyCallback listCallback, NoConnectionCallback failCallback)
 {
     if (m_CachedUsernames != null && callback != null)
     {
         callback(m_CachedUsernames);
     }
     else
     {
         StartCoroutine(GetRolesCoroutine());
         StartCoroutine(GetMailingListsCoroutine(listCallback, failCallback));
         StartCoroutine(GetUsersCoroutine(() => { callback(m_CachedUsernames); }, failCallback));
     }
 }
Ejemplo n.º 8
0
    private IEnumerator GetUsersCoroutine(UsersReadyCallback callback, NoConnectionCallback noconnectionCallback)
    {
        UnityWebRequest request = UnityWebRequest.Get(Constants.serverAddress + "/api/users");

        request.chunkedTransfer = false;

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot get users: " + request.error + ", Code = " + request.responseCode);
            if (noconnectionCallback != null)
            {
                noconnectionCallback();
            }
        }
        else if (request.isHttpError)
        {
            Debug.Log("Http error: Cannot get users: " + request.error + ", Code = " + request.responseCode);
            if (noconnectionCallback != null)
            {
                noconnectionCallback();
            }
        }
        else
        {
            Dictionary <string, UserInfo> users = null;
            try
            {
                users             = JsonConvert.DeserializeObject <Dictionary <string, UserInfo> >(request.downloadHandler.text);
                m_CachedUsernames = new List <string>();
                m_CachedUsers     = new Dictionary <string, UserInfo>();
            }
            catch (Exception)
            {
                Debug.LogError("Error: cannot deserialize user data");
                m_CachedUsernames = null;
                m_CachedUsers     = null;
            }

            if (users != null)
            {
                foreach (var user in users)
                {
                    //Debug.Log(user.Value.username + ": " + user.Value._id);
                    m_CachedUsernames.Add(user.Value.username);
                    m_CachedUsers[user.Value._id] = user.Value;
                }

                int loops = 0;
                while ((m_CachedLists == null || m_CachedRoles == null) && loops < 500)
                {
                    ++loops;
                    yield return(new WaitForEndOfFrame());
                }

                if (callback != null)
                {
                    callback();
                }
            }
        }
    }
Ejemplo n.º 9
0
 public void SendNews(string topic, string message, string updateId, NewsSentCallback success, NoConnectionCallback noconnection)
 {
     if (updateId == null)
     {
         StartCoroutine(SendNewsCoroutine(topic, message, success, noconnection));
     }
     else
     {
         StartCoroutine(EditNewsCoroutine(topic, message, updateId, success, noconnection));
     }
 }
Ejemplo n.º 10
0
    public IEnumerator GetMessagesCoroutine(MessagesReceivedCallback success, NoConnectionCallback noconnection, MessageReceivingError failure)
    {
        bool hack = m_UserManager.CurrentHackedUser != null;

        UnityWebRequest request;

        if (hack)
        {
            WWWForm form = new WWWForm();
            form.AddField("targetId", m_UserManager.CurrentHackedUser);

            request = UnityWebRequest.Get(Constants.serverAddress + "api/hack/messages/" + m_UserManager.CurrentHackedUser);
        }
        else
        {
            request = UnityWebRequest.Get(Constants.serverAddress + "api/messages");
        }
        m_UserManager.SetCurrentUserAuthorization(request);

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot get messages: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 500)
            {
                Debug.Log("Http error: Database search failed: " + request.error + ", Code = " + request.responseCode);
                if (failure != null)
                {
                    failure();
                }
            }
        }
        else if (request.responseCode == 200)
        {
            // response contains the message
            //Debug.Log("Messages received: " + request.downloadHandler.text);
            Dictionary <string, MessageInfo> messageData = null;

            try
            {
                messageData = JsonConvert.DeserializeObject <Dictionary <string, MessageInfo> >(request.downloadHandler.text);
            } catch (Exception)
            {
                Debug.LogError("Error: cannot deserialize messages.");
            }

            if (messageData != null)
            {
                m_CachedMessages = new Dictionary <string, MessageInfo>();
                foreach (MessageInfo m in messageData.Values)
                {
                    if (m._id != null)
                    {
                        m_CachedMessages[m._id] = m;
                    }
                }
                m_CachedMessageUser = GetUserToShow();

                if (success != null)
                {
                    success();
                }
            }
        }
    }
Ejemplo n.º 11
0
 public void SendMessage(MessageInfo m, MessageSentCallback success, MessageSendingError failure, NoConnectionCallback noconnection)
 {
     StartCoroutine(SendMessageCoroutine(m, success, failure, noconnection));
 }
Ejemplo n.º 12
0
    public IEnumerator SendMessageCoroutine(MessageInfo m, MessageSentCallback success, MessageSendingError failure, NoConnectionCallback noconnection)
    {
        WWWForm form = new WWWForm();

        form.AddField("recipientId", m.recipient);
        form.AddField("title", m.title);
        form.AddField("messageBody", m.body);
        if (m.replyTo != null)
        {
            form.AddField("replyTo", m.replyTo);
        }

        bool hacked = m_UserManager.CurrentHackedUser != null;

        UnityWebRequest request;

        if (hacked)
        {
            form.AddField("targetId", m_UserManager.CurrentHackedUser);
            request = UnityWebRequest.Post(Constants.serverAddress + "api/hack/messages", form);
        }
        else
        {
            request = UnityWebRequest.Post(Constants.serverAddress + "api/messages", form);
        }

        m_UserManager.SetCurrentUserAuthorization(request);

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot send message: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 400)
            {
                Debug.Log("Http error: Message data missing: " + request.error + ", Code = " + request.responseCode + " " + request.downloadHandler.text);
            }
            else if (request.responseCode == 404)
            {
                Debug.Log("Http error: Recipient or parent not found: " + request.error + ", Code = " + request.responseCode + " " + request.downloadHandler.text);
            }
            else if (request.responseCode == 500)
            {
                Debug.Log("Http error: Database search failed: " + request.error + ", Code = " + request.responseCode);
            }

            if (failure != null)
            {
                failure();
            }
        }
        else if (request.responseCode == 200)
        {
            // response contains the message
            //Debug.Log("Message sent: " + request.downloadHandler.text);
            GetMessages(true, () => { success(); }, noconnection, () => { failure(); });
        }
    }
Ejemplo n.º 13
0
    public IEnumerator SendNewsCoroutine(string topic, string message, NewsSentCallback success, NoConnectionCallback noconnection)
    {
        WWWForm form = new WWWForm();

        form.AddField("title", topic);
        form.AddField("newsBody", message);

        UnityWebRequest request = UnityWebRequest.Post(Constants.serverAddress + "api/news", form);

        m_UserManager.SetCurrentUserAuthorization(request);

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot send news: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 400)
            {
                Debug.Log("Http error: News data missing: " + request.error + ", Code = " + request.responseCode + " " + request.downloadHandler.text);
            }
            else if (request.responseCode == 500)
            {
                Debug.Log("Http error: Database search failed: " + request.error + ", Code = " + request.responseCode);
            }
        }
        else if (request.responseCode == 200)
        {
            if (success != null)
            {
                success();
            }
        }
    }
Ejemplo n.º 14
0
    public IEnumerator GetNewsCoroutine(NewsReceivedCallback success, NoConnectionCallback noconnection, NewsReceivingError failure)
    {
        UnityWebRequest request;

        request = UnityWebRequest.Get(Constants.serverAddress + "api/news");

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot get news: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 500)
            {
                Debug.Log("Http error: Database search failed: " + request.error + ", Code = " + request.responseCode);
                if (failure != null)
                {
                    failure();
                }
            }
        }
        else if (request.responseCode == 200)
        {
            // response contains the message
            //Debug.Log("News received: " + request.downloadHandler.text);

            Dictionary <string, News> newsData = null;
            try
            {
                newsData = JsonConvert.DeserializeObject <Dictionary <string, News> >(request.downloadHandler.text);
            } catch (Exception)
            {
                Debug.LogWarning("warning: Cannot deserialize news.");
            }

            if (newsData != null)
            {
                m_CachedNews = new List <News>();
                foreach (News n in newsData.Values)
                {
                    m_CachedNews.Add(n);
                }
                m_CachedNews.Sort((t1, t2) => { return(MessageManager.ParseTimeStamp(t2.createdAt).CompareTo(MessageManager.ParseTimeStamp(t1.createdAt))); });

                if (success != null)
                {
                    success(m_CachedNews);
                }
            }
        }
    }
Ejemplo n.º 15
0
    private IEnumerator TryHackCoroutine(string target, HackSuccessfulCallback success, HackFailedCallback fail, NoConnectionCallback noconnection)
    {
        WWWForm form = new WWWForm();

        form.AddField("targetId", target);
        form.AddField("terminalId", m_TerminalName);

        UnityWebRequest request = UnityWebRequest.Post(Constants.serverAddress + "/api/hack/intiate", form);

        SetCurrentUserAuthorization(request);
        request.chunkedTransfer = false;

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot hack: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 400)
            {
                Debug.Log("Http error: Missing data in hack: " + request.error + ", Code = " + request.responseCode);
            }
            else if (request.responseCode == 403)
            {
                Debug.Log("Http error: Not allowed to hack: " + request.error + ", Code = " + request.responseCode);
            }
            else if (request.responseCode == 404)
            {
                Debug.Log("Http error: Hack target does not exist: " + request.error + ", Code = " + request.responseCode);
            }
            else if (request.responseCode == 500)
            {
                Debug.Log("Http error: Internal database error: " + request.error + ", Code = " + request.responseCode);
            }
            if (fail != null)
            {
                fail();
            }
        }
        else
        {
            Duration duration = default(Duration);
            try
            {
                duration = JsonConvert.DeserializeObject <Duration>(request.downloadHandler.text);
            } catch (Exception)
            {
                Debug.LogError("Error: Cannot deserialize hacking duration");
            }

            if (success != null)
            {
                success(duration.hackingDuration);
            }
        }
    }
Ejemplo n.º 16
0
    public void Hack(string target, HackSuccessfulCallback success, HackFailedCallback fail, NoConnectionCallback noconnection)
    {
        // Uncomment this if a finalize hack call should be sent after hacking each user and not just the session.
        //EndHacking();

        StartCoroutine(TryHackCoroutine(target, success, fail, noconnection));
    }
Ejemplo n.º 17
0
    public IEnumerator EditNewsCoroutine(string topic, string message, string editId, NewsSentCallback success, NoConnectionCallback noconnection)
    {
        UpdatedNews un = new UpdatedNews
        {
            title    = topic,
            newsBody = message
        };
        string          json    = JsonConvert.SerializeObject(un);
        UnityWebRequest request = UnityWebRequest.Put(Constants.serverAddress + "api/news/" + editId, Encoding.UTF8.GetBytes(json));

        request.SetRequestHeader("Content-Type", "application/json");
        m_UserManager.SetCurrentUserAuthorization(request);

        yield return(request.SendWebRequest());

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

        if (request.isNetworkError)
        {
            Debug.Log("Network error: Cannot edit news: " + request.error + ", Code = " + request.responseCode);
            if (noconnection != null)
            {
                noconnection();
            }
        }
        else if (request.isHttpError)
        {
            if (request.responseCode == 400)
            {
                Debug.Log("Http error: News data missing: " + request.error + ", Code = " + request.responseCode + " " + request.downloadHandler.text);
            }
            else if (request.responseCode == 500)
            {
                Debug.Log("Http error: Database search failed: " + request.error + ", Code = " + request.responseCode);
            }
        }
        else if (request.responseCode == 200)
        {
            if (success != null)
            {
                success();
            }
        }
    }