public static async Task <bool> MonitorUser( [ActivityTrigger] string username, [OrchestrationClient] IDurableOrchestrationClient client, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { var user = await client.ReadUserEntityAsync <User>(username); if (!user.EntityExists) { throw new Exception($"User {username} not found!"); } user.EntityState.RestoreLists(); var inventoryNames = await client.ReadUserEntityAsync <InventoryList>(username); if (!inventoryNames.EntityExists) { logger.LogInformation("No inventory found for user {user}", username); return(false); } var inventoryList = await inventoryNames.EntityState.DeserializeListForUserWithClient(username, client); var treasure = inventoryList.FirstOrDefault(i => i.IsTreasure); if (treasure == null) { logger.LogInformation("No treasure found for user {user}", username); return(false); } if (user.EntityState.IsAlive) { logger.LogInformation("User {user} is alive!", username); if (user.EntityState.InventoryList != null && user.EntityState.InventoryList.Any(i => i == treasure.Name)) { logger.LogInformation("User {user} has the treasure!!!", username); await console.AddAsync($"{username} has the treasure."); return(true); } else { logger.LogInformation("User {user} does not have the treasure yet.", username); await console.AddAsync($"{username} is alive but has not found the treasure."); return(false); } } else { logger.LogInformation("User {user} has died.", username); await console.AddAsync($"{username} has died."); return(true); } }
public static async Task <bool> KillUser( [ActivityTrigger] string username, [OrchestrationClient] IDurableOrchestrationClient client, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Kill user: {user}", username); var user = await client.ReadUserEntityAsync <User>(username); if (!user.EntityExists) { throw new Exception($"KillUser: User {username} not found!"); } await client.SignalEntityAsync <IUserOperations>( username.AsEntityIdFor <User>(), operation => operation.Kill()); await client.SignalEntityAsync( UserCounter.Id, UserCounter.UserDone); await console.AddAsync($"Unfortunately user {username} died from waiting too long!"); logger.LogInformation("KillUser {user} successful", username); return(true); }
public static async Task <T> GetEntityForUserOrThrow <T>(this string username, IDurableOrchestrationClient client) { var check = await client.ReadUserEntityAsync <T>(username); if (!check.EntityExists) { throw new Exception($"No {typeof(T)} found for user {username}"); } return(check.EntityState); }
public static async Task <IActionResult> GameStatus( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "GameStatus/{username}")] HttpRequest req, [OrchestrationClient] IDurableOrchestrationClient client, string username, ILogger log) { if (string.IsNullOrWhiteSpace(username)) { log.LogWarning("No username passed."); return(new BadRequestObjectResult("Username is required.")); } var userCheck = await client.ReadUserEntityAsync <User>(username); if (!userCheck.EntityExists) { log.LogWarning("Username {0} not found", username); return(new BadRequestObjectResult($"Username '{username}' does not exist.")); } var monsterCheck = await client.ReadUserEntityAsync <Monster>(username); var inventoryCheck = await client.ReadUserEntityAsync <InventoryList>(username); if (inventoryCheck.EntityExists) { inventoryCheck.EntityState.RestoreLists(); } var roomCheck = await client.ReadUserEntityAsync <Room>(username); var userCount = await client.ReadEntityStateAsync <int>( UserCounter.Id); return(new OkObjectResult(new { user = userCheck.EntityState, activeUsers = userCount.EntityState, monster = monsterCheck.EntityState, inventory = inventoryCheck.EntityState?.InventoryList, room = roomCheck.EntityState })); }
public static async Task <IActionResult> NewUser( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [OrchestrationClient] IDurableOrchestrationClient starter, ILogger log) { log.LogInformation("NewUser called."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string name = data?.name; if (string.IsNullOrWhiteSpace(name)) { await console.AddAsync("An attempt to create a user with no name was made."); return(new BadRequestObjectResult("User name is required.")); } var userCheck = await starter.ReadUserEntityAsync <User>(name); if (userCheck.EntityExists) { await console.AddAsync($"Attempt to add duplicate user {name} failed."); return(new BadRequestObjectResult("Duplicate username is not allowed.")); } // create the user here var id = name.AsEntityIdFor <User>(); await starter.SignalEntityAsync <IUserOperations>( id, user => user.New(name)); await starter.SignalEntityAsync( UserCounter.Id, UserCounter.NewUser); await starter.StartNewAsync(nameof(NewUserParallelFunctions.RunUserParallelWorkflow), name); log.LogInformation("Started new parallel workflow for user {user}", name); await starter.StartNewAsync(nameof(MonitorFunctions.UserMonitorWorkflow), name); log.LogInformation("Started new monitor workflow for user {user}", name); return(new OkResult()); }
public static async Task <IActionResult> ConfirmUser( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [OrchestrationClient] IDurableOrchestrationClient durableClient, ILogger log) { log.LogInformation("ConfirmUser called."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string name = data?.name; if (string.IsNullOrWhiteSpace(name)) { await console.AddAsync("An attempt to confirm a user with no name was made."); return(new BadRequestObjectResult("User name is required.")); } var userCheck = await durableClient.ReadUserEntityAsync <User>(name); if (!userCheck.EntityExists) { await console.AddAsync($"Attempt to confirm missing user {name} failed."); return(new BadRequestObjectResult("Username does not exist.")); } log.LogInformation("User {user} is valid, searching for workflow.", name); var instance = await durableClient.FindJob( DateTime.UtcNow, nameof(UserConfirmationWorkflow), name); if (instance == null) { log.LogInformation("Workflow not found for user {user}.", name); return(new NotFoundResult()); } log.LogInformation("Workflow with id {instanceId} found for user {user}.", instance.InstanceId, name); await durableClient.RaiseEventAsync(instance.InstanceId, APPROVAL_TASK, true); return(new OkResult()); }
public static async Task <IActionResult> Action( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [OrchestrationClient] IDurableOrchestrationClient client, ILogger log) { log.LogInformation("Action called."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string name = data?.name; string action = data?.action; string target = data?.target; string with = data?.with; if (string.IsNullOrWhiteSpace(name)) { await console.AddAsync("An attempt to initiate an action with no name was made."); return(new BadRequestObjectResult("User name is required.")); } var user = await client.ReadUserEntityAsync <User>(name); var userCheck = user.EntityState; if (!user.EntityExists || !userCheck.IsAlive) { await console.AddAsync($"Attempt to use missing or dead user {name} failed."); return(new BadRequestObjectResult("Dead or missing username is not allowed.")); } if (action == "get") { return(await GetMethod(console, client, name, target)); } else if (action == "kill") { return(await KillMethod(console, client, name, target, with, userCheck)); } return(new BadRequestObjectResult($"I do not understand {action}.")); }