Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

641 lines
16 KiB

  1. namespace Oculus.Platform.Samples.SimplePlatformSample
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using Oculus.Platform;
  9. using Oculus.Platform.Models;
  10. public class DataEntry : MonoBehaviour
  11. {
  12. public Text dataOutput;
  13. void Start()
  14. {
  15. Core.Initialize();
  16. checkEntitlement();
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. string currentText = GetComponent<InputField>().text;
  22. if (Input.GetKey(KeyCode.Return))
  23. {
  24. if (currentText != "")
  25. {
  26. SubmitCommand(currentText);
  27. }
  28. GetComponent<InputField>().text = "";
  29. }
  30. // Handle all messages being returned
  31. Request.RunCallbacks();
  32. }
  33. private void SubmitCommand(string command)
  34. {
  35. string[] commandParams = command.Split(' ');
  36. if (commandParams.Length > 0)
  37. {
  38. switch (commandParams[0])
  39. {
  40. case "p":
  41. if (commandParams.Length > 2)
  42. {
  43. createAndJoinPrivateRoom(commandParams[1], commandParams[2]);
  44. }
  45. break;
  46. case "c":
  47. getCurrentRoom();
  48. break;
  49. case "g":
  50. if (commandParams.Length > 1)
  51. {
  52. getRoom(commandParams[1]);
  53. }
  54. break;
  55. case "j":
  56. if (commandParams.Length > 1)
  57. {
  58. joinRoom(commandParams[1]);
  59. }
  60. break;
  61. case "l":
  62. if (commandParams.Length > 1)
  63. {
  64. leaveRoom(commandParams[1]);
  65. }
  66. break;
  67. case "k":
  68. if (commandParams.Length > 2)
  69. {
  70. kickUser(commandParams[1], commandParams[2]);
  71. }
  72. break;
  73. case "m":
  74. getLoggedInUser();
  75. break;
  76. case "u":
  77. if (commandParams.Length > 1)
  78. {
  79. getUser(commandParams[1]);
  80. }
  81. break;
  82. case "d":
  83. getLoggedInFriends();
  84. break;
  85. case "i":
  86. getInvitableUsers();
  87. break;
  88. case "o":
  89. if (commandParams.Length > 2)
  90. {
  91. inviteUser(commandParams[1], commandParams[2]);
  92. }
  93. break;
  94. case "s":
  95. if (commandParams.Length > 2)
  96. {
  97. setRoomDescription(commandParams[1], commandParams[2]);
  98. }
  99. break;
  100. case "w":
  101. if (commandParams.Length > 3)
  102. {
  103. updateRoomDataStore(commandParams[1], commandParams[2], commandParams[3]);
  104. }
  105. break;
  106. case "n":
  107. getUserNonce();
  108. break;
  109. case "e":
  110. checkEntitlement();
  111. break;
  112. case "a":
  113. if (commandParams.Length > 1)
  114. {
  115. getAchievementDefinition(commandParams[1]);
  116. }
  117. break;
  118. case "b":
  119. if (commandParams.Length > 1)
  120. {
  121. getAchievementProgress(commandParams[1]);
  122. }
  123. break;
  124. case "3":
  125. if (commandParams.Length > 1)
  126. {
  127. unlockAchievement(commandParams[1]);
  128. }
  129. break;
  130. case "4":
  131. if (commandParams.Length > 2)
  132. {
  133. addCountAchievement(commandParams[1], commandParams[2]);
  134. }
  135. break;
  136. case "5":
  137. if (commandParams.Length > 2)
  138. {
  139. addFieldsAchievement(commandParams[1], commandParams[2]);
  140. }
  141. break;
  142. case "1":
  143. if (commandParams.Length > 2)
  144. {
  145. writeLeaderboardEntry(commandParams[1], commandParams[2]);
  146. }
  147. break;
  148. case "2":
  149. if (commandParams.Length > 1)
  150. {
  151. getLeaderboardEntries(commandParams[1]);
  152. }
  153. break;
  154. default:
  155. printOutputLine("Invalid Command");
  156. break;
  157. }
  158. }
  159. }
  160. void getLeaderboardEntries(string leaderboardName)
  161. {
  162. Leaderboards.GetEntries(leaderboardName, 10, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(leaderboardGetCallback);
  163. }
  164. void writeLeaderboardEntry(string leaderboardName, string value)
  165. {
  166. byte[] extraData = new byte[] { 0x54, 0x65, 0x73, 0x74 };
  167. Leaderboards.WriteEntry(leaderboardName, Convert.ToInt32(value), extraData, false).OnComplete(leaderboardWriteCallback);
  168. }
  169. void addFieldsAchievement(string achievementName, string fields)
  170. {
  171. Achievements.AddFields(achievementName, fields).OnComplete(achievementFieldsCallback);
  172. }
  173. void addCountAchievement(string achievementName, string count)
  174. {
  175. Achievements.AddCount(achievementName, Convert.ToUInt64(count)).OnComplete(achievementCountCallback);
  176. }
  177. void unlockAchievement(string achievementName)
  178. {
  179. Achievements.Unlock(achievementName).OnComplete(achievementUnlockCallback);
  180. }
  181. void getAchievementProgress(string achievementName)
  182. {
  183. string[] Names = new string[1];
  184. Names[0] = achievementName;
  185. Achievements.GetProgressByName(Names).OnComplete(achievementProgressCallback);
  186. }
  187. void getAchievementDefinition(string achievementName)
  188. {
  189. string[] Names = new string[1];
  190. Names[0] = achievementName;
  191. Achievements.GetDefinitionsByName(Names).OnComplete(achievementDefinitionCallback);
  192. }
  193. void checkEntitlement()
  194. {
  195. Entitlements.IsUserEntitledToApplication().OnComplete(getEntitlementCallback);
  196. }
  197. void getUserNonce()
  198. {
  199. printOutputLine("Trying to get user nonce");
  200. Users.GetUserProof().OnComplete(userProofCallback);
  201. }
  202. void createAndJoinPrivateRoom(string joinPolicy, string maxUsers)
  203. {
  204. printOutputLine("Trying to create and join private room");
  205. Rooms.CreateAndJoinPrivate((RoomJoinPolicy)Convert.ToUInt32(joinPolicy), Convert.ToUInt32(maxUsers)).OnComplete(createAndJoinPrivateRoomCallback);
  206. }
  207. void getCurrentRoom()
  208. {
  209. printOutputLine("Trying to get current room");
  210. Rooms.GetCurrent().OnComplete(getCurrentRoomCallback);
  211. }
  212. void getRoom(string roomID)
  213. {
  214. printOutputLine("Trying to get room " + roomID);
  215. Rooms.Get(Convert.ToUInt64(roomID)).OnComplete(getCurrentRoomCallback);
  216. }
  217. void joinRoom(string roomID)
  218. {
  219. printOutputLine("Trying to join room " + roomID);
  220. Rooms.Join(Convert.ToUInt64(roomID), true).OnComplete(joinRoomCallback);
  221. }
  222. void leaveRoom(string roomID)
  223. {
  224. printOutputLine("Trying to leave room " + roomID);
  225. Rooms.Leave(Convert.ToUInt64(roomID)).OnComplete(leaveRoomCallback);
  226. }
  227. void kickUser(string roomID, string userID)
  228. {
  229. printOutputLine("Trying to kick user " + userID + " from room " + roomID);
  230. Rooms.KickUser(Convert.ToUInt64(roomID), Convert.ToUInt64(userID), 10 /*kick duration */).OnComplete(getCurrentRoomCallback);
  231. }
  232. void getLoggedInUser()
  233. {
  234. printOutputLine("Trying to get currently logged in user");
  235. Users.GetLoggedInUser().OnComplete(getUserCallback);
  236. }
  237. void getUser(string userID)
  238. {
  239. printOutputLine("Trying to get user " + userID);
  240. Users.Get(Convert.ToUInt64(userID)).OnComplete(getUserCallback);
  241. }
  242. void getLoggedInFriends()
  243. {
  244. printOutputLine("Trying to get friends of logged in user");
  245. Users.GetLoggedInUserFriends().OnComplete(getFriendsCallback);
  246. }
  247. void getInvitableUsers()
  248. {
  249. printOutputLine("Trying to get invitable users");
  250. Rooms.GetInvitableUsers().OnComplete(getInvitableUsersCallback);
  251. }
  252. void inviteUser(string roomID, string inviteToken)
  253. {
  254. printOutputLine("Trying to invite token " + inviteToken + " to room " + roomID);
  255. Rooms.InviteUser(Convert.ToUInt64(roomID), inviteToken).OnComplete(inviteUserCallback);
  256. }
  257. void setRoomDescription(string roomID, string description)
  258. {
  259. printOutputLine("Trying to set description " + description + " to room " + roomID);
  260. Rooms.SetDescription(Convert.ToUInt64(roomID), description).OnComplete(getCurrentRoomCallback);
  261. }
  262. void updateRoomDataStore(string roomID, string key, string value)
  263. {
  264. Dictionary<string, string> kvPairs = new Dictionary<string, string>();
  265. kvPairs.Add(key, value);
  266. printOutputLine("Trying to set k=" + key + " v=" + value + " for room " + roomID);
  267. Rooms.UpdateDataStore(Convert.ToUInt64(roomID), kvPairs).OnComplete(getCurrentRoomCallback);
  268. }
  269. void printOutputLine(String newLine)
  270. {
  271. dataOutput.text = "> " + newLine + System.Environment.NewLine + dataOutput.text;
  272. }
  273. void outputRoomDetails(Room room)
  274. {
  275. printOutputLine("Room ID: " + room.ID + ", AppID: " + room.ApplicationID + ", Description: " + room.Description);
  276. int numUsers = (room.UsersOptional != null) ? room.UsersOptional.Count : 0;
  277. printOutputLine("MaxUsers: " + room.MaxUsers.ToString() + " Users in room: " + numUsers);
  278. if (room.OwnerOptional != null)
  279. {
  280. printOutputLine("Room owner: " + room.OwnerOptional.ID + " " + room.OwnerOptional.OculusID);
  281. }
  282. printOutputLine("Join Policy: " + room.JoinPolicy.ToString());
  283. printOutputLine("Room Type: " + room.Type.ToString());
  284. Message.MessageType.Matchmaking_Enqueue.GetHashCode();
  285. }
  286. void outputUserArray(UserList users)
  287. {
  288. foreach (User user in users)
  289. {
  290. printOutputLine("User: " + user.ID + " " + user.OculusID + " " + user.Presence + " " + user.InviteToken);
  291. }
  292. }
  293. // Callbacks
  294. void userProofCallback(Message<UserProof> msg)
  295. {
  296. if (!msg.IsError)
  297. {
  298. printOutputLine("Received user nonce generation success");
  299. UserProof userNonce = msg.Data;
  300. printOutputLine("Nonce: " + userNonce.Value);
  301. }
  302. else
  303. {
  304. printOutputLine("Received user nonce generation error");
  305. Error error = msg.GetError();
  306. printOutputLine("Error: " + error.Message);
  307. }
  308. }
  309. void getEntitlementCallback(Message msg)
  310. {
  311. if (!msg.IsError)
  312. {
  313. printOutputLine("You are entitled to use this app.");
  314. }
  315. else
  316. {
  317. printOutputLine("You are NOT entitled to use this app.");
  318. }
  319. }
  320. void leaderboardGetCallback(Message<LeaderboardEntryList> msg)
  321. {
  322. if (!msg.IsError)
  323. {
  324. printOutputLine("Leaderboard entry get success.");
  325. var entries = msg.Data;
  326. foreach (var entry in entries)
  327. {
  328. printOutputLine(entry.Rank + ". " + entry.User.OculusID + " " + entry.Score + " " + entry.Timestamp);
  329. }
  330. }
  331. else
  332. {
  333. printOutputLine("Received leaderboard get error");
  334. Error error = msg.GetError();
  335. printOutputLine("Error: " + error.Message);
  336. }
  337. }
  338. void leaderboardWriteCallback(Message msg)
  339. {
  340. if (!msg.IsError)
  341. {
  342. printOutputLine("Leaderboard entry write success.");
  343. var didUpdate = (Message<bool>)msg;
  344. if (didUpdate.Data)
  345. {
  346. printOutputLine("Score updated.");
  347. }
  348. else
  349. {
  350. printOutputLine("Score NOT updated.");
  351. }
  352. }
  353. else
  354. {
  355. printOutputLine("Received leaderboard write error");
  356. Error error = msg.GetError();
  357. printOutputLine("Error: " + error.Message);
  358. }
  359. }
  360. void achievementFieldsCallback(Message msg)
  361. {
  362. if (!msg.IsError)
  363. {
  364. printOutputLine("Achievement fields added.");
  365. }
  366. else
  367. {
  368. printOutputLine("Received achievement fields add error");
  369. Error error = msg.GetError();
  370. printOutputLine("Error: " + error.Message);
  371. }
  372. }
  373. void achievementCountCallback(Message msg)
  374. {
  375. if (!msg.IsError)
  376. {
  377. printOutputLine("Achievement count added.");
  378. }
  379. else
  380. {
  381. printOutputLine("Received achievement count add error");
  382. Error error = msg.GetError();
  383. printOutputLine("Error: " + error.Message);
  384. }
  385. }
  386. void achievementUnlockCallback(Message msg)
  387. {
  388. if (!msg.IsError)
  389. {
  390. printOutputLine("Achievement unlocked");
  391. }
  392. else
  393. {
  394. printOutputLine("Received achievement unlock error");
  395. Error error = msg.GetError();
  396. printOutputLine("Error: " + error.Message);
  397. }
  398. }
  399. void achievementProgressCallback(Message<AchievementProgressList> msg)
  400. {
  401. if (!msg.IsError)
  402. {
  403. printOutputLine("Received achievement progress success");
  404. AchievementProgressList progressList = msg.GetAchievementProgressList();
  405. foreach (var progress in progressList)
  406. {
  407. if (progress.IsUnlocked)
  408. {
  409. printOutputLine("Achievement Unlocked");
  410. }
  411. else
  412. {
  413. printOutputLine("Achievement Locked");
  414. }
  415. printOutputLine("Current Bitfield: " + progress.Bitfield.ToString());
  416. printOutputLine("Current Count: " + progress.Count.ToString());
  417. }
  418. }
  419. else
  420. {
  421. printOutputLine("Received achievement progress error");
  422. Error error = msg.GetError();
  423. printOutputLine("Error: " + error.Message);
  424. }
  425. }
  426. void achievementDefinitionCallback(Message<AchievementDefinitionList> msg)
  427. {
  428. if (!msg.IsError)
  429. {
  430. printOutputLine("Received achievement definitions success");
  431. AchievementDefinitionList definitionList = msg.GetAchievementDefinitions();
  432. foreach (var definition in definitionList)
  433. {
  434. switch (definition.Type)
  435. {
  436. case AchievementType.Simple:
  437. printOutputLine("Achievement Type: Simple");
  438. break;
  439. case AchievementType.Bitfield:
  440. printOutputLine("Achievement Type: Bitfield");
  441. printOutputLine("Bitfield Length: " + definition.BitfieldLength.ToString());
  442. printOutputLine("Target: " + definition.Target.ToString());
  443. break;
  444. case AchievementType.Count:
  445. printOutputLine("Achievement Type: Count");
  446. printOutputLine("Target: " + definition.Target.ToString());
  447. break;
  448. case AchievementType.Unknown:
  449. default:
  450. printOutputLine("Achievement Type: Unknown");
  451. break;
  452. }
  453. }
  454. }
  455. else
  456. {
  457. printOutputLine("Received achievement definitions error");
  458. Error error = msg.GetError();
  459. printOutputLine("Error: " + error.Message);
  460. }
  461. }
  462. void createAndJoinPrivateRoomCallback(Message<Room> msg)
  463. {
  464. if (!msg.IsError)
  465. {
  466. printOutputLine("Received create and join room success");
  467. outputRoomDetails(msg.Data);
  468. }
  469. else
  470. {
  471. printOutputLine("Received create and join room error");
  472. Error error = msg.GetError();
  473. printOutputLine("Error: " + error.Message);
  474. }
  475. }
  476. void getCurrentRoomCallback(Message<Room> msg)
  477. {
  478. if (!msg.IsError)
  479. {
  480. printOutputLine("Received get room success");
  481. outputRoomDetails(msg.Data);
  482. }
  483. else
  484. {
  485. printOutputLine("Received get room error");
  486. Error error = msg.GetError();
  487. printOutputLine("Error: " + error.Message);
  488. }
  489. }
  490. void joinRoomCallback(Message<Room> msg)
  491. {
  492. if (!msg.IsError)
  493. {
  494. printOutputLine("Received join room success");
  495. outputRoomDetails(msg.Data);
  496. }
  497. else
  498. {
  499. printOutputLine("Received join room error");
  500. Error error = msg.GetError();
  501. printOutputLine("Error: " + error.Message);
  502. }
  503. }
  504. void leaveRoomCallback(Message<Room> msg)
  505. {
  506. if (!msg.IsError)
  507. {
  508. printOutputLine("Received leave room success");
  509. outputRoomDetails(msg.Data);
  510. }
  511. else
  512. {
  513. printOutputLine("Received leave room error");
  514. Error error = msg.GetError();
  515. printOutputLine("Error: " + error.Message);
  516. }
  517. }
  518. void getUserCallback(Message<User> msg)
  519. {
  520. if (!msg.IsError)
  521. {
  522. printOutputLine("Received get user success");
  523. User user = msg.Data;
  524. printOutputLine("User: " + user.ID + " " + user.OculusID + " " + user.Presence + " " + user.InviteToken);
  525. }
  526. else
  527. {
  528. printOutputLine("Received get user error");
  529. Error error = msg.GetError();
  530. printOutputLine("Error: " + error.Message);
  531. }
  532. }
  533. void getFriendsCallback(Message<UserList> msg)
  534. {
  535. if (!msg.IsError)
  536. {
  537. printOutputLine("Received get friends success");
  538. UserList users = msg.Data;
  539. outputUserArray(users);
  540. }
  541. else
  542. {
  543. printOutputLine("Received get friends error");
  544. Error error = msg.GetError();
  545. printOutputLine("Error: " + error.Message);
  546. }
  547. }
  548. void getInvitableUsersCallback(Message<UserList> msg)
  549. {
  550. if (!msg.IsError)
  551. {
  552. printOutputLine("Received get invitable users success");
  553. UserList users = msg.Data;
  554. outputUserArray(users);
  555. }
  556. else
  557. {
  558. printOutputLine("Received get invitable users error");
  559. Error error = msg.GetError();
  560. printOutputLine("Error: " + error.Message);
  561. }
  562. }
  563. void inviteUserCallback(Message msg)
  564. {
  565. if (!msg.IsError)
  566. {
  567. printOutputLine("Received invite user success");
  568. }
  569. else
  570. {
  571. printOutputLine("Received invite user error");
  572. Error error = msg.GetError();
  573. printOutputLine("Error: " + error.Message);
  574. }
  575. }
  576. }
  577. }