Example #1
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));
    }
Example #2
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);
            }
        }
    }