public void Handle(INetworkStateContext <TWorld> gameStateContext, NetworkCommandConnection networkCommandConnectionToHandle)
        {
            var typeToProofAgainst = typeof(UserData).FullName;

            //Now we look what the roles are and deliver answers
            if (networkCommandConnectionToHandle.CommandArgument != typeToProofAgainst)
            {
                ThrowError(gameStateContext, networkCommandConnectionToHandle);
            }

            var convertedInstance = NetworkCommandDataConverterService.ConvertToObject(networkCommandConnectionToHandle);

            if (convertedInstance as UserData == null)
            {
                ThrowError(gameStateContext, networkCommandConnectionToHandle);
            }

            //Actual router. Only one state will fire. See that the order of the entries in Router also provides the priority.
            //TODO: this should be a feature/library in the future
            var userRoles        = UserDataRoleService.GetRoles(convertedInstance as UserData);
            var userRolesHandler = gameStateContext.GameStateRegistrar.GetNeworkLayerState((byte)Routes.FirstOrDefault(kv => userRoles.HasFlag(kv.Key)).Value);

            gameStateContext.ChangeHandlerEasier(userRolesHandler, networkCommandConnectionToHandle);
            userRolesHandler.Handle(gameStateContext, networkCommandConnectionToHandle);
        }
        public void ProcessIncomingMessages()
        {
            var dataInputs = Input.FetchMessageChunk()
                             .Where(msg => msg.MessageType == NetIncomingMessageType.Data);

            var inputs = dataInputs
                         .Select(msg =>
            {
                try
                {
                    return(NetIncomingMessageNetworkCommand.Translate(msg));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                return(null);
            })
                         .Where(msg => msg != null)    // why do I receive gamestate data?? => because a player needs to be physically bound to a game .else other mechanics depending on getting the players game state for every registered player will fail.
                         .Where(msg => msg.CommandName == NetworkCommandConstants.ReceiveWorkCommand)
                         .Select(msg =>
            {
                try
                {
                    var ret = NetworkCommandDataConverterService.ConvertToObject(msg);
                    Console.WriteLine(ret);
                    return(ret);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                return(null);
            })
                         .ToList();


            //happens when data layer worker receives an "ACK" from server

            if (inputs.Any(msg => (msg is bool) && (bool)msg == true))
            {
                Output.SendToClient <string>(NetworkCommandConstants.ReceiveWorkCommand, "hub", Client.ServerConnection);
                Console.WriteLine("sent package");
                return;
            }

            foreach (var obj in inputs)
            {
                //Console.WriteLine(dataLayer.Enemies.Select(enemy => enemy));
                Console.WriteLine((obj as GameStateDataLayer).Enemies.FirstOrDefault().DisplayName);
                Output.SendToClient <string>(NetworkCommandConstants.ReceiveWorkCommand, "hub", Client.ServerConnection);
            }
        }
        private void Initialize()
        {
            Client = new NetClient(
                new NetPeerConfiguration(ClientConfiguration.ServerPeerName)
            {
                AcceptIncomingConnections = true,
                EnableUPnP         = true,
                AutoFlushSendQueue = true
            });
            Client.Start();
            Client.Connect(ClientConfiguration.ServerIp, ClientConfiguration.ServerPort);

            var serializerType = GetSerializer(ClientConfiguration.SerializationAdapterType);

            //var serializerType = ClientConfiguration.SerializationAdapterType.LoadType(true, false)?.FirstOrDefault();
            SerializationAdapter = Activator.CreateInstance(serializerType) as ISerializationAdapter;
            Input  = new NetIncomingMessageBusService <NetClient>(Client);
            Output = new NetOutgoingMessageBusService <NetClient>(Client, SerializationAdapter);
            NetworkCommandDataConverterService = new NetworkCommandDataConverterService(SerializationAdapter);
            NetIncomingMessageNetworkCommand   = new NetIncomingMessageNetworkCommandConnectionTranslator(new NetworkCommandTranslator(SerializationAdapter));
        }
 public UserRoleGateWayNetworkGameState(IUserDataRoleService userDataRoleService, NetworkCommandDataConverterService networkCommandDataConverterService)
 {
     UserDataRoleService = userDataRoleService ?? throw new ArgumentNullException(nameof(userDataRoleService));
     NetworkCommandDataConverterService = networkCommandDataConverterService ?? throw new ArgumentNullException(nameof(networkCommandDataConverterService));
 }
Example #5
0
 public UpdateMetadataState(NetworkCommandDataConverterService dataConverter)
 {
     DataConverter = dataConverter ?? throw new ArgumentNullException(nameof(dataConverter));
 }
Example #6
0
        public IStateMachine <string, INetworkGameScreen> BuildClientStateMachine(GameClientConfiguration gameClientConfiguration)
        {
            var inMemoryStorage = new Dictionary <string, IState <string, INetworkGameScreen> >();

            var serializer = Activator.CreateInstance(gameClientConfiguration.SerializationAdapterType.LoadType(true, false)[0]) as ISerializationAdapter;

            inMemoryStorage.Add(ClientGameScreenBook.CheckLoginSent, new CheckLoginSentState());
            inMemoryStorage.Add(ClientGameScreenBook.CreateFetchDataIfNotExists, new CommandStateStateDelegate <string, INetworkGameScreen>((machine) =>
            {
                try
                {
                    return(machine.GetService.Get(ClientGameScreenBook.FetchData));
                }
                catch (Exception ex)
                {
                    machine.SharedContext.Messages.Add(ex.Message);
                    var identifier = machine.NewService.New(ClientGameScreenBook.FetchData, new FetchDataState(
                                                                new NetIncomingMessageBusService <NetClient>(machine.SharedContext.Client),
                                                                serializer
                                                                ));
                    return(machine.GetService.Get(ClientGameScreenBook.FetchData));
                }
            }));

            inMemoryStorage.Add("BuildConfig", new CommandStateActionDelegate <string, INetworkGameScreen>((machine) =>
            {
                machine.SharedContext.Configuration = gameClientConfiguration;
            }));

            inMemoryStorage.Add(ClientGameScreenBook.FillWithDefaultServerDataState, new FillWithDefaultServerDataState());
            //inMemoryStorage.Add(ClientGameScreenBook.ReadServerDataFromConsole, new ReadServerDataFromConsoleState());
            inMemoryStorage.Add(ClientGameScreenBook.BuildClientSideState, new BuildClientSideState(
                                    new RectangularFrameGeneratorService(),
                                    gameClientConfiguration.Background));
            inMemoryStorage.Add(ClientGameScreenBook.BuildGameSheetFrames, new BuildGameSheetFramesState(new RectangularFrameGeneratorService()));

            inMemoryStorage.Add(ClientGameScreenBook.BuildCursor, new BuildCursorState());
            inMemoryStorage.Add(ClientGameScreenBook.BuildClient, new BuildClientState());

            //inMemoryStorage.Add(ClientGameScreenBook.BuildInputService, new BuildInputServiceState(null));

            inMemoryStorage.Add(ClientGameScreenBook.BuildDefaultInputService, new BuildDefaultInputServiceState());
            inMemoryStorage.Add(ClientGameScreenBook.Connect, new ConnectState());
            inMemoryStorage.Add(ClientGameScreenBook.Init, new RoutineState <string, INetworkGameScreen>()
            {
                Operations = new List <string>()
                {
                    "BuildConfig",
                    ClientGameScreenBook.BuildClientSideState,
                    ClientGameScreenBook.BuildGameSheetFrames,
                    ClientGameScreenBook.BuildClient,
                    ClientGameScreenBook.BuildCursor,
                    ClientGameScreenBook.BuildDefaultInputService,
                    ClientGameScreenBook.BuildCharacterWindow,
                    ClientGameScreenBook.BuildInfoWindow,
                    ClientGameScreenBook.BuildInventoryWindow,
                    nameof(BuildDialogWindowState),
                    //ClientGameScreenBook.ReadServerDataFromConsole,
                    // ClientGameScreenBook.FillWithDefaultServerDataState,
                    ClientGameScreenBook.Connect,
                    ClientGameScreenBook.CreateFetchDataIfNotExists,
                    ClientGameScreenBook.Login
                }
            });


            inMemoryStorage.Add("UpdateLogoPosition", new CommandStateActionDelegate <string, INetworkGameScreen>((machine) =>
            {
                var logo = machine.SharedContext.As <Image>("logo");
                if (logo == null)
                {
                    return;
                }
                if (machine.SharedContext.InventoryWindow == null)
                {
                    return;
                }
                if (machine.SharedContext.LastMetadataBag == null)
                {
                    logo.Alpha.Alpha = 0.00f;
                }
                else
                {
                    logo.Alpha.Alpha = 0.25f;
                }
                logo.Scale = new Vector2()
                {
                    X = 0.25f, Y = 0.25f
                };
                logo.Position.X = machine.SharedContext.InventoryWindow.Position.X + 64;
                logo.Position.Y = machine.SharedContext.InventoryWindow.Position.Y + 232;
                logo.LayerDepth = DrawingPlainOrder.UI;
            }));

            inMemoryStorage.Add("UpdateGameOverPosition", new CommandStateActionDelegate <string, INetworkGameScreen>((machine) =>
            {
                if (machine.SharedContext.LastMetadataBag == null)
                {
                    return;
                }
                if (machine.SharedContext.LastMetadataBag.Stats == null)
                {
                    return;
                }

                var hideGameOver = machine.SharedContext.LastMetadataBag.Stats.Health > machine.SharedContext.LastMetadataBag.Stats.GetMinAttributeValue();

                var gameOver = machine.SharedContext.As <Image>("game_over");
                if (gameOver == null)
                {
                    return;
                }
                if (hideGameOver)
                {
                    gameOver.Alpha.Alpha = 0f;
                }
                else
                {
                    gameOver.Scale = new Vector2()
                    {
                        X = 1.75f, Y = 1.75f
                    };
                    gameOver.Alpha.Alpha = 1.00f;
                    gameOver.LayerDepth  = DrawingPlainOrder.Foreground;
                    gameOver.Position.X  = ScreenService.Instance.DefaultScreenCamera.Position.X;
                    gameOver.Position.Y  = ScreenService.Instance.DefaultScreenCamera.Position.Y;
                }
            }));

            inMemoryStorage.Add("UpdateBackground", new CommandStateActionDelegate <string, INetworkGameScreen>((machine) =>
            {
                foreach (Star starToMove in machine.SharedContext
                         .As <StarField>(nameof(StarField))?
                         .StarsAvailable
                         .Where(star => star.CannotDraw()))
                {
                    try
                    {
                        var minFactor = 1.5f; // need this for evading the randomisation starting values

                        var directionX        = Randomness.Instance.RollTheDice(5) ? 1 : -1;
                        var directionY        = Randomness.Instance.RollTheDice(10) ? 1 : -1;
                        starToMove.Position.X = ScreenService.Instance.DefaultScreenCamera.Position.X + (Randomness.Instance.From(0, (int)ScreenService.Instance.Resolution.X) * ScreenService.Instance.DefaultScreenCamera.Zoom * directionX);
                        starToMove.Position.Y = ScreenService.Instance.DefaultScreenCamera.Position.Y + (Randomness.Instance.From(0, (int)ScreenService.Instance.Resolution.Y) * ScreenService.Instance.DefaultScreenCamera.Zoom * directionY);
                    }
                    catch (Exception) { }
                }
            }));

            inMemoryStorage.Add(ClientGameScreenBook.Update, new RoutineState <string, INetworkGameScreen>()
            {
                Operations = new List <string>()
                {
                    ClientGameScreenBook.UpdateCursor,
                    ClientGameScreenBook.UpdateInput,
                    ClientGameScreenBook.FetchData,
                    ClientGameScreenBook.UpdateImages,
                    ClientGameScreenBook.UpdateCharacterWindow,
                    ClientGameScreenBook.UpdateMetadataState,
                    ClientGameScreenBook.UpdateInventoryImages,
                    ClientGameScreenBook.UpdateScreen,
                    ClientGameScreenBook.UpdateCamera,
                    ClientGameScreenBook.UpdateInventoryWindow,
                    nameof(UpdateDialogWindowState),
                    nameof(UpdateInventoryItemHoverTextState),
                    nameof(UpdateDialogHoverTextState),
                    ClientGameScreenBook.UpdateInfoWindow,
                    ClientGameScreenBook.SendGameStateUpdateData,
                    "UpdateBackground",
                    "UpdateLogoPosition",
                    "UpdateGameOverPosition"
                }
            });

            inMemoryStorage.Add(ClientGameScreenBook.UpdateInput, new UpdateInputsState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateCursor, new UpdateCursorState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateImages, new UpdateImagesState());


            inMemoryStorage.Add(nameof(UpdateInventoryItemHoverTextState), new UpdateInventoryItemHoverTextState());

            //converts metadata to an object
            var dataConverter = new NetworkCommandDataConverterService(serializer);

            inMemoryStorage.Add(ClientGameScreenBook.UpdateMetadataState, new UpdateMetadataState(dataConverter));
            inMemoryStorage.Add(ClientGameScreenBook.UpdateScreen, new UpdateScreenState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateCamera, new UpdateCameraState());
            inMemoryStorage.Add(ClientGameScreenBook.SendGameStateUpdateData, new SendGameStateUpdateDataState(serializer));
            inMemoryStorage.Add(ClientGameScreenBook.Login, new LoginState(
                                    gameClientConfiguration.User,
                                    gameClientConfiguration.ServerIp,
                                    gameClientConfiguration.ServerPort,
                                    serializer));

            //TODO: Dummy sheet dictionary for adding the only image needed . duh. definitely a
            var dummyHudSheetToChangeInTheFuture = new Dictionary <string, Rectangle>();

            dummyHudSheetToChangeInTheFuture.Add("hud_misc_edit_0_0", new Rectangle(0, 0, 32, 32));

            inMemoryStorage.Add(ClientGameScreenBook.BuildInventoryWindow, new BuildInventoryWindowState(dummyHudSheetToChangeInTheFuture));
            inMemoryStorage.Add(ClientGameScreenBook.UpdateInventoryWindow, new UpdateInventoryWindowState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateInventoryImages, new UpdateInventoryImagesState(new PlayerInventoryDrawingFactory()));
            inMemoryStorage.Add(nameof(UpdateDialogHoverTextState), new UpdateDialogHoverTextState());
            inMemoryStorage.Add(ClientGameScreenBook.BuildChatWindow, new BuildChatWindowState());

            inMemoryStorage.Add(nameof(BuildDialogWindowState), new BuildDialogWindowState());
            inMemoryStorage.Add(nameof(UpdateDialogWindowState), new UpdateDialogWindowState());

            inMemoryStorage.Add(ClientGameScreenBook.BuildInfoWindow, new BuildInfoWindowState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateInfoWindow, new UpdateInfoWindowState());

            inMemoryStorage.Add(ClientGameScreenBook.BuildCharacterWindow, new BuildCharacterWindowState());
            inMemoryStorage.Add(ClientGameScreenBook.UpdateCharacterWindow, new UpdateCharacterWindowState(new Core.FXBehaviour.FadeToBehaviour()));

            var getDelegation = new GetNetworkGameScreenDelegate(() => inMemoryStorage);
            var setDelegation = new SetNetworkGameScreenDelegate(() => inMemoryStorage);
            var newDelegation = new NewNetworkGameScreenDelegate(() => inMemoryStorage);

            return(new ClientGameContext(getDelegation, setDelegation, newDelegation));
        }
 public ReceiveWorkNetworkGameState(NetworkCommandDataConverterService networkCommandDataConverterService)
 {
     NetworkCommandDataConverterService = networkCommandDataConverterService ?? throw new ArgumentNullException(nameof(networkCommandDataConverterService));
 }