예제 #1
0
        // Clients should always be updated with the playerInfoPacket.
        // This will be changed when the board is added to the client.
        private void SendSellUnitPacket(NetConnection connection, RequestUnitSellPacket packet)
        {
            NetOutgoingMessage message = server.CreateMessage();

            new UnitSoldPacket()
            {
                Name     = packet.Name,
                Location = packet.Location
            }.PacketToNetOutgoingMessage(message);
            server.SendMessage(message, connection, NetDeliveryMethod.ReliableOrdered);
        }
예제 #2
0
        public void Update()
        {
            NetIncomingMessage message;

            while ((message = server.ReadMessage()) != null)
            {
                byte type = message.ReadByte();
                switch (message.MessageType)
                {
                case NetIncomingMessageType.ConnectionApproval:
                    if (type == (byte)IncomingPacketTypes.Connect)
                    {
                        Logger.Info("Player " + message.SenderEndPoint + " connected to game server");
                        message.SenderConnection.Approve();

                        var playerData = new PlayerData(message.SenderConnection);
                        NetOutgoingMessage response = server.CreateMessage();
                        new ConnectPacket()
                        {
                            playerId = playerData.Id
                        }.PacketToNetOutgoingMessage(response);

                        Thread.Sleep(50);
                        server.SendMessage(response, message.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                    }
                    else
                    {
                        message.SenderConnection.Deny();
                    }
                    break;

                case NetIncomingMessageType.Data:
                    IIncomingPacket packet;
                    switch (type)
                    {
                    case ((byte)IncomingPacketTypes.RequestReroll):
                        Logger.Info("Reroll was purchased by " + message.SenderConnection);
                        packet = new RequestRerollPacket();
                        packet.NetIncomingMessageToPacket(message);
                        hub.Publish(new PurchaseRerollRequested()
                        {
                            client = message.SenderConnection
                        });
                        break;

                    case ((byte)IncomingPacketTypes.RequestXPPurchase):
                        Logger.Info("XP was purchased by " + message.SenderConnection);
                        packet = new RequestXPPurchasePacket();
                        packet.NetIncomingMessageToPacket(message);
                        hub.Publish(new PurchaseXPRequested()
                        {
                            client = message.SenderConnection
                        });
                        break;

                    case ((byte)IncomingPacketTypes.RequestUnitPurchase):
                        packet = new RequestUnitPurchasePacket();
                        packet.NetIncomingMessageToPacket(message);
                        Logger.Info("Unit purchase requested by " + message.SenderConnection + " at index " + ((RequestUnitPurchasePacket)packet).ShopIndex);
                        hub.Publish(new PurchaseUnitRequested()
                        {
                            client = message.SenderConnection, packet = (RequestUnitPurchasePacket)packet
                        });
                        break;

                    case ((byte)IncomingPacketTypes.RequestMoveUnit):
                        if (isAllowedToProcessMessages)
                        {
                            Logger.Info(message.SenderConnection + " tried to move a unit to the bench from the board");
                            packet = new RequestMoveUnitPacket();
                            packet.NetIncomingMessageToPacket(message);
                            hub.Publish(new MoveUnitRequested()
                            {
                                client = message.SenderConnection, packet = (RequestMoveUnitPacket)packet
                            });
                        }
                        break;

                    case ((byte)IncomingPacketTypes.RequestUnitSell):
                        Logger.Info(message.SenderConnection + " tried to sell a unit from their bench");
                        packet = new RequestUnitSellPacket();
                        packet.NetIncomingMessageToPacket(message);
                        hub.Publish(new SellUnitRequested()
                        {
                            client = message.SenderConnection, packet = (RequestUnitSellPacket)packet
                        });
                        break;

                    default:
                        Logger.Error("Unhandled date / packet type: " + type);
                        break;
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    var status = (NetConnectionStatus)message.ReadByte();
                    switch (status)
                    {
                    case NetConnectionStatus.Disconnecting:
                        Logger.Warn("Client disconnecting");
                        break;

                    case NetConnectionStatus.Disconnected:
                        Logger.Warn("Client disconnected");
                        break;

                    default:
                        break;
                    }
                    break;

                case NetIncomingMessageType.DebugMessage:
                    Logger.Debug("Debug Message: " + message.ReadString());
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Logger.Warn("Warning Message: " + message.ReadString());
                    break;

                default:
                    Logger.Error("Unhandled message with type: " + message.MessageType);
                    break;
                }
            }
            server.Recycle(message);
        }