Exemple #1
0
    private static byte [] PositionData()
    {
        //Create package from player ID and position
        List <PositionDataPackage> packageCollection = new List <PositionDataPackage>();

        for (int i = 0; i < Players.Count; i++)
        {
            packageCollection.Add(
                new PositionDataPackage
            {
                PlayerID = Players[i].PlayerID,
                Position = Players[i].CurrentPos
            });
        }
        DataCollectionPackage package = new DataCollectionPackage()
        {
            PositionDataPackages = packageCollection
        };

        MessageType msgType = MessageType.ServerTick;

        string packageJson = JsonUtility.ToJson(package);
        string msg         = ((int)msgType).ToString() + HeadlessServer.MESSAGE_TYPE_INDICATOR + packageJson;

        //Convert to JSON
        byte [] packageData = System.Text.Encoding.ASCII.GetBytes(msg);

        byte [] totalPackage = AddSizeHeaderToPackage(packageData);

        return(totalPackage);
    }
Exemple #2
0
    public static byte[] PackageToByte(DataCollectionPackage package)
    {
        MessageType msgType = MessageType.ServerTick;

        string packageJson = JsonUtility.ToJson(package);
        string msg         = ((int)msgType).ToString() + Server.MESSAGE_TYPE_INDICATOR + packageJson;

        //Convert to JSON
        byte[] packageData = System.Text.Encoding.ASCII.GetBytes(msg);

        byte[] totalPackage = AddSizeHeaderToPackage(packageData);

        return(totalPackage);
    }
Exemple #3
0
    private void ClientConnected(uint playerID, Vector2 playerPos)
    {
        DataCollectionPackage package = new DataCollectionPackage();
        PositionDataPackage   pData   = new PositionDataPackage()
        {
            PlayerName = clientName,
            PlayerID   = playerID,
            Position   = playerPos
        };

        package.PositionDataPackages.Add(pData);

        //Første package er mappens dimensioner
        GridDataPackage gData = new GridDataPackage()
        {
            X = Server.MapGrid.gridWidth,
            Y = Server.MapGrid.gridHeigth
        };

        package.GridDataPackages.Add(gData);


        MessageType msgType     = MessageType.Connect;
        string      jsonPackage = JsonUtility.ToJson(package);
        string      msg         = ((int)msgType).ToString();

        msg += Server.MESSAGE_TYPE_INDICATOR + jsonPackage;
        byte[] byteData = System.Text.Encoding.ASCII.GetBytes(msg);

        byte[] totalPackage = Server.AddSizeHeaderToPackage(byteData);

        SendToClient(totalPackage);

        try
        {
            UserSession ses = new UserSession()
            {
                UserID = clientName, InGame = true, Request = SessionRequest.SetStatus
            };
            byte[] data = TCPHelper.MessageBytes(ses);
            Server.SessionClient.GetStream().Write(data, 0, data.Length);
        }
        catch
        {
        }
    }
Exemple #4
0
    public IEnumerator ListenToServer()
    {
        Debug.Log("ListenToServer Started");
        //StreamReader reader = new StreamReader(stream);

        byte[] readBuffer = new byte[4];
        while (true)
        {
            int packagesRead = 0;
            while (stream.DataAvailable && packagesRead < 2)
            {
                //Debug.Log("Data received!");

                int bytesRead = 0;

                while (bytesRead < 4)
                {
                    bytesRead += stream.Read(readBuffer, bytesRead, 4 - bytesRead);
                }

                //Debug.Log("4 Bytes received");

                bytesRead = 0;
                byte[] buffer = new byte[BitConverter.ToInt32(readBuffer, 0)];

                while (bytesRead < buffer.Length)
                {
                    bytesRead += stream.Read(buffer, bytesRead, buffer.Length - bytesRead);
                }
                string msg = System.Text.Encoding.UTF8.GetString(buffer);

                string[]    tempMsg = msg.Split(Server.MESSAGE_TYPE_INDICATOR);
                MessageType msgType = (MessageType)Int32.Parse(tempMsg[0]);

                switch (msgType)
                {
                case MessageType.Input:
                    break;

                case MessageType.Disconnect:
                {
                    Debug.Log("Disconnect detected");
                    uint       discPlayerID       = UInt32.Parse(tempMsg[1]);
                    GameObject disconnectedPlayer = null;
                    foreach (var item in actors)
                    {
                        if (item.playerID == discPlayerID)
                        {
                            disconnectedPlayer = item.gameObject;
                            break;
                        }
                    }
                    if (disconnectedPlayer != null)
                    {
                        GameObject.Destroy(disconnectedPlayer);
                    }
                }
                break;

                case MessageType.Connect:
                {
                    //SceneManager.LoadScene("Client");
                    //OnMapLoad();
                    DataCollectionPackage data = JsonUtility.FromJson <DataCollectionPackage>(tempMsg[1]);
                    //PositionDataPackage temp = JsonUtility.FromJson<PositionDataPackage>(tempMsg[1]);
                    map = GameObject.Find("GameMap").GetComponent <GridGenerater>();

                    map.Generate(data.GridDataPackages[0].X, data.GridDataPackages[0].Y);

                    LocalActor t = GameObject.FindGameObjectWithTag("Player").GetComponent <LocalActor>();
                    t.gameObject.GetComponentInChildren <Text>().text = data.PositionDataPackages[0].PlayerName;
                    t.playerID = data.PositionDataPackages[0].PlayerID;
                    t.gameObject.transform.position = data.PositionDataPackages[0].Position;
                    actors.Add(t);
                    //gameObject.transform.position = temp.Position;
                    connected = true;
                    break;
                }

                case MessageType.ServerTick:
                {
                    DataCollectionPackage data = JsonUtility.FromJson <DataCollectionPackage>(tempMsg[1]);
                    for (int i = 0; i < data.PositionDataPackages.Count; i++)
                    {
                        bool playerFound = false;
                        for (int j = 0; j < actors.Count; j++)
                        {
                            if (actors[j].playerID == data.PositionDataPackages[i].PlayerID)
                            {
                                actors[j].gameObject.transform.position = data.PositionDataPackages[i].Position;
                                playerFound = true;
                            }
                        }
                        foreach (var item in data.GridDataPackages)
                        {
                            map.grid[item.X, item.Y].GetComponent <Cell>().SetColor(item.Color);
                            //Debug.Log($"Set Color of tile:{item.X},{item.Y} to {item.Color}");
                        }
                        if (!playerFound)
                        {
                            GameObject actorObject = (GameObject)GameObject.Instantiate(playerPrefab, data.PositionDataPackages[i].Position, Quaternion.identity);
                            actorObject.GetComponentInChildren <Text>().text = data.PositionDataPackages[i].PlayerName;
                            LocalActor temp = actorObject.GetComponent <LocalActor>();
                            temp.playerID = data.PositionDataPackages[i].PlayerID;
                            actors.Add(temp);
                        }
                    }
                    break;
                }

                default:
                    break;
                }


                //gameObject.transform.position = data.PositionDataPackages[playerID].Position;
            }

            packagesRead++;
            //Debug.Log("Read: " + packagesRead + " packages");
            yield return(null);
        }
    }