Esempio n. 1
0
        static void TestPersistenceOperations(IDataStore <GameState> dataStore)
        {
            _consoleLogger.LogMessage($"Calling operations for {dataStore.GetType()}");

            var playerIds = new List <string>();
            var gameIds   = new List <string>();

            for (var i = 0; i < 5; i++)
            {
                playerIds.Add(Guid.NewGuid().ToString());
                gameIds.Add(Guid.NewGuid().ToString());
            }

            var rnd = new Random();

            // Lets create 50 records for one of the game ids
            for (var j = 20; j > 0; j--)
            {
                var gameState = new GameState()
                {
                    RecordId     = Guid.NewGuid().ToString(),
                    PlayerId     = playerIds[j % 5],
                    Health       = rnd.Next(1, 100),
                    CurrentLevel = rnd.Next(1, 10),
                    Inventory    = new Dictionary <string, string>()
                    {
                        { "Item", "Amulet" }, { "Weapon", "Ion Blaster" }
                    },
                    GameId          = gameIds[0],
                    RecordCreatedAt = DateTime.Now.AddDays(-j).ToUniversalTime()
                };

                gameState = dataStore.AddEntity(gameState).Result;
            }

            _consoleLogger.LogMessage($"Test Data Created");

            // Lets retrieve the records for the first player id above level 3 and above
            var searchCriteria = new SearchCriteria
            {
                PageSize      = 20,
                NextPageState = string.Empty,
                SearchFields  = new Dictionary <string, string>()
                {
                    { "PlayerId", playerIds[0] },
                    { "CurrentLevel", "3" }
                }
            };

            var(items, nextPageState) = dataStore.QueryEntity(searchCriteria).Result;

            _consoleLogger.LogMessage($"Called QueryEntity with RecordCount: {items.Count}");
            _consoleLogger.LogMessage($"NextPageState: {nextPageState}");

            if (items.Count > 0)
            {
                var retrievedItem = dataStore.GetEntity(items[0]).Result;
                _consoleLogger.LogMessage($"Single Record retrieved");

                retrievedItem.CurrentLevel += 1;
                retrievedItem.Health       += 10;

                // Lets update the record
                var updateOutcome = dataStore.UpdateEntity(retrievedItem).Result;
                _consoleLogger.LogMessage($"Entry updated {updateOutcome}");

                // Lets delete a row
                var deleteOutcome = dataStore.DeleteEntity(items[0]).Result;
                _consoleLogger.LogMessage($"Entry deleted {deleteOutcome} with PlatformKey:{items[0].PlatformKey}");
            }
            else
            {
                _consoleLogger.LogMessage($"No records returned for query");
            }
        }