Esempio n. 1
0
        private void SendRequestDeniedMessage(NetworkConnection c)
        {
            // denied message ID zegt welke denied message er op het scherm moet komen!
            //deniedMessageID++;
            var requestDeniedMessage = new RequestDeniedMessage {
                DeniedMessageID = (uint)deniedMessageID
            };

            SendMessage(requestDeniedMessage, c);
        }
Esempio n. 2
0
        void Update()
        {
            if (doneInitializing) // I want this to start executing if we did the manual start function
            {
                networkJobHandle.Complete();

                if (!connection.IsCreated)
                {
                    Debug.Log("Can't create connection.");
                    if (clientGameManager != null && !disconnectedMessage)
                    {
                        StartCoroutine(clientGameManager.ShowConnectionDisconnected());
                        disconnectedMessage = true;
                    }
                    return;
                }

                DataStreamReader  reader;
                NetworkEvent.Type cmd;
                while ((cmd = connection.PopEvent(networkDriver, out reader)) != NetworkEvent.Type.Empty)
                {
                    if (cmd == NetworkEvent.Type.Connect) // Connecting to the server
                    {
                        Debug.Log("Connected to server");
                    }
                    else if (cmd == NetworkEvent.Type.Data) // Getting in Data
                    {
                        var messageType = (MessageHeader.MessageType)reader.ReadUShort();
                        switch (messageType) // client reads all except setname.
                        {
                            #region Lobby Protocol
                        case MessageHeader.MessageType.None:
                            break;

                        case MessageHeader.MessageType.NewPlayer:
                            var newPlayerMessage = new NewPlayerMessage();
                            newPlayerMessage.DeserializeObject(ref reader);
                            MessageConnection mcNewPlayer = new MessageConnection(connection, newPlayerMessage);
                            messagesQueue.Enqueue(mcNewPlayer);
                            break;

                        case MessageHeader.MessageType.Welcome:
                            var welcomeMessage = new WelcomeMessage();
                            welcomeMessage.DeserializeObject(ref reader);
                            MessageConnection mcWelcome = new MessageConnection(connection, welcomeMessage);
                            messagesQueue.Enqueue(mcWelcome);

                            break;

                        case MessageHeader.MessageType.SetName:
                            break;

                        case MessageHeader.MessageType.RequestDenied:
                            var requestDeniedMessage = new RequestDeniedMessage();
                            requestDeniedMessage.DeserializeObject(ref reader);
                            MessageConnection mcDenied = new MessageConnection(connection, requestDeniedMessage);
                            messagesQueue.Enqueue(mcDenied);
                            break;

                        case MessageHeader.MessageType.PlayerLeft:
                            var playerLeftMessage = new PlayerLeftMessage();
                            playerLeftMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeft = new MessageConnection(connection, playerLeftMessage);
                            messagesQueue.Enqueue(mcPlayerLeft);
                            break;

                        case MessageHeader.MessageType.StartGame:
                            var startGameMessage = new StartGameMessage();
                            startGameMessage.DeserializeObject(ref reader);
                            MessageConnection mcStartGame = new MessageConnection(connection, startGameMessage);
                            messagesQueue.Enqueue(mcStartGame);
                            break;

                            #endregion
                            #region Game Protocol
                        case MessageHeader.MessageType.PlayerTurn:
                            var playerTurnMessage = new PlayerTurnMessage();
                            playerTurnMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerTurn = new MessageConnection(connection, playerTurnMessage);
                            messagesQueue.Enqueue(mcPlayerTurn);
                            break;

                        case MessageHeader.MessageType.RoomInfo:
                            var roomInfoMessage = new RoomInfoMessage();
                            roomInfoMessage.DeserializeObject(ref reader);
                            MessageConnection mcRoomInfo = new MessageConnection(connection, roomInfoMessage);
                            messagesQueue.Enqueue(mcRoomInfo);
                            break;

                        case MessageHeader.MessageType.PlayerEnterRoom:
                            var playerEnterRoomMessage = new PlayerEnterRoomMessage();
                            playerEnterRoomMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerEnterRoom = new MessageConnection(connection, playerEnterRoomMessage);
                            messagesQueue.Enqueue(mcPlayerEnterRoom);
                            break;

                        case MessageHeader.MessageType.PlayerLeaveRoom:
                            var playerLeaveRoomMessage = new PlayerLeaveRoomMessage();
                            playerLeaveRoomMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeaveRoom = new MessageConnection(connection, playerLeaveRoomMessage);
                            messagesQueue.Enqueue(mcPlayerLeaveRoom);
                            break;

                        case MessageHeader.MessageType.ObtainTreasure:
                            var obtainTreasureMessage = new ObtainTreasureMessage();
                            obtainTreasureMessage.DeserializeObject(ref reader);
                            MessageConnection mcObtainTreasure = new MessageConnection(connection, obtainTreasureMessage);
                            messagesQueue.Enqueue(mcObtainTreasure);
                            break;

                        case MessageHeader.MessageType.HitMonster:
                            var hitMonsterMessage = new HitMonsterMessage();
                            hitMonsterMessage.DeserializeObject(ref reader);
                            MessageConnection mcHitMonster = new MessageConnection(connection, hitMonsterMessage);
                            messagesQueue.Enqueue(mcHitMonster);
                            break;

                        case MessageHeader.MessageType.HitByMonster:
                            var hitByMonsterMessage = new HitByMonsterMessage();
                            hitByMonsterMessage.DeserializeObject(ref reader);
                            MessageConnection mcHitByMonster = new MessageConnection(connection, hitByMonsterMessage);
                            messagesQueue.Enqueue(mcHitByMonster);
                            break;

                        case MessageHeader.MessageType.PlayerDefends:
                            var playerDefendsMessage = new PlayerDefendsMessage();
                            playerDefendsMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerDefends = new MessageConnection(connection, playerDefendsMessage);
                            messagesQueue.Enqueue(mcPlayerDefends);
                            break;

                        case MessageHeader.MessageType.PlayerLeftDungeon:
                            var playerLeftDungeonMessage = new PlayerLeftDungeonMessage();
                            playerLeftDungeonMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeftDungeon = new MessageConnection(connection, playerLeftDungeonMessage);
                            messagesQueue.Enqueue(mcPlayerLeftDungeon);
                            break;

                        case MessageHeader.MessageType.PlayerDies:
                            var playerDiesMessage = new PlayerDiesMessage();
                            playerDiesMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerDies = new MessageConnection(connection, playerDiesMessage);
                            messagesQueue.Enqueue(mcPlayerDies);
                            break;

                        case MessageHeader.MessageType.EndGame:
                            var endGameMessage = new EndGameMessage();
                            endGameMessage.DeserializeObject(ref reader);
                            MessageConnection mcEndGame = new MessageConnection(connection, endGameMessage);
                            messagesQueue.Enqueue(mcEndGame);
                            break;

                        case MessageHeader.MessageType.MoveRequest:
                            break;

                        case MessageHeader.MessageType.AttackRequest:
                            break;

                        case MessageHeader.MessageType.DefendRequest:
                            break;

                        case MessageHeader.MessageType.ClaimTreasureRequest:
                            break;

                        case MessageHeader.MessageType.LeaveDungeonRequest:
                            break;

                            #endregion
                        case MessageHeader.MessageType.Count:
                            break;

                        default:
                            break;
                        }
                    }
                    else if (cmd == NetworkEvent.Type.Disconnect)
                    {
                        Debug.Log("Disconnected from server");
                        connection = default;
                        ClientGameManager.ShowHostTerminatedRoom();
                    }
                }

                networkJobHandle = networkDriver.ScheduleUpdate();
                networkJobHandle.Complete();
                ProcessMessagesQueue();
            }
        }