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