Repository navigation
Expand file tree
/
Copy pathInventoryDemo.cs
More file actions
360 lines (314 loc) · 10.4 KB
/
Copy pathInventoryDemo.cs
File metadata and controls
360 lines (314 loc) · 10.4 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using Azure.AppServices;
using RESTClient;
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Net;
using Tacticsoft;
using Prefabs;
using UnityEngine.SceneManagement;
/// <summary>
/// Virtual inventory item used to populate table cell
/// </summary>
public class InventoryItem
{
public string name;
public uint amount;
}
public class InventoryDemo : MonoBehaviour, ITableViewDataSource
{
/// <remarks>
/// Enter your Azure App Service url
/// </remarks>
[Header ("Azure App Service")]
// Azure Mobile App connection strings
[SerializeField]
private string _appUrl = "PASTE_YOUR_APP_URL";
[Header ("User Authentication")]
// Client-side login requires auth token from identity provider.
// NB: Remember to update Azure App Service authentication provider with your app key and secret and SAVE changes!
// Facebook - go to https://developers.facebook.com/tools/accesstoken/ to generate a "User Token".
[Header ("Facebook")]
[SerializeField]
private string _facebookUserToken = "";
// Twitter auth - go to https://apps.twitter.com/ and select "Keys and Access Tokens" to generate a access "Access Token" and "Access Token Secret".
[Header ("Twitter")]
[SerializeField]
private string _twitterAccessToken = "";
[SerializeField]
private string _twitterTokenSecret = "";
// App Service Rest Client
private AppServiceClient _client;
// App Service Table defined using a DataModel
private AppServiceTable<Inventory> _table;
// Data
private Inventory _inventory;
// List of virtual items (inventory)
private List<InventoryItem> _items = new List<InventoryItem> ();
[Header ("UI")]
// TSTableView for displaying list of results
[SerializeField]
private TableView _tableView;
[SerializeField]
private InventoryCell _cellPrefab;
bool HasNewData = false;
// to reload table view when data has changed
[Space (10)]
[SerializeField]
private ModalAlert _modalAlert;
private Message _message;
// to show user's inventory after successful login
bool DidLogin = false;
// Use this for initialization
void Start ()
{
// Create App Service client
_client = new AppServiceClient (_appUrl);
// Get App Service 'Highscores' table
_table = _client.GetTable<Inventory> ("Inventory");
// set TSTableView delegate
_tableView.dataSource = this;
// hide controls until login
CanvasGroup group = GameObject.Find ("UserDataGroup").GetComponent<CanvasGroup> ();
group.alpha = 0;
group.interactable = false;
UpdateUI ();
}
// Update is called once per frame
void Update ()
{
// show controls after login
if (DidLogin) {
CanvasGroup group = GameObject.Find ("UserDataGroup").GetComponent<CanvasGroup> ();
group.alpha = 1;
group.interactable = true;
DidLogin = false;
}
// update editor fields and reload table data
if (HasNewData && _inventory != null) {
InputField strawberries = GameObject.Find ("Input1").GetComponent<InputField> ();
InputField melons = GameObject.Find ("Input2").GetComponent<InputField> ();
InputField lemons = GameObject.Find ("Input3").GetComponent<InputField> ();
InputField medicine = GameObject.Find ("Input4").GetComponent<InputField> ();
//Debug.Log ("strawberries: " + _inventory.strawberries + " melons: " + _inventory.melons + " lemons: " + _inventory.lemons + " medicine: " + _inventory.medicine);
strawberries.text = _inventory.strawberries.ToString ();
melons.text = _inventory.melons.ToString ();
lemons.text = _inventory.lemons.ToString ();
medicine.text = _inventory.medicine.ToString ();
ReloadTableData ();
HasNewData = false;
}
// Display modal where there is a new message
if (_message != null) {
Debug.Log ("Show message:" + _message.message);
_modalAlert.Show (_message.message, _message.title);
_message = null;
}
}
public void Login ()
{
if (!string.IsNullOrEmpty(_facebookUserToken))
{
StartCoroutine (_client.LoginWithFacebook (_facebookUserToken, OnLoginCompleted));
return;
}
if (!string.IsNullOrEmpty(_twitterAccessToken) && !string.IsNullOrEmpty(_twitterTokenSecret))
{
StartCoroutine (_client.LoginWithTwitter (_twitterAccessToken, _twitterTokenSecret, OnLoginCompleted));
return;
}
Debug.LogWarning("Login requires Facebook or Twitter access tokens");
}
private void OnLoginCompleted (IRestResponse<AuthenticatedUser> response)
{
if (!response.IsError) {
Debug.Log ("OnLoginCompleted: " + response.Content + " Status: " + response.StatusCode + " Url:" + response.Url);
Debug.Log ("Authorized UserId: " + _client.User.user.userId);
DidLogin = true;
Load (); // auto load user data
} else {
Debug.LogWarning ("Authorization Error: " + response.StatusCode);
_message = Message.Create ("Login failed", "Error");
}
}
public void Load ()
{
string filterPredicate = string.Format ("userId eq '{0}'", _client.User.user.userId);
TableQuery query = new TableQuery (filterPredicate);
Debug.Log ("Load data for UserId: " + _client.User.user.userId + " query:" + query);
StartCoroutine (_table.Query<Inventory> (query, OnLoadCompleted));
}
private void OnLoadCompleted (IRestResponse<Inventory[]> response)
{
if (!response.IsError) {
Debug.Log ("OnLoadItemsCompleted data: " + response.Content);
Inventory[] results = response.Data;
Debug.Log ("Load results count: " + results.Length);
// no record
if (results.Length == 0) {
_inventory = new Inventory ();
}
if (results.Length >= 1) {
Debug.Log ("inventory result: " + results [0]);
_inventory = results [0];
}
HasNewData = true;
} else {
Debug.LogWarning ("Read Error Status:" + response.StatusCode + " Url: " + response.Url);
}
}
public void Save ()
{
if (_inventory == null) {
Debug.Log ("Error, no inventory");
return;
}
// If new Insert, else Update existing user inventory
if (String.IsNullOrEmpty (_inventory.id)) {
InsertInventory ();
} else {
UpdateInventory ();
}
}
private void InsertInventory ()
{
RecalculateInventoryItems ();
Debug.Log ("Insert:" + _inventory.ToString ());
StartCoroutine (_table.Insert<Inventory> (_inventory, OnInsertCompleted));
}
private void OnInsertCompleted (IRestResponse<Inventory> response)
{
if (!response.IsError && response.StatusCode == HttpStatusCode.Created) {
Debug.Log ("OnInsertItemCompleted: " + response.Data);
Inventory item = response.Data; // if successful the item will have an 'id' property value
_inventory = item;
_message = Message.Create ("Inventory saved", "Inserted"); // show confirmation message
} else {
Debug.LogWarning ("Insert Error Status:" + response.StatusCode + " " + response.ErrorMessage + " Url: " + response.Url);
}
}
private void UpdateInventory ()
{
RecalculateInventoryItems ();
Debug.Log ("Update:" + _inventory.ToString ());
StartCoroutine (_table.Update<Inventory> (_inventory, OnUpdateCompleted));
}
private void OnUpdateCompleted (IRestResponse<Inventory> response)
{
if (!response.IsError) {
Debug.Log ("OnUpdateCompleted: " + response.Content);
_message = Message.Create ("Inventory saved", "Updated"); // show confirmation message
} else {
Debug.LogWarning ("Update Error Status:" + response.StatusCode + " " + response.ErrorMessage + " Url: " + response.Url);
}
}
#region UI
/// <summary>
/// Update inventory using input values
/// </summary>
private void RecalculateInventoryItems ()
{
if (_inventory == null) {
Debug.Log ("Error, no inventory");
}
InputField strawberries = GameObject.Find ("Input1").GetComponent<InputField> ();
InputField melons = GameObject.Find ("Input2").GetComponent<InputField> ();
InputField lemons = GameObject.Find ("Input3").GetComponent<InputField> ();
InputField medicine = GameObject.Find ("Input4").GetComponent<InputField> ();
_inventory.strawberries = Convert.ToUInt32 (strawberries.text);
_inventory.melons = Convert.ToUInt32 (melons.text);
_inventory.lemons = Convert.ToUInt32 (lemons.text);
_inventory.medicine = Convert.ToUInt32 (medicine.text);
}
/// <summary>
/// Handler for text changed event to update view state
/// </summary>
public void TextChanged ()
{
UpdateUI ();
}
/// <summary>
/// Method to manage UI view state
/// </summary>
private void UpdateUI ()
{
// Close dialog if no message
if (_message == null) {
_modalAlert.Close ();
}
}
/// <summary>
/// Reloads table data
/// </summary>
private void ReloadTableData ()
{
// start with new list
_items = new List<InventoryItem> ();
if (_inventory == null) {
return;
}
// Inventory data model properties
string[] properties = { "strawberries", "melons", "lemons", "medicine" };
foreach (string property in properties) {
// Check property exists in data model then check amount value
if (ReflectionHelper.HasField (_inventory, property)) {
var x = ReflectionHelper.GetField (_inventory, property);
Nullable<uint> value = x.GetValue (_inventory) as Nullable<uint>;
uint amount = value ?? 0;
// Only display items with 1 or more
if (amount > 0) {
InventoryItem item = new InventoryItem ();
item.name = property;
item.amount = amount;
// Add to table view list
_items.Add (item);
}
}
}
// reload table data
_tableView.ReloadData ();
}
#endregion
#region ITableViewDataSource
public int GetNumberOfRowsForTableView (TableView tableView)
{
return _items.Count;
}
public float GetHeightForRowInTableView (TableView tableView, int row)
{
return (_cellPrefab.transform as RectTransform).rect.height; //50.0f;
}
public TableViewCell GetCellForRowInTableView (TableView tableView, int row)
{
InventoryCell cell = tableView.GetReusableCell (_cellPrefab.reuseIdentifier) as InventoryCell;
if (cell == null) {
cell = (InventoryCell)GameObject.Instantiate (_cellPrefab);
}
InventoryItem data = _items [row];
cell.Name.text = data.name;
cell.Amount.text = data.amount.ToString ();
cell.Icon.sprite = LoadImage (data.name);
cell.Btn.name = row.ToString (); // save index to button name
return cell;
}
#endregion
public Sprite LoadImage (string filename)
{
Texture2D texture = Resources.Load (filename) as Texture2D;
if (texture == null) {
return null;
}
Rect rect = new Rect (0, 0, texture.width, texture.height);
Vector2 pivot = new Vector2 (0.0f, 0.0f);
return Sprite.Create (texture, rect, pivot);
}
/// <summary>
/// Handler to go to next scene
/// </summary>
public void GoNextScene ()
{
SceneManager.LoadScene ("HighscoresDemo");
}
}