Example #1
0
        /// <summary>
        /// Send a player action with the given type and parameters.
        /// Then executes the given callback when the response is received.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public static IEnumerator SendAction(string type, object parameters, Action <MatchableResponse> callback)
        {
            if (MatchableSettings.IsPluginEnabled())
            {
                if (type == null)
                {
                    Debug.LogError("SendAction(): parameter 'type' is required");
                    yield return(null);
                }

                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Content-Type", "application/json");
                headers.Add("Authorization", "api_key " + MatchableSettings.GetAppKey());

                Hashtable action = MatchableAction.Create(type, parameters);

                // Simple hack to wrap the action inside a JSON array
                string data = "[" + MJSON.Serialize(action) + "]";
                if (MatchableSettings.IsLoggingEnabled())
                {
                    Debug.Log("Sent action:" + data);
                }
                byte[] postData = AsciiEncoding.StringToAscii(data);

                WWW request = new WWW(MatchableSettings.GetActionsEndpoint(), postData, headers);
                yield return(request);

                MatchableResponse response = new MatchableResponse(request);
                yield return(response);

                callback(response);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MatchableResponse"/> class.
 /// </summary>
 /// <param name="request">The original matchable request.</param>
 public MatchableResponse(WWW request)
 {
     _text = request.text;
     try
     {
         _data = MJSON.Deserialize(_text);
     } catch (OverflowException e) {
         Debug.Log("Trying to deserialize empty json object");
         _data = null;
     }
 }