· 6 years ago · Jan 21, 2019, 05:48 PM
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine.Networking;
5using UnityEngine.UI;
6using UnityEngine;
7
8public class API : MonoBehaviour {
9 private const string SfOauthUrl = "https://login.salesforce.com/services/oauth2/token";
10 private const string SfOpportunityUrl = "YOUR SF ORG URL/services/data/v43.0/sobjects/opportunity";
11 private const string ApiKey = API_KEY;
12
13 public GameObject prefab;
14
15 // Use this for initialization
16 void Start()
17 {
18
19 }
20
21 public void GetSfOpportunities()
22 {
23 WWWForm form = new WWWForm();
24 form.AddField("grant_type", "password");
25 form.AddField("client_id", "YOUR SF CLIENT ID");
26 form.AddField("client_secret", "YOUR SF CLIENT SECRET");
27 form.AddField("username", "YOUR USERNAME");
28 form.AddField("password", "YOUR PASSWORD");
29
30 UnityWebRequest req = UnityWebRequest.Post(SfOauthUrl, form);
31
32 StartCoroutine(GetSfOauth(req));
33 }
34
35 IEnumerator GetSfOauth(UnityWebRequest req)
36 {
37 List<OpportunityDto> opportunityDtos = new List<OpportunityDto>();
38
39 yield return req.SendWebRequest();
40
41 if (req.isNetworkError || req.isHttpError)
42 {
43 Debug.Log(req.error);
44 }
45 else
46 {
47 var oauthToken = req.downloadHandler.text.Split('"')[3];
48
49 UnityWebRequest opportunityRequest = UnityWebRequest.Get(SfOpportunityUrl);
50 opportunityRequest.SetRequestHeader("Authorization", "Bearer " + oauthToken);
51
52 yield return opportunityRequest.SendWebRequest();
53
54 if (opportunityRequest.isNetworkError || opportunityRequest.isHttpError)
55 {
56 Debug.Log(opportunityRequest.error);
57 }
58 else
59 {
60 var recentItemsIndex = opportunityRequest.downloadHandler.text.IndexOf("recentItems");
61 var recentItems = opportunityRequest.downloadHandler.text.Remove(0, recentItemsIndex);
62
63 var recentItemObjectList = recentItems.Remove(0, 14);
64
65 List<string> ids = new List<string>();
66
67 while (recentItemObjectList.Contains("Id"))
68 {
69 var idIndex = recentItemObjectList.IndexOf("Id");
70 recentItemObjectList = recentItemObjectList.Substring(idIndex);
71 recentItemObjectList = recentItemObjectList.Remove(0, 5);
72 ids.Add(recentItemObjectList.Substring(0,18));
73 }
74
75 foreach (var id in ids)
76 {
77 UnityWebRequest opportunityDetailsRequest = UnityWebRequest.Get(SfOpportunityUrl + "/" + id);
78 opportunityDetailsRequest.SetRequestHeader("Authorization", "Bearer " + oauthToken);
79
80 yield return opportunityDetailsRequest.SendWebRequest();
81
82 if (opportunityDetailsRequest.isNetworkError || opportunityDetailsRequest.isHttpError)
83 {
84 Debug.Log(opportunityDetailsRequest.error);
85 }
86 else
87 {
88 var opportunityDto = new OpportunityDto();
89 var opportunityText = opportunityDetailsRequest.downloadHandler.text;
90
91 //Get Name
92 var nameIndex = opportunityText.IndexOf("Name");
93 //8 is symbols before actual value
94 opportunityText = opportunityText.Substring(nameIndex + 7);
95 var quoteIndex = opportunityText.IndexOf("\""); ;
96 opportunityDto.Name = opportunityText.Substring(0, quoteIndex);
97
98 //Get Description
99 var descrIndex = opportunityText.IndexOf("Description");
100 //15 is symbols before actual value
101 opportunityText = opportunityText.Substring(descrIndex + 14);
102 quoteIndex = opportunityText.IndexOf("\""); ;
103 opportunityDto.Description = opportunityText.Substring(0, quoteIndex);
104
105 //Get StageName
106 var stgNameIndex = opportunityText.IndexOf("StageName");
107 //13 is symbols before actual value
108 opportunityText = opportunityText.Substring(stgNameIndex + 12);
109 quoteIndex = opportunityText.IndexOf("\""); ;
110 opportunityDto.StageName = opportunityText.Substring(0, quoteIndex);
111
112 //Get Amount
113 var amountIndex = opportunityText.IndexOf("Amount");
114 //13 is symbols before actual value
115 opportunityText = opportunityText.Substring(amountIndex + 8);
116 quoteIndex = opportunityText.IndexOf("\""); ;
117 // -2 to trim the ," since numbers don't have quotes
118 var amount = opportunityText.Substring(0, quoteIndex - 2);
119 opportunityDto.Amount = decimal.Parse(amount);
120
121 opportunityDtos.Add(opportunityDto);
122 }
123 }
124 }
125 }
126 var counter = 1;
127 foreach (var opportunityDto in opportunityDtos)
128 {
129 var x = counter * 2f - 3;
130 Debug.Log(counter);
131 var gameObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
132
133 //-3 so we can fit the platform
134 gameObj.transform.position = new Vector3(x, 1, 2.5f);
135 gameObj.AddComponent<Rigidbody>();
136
137 gameObj.GetComponent<Renderer>().material.color = getColorByStage(opportunityDto.StageName);
138 //Instantiates the Object
139 GameObject tempTextBox = Instantiate(textMesh, gameObj.transform);
140 GameObject tempDescBox = Instantiate(textMesh, gameObj.transform);
141
142 tempTextBox.transform.localPosition = new Vector3(0, 1, 0);
143 tempDescBox.transform.localPosition = new Vector3(0, 0, 0.5f);
144
145 //Grabs the TextMesh component from the game object
146 TextMesh theText = tempTextBox.GetComponent<TextMesh>();
147 theText.text = opportunityDto.Name;
148
149 TextMesh theDescription = tempDescBox.GetComponent<TextMesh>();
150 theDescription.text = opportunityDto.Description;
151
152 for (int i = 1; i < opportunityDto.Amount/100; i++)
153 {
154 var ammountCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
155 ammountCube.AddComponent<Rigidbody>();
156 ammountCube.GetComponent<Renderer>().material.color = getColorByStage(opportunityDto.StageName);
157
158 ammountCube.transform.position = new Vector3(x, 1, 2.5f + i*1.5f);
159 }
160
161 //Sets the text.
162 counter++;
163 }
164 }
165
166 private Color getColorByStage(string stageName)
167 {
168 Color color;
169 switch (stageName)
170 {
171 case "Prospecting":
172 Debug.Log(11);
173 color = new Color(1, 0.2f, 0);
174 break;
175 case "Qualification":
176 Debug.Log(22);
177 color = new Color(1, 0.4f, 0);
178 break;
179 case "Needs Analysis":
180 Debug.Log(33);
181 color = new Color(1, 0.6f, 0);
182 break;
183 case "Value Proposition":
184 Debug.Log(44);
185 color = new Color(1, 0.8f, 0);
186 break;
187 case "Id. Decision Makers":
188 color = new Color(1, 1, 0);
189 break;
190 case "Perception Analysis":
191 color = new Color(0.8f, 1, 0);
192 break;
193 case "Proposal/Price Quote":
194 color = new Color(0.6f, 1, 0);
195 break;
196 case "Negotiation/Review":
197 color = new Color(0.4f, 1, 0);
198 break;
199 case "Closed":
200 color = new Color(0.2f, 1, 0);
201 break;
202 default:
203 color = new Color(0, 0, 0);
204 break;
205 }
206
207 return color;
208 }
209}