-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebrequest.cs
98 lines (79 loc) · 2.38 KB
/
webrequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* In this file is the Webrequest done for sending the cubes to the server.
* This is done over POST.
*
*
*/
using UnityEngine;
using System.Collections;
public class webrequest : MonoBehaviour{
void Start(){
;
}
public void build_bahn(int number){
//This function sends the command to build the lager.
string url = "http://192.168.50.236/build_bahn";
WWWForm form = new WWWForm();
form.AddField("number", number);
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
public void send_cubes(){
//This function gets coorindates of the cubes from the scene.
//Then it sends it to the server.
//get ammount of cubes
GameObject[] objs = GameObject.FindGameObjectsWithTag("cuboro-cube");
var ammount_of_cubes = 0;
foreach (GameObject go in objs) {
ammount_of_cubes=ammount_of_cubes+1;
}
//leave if no cubes
if (ammount_of_cubes==0){
return;
}
//Create JSON by hand with string addition
//Start JSON with: [
var json_cubes = "[";
//Loop trough cubes
var i = 0;
foreach (GameObject go in objs) {
//Get cube typ
CubeBehavior CB = (CubeBehavior) go.GetComponent(typeof(CubeBehavior));
int typ = CB.typ;
//count i up
i=i+1;
//Create cube string
var j_x = go.transform.position.x;
var j_z = go.transform.position.y-0.5;
var j_y = go.transform.position.z;
var j_typ = typ;
var j_rot = Mathf.Round(go.transform.rotation.eulerAngles.y);
var json_cube = "{\"x\":"+j_x+", \"y\":"+j_y+", \"z\":"+j_z+", \"typ\":\""+j_typ+"\", \"rot\":"+j_rot+"}";
//Add comma, except the last one
if (i!=ammount_of_cubes){
json_cube = json_cube+",";
}
//String addition
json_cubes=json_cubes+json_cube;
}
//Finish JSON string with ]
json_cubes=json_cubes+"]";
//Send JSON string via POST
string url = "http://192.168.50.236/process";
WWWForm form = new WWWForm();
form.AddField("data", json_cubes);
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www){
//This function actuelly sends the request and check it for errors.
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("RESPONSE: " + www.text);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}