Exemplo n.º 1
0
        //Send one shot
        private IEnumerator SendNewShot(ShotInfo shotInfo, Action <ShotInfo> onSuccess)
        {
            GetComponent <OnlineBattleManager>().PrintShotInfo(shotInfo);
            //Construct body of request with WWWForm
            WWWForm form = new WWWForm();

            form.AddField(FIELD2, shotInfo.pawn);
            DebugLog.DebugMessage(FIELD2 + " : " + shotInfo.pawn, true);
            form.AddField(FIELD3, shotInfo.slot_1);
            DebugLog.DebugMessage(FIELD3 + " : " + shotInfo.slot_1, true);
            form.AddField(FIELD4, shotInfo.slot_2);
            DebugLog.DebugMessage(FIELD4 + " : " + shotInfo.slot_2, true);
            for (int i = 0; i < shotInfo.shot_eat.Count; i++)
            {
                form.AddField(FIELD1 + "[" + i + "]", shotInfo.shot_eat[i]);
                DebugLog.DebugMessage(FIELD1 + "[" + i + "] : " + shotInfo.shot_eat[i], true);
            }
            form.AddField(FIELD5, shotInfo.id_game);
            DebugLog.DebugMessage(FIELD5 + " : " + shotInfo.id_game, true);
            form.AddField(FIELD6, shotInfo.id_shot);
            DebugLog.DebugMessage(FIELD6 + " : " + shotInfo.id_shot, true);
            form.AddField(FIELD7, shotInfo.surrender.ToString());
            DebugLog.DebugMessage(FIELD7 + " : " + shotInfo.surrender.ToString(), true);

            using (UnityWebRequest req = UnityWebRequest.Post(String.Format(API_SHOTS_URL), form))
            {
                //Send request
                yield return(req.SendWebRequest());

                //if an error occured
                if (req.isNetworkError || req.isHttpError)
                {
                    DebugLog.DebugMessage(req.error, true);
                    onSuccess(null);
                }
                else
                {
                    //wait until the request is done
                    while (!req.isDone)
                    {
                        yield return(null);
                    }
                    //get byte array result
                    byte[] result = req.downloadHandler.data;

                    //Get JSON result, and Serialize it
                    string shotJson = System.Text.Encoding.Default.GetString(result);
                    DebugLog.DebugMessage(shotJson, true);
                    ShotInfo shot = JsonUtility.FromJson <ShotInfo>(shotJson);

                    //Call success function
                    onSuccess(shot);
                }
            }
        }
Exemplo n.º 2
0
 //public method to post new shot
 public void LaunchPostNewShot(ShotInfo shot)
 {
     //StartCoroutine(SendNewShot(shot, ShareCreatedNewShot));
 }