using System; using System.Collections.Generic; using Oculus.Platform.Models; using UnityEngine; using UnityEngine.UI; namespace Oculus.Platform.Samples.NetChat { public class DataEntry : MonoBehaviour { public Text dataOutput; private states currentState; private User localUser; private User remoteUser; private Oculus.Platform.Models.Room currentRoom; private int lastPacketID; private bool ratedMatchStarted; private void Start() { currentState = states.NOT_INIT; localUser = null; remoteUser = null; currentRoom = null; lastPacketID = 0; ratedMatchStarted = false; Core.Initialize(); Rooms.SetUpdateNotificationCallback(updateRoom); Matchmaking.SetMatchFoundNotificationCallback(foundMatch); checkEntitlement(); } private void Update() { string text = GetComponent().text; if (Input.GetKey(KeyCode.Return)) { if (text != string.Empty) { SubmitCommand(text); } GetComponent().text = string.Empty; } processNetPackets(); Request.RunCallbacks(); } private void SubmitCommand(string command) { string[] array = command.Split('!'); if (array.Length <= 0) { return; } switch (array[0]) { case "c": requestCreateRoom(); break; case "d": requestCreateFilterRoom(); break; case "f": requestFindMatch(); break; case "g": requestFindRoom(); break; case "i": requestFindFilteredRoom(); break; case "s": if (array.Length > 1) { sendChat(array[1]); } break; case "l": requestLeaveRoom(); break; case "1": requestStartRatedMatch(); break; case "2": requestReportResults(); break; default: printOutputLine("Invalid Command"); break; } } private void printOutputLine(string newLine) { dataOutput.text = "> " + newLine + Environment.NewLine + dataOutput.text; } private void checkEntitlement() { Entitlements.IsUserEntitledToApplication().OnComplete(getEntitlementCallback); } private void getEntitlementCallback(Message msg) { if (!msg.IsError) { printOutputLine("You are entitled to use this app."); Users.GetLoggedInUser().OnComplete(init); } else { printOutputLine("You are NOT entitled to use this app."); } } private void init(Message msg) { if (!msg.IsError) { User data = msg.Data; localUser = data; currentState = states.IDLE; } else { printOutputLine("Received get current user error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); Users.GetLoggedInUser().OnComplete(init); currentState = states.NOT_INIT; } } private void requestCreateRoom() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: printOutputLine("Trying to create a matchmaking room"); Matchmaking.CreateAndEnqueueRoom("filter_pool", 8u, true).OnComplete(createRoomResponse); currentState = states.REQUEST_CREATE; break; case states.REQUEST_FIND: printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); break; case states.FINDING_ROOM: printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); break; case states.REQUEST_JOIN: printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); break; case states.REQUEST_CREATE: printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); break; case states.IN_EMPTY_ROOM: printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); break; case states.IN_FULL_ROOM: printOutputLine("You have already in a match."); break; default: printOutputLine("You have hit an unknown state."); break; } } private void createRoomResponse(Message msg) { if (!msg.IsError) { printOutputLine("Received create matchmaking room success"); printOutputLine("RoomID: " + (currentRoom = msg.Data.Room).ID); currentState = states.IN_EMPTY_ROOM; } else { printOutputLine("Received create matchmaking room Error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); 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"); currentState = states.IDLE; } } private void requestCreateFilterRoom() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID.\n"); break; case states.IDLE: { printOutputLine("Trying to create a matchmaking room"); Matchmaking.CustomQuery customQuery = new Matchmaking.CustomQuery(); customQuery.criteria = null; customQuery.data = new Dictionary(); customQuery.data.Add("game_type_name", "CTF"); customQuery.data.Add("map_name", "Really_Big_Map"); Matchmaking.CreateAndEnqueueRoom("filter_pool", 8u, true, customQuery).OnComplete(createRoomResponse); currentState = states.REQUEST_CREATE; break; } case states.REQUEST_FIND: printOutputLine("You have already made a request to find a room. Please wait for that request to complete.\n"); break; case states.FINDING_ROOM: printOutputLine("You have already currently looking for a room. Please wait for the match to be made.\n"); break; case states.REQUEST_JOIN: printOutputLine("We are currently trying to join a room. Please wait to see if we can join it.\n"); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it.\n"); break; case states.REQUEST_CREATE: printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made.\n"); break; case states.IN_EMPTY_ROOM: printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join.\n"); break; case states.IN_FULL_ROOM: printOutputLine("You have already in a match.\n"); break; default: printOutputLine("You have hit an unknown state.\n"); break; } } private void requestFindRoom() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: printOutputLine("\nTrying to find a matchmaking room\n"); Matchmaking.Enqueue("filter_pool").OnComplete(searchingStarted); currentState = states.REQUEST_FIND; break; case states.REQUEST_FIND: printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); break; case states.FINDING_ROOM: printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); break; case states.REQUEST_JOIN: printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); break; case states.REQUEST_CREATE: printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); break; case states.IN_EMPTY_ROOM: printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); break; case states.IN_FULL_ROOM: printOutputLine("You have already in a match."); break; default: printOutputLine("You have hit an unknown state."); break; } } private void requestFindFilteredRoom() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: { printOutputLine("Trying to find a matchmaking room"); Matchmaking.CustomQuery customQuery = new Matchmaking.CustomQuery(); Matchmaking.CustomQuery.Criterion[] array = new Matchmaking.CustomQuery.Criterion[2]; array[0].key = "map"; array[0].importance = MatchmakingCriterionImportance.Required; array[0].parameters = new Dictionary(); array[0].parameters.Add("map_param_1", "Really_Big_Map"); array[0].parameters.Add("map_param_2", "Big_Map"); array[1].key = "game_type"; array[1].importance = MatchmakingCriterionImportance.Required; array[1].parameters = new Dictionary(); array[1].parameters.Add("game_type_param", "CTF"); customQuery.criteria = array; customQuery.data = null; Matchmaking.Enqueue("filter_pool", customQuery); currentState = states.REQUEST_FIND; break; } case states.REQUEST_FIND: printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); break; case states.FINDING_ROOM: printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); break; case states.REQUEST_JOIN: printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); break; case states.REQUEST_CREATE: printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); break; case states.IN_EMPTY_ROOM: printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); break; case states.IN_FULL_ROOM: printOutputLine("You have already in a match."); break; default: printOutputLine("You have hit an unknown state."); break; } } private void foundMatch(Message msg) { if (!msg.IsError) { printOutputLine("Received find match success. We are now going to request to join the room."); Oculus.Platform.Models.Room data = msg.Data; Rooms.Join(data.ID, true).OnComplete(joinRoomResponse); currentState = states.REQUEST_JOIN; } else { printOutputLine("Received find match error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); currentState = states.IDLE; } } private void joinRoomResponse(Message msg) { if (!msg.IsError) { printOutputLine("Received join room success."); currentRoom = msg.Data; currentState = states.IN_EMPTY_ROOM; if (currentRoom.UsersOptional == null) { return; } { foreach (User item in currentRoom.UsersOptional) { if (item.ID != localUser.ID) { remoteUser = item; currentState = states.IN_FULL_ROOM; } } return; } } printOutputLine("Received join room error"); printOutputLine("It's possible the room filled up before you could join it."); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); currentState = states.IDLE; } private void requestFindMatch() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: printOutputLine("Trying to find a matchmaking room"); Matchmaking.Enqueue("bout_pool").OnComplete(searchingStarted); currentState = states.REQUEST_FIND; break; case states.REQUEST_FIND: printOutputLine("You have already made a request to find a room. Please wait for that request to complete."); break; case states.FINDING_ROOM: printOutputLine("You have already currently looking for a room. Please wait for the match to be made."); break; case states.REQUEST_JOIN: printOutputLine("We are currently trying to join a room. Please wait to see if we can join it."); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); break; case states.REQUEST_CREATE: printOutputLine("You have already requested a matchmaking room to be created. Please wait for the room to be made."); break; case states.IN_EMPTY_ROOM: printOutputLine("You have already in a matchmaking room. Please wait for an opponent to join."); break; case states.IN_FULL_ROOM: printOutputLine("You have already in a match."); break; default: printOutputLine("You have hit an unknown state."); break; } } private void searchingStarted(Message msg) { if (!msg.IsError) { printOutputLine("Searching for a match successfully started"); currentState = states.REQUEST_FIND; } else { printOutputLine("Searching for a match error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); } } private void updateRoom(Message msg) { if (!msg.IsError) { printOutputLine("Received room update notification"); Oculus.Platform.Models.Room data = msg.Data; if (currentState == states.IN_EMPTY_ROOM) { if (data.UsersOptional == null) { return; } { foreach (User item in data.UsersOptional) { if (item.ID != localUser.ID) { remoteUser = item; currentState = states.IN_FULL_ROOM; } } return; } } if (data.UsersOptional != null && data.UsersOptional.Count == 1) { printOutputLine("User ID: " + remoteUser.ID + "has left"); remoteUser = null; currentState = states.IN_EMPTY_ROOM; } } else { printOutputLine("Received room update error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); } } private void sendChat(string chatMessage) { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: case states.REQUEST_FIND: case states.FINDING_ROOM: case states.REQUEST_CREATE: case states.REQUEST_JOIN: case states.REQUEST_LEAVE: case states.IN_EMPTY_ROOM: printOutputLine("You need to be in a room with another player to send a message."); break; case states.IN_FULL_ROOM: { chatPacket chatPacket2 = new chatPacket(); lastPacketID++; chatPacket2.packetID = lastPacketID; chatPacket2.textString = chatMessage; Net.SendPacket(remoteUser.ID, chatPacket2.Serialize(), SendPolicy.Reliable); break; } default: printOutputLine("You have hit an unknown state."); break; } } private void processNetPackets() { for (Packet packet = Net.ReadPacket(); packet != null; packet = Net.ReadPacket()) { byte[] array = new byte[packet.Size]; packet.ReadBytes(array); chatPacket chatPacket2 = chatPacket.Deserialize(array); printOutputLine("Chat Text: " + chatPacket2.textString.ToString()); printOutputLine("Received Packet from UserID: " + packet.SenderID); printOutputLine("Received Packet ID: " + chatPacket2.packetID); } } private void requestLeaveRoom() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: case states.REQUEST_FIND: case states.FINDING_ROOM: case states.REQUEST_CREATE: case states.REQUEST_JOIN: printOutputLine("You are currently not in a room to leave."); break; case states.REQUEST_LEAVE: printOutputLine("We are currently trying to leave a room. Please wait to see if we can leave it."); break; case states.IN_EMPTY_ROOM: case states.IN_FULL_ROOM: printOutputLine("Trying to leave room."); Rooms.Leave(currentRoom.ID).OnComplete(leaveRoomResponse); break; default: printOutputLine("You have hit an unknown state."); break; } } private void leaveRoomResponse(Message msg) { if (!msg.IsError) { printOutputLine("We were able to leave the room"); currentRoom = null; remoteUser = null; currentState = states.IDLE; } else { printOutputLine("Leave room error"); Error error = msg.GetError(); printOutputLine("Error: " + error.Message); } } private void requestStartRatedMatch() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: case states.REQUEST_FIND: case states.FINDING_ROOM: case states.REQUEST_CREATE: case states.REQUEST_JOIN: case states.REQUEST_LEAVE: case states.IN_EMPTY_ROOM: printOutputLine("You need to be in a room with another player to start a rated match."); break; case states.IN_FULL_ROOM: 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."); Matchmaking.StartMatch(currentRoom.ID).OnComplete(startRatedMatchResponse); break; default: printOutputLine("You have hit an unknown state."); break; } } private void startRatedMatchResponse(Message msg) { if (!msg.IsError) { printOutputLine("Started a rated match"); ratedMatchStarted = true; } else { Error error = msg.GetError(); printOutputLine("Received starting rated match failure: " + error.Message); printOutputLine("Your matchmaking pool needs to have a skill pool associated with it to play rated matches"); } } private void requestReportResults() { switch (currentState) { case states.NOT_INIT: printOutputLine("The app has not initialized properly and we don't know your userID."); break; case states.IDLE: case states.REQUEST_FIND: case states.FINDING_ROOM: case states.REQUEST_CREATE: case states.REQUEST_JOIN: case states.REQUEST_LEAVE: printOutputLine("You need to be in a room with another player to report results on a rated match."); break; case states.IN_EMPTY_ROOM: case states.IN_FULL_ROOM: if (ratedMatchStarted) { printOutputLine("Submitting rated match results."); Dictionary dictionary = new Dictionary(); dictionary.Add(localUser.ID.ToString(), 1); dictionary.Add(remoteUser.ID.ToString(), 2); Matchmaking.ReportResultsInsecure(currentRoom.ID, dictionary).OnComplete(reportResultsResponse); } else { printOutputLine("You can't report results unless you've already started a rated match"); } break; default: printOutputLine("You have hit an unknown state."); break; } } private void reportResultsResponse(Message msg) { if (!msg.IsError) { printOutputLine("Rated match results reported. Now attempting to leave room."); ratedMatchStarted = false; requestLeaveRoom(); } else { Error error = msg.GetError(); printOutputLine("Received reporting rated match failure: " + error.Message); } } } }