Ejemplo n.º 1
0
 void SpawnOtherPlayer(PlayerSpawnMsg msg)
 {
     if (msg.user.user_id != PlayerID)
     {
         if (loginInfo.IsHost)
         {
             if (msg.user.rank == playerInfo.playerRank)
             {
                 AllPlayersGO[1].GetComponent <NetInfo>().playerID    = msg.user.user_id;
                 AllPlayersGO[1].GetComponent <NetInfo>().playerEmail = msg.user.email;
                 AllPlayersGO[1].GetComponent <NetInfo>().playerRank  = msg.user.rank;
                 AllPlayersGO[1].GetComponent <NetInfo>().playerScore = msg.user.score;
                 player2Name.text = msg.user.user_id;
             }
         }
         else
         {
             if (msg.user.rank == playerInfo.playerRank)
             {
                 AllPlayersGO[0].GetComponent <NetInfo>().playerID    = msg.user.user_id;
                 AllPlayersGO[0].GetComponent <NetInfo>().playerEmail = msg.user.email;
                 AllPlayersGO[0].GetComponent <NetInfo>().playerRank  = msg.user.rank;
                 AllPlayersGO[0].GetComponent <NetInfo>().playerScore = msg.user.score;
                 player1Name.text = msg.user.user_id;
             }
             else
             {
                 SceneManager.LoadScene("S_CreateFind");
             }
         }
     }
 }
Ejemplo n.º 2
0
 void SpawnNewPlayer(PlayerSpawnMsg msg)
 {
     foreach (NetworkConnection c in m_Connections)
     {
         SendToClient(JsonUtility.ToJson(msg), c);
     }
 }
Ejemplo n.º 3
0
 void SpawnOtherPlayer(PlayerSpawnMsg msg)
 {
     if (msg.ID != PlayerID)
     {
         GameObject otherPlayerGO = Instantiate(PlayerPrefab, msg.Position, new Quaternion());
         otherPlayerGO.GetComponent <NetInfo>().playerID = msg.ID;
         AllPlayersGO.Add(otherPlayerGO);
     }
 }
    void OnClientLogIn(int i, string clientId)
    {
        m_ClientIds.Add(m_Connections[i], clientId);
        PlayerData playerData = GameServerManager.Instance.SpawnPlayer(clientId);

        if (playerData != null)
        {
            PlayerSpawnMsg msg = new PlayerSpawnMsg(clientId, playerData);
            SendToClient(JsonUtility.ToJson(msg), m_Connections[i]);
        }
    }
Ejemplo n.º 5
0
    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.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            //Debug.Log("Handshake message received!");
            break;

        case Commands.PLAYER_UPDATE:
            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!");
            break;

        case Commands.REQUEST_ID:
            RequestIDMsg riMsg = JsonUtility.FromJson <RequestIDMsg>(recMsg);
            playerInfo.serverID = riMsg.ID;
            Debug.Log("Request ID message received!");
            break;

        case Commands.PLAYER_SPAWN:
            PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg);
            SpawnOtherPlayer(psMsg);
            break;

        case Commands.UPDATE_STATS:
            UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg);
            UpdateOtherPlayer(usMsg);
            break;

        case Commands.PLAYER_DC:
            PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg);
            KillPlayer(pdMsg);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
    void OnData(DataStreamReader stream, int i)
    {
        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.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            //Debug.Log("Handshake message received!");
            break;

        case Commands.PLAYER_UPDATE:
            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!");
            break;

        case Commands.PLAYER_SPAWN:
            PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg);
            AllSpawnMsg.Add(psMsg);
            SpawnNewPlayer(psMsg);
            Debug.Log(psMsg.ID + " has joined the server!");
            break;

        case Commands.UPDATE_STATS:
            UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg);
            UpdatePlayerStats(usMsg);
            break;

        case Commands.PLAYER_DC:
            PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg);
            Debug.Log("Removed Spawn data of: " + pdMsg.PlayerID);
            AllSpawnMsg.Remove(FindPlayerSpawnMsg(pdMsg.PlayerID));
            DCPlayer(pdMsg);
            break;

        default:
            Debug.Log("SERVER ERROR: Unrecognized message received!");
            break;
        }
    }
Ejemplo n.º 7
0
    void SetPlayerInformation()
    {
        playerInfo             = playerGO.GetComponent <NetInfo>();
        playerInfo.localID     = m_Connection.InternalId.ToString();
        playerInfo.playerID    = PlayerID;
        playerInfo.playerEmail = loginInfo.http.loginUser.email;
        playerInfo.playerRank  = loginInfo.http.loginUser.rank;
        playerInfo.playerScore = loginInfo.http.loginUser.score;
        PlayerSpawnMsg sm = new PlayerSpawnMsg();
        User           u  = new User();

        u.user_id = PlayerID;
        u.email   = loginInfo.http.loginUser.email;
        u.rank    = loginInfo.http.loginUser.rank;
        u.score   = loginInfo.http.loginUser.score;
        sm.user   = u;
        SendToServer(JsonUtility.ToJson(sm));
    }
    void SpawnPlayer()
    {
        Vector3 pos = new Vector3(UnityEngine.Random.Range(-2.0f, 2.0f), 0.0f, 0.0f);

        playerGO = Instantiate(PlayerPrefab, pos, new Quaternion());

        playerInfo = playerGO.GetComponent <NetInfo>();
        playerGO.GetComponent <NetInfo>().playerID = PlayerID;

        playerInfo.playerID = PlayerID;
        AllPlayersGO.Add(playerGO);

        //// Example to send a handshake message:
        PlayerSpawnMsg m = new PlayerSpawnMsg();

        m.Position = pos;
        m.ID       = PlayerID;
        SendToServer(JsonUtility.ToJson(m));
    }
Ejemplo n.º 9
0
    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.CONNECTED:
            ConnectMsg cMsg = JsonUtility.FromJson <ConnectMsg>(recMsg);
            Debug.Log("[Client] Client connected to the server : " + clientId);
            break;

        case Commands.LOGIN:
            LoginMsg lMsg = JsonUtility.FromJson <LoginMsg>(recMsg);
            Debug.Log("[Client] Client logged in to the server : " + lMsg.clientId);
            break;

        case Commands.PLAYER_SPAWNED:
            PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg);
            Debug.Log("[Client] Player Spawn message received! " + psMsg.player.id);
            GameplayManager.Instance.SpawnPlayer(psMsg.player, true);
            break;

        case Commands.PLAYER_UPDATE:
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            Debug.Log("[Client] Player update message received!");
            break;

        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            //Debug.Log( "[Client] Server update message received!" + suMsg.players.ToArrayString() );
            GameplayManager.Instance.UpdatePlayers(suMsg.players, suMsg.playerCommands);
            break;

        default:
            Debug.Log("[Client] Unrecognized message received!");
            break;
        }
    }
Ejemplo n.º 10
0
    void SpawnPlayer()
    {
        Debug.Log("Player spawned.");

        Vector3 pos = new Vector3(UnityEngine.Random.Range(-2.0f, 2.0f), 0.0f, 0.0f);

        playerGO            = Instantiate(PlayerPrefab, pos, new Quaternion());
        playerInfo          = playerGO.GetComponent <NetInfo>();
        playerInfo.localID  = m_Connection.InternalId.ToString();
        playerInfo.playerID = PlayerID;
        playerInfo.ActivateCam();
        playerGO.AddComponent <PlayerControl>();
        AllPlayersGO.Add(playerGO);

        //// Example to send a handshake message:
        PlayerSpawnMsg m = new PlayerSpawnMsg();

        m.Position = pos;
        m.ID       = PlayerID;
        SendToServer(JsonUtility.ToJson(m));
    }
Ejemplo n.º 11
0
    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.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            //Debug.Log("Handshake message received!");
            break;

        case Commands.PLAYER_UPDATE:
            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!");
            break;

        case Commands.REQUEST_ID:
            RequestIDMsg riMsg = JsonUtility.FromJson <RequestIDMsg>(recMsg);
            //playerInfo.serverID = riMsg.ID;
            Debug.Log("Request ID message received!");
            break;

        case Commands.PLAYER_SPAWN:
            PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg);
            SpawnOtherPlayer(psMsg);
            break;

        case Commands.UPDATE_STATS:
            UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg);
            UpdateOtherPlayer(usMsg);
            break;

        case Commands.GAME_RESULT:
            GameEndMsg geMsg = JsonUtility.FromJson <GameEndMsg>(recMsg);

            P1Diff.text = (int.Parse(geMsg.p1.points) - int.Parse(clientScript.AllPlayers[0].points)).ToString();
            P2Diff.text = (int.Parse(geMsg.p2.points) - int.Parse(clientScript.AllPlayers[1].points)).ToString();
            P3Diff.text = (int.Parse(geMsg.p3.points) - int.Parse(clientScript.AllPlayers[2].points)).ToString();

            clientScript.AllPlayers[0] = geMsg.p1;
            clientScript.AllPlayers[1] = geMsg.p2;
            clientScript.AllPlayers[2] = geMsg.p3;

            File.WriteAllText(logPath, geMsg.LogData);
            break;

        case Commands.PLAYER_DC:
            PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg);
            KillPlayer(pdMsg);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
Ejemplo n.º 12
0
    void OnData(DataStreamReader stream, int i)
    {
        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.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            //Debug.Log("Handshake message received!");
            break;

        case Commands.PLAYER_UPDATE:
            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!");
            break;

        case Commands.PLAYER_SPAWN:
            PlayerSpawnMsg psMsg = JsonUtility.FromJson <PlayerSpawnMsg>(recMsg);
            AllSpawnMsg.Add(psMsg);
            SpawnNewPlayer(psMsg);
            Debug.Log(psMsg.ID + " has joined the server!");
            break;

        case Commands.UPDATE_STATS:
            UpdateStatsMsg usMsg = JsonUtility.FromJson <UpdateStatsMsg>(recMsg);
            UpdatePlayerStats(usMsg);
            break;

        case Commands.GAME_START:
            // Log
            WriteToLog("\n\nGame ID: " + System.DateTime.Now.ToString() + UnityEngine.Random.value.ToString());

            GameStartMsg gsMsg = JsonUtility.FromJson <GameStartMsg>(recMsg);

            // Log
            WriteToLog(gsMsg.p1.user_id + " has joined the match. - " + gsMsg.p1.joinTime);
            WriteToLog(gsMsg.p2.user_id + " has joined the match. - " + gsMsg.p1.joinTime);
            WriteToLog(gsMsg.p3.user_id + " has joined the match. - " + gsMsg.p1.joinTime);
            int    result = UnityEngine.Random.Range(1, 4);
            Player winner = new Player();

            // Log
            WriteToLog("Game started.");
            WriteToLog("RESULT: ");

            int p1Points = int.Parse(gsMsg.p1.points);
            int p2Points = int.Parse(gsMsg.p2.points);
            int p3Points = int.Parse(gsMsg.p3.points);
            int add;
            switch (result)
            {
            case 1:
                add = (int)((p2Points + p3Points) / p1Points) + 20;
                WriteToLog(gsMsg.p1.user_id + " earned: " + add + " points." + " (" + p1Points + " -> " + (p1Points + add) + ")");
                p1Points       += add;
                gsMsg.p1.points = p1Points.ToString();
                winner          = gsMsg.p1;

                WriteToLog(gsMsg.p2.user_id + " lost: " + (int)((p2Points * 0.05) + 20) + " points." + " (" + p2Points + " -> " + (p2Points - (int)((p2Points * 0.05) + 20)) + ")");
                p2Points       -= (int)((p2Points * 0.05) + 20);
                gsMsg.p2.points = p2Points.ToString();
                WriteToLog(gsMsg.p3.user_id + " lost: " + (int)((p3Points * 0.05) + 20) + " points." + " (" + p3Points + " -> " + (p3Points - (int)((p3Points * 0.05) + 20)) + ")");
                p3Points       -= (int)((p3Points * 0.05) + 20);
                gsMsg.p3.points = p3Points.ToString();
                break;

            case 2:
                add = (int)((p1Points + p3Points) / p2Points) + 20;
                WriteToLog(gsMsg.p2.user_id + " earned: " + add + " points." + " (" + p2Points + " -> " + (p2Points + add) + ")");
                p2Points       += add;
                gsMsg.p2.points = p2Points.ToString();
                winner          = gsMsg.p2;

                WriteToLog(gsMsg.p1.user_id + " lost: " + (int)((p1Points * 0.05) + 20) + " points." + " (" + p1Points + " -> " + (p1Points - (int)((p1Points * 0.05) + 20)) + ")");
                p1Points       -= (int)((p1Points * 0.05) + 20);
                gsMsg.p1.points = p1Points.ToString();
                WriteToLog(gsMsg.p3.user_id + " lost: " + (int)((p3Points * 0.05) + 20) + " points." + " (" + p3Points + " -> " + (p3Points - (int)((p3Points * 0.05) + 20)) + ")");
                p3Points       -= (int)((p3Points * 0.05) + 20);
                gsMsg.p3.points = p3Points.ToString();
                break;

            case 3:
                add = (int)((p2Points + p1Points) / p3Points) + 20;
                WriteToLog(gsMsg.p3.user_id + " earned: " + add + " points." + " (" + p3Points + " -> " + (p3Points + add) + ")");
                p3Points       += add;
                gsMsg.p3.points = p3Points.ToString();
                winner          = gsMsg.p3;

                WriteToLog(gsMsg.p2.user_id + " lost: " + (int)((p2Points * 0.05) + 20) + " points." + " (" + p2Points + " -> " + (p2Points - (int)((p2Points * 0.05) + 20)) + ")");
                p2Points       -= (int)((p2Points * 0.05) + 20);
                gsMsg.p2.points = p2Points.ToString();
                WriteToLog(gsMsg.p1.user_id + " lost: " + (int)((p1Points * 0.05) + 20) + " points." + " (" + p1Points + " -> " + (p1Points - (int)((p1Points * 0.05) + 20)) + ")");
                p1Points       -= (int)((p1Points * 0.05) + 20);
                gsMsg.p1.points = p1Points.ToString();
                break;
            }
            WriteToLog(winner.user_id + " WON!");
            foreach (NetworkConnection c in m_Connections)
            {
                //// Example to send a handshake message:
                GameEndMsg m = new GameEndMsg();
                m.p1      = gsMsg.p1;
                m.p2      = gsMsg.p2;
                m.p3      = gsMsg.p3;
                m.winner  = winner;
                m.LogData = File.ReadAllText(logPath);
                SendToClient(JsonUtility.ToJson(m), c);
            }

            // Do the match
            break;

        case Commands.PLAYER_DC:
            PlayerDCMsg pdMsg = JsonUtility.FromJson <PlayerDCMsg>(recMsg);
            Debug.Log("Removed Spawn data of: " + pdMsg.PlayerID);
            AllSpawnMsg.Remove(FindPlayerSpawnMsg(pdMsg.PlayerID));
            DCPlayer(pdMsg);
            break;

        default:
            Debug.Log("SERVER ERROR: Unrecognized message received!");
            break;
        }
    }
Ejemplo n.º 13
0
 public void respawnPlayer()
 {
     int playerId = pom.mostRecentDeath();
     if(playerId == -25) return;
     PlayerSpawnMsg ps = new PlayerSpawnMsg();
     ps.playerId = playerId;
     Transform obsCamTrans = GameObject.FindGameObjectsWithTag("Observer")[0].transform.GetChild(0);
     ps.pos = obsCamTrans.transform.position; //Observer cam;
     if(pm.getTeam(playerId) == TeamID.TEAM_PIRATES) ps.pos += -obsCamTrans.right*5;
     else ps.pos += obsCamTrans.right*5;
     nm.client.Send(Msgs.spawnPlayer, ps);
 }