Beispiel #1
0
        public async Task <OperationResult <IEnumerable <Friend> > > FindFriends(Guid userId)
        {
            var entity = await db.UserTable.Find <UserEntity>(
                UserEntity.FormatPartitionKey(userId),
                UserEntity.FormatRowKey(),
                new List <string> {
                nameof(UserEntity.FriendsJson)
            });

            if (entity == null)
            {
                return(OperationResult <IEnumerable <Friend> > .Failed(ErrorCode.NotFound, "User not found"));
            }

            var tasks = entity.Friends.Select(friendId =>
            {
                return(db.UserTable.Find <UserEntity>(
                           UserEntity.FormatPartitionKey(friendId),
                           UserEntity.FormatRowKey(),
                           new List <string> {
                    nameof(UserEntity.Name), nameof(UserEntity.Highscore)
                }));
            });

            await Task.WhenAll(tasks);

            var friends = tasks
                          .Select(task => task.Result)
                          .Where(e => e != null)
                          .Select(e => new Friend {
                Id = e.Id, Name = e.Name, Highscore = e.Highscore
            });

            return(OperationResult <IEnumerable <Friend> > .Success(friends));
        }
Beispiel #2
0
        public async Task <OperationResult <State> > FindGameState(Guid userId)
        {
            var entity = await db.UserTable.Find <UserEntity>(
                UserEntity.FormatPartitionKey(userId),
                UserEntity.FormatRowKey(),
                new List <string> {
                nameof(UserEntity.GamesPlayed), nameof(UserEntity.Highscore)
            });

            if (entity == null)
            {
                return(OperationResult <State> .Failed(ErrorCode.NotFound, "User not found"));
            }

            return(OperationResult <State> .Success(new State { GamesPlayed = entity.GamesPlayed, Score = entity.Highscore }));
        }
Beispiel #3
0
        public async Task <OperationResult> UpdateGameState(Guid userId, State state)
        {
            var entity = new DynamicTableEntity(UserEntity.FormatPartitionKey(userId), UserEntity.FormatRowKey())
            {
                ETag = "*"
            };

            entity.Properties.Add(nameof(UserEntity.GamesPlayed), new EntityProperty(state.GamesPlayed));
            entity.Properties.Add(nameof(UserEntity.Highscore), new EntityProperty(state.Score));
            await db.UserTable.ExecuteAsync(TableOperation.Merge(entity));

            return(OperationResult.Success);
        }
Beispiel #4
0
        public async Task <OperationResult> UpdateFriends(Guid userId, IEnumerable <Guid> friendIds)
        {
            var entity = new DynamicTableEntity(UserEntity.FormatPartitionKey(userId), UserEntity.FormatRowKey())
            {
                ETag = "*"
            };

            entity.Properties.Add(nameof(UserEntity.FriendsJson), new EntityProperty(UserEntity.FormatFriendsJson(friendIds)));
            await db.UserTable.ExecuteAsync(TableOperation.Merge(entity));

            return(OperationResult.Success);
        }