| 1 | namespace Oculus.Platform.Samples.NetChat |
| 2 | { |
| 3 | using UnityEngine; |
| 4 | using UnityEngine.UI; |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Collections.Generic; |
| 8 | using Oculus.Platform; |
| 9 | using Oculus.Platform.Models; |
| 10 | |
| 11 | enum states |
| 12 | { |
| 13 | NOT_INIT = 0, |
| 14 | IDLE, |
| 15 | REQUEST_FIND, |
| 16 | FINDING_ROOM, |
| 17 | REQUEST_CREATE, |
| 18 | REQUEST_JOIN, |
| 19 | REQUEST_LEAVE, |
| 20 | IN_EMPTY_ROOM, |
| 21 | IN_FULL_ROOM |
| 22 | } |
| 23 | |
| 24 | // Pools are defined on the Oculus developer portal |
| 25 | // |
| 26 | // For this test we have a pool created with the pool key set as 'filter_pool' |
| 27 | // Mode is set to 'Room' |
| 28 | // Skill Pool is set to 'None' |
| 29 | // We are not considering Round Trip Time |
| 30 | // The following Data Settings are set: |
| 31 | // key: map_name, Type: STRING, String options: Small_Map, Big_Map, Really_Big_Map |
| 32 | // key: game_type, Type: STRING, String Options: deathmatch, CTF |
| 33 | // |
| 34 | // We also have the following two queries defined: |
| 35 | // Query Key: map |
| 36 | // Template: Set (String) |
| 37 | // Key: map_name |
| 38 | // Wildcards: map_param_1, map_param_2 |
| 39 | // |
| 40 | // Query Key: game_type |
| 41 | // Template: Set (String) |
| 42 | // Key: game_type_name |
| 43 | // Wildcards: game_type_param |
| 44 | // |
| 45 | // For this test we have a pool created with the pool key set as 'bout_pool' |
| 46 | // Mode is set to 'Bout' |
| 47 | // Skill Pool is set to 'None' |
| 48 | // We are not considering Round Trip Time |
| 49 | // No Data Settings are set: |
| 50 | // |
| 51 | |
| 52 | public static class Constants |
| 53 | { |
| 54 | public const int BUFFER_SIZE = 512; |
| 55 | public const string BOUT_POOL = "bout_pool"; |
| 56 | public const string FILTER_POOL = "filter_pool"; |
| 57 | } |
| 58 | |
| 59 | public class chatPacket |
| 60 | { |
| 61 | public int packetID { get; set; } |
| 62 | public string textString { get; set; } |
| 63 | |
| 64 | public byte[] Serialize() |
| 65 | { |
| 66 | using (MemoryStream m = new MemoryStream()) |
| 67 | { |
| 68 | using (BinaryWriter writer = new BinaryWriter(m)) |
| 69 | { |
| 70 | // Limit our string to BUFFER_SIZE |
| 71 | if (textString.Length > Constants.BUFFER_SIZE) |
| 72 | { |
| 73 | textString = textString.Substring(0, Constants.BUFFER_SIZE-1); |
| 74 | } |
| 75 | writer.Write(packetID); |
| 76 | writer.Write(textString.ToCharArray()); |
| 77 | writer.Write('\0'); |
| 78 | } |
| 79 | return m.ToArray(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public static chatPacket Deserialize(byte[] data) |
| 84 | { |
| 85 | chatPacket result = new chatPacket(); |
| 86 | using (MemoryStream m = new MemoryStream(data)) |
| 87 | { |
| 88 | using (BinaryReader reader = new BinaryReader(m)) |
| 89 | { |
| 90 | result.packetID = reader.ReadInt32(); |
| 91 | result.textString = System.Text.Encoding.Default.GetString(reader.ReadBytes(Constants.BUFFER_SIZE)); |
| 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public class DataEntry : MonoBehaviour { |
| 99 | |
| 100 | public Text dataOutput; |
| 101 | |
| 102 | states currentState; |
| 103 | User localUser; |
| 104 | User remoteUser; |
| 105 | Room currentRoom; |
| 106 | int lastPacketID; |
| 107 | bool ratedMatchStarted; |
| 108 | |
| 109 | // Use this for initialization |
| 110 | void Start () { |
| 111 | currentState = states.NOT_INIT; |
| 112 | localUser = null; |
| 113 | remoteUser = null; |
| 114 | currentRoom = null; |
| 115 | lastPacketID = 0; |
| 116 | ratedMatchStarted = false; |
| 117 | |
| 118 | Core.Initialize(); |
| 119 | |
| 120 | // Setup our room update handler |
| 121 | Rooms.SetUpdateNotificationCallback(updateRoom); |
| 122 | // Setup our match found handler |
| 123 | Matchmaking.SetMatchFoundNotificationCallback(foundMatch); |
| 124 | |
| 125 | checkEntitlement(); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | // Update is called once per frame |
| 130 | void Update() |
| 131 | { |
| 132 | string currentText = GetComponent<InputField>().text; |
| 133 | |
| 134 | if (Input.GetKey(KeyCode.Return)) |
| 135 | { |
| 136 | if (currentText != "") |
| 137 | { |
| 138 | SubmitCommand(currentText); |
| 139 | } |
| 140 | |
| 141 | GetComponent<InputField>().text = ""; |
| 142 | } |
| 143 | |
| 144 | processNetPackets(); |
| 145 | // Handle all messages being returned |
| 146 | Request.RunCallbacks(); |
| 147 | } |
| 148 | |
| 149 | void SubmitCommand(string command) |
| 150 | { |
| 151 | string[] commandParams = command.Split('!'); |
| 152 | |
| 153 | if (commandParams.Length > 0) |
| 154 | { |
| 155 | switch (commandParams[0]) |
| 156 | { |
| 157 | case "c": |
| 158 | requestCreateRoom(); |
| 159 | break; |
| 160 | case "d": |
| 161 | requestCreateFilterRoom(); |
| 162 | break; |
| 163 | case "f": |
| 164 | requestFindMatch(); |
| 165 | break; |
| 166 | case "g": |
| 167 | requestFindRoom(); |
| 168 | break; |
| 169 | case "i": |
| 170 | requestFindFilteredRoom(); |
| 171 | break; |
| 172 | case "s": |
| 173 | if (commandParams.Length > 1) |
| 174 | { |
| 175 | sendChat(commandParams[1]); |
| 176 | } |
| 177 | break; |
| 178 | case "l": |
| 179 | requestLeaveRoom(); |
| 180 | break; |
| 181 | case "1": |
| 182 | requestStartRatedMatch(); |
| 183 | break; |
| 184 | case "2": |
| 185 | requestReportResults(); |
| 186 | break; |
| 187 | default: |
| 188 | printOutputLine("Invalid Command"); |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void printOutputLine(String newLine) |
| 195 | { |
| 196 | dataOutput.text = "> " + newLine + System.Environment.NewLine + dataOutput.text; |
| 197 | } |
| 198 | |
| 199 | void checkEntitlement() |
| 200 | { |
| 201 | Entitlements.IsUserEntitledToApplication().OnComplete(getEntitlementCallback); |
| 202 | } |
| 203 | |
| 204 | void getEntitlementCallback(Message msg) |
| 205 | { |
| 206 | if (!msg.IsError) |
| 207 | { |
| 208 | printOutputLine("You are entitled to use this app."); |
| 209 | Users.GetLoggedInUser().OnComplete(init); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | printOutputLine("You are NOT entitled to use this app."); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void init(Message<User> msg) |
| 218 | { |
| 219 | if (!msg.IsError) |
| 220 | { |
| 221 | User user = msg.Data; |
| 222 | localUser = user; |
| 223 | |
| 224 | currentState = states.IDLE; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | printOutputLine("Received get current user error"); |
| 229 | Error error = msg.GetError(); |
| 230 | printOutputLine("Error: " + error.Message); |
| 231 | |
| 232 | // Retry getting the current user |
| 233 | Users.GetLoggedInUser().OnComplete(init); |
| 234 | currentState = states.NOT_INIT; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void requestCreateRoom() |
| 239 | { |
| 240 | switch (currentState) |
| 241 | { |
| 242 | case states.NOT_INIT: |
| 243 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 244 | break; |
| 245 | case states.IDLE: |
| 246 | printOutputLine("Trying to create a matchmaking room"); |
| 247 | Matchmaking.CreateAndEnqueueRoom(Constants.FILTER_POOL, 8, true, null).OnComplete(createRoomResponse); |
| 248 | currentState = states.REQUEST_CREATE; |
| 249 | break; |
| 250 | case states.REQUEST_FIND: |
| 251 | printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); |
| 252 | break; |
| 253 | case states.FINDING_ROOM: |
| 254 | printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); |
| 255 | break; |
| 256 | case states.REQUEST_JOIN: |
| 257 | printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); |
| 258 | break; |
| 259 | case states.REQUEST_LEAVE: |
| 260 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); |
| 261 | break; |
| 262 | case states.REQUEST_CREATE: |
| 263 | printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); |
| 264 | break; |
| 265 | case states.IN_EMPTY_ROOM: |
| 266 | printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); |
| 267 | break; |
| 268 | case states.IN_FULL_ROOM: |
| 269 | printOutputLine("You have already in a match."); |
| 270 | break; |
| 271 | default: |
| 272 | printOutputLine("You have hit an unknown state."); |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void createRoomResponse(Message<MatchmakingEnqueueResultAndRoom> msg) |
| 278 | { |
| 279 | if (!msg.IsError) |
| 280 | { |
| 281 | printOutputLine("Received create matchmaking room success"); |
| 282 | Room room = msg.Data.Room; |
| 283 | currentRoom = room; |
| 284 | |
| 285 | printOutputLine("RoomID: " + room.ID.ToString()); |
| 286 | currentState = states.IN_EMPTY_ROOM; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | printOutputLine("Received create matchmaking room Error"); |
| 291 | Error error = msg.GetError(); |
| 292 | printOutputLine("Error: " + error.Message); |
| 293 | printOutputLine("You can only create a matchmaking room for pools of mode Room. Make sure you have an appropriate pool setup on the Developer portal.\n"); |
| 294 | currentState = states.IDLE; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void requestCreateFilterRoom() |
| 299 | { |
| 300 | switch (currentState) |
| 301 | { |
| 302 | case states.NOT_INIT: |
| 303 | printOutputLine("The app has not initialized properly and we don't know your userID.\n"); |
| 304 | break; |
| 305 | |
| 306 | case states.IDLE: |
| 307 | printOutputLine("Trying to create a matchmaking room"); |
| 308 | |
| 309 | // We're going to create a room that has the following values set: |
| 310 | // game_type_name = "CTF" |
| 311 | // map_name = "Really_Big_Map" |
| 312 | // |
| 313 | |
| 314 | Matchmaking.CustomQuery roomCustomQuery = new Matchmaking.CustomQuery(); |
| 315 | |
| 316 | roomCustomQuery.criteria = null; |
| 317 | roomCustomQuery.data = new Dictionary<string, object>(); |
| 318 | |
| 319 | roomCustomQuery.data.Add("game_type_name", "CTF"); |
| 320 | roomCustomQuery.data.Add("map_name", "Really_Big_Map"); |
| 321 | |
| 322 | Matchmaking.CreateAndEnqueueRoom(Constants.FILTER_POOL, 8, true, roomCustomQuery).OnComplete(createRoomResponse); |
| 323 | currentState = states.REQUEST_CREATE; |
| 324 | break; |
| 325 | |
| 326 | case states.REQUEST_FIND: |
| 327 | printOutputLine("You have already made a request to find a room. Please wait for that request to complete.\n"); |
| 328 | break; |
| 329 | case states.FINDING_ROOM: |
| 330 | printOutputLine("You have already currently looking for a room. Please wait for the match to be made.\n"); |
| 331 | break; |
| 332 | case states.REQUEST_JOIN: |
| 333 | printOutputLine("We are currently trying to join a room. Please wait to see if we can join it.\n"); |
| 334 | break; |
| 335 | case states.REQUEST_LEAVE: |
| 336 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it.\n"); |
| 337 | break; |
| 338 | case states.REQUEST_CREATE: |
| 339 | printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made.\n"); |
| 340 | break; |
| 341 | case states.IN_EMPTY_ROOM: |
| 342 | printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join.\n"); |
| 343 | break; |
| 344 | case states.IN_FULL_ROOM: |
| 345 | printOutputLine("You have already in a match.\n"); |
| 346 | break; |
| 347 | default: |
| 348 | printOutputLine("You have hit an unknown state.\n"); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void requestFindRoom() |
| 354 | { |
| 355 | switch (currentState) |
| 356 | { |
| 357 | case states.NOT_INIT: |
| 358 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 359 | break; |
| 360 | |
| 361 | case states.IDLE: |
| 362 | printOutputLine("\nTrying to find a matchmaking room\n"); |
| 363 | |
| 364 | Matchmaking.Enqueue(Constants.FILTER_POOL, null).OnComplete(searchingStarted); |
| 365 | currentState = states.REQUEST_FIND; |
| 366 | break; |
| 367 | |
| 368 | case states.REQUEST_FIND: |
| 369 | printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); |
| 370 | break; |
| 371 | |
| 372 | case states.FINDING_ROOM: |
| 373 | printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); |
| 374 | break; |
| 375 | |
| 376 | case states.REQUEST_JOIN: |
| 377 | printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); |
| 378 | break; |
| 379 | |
| 380 | case states.REQUEST_LEAVE: |
| 381 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); |
| 382 | break; |
| 383 | |
| 384 | case states.REQUEST_CREATE: |
| 385 | printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); |
| 386 | break; |
| 387 | |
| 388 | case states.IN_EMPTY_ROOM: |
| 389 | printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); |
| 390 | break; |
| 391 | |
| 392 | case states.IN_FULL_ROOM: |
| 393 | printOutputLine("You have already in a match."); |
| 394 | break; |
| 395 | |
| 396 | default: |
| 397 | printOutputLine("You have hit an unknown state."); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void requestFindFilteredRoom() |
| 403 | { |
| 404 | switch (currentState) |
| 405 | { |
| 406 | case states.NOT_INIT: |
| 407 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 408 | break; |
| 409 | |
| 410 | case states.IDLE: |
| 411 | printOutputLine("Trying to find a matchmaking room"); |
| 412 | |
| 413 | // Our search filter criterion |
| 414 | // |
| 415 | // We're filtering using two different queries setup on the developer portal |
| 416 | // |
| 417 | // map - query to filter by map. The query allows you to filter with up to two different maps using keys called 'map_1' and 'map_2' |
| 418 | // game_type - query to filter by game type. The query allows you to filter with up to two different game types using keys called 'type_1' and 'type_2' |
| 419 | // |
| 420 | // In the example below we are filtering for matches that are of type CTF and on either Big_Map or Really_Big_Map. |
| 421 | // |
| 422 | |
| 423 | Matchmaking.CustomQuery roomCustomQuery = new Matchmaking.CustomQuery(); |
| 424 | Matchmaking.CustomQuery.Criterion[] queries = new Matchmaking.CustomQuery.Criterion[2]; |
| 425 | |
| 426 | queries[0].key = "map"; |
| 427 | queries[0].importance = MatchmakingCriterionImportance.Required; |
| 428 | queries[0].parameters = new Dictionary<string, object>(); |
| 429 | queries[0].parameters.Add("map_param_1","Really_Big_Map"); |
| 430 | queries[0].parameters.Add("map_param_2", "Big_Map"); |
| 431 | |
| 432 | queries[1].key = "game_type"; |
| 433 | queries[1].importance = MatchmakingCriterionImportance.Required; |
| 434 | queries[1].parameters = new Dictionary<string, object>(); |
| 435 | queries[1].parameters.Add("game_type_param", "CTF"); |
| 436 | |
| 437 | roomCustomQuery.criteria = queries; |
| 438 | roomCustomQuery.data = null; |
| 439 | |
| 440 | Matchmaking.Enqueue(Constants.FILTER_POOL, roomCustomQuery); |
| 441 | currentState = states.REQUEST_FIND; |
| 442 | break; |
| 443 | |
| 444 | case states.REQUEST_FIND: |
| 445 | printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); |
| 446 | break; |
| 447 | |
| 448 | case states.FINDING_ROOM: |
| 449 | printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); |
| 450 | break; |
| 451 | |
| 452 | case states.REQUEST_JOIN: |
| 453 | printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); |
| 454 | break; |
| 455 | |
| 456 | case states.REQUEST_LEAVE: |
| 457 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); |
| 458 | break; |
| 459 | |
| 460 | case states.REQUEST_CREATE: |
| 461 | printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); |
| 462 | break; |
| 463 | |
| 464 | case states.IN_EMPTY_ROOM: |
| 465 | printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); |
| 466 | break; |
| 467 | |
| 468 | case states.IN_FULL_ROOM: |
| 469 | printOutputLine("You have already in a match."); |
| 470 | break; |
| 471 | |
| 472 | default: |
| 473 | printOutputLine("You have hit an unknown state."); |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void foundMatch(Message<Room> msg) |
| 479 | { |
| 480 | if (!msg.IsError) |
| 481 | { |
| 482 | printOutputLine("Received find match success. We are now going to request to join the room."); |
| 483 | Room room = msg.Data; |
| 484 | |
| 485 | Rooms.Join(room.ID, true).OnComplete(joinRoomResponse); |
| 486 | currentState = states.REQUEST_JOIN; |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | printOutputLine("Received find match error"); |
| 491 | Error error = msg.GetError(); |
| 492 | printOutputLine("Error: " + error.Message); |
| 493 | currentState = states.IDLE; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void joinRoomResponse(Message<Room> msg) |
| 498 | { |
| 499 | if (!msg.IsError) |
| 500 | { |
| 501 | printOutputLine("Received join room success."); |
| 502 | currentRoom = msg.Data; |
| 503 | |
| 504 | currentState = states.IN_EMPTY_ROOM; |
| 505 | |
| 506 | // Try to pull out remote user's ID if they have already joined |
| 507 | if (currentRoom.UsersOptional != null) |
| 508 | { |
| 509 | foreach (User element in currentRoom.UsersOptional) |
| 510 | { |
| 511 | if (element.ID != localUser.ID) |
| 512 | { |
| 513 | remoteUser = element; |
| 514 | currentState = states.IN_FULL_ROOM; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | printOutputLine("Received join room error"); |
| 522 | printOutputLine("It's possible the room filled up before you could join it."); |
| 523 | |
| 524 | Error error = msg.GetError(); |
| 525 | printOutputLine("Error: " + error.Message); |
| 526 | currentState = states.IDLE; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void requestFindMatch() |
| 531 | { |
| 532 | switch (currentState) |
| 533 | { |
| 534 | case states.NOT_INIT: |
| 535 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 536 | break; |
| 537 | case states.IDLE: |
| 538 | printOutputLine("Trying to find a matchmaking room"); |
| 539 | Matchmaking.Enqueue(Constants.BOUT_POOL, null).OnComplete(searchingStarted); |
| 540 | currentState = states.REQUEST_FIND; |
| 541 | break; |
| 542 | case states.REQUEST_FIND: |
| 543 | printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); |
| 544 | break; |
| 545 | case states.FINDING_ROOM: |
| 546 | printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); |
| 547 | break; |
| 548 | case states.REQUEST_JOIN: |
| 549 | printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); |
| 550 | break; |
| 551 | case states.REQUEST_LEAVE: |
| 552 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); |
| 553 | break; |
| 554 | case states.REQUEST_CREATE: |
| 555 | printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); |
| 556 | break; |
| 557 | case states.IN_EMPTY_ROOM: |
| 558 | printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); |
| 559 | break; |
| 560 | case states.IN_FULL_ROOM: |
| 561 | printOutputLine("You have already in a match."); |
| 562 | break; |
| 563 | default: |
| 564 | printOutputLine("You have hit an unknown state."); |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void searchingStarted(Message msg) |
| 570 | { |
| 571 | if (!msg.IsError) |
| 572 | { |
| 573 | printOutputLine("Searching for a match successfully started"); |
| 574 | currentState = states.REQUEST_FIND; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | printOutputLine("Searching for a match error"); |
| 579 | |
| 580 | Error error = msg.GetError(); |
| 581 | printOutputLine("Error: " + error.Message); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | void updateRoom(Message<Room> msg) |
| 586 | { |
| 587 | if (!msg.IsError) |
| 588 | { |
| 589 | printOutputLine("Received room update notification"); |
| 590 | Room room = msg.Data; |
| 591 | |
| 592 | if (currentState == states.IN_EMPTY_ROOM) |
| 593 | { |
| 594 | // Check to see if this update is another user joining |
| 595 | if (room.UsersOptional != null) |
| 596 | { |
| 597 | foreach (User element in room.UsersOptional) |
| 598 | { |
| 599 | if (element.ID != localUser.ID) |
| 600 | { |
| 601 | remoteUser = element; |
| 602 | currentState = states.IN_FULL_ROOM; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | // Check to see if this update is another user leaving |
| 610 | if (room.UsersOptional != null && room.UsersOptional.Count == 1) |
| 611 | { |
| 612 | printOutputLine("User ID: " + remoteUser.ID.ToString() + "has left"); |
| 613 | remoteUser = null; |
| 614 | currentState = states.IN_EMPTY_ROOM; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | printOutputLine("Received room update error"); |
| 621 | |
| 622 | Error error = msg.GetError(); |
| 623 | printOutputLine("Error: " + error.Message); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void sendChat(string chatMessage) |
| 628 | { |
| 629 | switch (currentState) |
| 630 | { |
| 631 | case states.NOT_INIT: |
| 632 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 633 | break; |
| 634 | |
| 635 | case states.IDLE: |
| 636 | case states.REQUEST_FIND: |
| 637 | case states.FINDING_ROOM: |
| 638 | case states.REQUEST_JOIN: |
| 639 | case states.REQUEST_CREATE: |
| 640 | case states.REQUEST_LEAVE: |
| 641 | case states.IN_EMPTY_ROOM: |
| 642 | printOutputLine("You need to be in a room with another player to send a message."); |
| 643 | break; |
| 644 | |
| 645 | case states.IN_FULL_ROOM: |
| 646 | { |
| 647 | chatPacket newMessage = new chatPacket(); |
| 648 | |
| 649 | // Create a packet to send with the packet ID and string payload |
| 650 | lastPacketID++; |
| 651 | newMessage.packetID = lastPacketID; |
| 652 | newMessage.textString = chatMessage; |
| 653 | |
| 654 | Oculus.Platform.Net.SendPacket(remoteUser.ID, newMessage.Serialize(), SendPolicy.Reliable); |
| 655 | } |
| 656 | break; |
| 657 | |
| 658 | default: |
| 659 | printOutputLine("You have hit an unknown state."); |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | void processNetPackets() |
| 665 | { |
| 666 | Packet incomingPacket = Net.ReadPacket(); |
| 667 | |
| 668 | while (incomingPacket != null) |
| 669 | { |
| 670 | byte[] rawBits = new byte[incomingPacket.Size]; |
| 671 | incomingPacket.ReadBytes(rawBits); |
| 672 | |
| 673 | chatPacket newMessage = chatPacket.Deserialize(rawBits); |
| 674 | |
| 675 | printOutputLine("Chat Text: " + newMessage.textString.ToString()); |
| 676 | printOutputLine("Received Packet from UserID: " + incomingPacket.SenderID.ToString()); |
| 677 | printOutputLine("Received Packet ID: " + newMessage.packetID.ToString()); |
| 678 | |
| 679 | // Look to see if there's another packet waiting |
| 680 | incomingPacket = Net.ReadPacket(); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | void requestLeaveRoom() |
| 685 | { |
| 686 | switch (currentState) |
| 687 | { |
| 688 | case states.NOT_INIT: |
| 689 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 690 | break; |
| 691 | |
| 692 | case states.IDLE: |
| 693 | case states.REQUEST_FIND: |
| 694 | case states.FINDING_ROOM: |
| 695 | case states.REQUEST_JOIN: |
| 696 | case states.REQUEST_CREATE: |
| 697 | printOutputLine("You are currently not in a room to leave."); |
| 698 | break; |
| 699 | |
| 700 | case states.REQUEST_LEAVE: |
| 701 | printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); |
| 702 | break; |
| 703 | |
| 704 | case states.IN_EMPTY_ROOM: |
| 705 | case states.IN_FULL_ROOM: |
| 706 | printOutputLine("Trying to leave room."); |
| 707 | Rooms.Leave(currentRoom.ID).OnComplete(leaveRoomResponse); |
| 708 | break; |
| 709 | |
| 710 | default: |
| 711 | printOutputLine("You have hit an unknown state."); |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void leaveRoomResponse(Message<Room> msg) |
| 717 | { |
| 718 | if (!msg.IsError) |
| 719 | { |
| 720 | printOutputLine("We were able to leave the room"); |
| 721 | currentRoom = null; |
| 722 | remoteUser = null; |
| 723 | currentState = states.IDLE; |
| 724 | } |
| 725 | else |
| 726 | { |
| 727 | printOutputLine("Leave room error"); |
| 728 | |
| 729 | Error error = msg.GetError(); |
| 730 | printOutputLine("Error: " + error.Message); |
| 731 | } |
| 732 | |
| 733 | } |
| 734 | |
| 735 | void requestStartRatedMatch() |
| 736 | { |
| 737 | switch (currentState) |
| 738 | { |
| 739 | case states.NOT_INIT: |
| 740 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 741 | break; |
| 742 | |
| 743 | case states.IDLE: |
| 744 | case states.REQUEST_FIND: |
| 745 | case states.FINDING_ROOM: |
| 746 | case states.REQUEST_JOIN: |
| 747 | case states.REQUEST_CREATE: |
| 748 | case states.REQUEST_LEAVE: |
| 749 | case states.IN_EMPTY_ROOM: |
| 750 | printOutputLine("You need to be in a room with another player to start a rated match."); |
| 751 | break; |
| 752 | |
| 753 | case states.IN_FULL_ROOM: |
| 754 | printOutputLine("Trying to start a rated match. This call should be made once a rated match begins so we will be able to submit results after the game is done."); |
| 755 | |
| 756 | Matchmaking.StartMatch(currentRoom.ID).OnComplete(startRatedMatchResponse); |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | printOutputLine("You have hit an unknown state."); |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | void startRatedMatchResponse(Message msg) |
| 766 | { |
| 767 | if(!msg.IsError) |
| 768 | { |
| 769 | printOutputLine("Started a rated match"); |
| 770 | ratedMatchStarted = true; |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | Error error = msg.GetError(); |
| 775 | printOutputLine("Received starting rated match failure: " + error.Message); |
| 776 | printOutputLine("Your matchmaking pool needs to have a skill pool associated with it to play rated matches"); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | void requestReportResults() |
| 781 | { |
| 782 | switch (currentState) |
| 783 | { |
| 784 | case states.NOT_INIT: |
| 785 | printOutputLine("The app has not initialized properly and we don't know your userID."); |
| 786 | break; |
| 787 | |
| 788 | case states.IDLE: |
| 789 | case states.REQUEST_FIND: |
| 790 | case states.FINDING_ROOM: |
| 791 | case states.REQUEST_JOIN: |
| 792 | case states.REQUEST_CREATE: |
| 793 | case states.REQUEST_LEAVE: |
| 794 | printOutputLine("You need to be in a room with another player to report results on a rated match."); |
| 795 | break; |
| 796 | |
| 797 | case states.IN_EMPTY_ROOM: |
| 798 | case states.IN_FULL_ROOM: |
| 799 | if (ratedMatchStarted) |
| 800 | { |
| 801 | printOutputLine("Submitting rated match results."); |
| 802 | |
| 803 | Dictionary <string, int> results = new Dictionary<string, int>(); |
| 804 | results.Add(localUser.ID.ToString(), 1); |
| 805 | results.Add(remoteUser.ID.ToString(), 2); |
| 806 | |
| 807 | Matchmaking.ReportResultsInsecure(currentRoom.ID, results).OnComplete(reportResultsResponse); |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | printOutputLine("You can't report results unless you've already started a rated match"); |
| 812 | } |
| 813 | break; |
| 814 | |
| 815 | default: |
| 816 | printOutputLine("You have hit an unknown state."); |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | void reportResultsResponse(Message msg) |
| 822 | { |
| 823 | if (!msg.IsError) |
| 824 | { |
| 825 | printOutputLine("Rated match results reported. Now attempting to leave room."); |
| 826 | ratedMatchStarted = false; |
| 827 | requestLeaveRoom(); |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | Error error = msg.GetError(); |
| 832 | printOutputLine("Received reporting rated match failure: " + error.Message); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |