DataEntry.cs 23 KB

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