Esempio n. 1
0
    // receives data from the server.
    void ReceiveDataFromServer()
    {
        // Time (float) - 4 bytes
        // Number of Players (Int) - 4 Bytes
        // Number of Active Item Boxes (Int) - 4 bytes
        // Players
        // Items

        // data to be sent out to clients.
        byte[] recData = client.client.GetReceiveBufferData();
        int    index   = 0;

        // list of other players
        List <RemotePlayer> otherPlayers = new List <RemotePlayer>(players);

        otherPlayers.Remove(localPlayer); // remove local player from list.

        // list of unmatched data. This should always be of size 1 since the controlled player getsi gnored..
        List <RemotePlayer.RemotePlayerData> unmatchedData = new List <RemotePlayer.RemotePlayerData>();

        // the list of found id values.
        List <int> foundIds = new List <int>();

        // values
        float time      = -1.0F;
        int   plyrCount = -1;
        int   itemCount = -1;

        // Timer
        {
            time = BitConverter.ToSingle(recData, index);

            // TODO: check to see if the difference is great enough to make a change.
            // sets time.
            timer.SetCurrentTimeValue(time);
            index += sizeof(float);
        }

        // Number of Players
        {
            plyrCount = BitConverter.ToInt32(recData, index);
            // TODO: check to see if number of players changed?
            index += sizeof(int);
        }

        // Number of Item Boxes
        {
            itemCount = BitConverter.ToInt32(recData, index);
            // TODO: adjust item box amount
            index += sizeof(int);
        }


        // Players - getting player data
        for (int i = 0; i < plyrCount; i++)
        {
            // the player count is different.
            // if (i >= otherPlayers.Count)
            //     continue;

            // gets data section
            byte[] pData = new byte[RemotePlayer.DATA_SIZE];
            Buffer.BlockCopy(recData, index, pData, 0, RemotePlayer.DATA_SIZE);
            index += pData.Length; // move onto next slot.

            // otherPlayers[i].ApplyData(pData);

            // converts data first before finding who it belongs to.
            RemotePlayer.RemotePlayerData rpd = RemotePlayer.BytesToRemotePlayerData(pData);

            // if the scale is 0, that means that no data was received.
            // you know this because under no circumstance should the scale EVER be zero.
            // TODO: this fix didn't stop the models swapping positions constantly.
            if (rpd.scale == Vector3.zero)
            {
                // since no data was received, skip.
                continue;
            }

            // if the id exists in the list, then it's that data has already been used.
            // as such, the data should be ignored.
            if (foundIds.Contains(rpd.idNumber))
            {
                continue;
            }

            // if the id number for the provided data is the same as that for the local player it is ignored.
            // The list otherPlayers will always be one less than plyrCount due to the local player already being removed.
            if (rpd.idNumber == localPlayer.idNumber)
            {
                continue;
            }

            // checks to see if the data matched.
            bool matched = false;

            // goes through list to find which player to put it on.
            // goes through all player data passed.
            for (int j = 0; j < otherPlayers.Count; j++)
            {
                // if the id numbers match, apply the data.
                if (otherPlayers[j].idNumber == rpd.idNumber)
                {
                    otherPlayers[j].ApplyData(rpd); // applies data
                    otherPlayers.RemoveAt(j);       // removes matched player
                    matched = true;                 // data matched.

                    // adds the id to the list so that you know the data hasn't been set twice.
                    foundIds.Add(rpd.idNumber);

                    break;
                }
            }

            // saves data if it did not find a match.
            if (matched == false)
            {
                unmatchedData.Add(rpd);
            }


            // applies the data.
            // otherPlayers[i].ApplyData(pData);
        }


        // unmatched data is applied to any remaining objects.
        if (unmatchedData.Count != 0 && otherPlayers.Count != 0)
        {
            // applies data based on placement in list.
            do
            {
                // this should match the id number, meaning that it shouldn't be matched here ever again.
                otherPlayers[0].ApplyData(unmatchedData[0]);
                unmatchedData.RemoveAt(0);
                otherPlayers.RemoveAt(0);
            }while (unmatchedData.Count != 0 && otherPlayers.Count != 0);

            // // applies data based on placement in list.
            // for(int i = 0; i < otherPlayers.Count && i < unmatchedData.Count; i++)
            // {
            //     otherPlayers[i].ApplyData(unmatchedData[i]);
            // }
        }

        // Item Box Data
        // TODO: turn off item spawner for client so that it doesn't make new items.
        if (itemSpawner != null) // item spawner must be available for this to work.
        {
            // disables the spawner
            // itemSpawner.spawnerEnabled = false;

            FieldItem[] activeItems = FindObjectsOfType <FieldItem>(false);
            List <FieldItem.FieldItemData> recItems = new List <FieldItem.FieldItemData>();
            int arrIndex = 0;

            // gets all item transformation data.
            for (int i = 0; i < itemCount; i++)
            {
                // gets data section
                byte[] iData = new byte[FieldItem.DATA_SIZE];
                Buffer.BlockCopy(recData, index, iData, 0, FieldItem.DATA_SIZE);
                recItems.Add(FieldItem.BytesToFieldItemData(iData)); // adds data to list.

                index += iData.Length;                               // move onto next slot.
            }

            // while the array index is less than the amount of active items and received items.
            while (arrIndex < activeItems.Length && arrIndex < recItems.Count)
            {
                activeItems[arrIndex].ApplyData(recItems[arrIndex]);
                arrIndex++; // increment.
            }

            // gets the instance
            ItemManager im = ItemManager.GetInstance();

            // more items on the field than there should be.
            if (arrIndex < activeItems.Length)
            {
                // returns the item, and increments it.
                while (arrIndex < activeItems.Length)
                {
                    im.ReturnItem(activeItems[arrIndex]);
                    arrIndex++;
                }
            }
            // less items on the field than there should be.
            else if (arrIndex < recItems.Count)
            {
                // gets a new item, and adds it in.
                while (arrIndex < recItems.Count)
                {
                    FieldItem fieldItem = im.GetItem();
                    fieldItem.ApplyData(recItems[arrIndex]);
                    arrIndex++;
                }
            }
        }
    }
Esempio n. 2
0
    // SERVER -> CLIENT (MASTER) //

    // gets the data from client
    void ReceiveDataFromClients()
    {
        // Step 1: Get Info From Players
        // Step 2: Apply Data
        // Step 3: Send Off Data

        // byte[] data = ;

        // NOTE: only the player data gets received.
        // NOTE: figure out how to track if players get items
        // would be easier just to have each side generate their own items to be honest.


        // removes the local player
        List <RemotePlayer> otherPlayers = new List <RemotePlayer>(players);

        otherPlayers.Remove(localPlayer); // removes the local player.

        // list of data that hasn't been matched.
        List <RemotePlayer.RemotePlayerData> unmatchedData = new List <RemotePlayer.RemotePlayerData>();

        // the list of found id values.
        List <int> foundIds = new List <int>();

        // gets the amount of endpoints
        int epAmount = server.server.GetEndPointCount();
        // int playerCount = otherPlayers.Count; // amount of players
        int playerCount = gameManager.playerCount; // amount of players

        // TODO: maybe check something with player count?

        // goes through each endpoint to get the data. There is a max of 3.
        for (int i = 0; i < epAmount; i++)
        {
            // remote player object.
            // RemotePlayer rp;
            //
            // // bounds check for getting other player
            // if (i < otherPlayers.Count)
            //     rp = otherPlayers[i];
            // else
            //     break;



            // if the player is not equal to null.
            // if (rp.player != null)
            // {
            //     // if the player is controllable, then it should be ignored.
            //
            //     if (rp.player.controllablePlayer)
            //         continue;
            // }

            // original
            // this didn't fix the problem, making restore original?
            // gets the proper data index and sends it to the player.
            // TODO: models keep flickering because the endpoint they match up with keeps getting changed.
            byte[] data = server.server.GetReceiveBufferData(i);
            // rp.ApplyData(data); // original

            // converted data.
            RemotePlayer.RemotePlayerData rpd = RemotePlayer.BytesToRemotePlayerData(data);

            // if the scale is 0, that means that no data was received.
            // you know this because under no circumstance should the scale EVER be zero.
            // this fix didn't stop the models swapping positions constantly.
            if (rpd.scale == Vector3.zero)
            {
                // since no data was received, skip.
                continue;
            }

            // if the id exists in the list, then it's that data has already been used.
            // as such, the data should be ignored.
            if (foundIds.Contains(rpd.idNumber))
            {
                continue;
            }

            // checks to see if the data was matched with anything.
            bool matched = false;

            // goes through list to find which player to apply the data to.
            for (int j = 0; j < otherPlayers.Count; j++)
            {
                // if the id numbers match, apply the data.
                if (otherPlayers[j].idNumber == rpd.idNumber)
                {
                    otherPlayers[j].ApplyData(rpd); // applies data
                    otherPlayers.RemoveAt(j);       // removes matched player
                    matched = true;                 // data matched.

                    // adds the id to the list so that you know the data hasn't been set twice.
                    foundIds.Add(rpd.idNumber);
                    break;
                }
            }

            // saves data if it did not find a match.
            if (matched == false)
            {
                unmatchedData.Add(rpd);
            }
        }

        // unmatched data is applied to any remaining objects.
        if (unmatchedData.Count != 0 && otherPlayers.Count != 0)
        {
            // applies data based on placement in list.
            do
            {
                // Debug.Log("In Unmatched Data");

                // this should match the id number, meaning that it shouldn't be matched here ever again.
                otherPlayers[0].ApplyData(unmatchedData[0]);
                unmatchedData.RemoveAt(0);
                otherPlayers.RemoveAt(0);
            }while (unmatchedData.Count != 0 && otherPlayers.Count != 0);

            // // applies data based on placement in list.
            // for (int i = 0; i < otherPlayers.Count && i < unmatchedData.Count; i++)
            // {
            //     // this should match the id number, meaning that it shouldn't be matched here ever again.
            //     otherPlayers[i].ApplyData(unmatchedData[i]);
            //     unmatchedData.RemoveAt(i);
            // }
        }
    }