void OnOtherPlayerDisconnected() { SceneManager.LoadScene("Lobbies"); MyLobby = null; MyPlayerCharacter = -1; EnemyPlayerCharacter = -1; }
public void GenerateItem(NetworkObjects.Lobby lobby, int Wins = -1) { GameObject scrollItemObj = Instantiate(scrollItemprefab); scrollItemObj.transform.SetParent(scrollContent.transform, false); int levelOfRoom = (int)(Wins / 10); int myLevel = (int)(int.Parse(networkClient.Player.Wins) / 10); scrollItemObj.transform.Find("PlayerLevel").gameObject.GetComponent <TMP_Text>().text = levelOfRoom.ToString(); scrollItemObj.transform.Find("PlayerName").gameObject.GetComponent <TMP_Text>().text = lobby.Player1; Color newColor = Color.white; if (levelOfRoom - myLevel > 5) //5 level or more greater { newColor = Color.red; } else if (levelOfRoom - myLevel > 2) //more than 3 levels { newColor = Color.yellow; } else if (levelOfRoom - myLevel >= -2) //no more than 2 levels below us { newColor = Color.green; } scrollItemObj.transform.Find("Background").gameObject.GetComponent <Image>().color = newColor; scrollItemObj.transform.GetComponent <JoinablePlayer>().AvailableLobby = lobby; scrollView.verticalNormalizedPosition = 1; }
public void UpdateLobby() { ActiveLobby = FindObjectOfType <NetworkClient>().MyLobby; if (ActiveLobby.Player2 != "") { Player2.SetActive(true); Player2.GetComponent <Image>().color = Color.red; } }
public void JoinLobby(NetworkObjects.Lobby joiningLobby) { Debug.Log("Sending join game message"); Debug.Log(joiningLobby); JoinGameMsg m = new JoinGameMsg(); m.player.id = PlayerUserID; m.joinLobby = joiningLobby; SendToServer(JsonUtility.ToJson(m)); }
void Start() { Player = new NetworkObjects.Item(); MyLobby = new NetworkObjects.Lobby(); m_Driver = NetworkDriver.Create(); m_Connection = default(NetworkConnection); var endpoint = NetworkEndPoint.Parse(serverIP, serverPort); m_Connection = m_Driver.Connect(endpoint); DontDestroyOnLoad(gameObject); }
IEnumerator HostsNewLobby(string UserID, int connection) { NetworkObjects.Item test; string url = "https://pnz7w1hjm3.execute-api.us-east-2.amazonaws.com/default/FinalAssignmentGetPlayer?UserID=" + UserID; UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("Content-Type", "application/json"); yield return(www.SendWebRequest()); Debug.Log(" player info finished requesting from db "); if (www.isNetworkError || www.isHttpError) { Debug.Log(" player info error getting "); Debug.Log(www.error); HostGameMsg m = new HostGameMsg(); m.player.id = UserID; SendToClient(JsonUtility.ToJson(m), m_Connections[connection]); // register unsuccessful } else { Debug.Log(" player info successfully gotten. Sending to player ..... "); test = JsonUtility.FromJson <NetworkObjects.Item>(www.downloadHandler.text); ///////////////////////////////////// HostGameMsg m = new HostGameMsg(); m.player.id = UserID; NetworkObjects.Lobby newLobby = new NetworkObjects.Lobby(); if (newLobby != null) { m.successful = true; newLobby.lobbyID = LOBBYCURRENTMAXID; newLobby.Player1 = UserID; newLobby.player1addr = connection; newLobby.HostWins = int.Parse(test.Wins); m.newLobby = newLobby; AvailableLobbies[newLobby.lobbyID] = newLobby; LOBBYCURRENTMAXID++; Debug.Log("Lobby ID = " + newLobby.lobbyID); //Debug.Log(JsonUtility.ToJson(newLobby)); //Debug.Log(JsonUtility.ToJson(AvailableLobbies[0])); } else { Debug.Log("Host Lobby Failed"); } SendToClient(JsonUtility.ToJson(m), m_Connections[connection]); } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_LOGIN: PlayerLoginMsg loginMsg = JsonUtility.FromJson <PlayerLoginMsg>(recMsg); // check if successful is true if (loginMsg.successful) { Debug.Log("Successful Login"); PlayerUserID = loginMsg.userID; //RequestPlayerInfo(); SceneManager.LoadScene("Lobbies"); } else { Debug.Log("UNSuccessful Login"); FindObjectOfType <LoginButtonBehaviour>().DisplayError(); } break; case Commands.PLAYER_REGISTER: PlayerRegisterMsg registerMsg = JsonUtility.FromJson <PlayerRegisterMsg>(recMsg); if (registerMsg.successful) { Debug.Log("Successful Register"); PlayerUserID = registerMsg.userID; //RequestPlayerInfo(); SceneManager.LoadScene("Lobbies"); } else { Debug.Log("UNSuccessful Register"); FindObjectOfType <RegisterButtonBehaviour>().DisplayError(); } // check if successful is true break; case Commands.HOST_GAME: HostGameMsg hostmsg = JsonUtility.FromJson <HostGameMsg>(recMsg); if (hostmsg.successful && (hostmsg.newLobby.Player1 == PlayerUserID || hostmsg.newLobby.Player2 == PlayerUserID)) { MyLobby = hostmsg.newLobby; SceneManager.LoadScene("Lobby"); // success move to the lobby scene } else { // display error } break; case Commands.JOIN_GAME: JoinGameMsg joinmsg = JsonUtility.FromJson <JoinGameMsg>(recMsg); if (joinmsg.successful && (joinmsg.joinLobby.Player1 == PlayerUserID || joinmsg.joinLobby.Player2 == PlayerUserID)) { MyLobby = joinmsg.joinLobby; if (joinmsg.joinLobby.Player1 == PlayerUserID) { MyLevel = joinmsg.joinLobby.HostWins; OpponentLevel = joinmsg.joinLobby.Player2Wins; FindObjectOfType <LobbyHandler>().UpdateLobby(); } else { MyLevel = joinmsg.joinLobby.Player2Wins; OpponentLevel = joinmsg.joinLobby.HostWins; SceneManager.LoadScene("Lobby"); } // success move to the lobby scene } else { // display error } break; case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); //myServerId = hsMsg.InternalServerID; //Debug.Log("My id is:" + myServerId); ////add our own id so we know who we are //if (myId == "") //{ // myId = hsMsg.player.id; // Debug.Log("My id is:" + myId); //} break; case Commands.START_GAME: StartGameMsg startMsg = JsonUtility.FromJson <StartGameMsg>(recMsg); if (startMsg.LobbyToStart.Player1 == PlayerUserID) // i am player 1 { MyPlayerCharacter = startMsg.Player1Char; EnemyPlayerCharacter = startMsg.Player2Char; } else // i am player 2 { MyPlayerCharacter = startMsg.Player2Char; EnemyPlayerCharacter = startMsg.Player1Char; } if (startMsg.successful) { EnterPlay(); } break; case Commands.PLAYER_UPDATE: //not really receiveing player update messages as this is this client and not the server PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); for (int i = 0; i < suMsg.players.Count; i++) { } break; case Commands.REQUEST_ALL_LOBBIES: AllAvailableLobbies alMsg = JsonUtility.FromJson <AllAvailableLobbies>(recMsg); Debug.Log("Server update message received!"); for (int i = 0; i < alMsg.Lobbies.Count; i++) { Debug.Log(JsonUtility.ToJson(alMsg.Lobbies[i])); } ScrollFiller scrollFiller = FindObjectOfType <ScrollFiller>(); scrollFiller.ClearLobbies(); foreach (var lobby in alMsg.Lobbies) { //if (!lobby.full) scrollFiller.GenerateItem(lobby, lobby.HostWins); } break; case Commands.MOVE_TAKEN: MoveTakenMsg moveMsg = JsonUtility.FromJson <MoveTakenMsg>(recMsg); Debug.Log("Received move from player"); FindObjectOfType <BattleSystem>().EnemyAttack(moveMsg.move); break; case Commands.PLAYER_INFO: MyInfoMsg infoMsg = JsonUtility.FromJson <MyInfoMsg>(recMsg); Debug.Log("Received info about player"); Debug.Log("RAW MESSAGE PLAYER INFO " + recMsg); Player = infoMsg.Player; break; case Commands.LOBBY_DISCONNECTED: Debug.Log("Other player disconnected form lobby"); OnOtherPlayerDisconnected(); break; default: Debug.Log("Unrecognized message received!"); Debug.Log(recMsg); break; } }