public static async Task PlaceMonsterInRoom( [ActivityTrigger] string username, [Table(nameof(Monster))] CloudTable monsterTable, [Table(nameof(Room))] CloudTable roomTable, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Placing monster in room for {user}", username); var monsterClient = monsterTable.AsClientFor <Monster>(); var monster = (await monsterClient.GetAllAsync(username)).First(); var roomClient = roomTable.AsClientFor <Room>(); var room = (await roomClient.GetAllAsync(username)).First(); logger.LogInformation("Found monster {monster} to place in room {room} for user {user}", monster.Name, room.Name, username); room.Monster = monster.Name; monster.CurrentRoom = room.Name; await monsterClient.ReplaceAsync(monster); await roomClient.ReplaceAsync(room); logger.LogInformation("Placing monster {monster} in room {room} for user {user} successful.", monster.Name, room.Name, username); await console.AddAsync($"{username} notices a {monster.Name} in {room.Name}."); }
public static async Task <bool> KillUser( [ActivityTrigger] string username, [Table(nameof(User))] CloudTable table, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Kill user: {user}", username); var client = table.AsClientFor <User>(); var userLoad = new User { Name = username }; var user = await client.GetAsync(userLoad.PartitionKey, userLoad.RowKey); if (user == null) { throw new Exception($"KillUser: User {username} not found!"); } user.IsAlive = false; await client.ReplaceAsync(user); await console.AddAsync($"Unfortunately user {username} died from waiting too long!"); logger.LogInformation("KillUser {user} successful", username); return(true); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "confirm/{quoteId}")] HttpRequest req, string quoteId, [Table(nameof(Quote))] CloudTable quotes, [OrchestrationClient] DurableOrchestrationClient durableClient, ILogger log) { var quotesClient = quotes.AsClientFor <Quote>(); var quote = new Quote { Id = quoteId }; var quoteFromDb = await quotesClient.GetAsync(quote.PartitionKey, quote.RowKey); if (quoteFromDb == null) { log.LogError($"Quote with id {quoteId} not found."); return(new NotFoundObjectResult("Quote not found.")); } var instance = await durableClient.FindJob(nameof(QuoteRequestConfirmationWorkflow), quoteId); if (instance == null) { log.LogInformation($"{nameof(QuoteRequestConfirmationWorkflow)} not found for quote {quoteId}."); return(new NotFoundResult()); } log.LogInformation( $"{nameof(QuoteRequestConfirmationWorkflow)} with id {instance.InstanceId} found for quote {quoteId}."); await durableClient.RaiseEventAsync(instance.InstanceId, Constants.QUOTE_REQUEST_CONFIRMED, null); return(new OkResult()); }
public static async Task PlaceUserInRoom( [ActivityTrigger] string username, [Table(nameof(User))] CloudTable userTable, [Table(nameof(Room))] CloudTable roomTable, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Placing user in room for {user}", username); var userClient = userTable.AsClientFor <User>(); var userKey = new User { Name = username }; var user = await userClient.GetAsync(userKey.PartitionKey, userKey.RowKey); var roomClient = roomTable.AsClientFor <Room>(); var room = (await roomClient.GetAllAsync(username)).First(); logger.LogInformation("Found room {room} for user {user}", room.Name, username); room.User = user.Name; user.CurrentRoom = room.Name; await userClient.ReplaceAsync(user); await roomClient.ReplaceAsync(room); logger.LogInformation("Placed user {user} in room {room}.", username, room.Name); await console.AddAsync($"{username} looks around: {room.Description}"); }
public static async Task PlaceInventoryInRoom( [ActivityTrigger] string username, [Table(nameof(Inventory))] CloudTable inventoryTable, [Table(nameof(Room))] CloudTable roomTable, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Placing inventory in room for {user}", username); var inventoryClient = inventoryTable.AsClientFor <Inventory>(); var inventoryList = await inventoryClient.GetAllAsync(username); var inventory = inventoryList.Where(i => !i.IsTreasure).First(); var roomClient = roomTable.AsClientFor <Room>(); var room = (await roomClient.GetAllAsync(username)).First(); logger.LogInformation("Found weapon {inventory} and room {room} for user {user}", inventory.Name, room.Name, username); inventory.Room = room.Name; room.InventoryList.Add(inventory.Name); await roomClient.ReplaceAsync(room); await inventoryClient.ReplaceAsync(inventory); logger.LogInformation("Place treasure for user {user} successful."); await console.AddAsync($"{username} sees a {inventory.Name} inside the room!"); }
public static async Task PlaceInventoryOnMonster( [ActivityTrigger] string username, [Table(nameof(Inventory))] CloudTable inventoryTable, [Table(nameof(Monster))] CloudTable monsterTable, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Placing inventory on monster for {user}", username); var inventoryClient = inventoryTable.AsClientFor <Inventory>(); var inventoryList = await inventoryClient.GetAllAsync(username); var inventory = inventoryList.Where(i => i.IsTreasure).First(); var monsterClient = monsterTable.AsClientFor <Monster>(); var monster = (await monsterClient.GetAllAsync(username)).First(); logger.LogInformation("Found treasure {inventory} for monster {monster} and user {user}", inventory.Name, monster.Name, username); inventory.Monster = monster.Name; monster.InventoryList.Add(inventory.Name); await monsterClient.ReplaceAsync(monster); await inventoryClient.ReplaceAsync(inventory); logger.LogInformation("Placing treasure {inventory} on monter {monster} for user {user} successful.", inventory.Name, monster.Name, username); await console.AddAsync($"{username} notices a {monster.Name} with a {inventory.Name}."); }
public static async Task <bool> MonitorUser( [ActivityTrigger] string username, [Table(nameof(User))] CloudTable userTable, [Table(nameof(Inventory))] CloudTable inventoryTable, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { var temp = new User { Name = username }; var userClient = userTable.AsClientFor <User>(); var user = await userClient.GetAsync(temp.PartitionKey, temp.RowKey); if (user == null) { throw new Exception($"User {username} not found!"); } var inventoryClient = inventoryTable.AsClientFor <Inventory>(); var inventoryList = await inventoryClient.GetAllAsync(username); var treasure = inventoryList.FirstOrDefault(i => i.IsTreasure); if (treasure == null) { logger.LogInformation("No treasure found for user {user}", username); return(false); } if (user.IsAlive) { logger.LogInformation("User {user} is alive!", username); if (user.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 <IActionResult> Action( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [Table(nameof(User))] CloudTable userTable, [Table(nameof(Room))] CloudTable roomTable, [Table(nameof(Monster))] CloudTable monsterTable, [Table(nameof(Inventory))] CloudTable inventoryTable, [OrchestrationClient] DurableOrchestrationClient 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 userClient = userTable.AsClientFor <User>(); var tempUser = new User { Name = name }; var userCheck = await userClient.GetAsync(tempUser.PartitionKey, name); if (userCheck == null || !userCheck.IsAlive) { await console.AddAsync($"Attempt to use missing or dead user {name} failed."); return(new BadRequestObjectResult("Dead or missing username is not allowed.")); } var monsterClient = monsterTable.AsClientFor <Monster>(); var roomClient = roomTable.AsClientFor <Room>(); var inventoryClient = inventoryTable.AsClientFor <Inventory>(); if (action == "get") { return(await GetMethod(console, client, name, target, userClient, userCheck, roomClient, inventoryClient)); } else if (action == "kill") { return(await KillMethod(console, client, name, target, with, userCheck, monsterClient, roomClient, inventoryClient)); } return(new BadRequestObjectResult($"I do not understand {action}.")); }
public static async Task <IActionResult> ConfirmUser( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [Table(nameof(User))] CloudTable table, [OrchestrationClient] DurableOrchestrationClient 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 client = table.AsClientFor <User>(); var tempUser = new User { Name = name }; var userCheck = await client.GetAsync(tempUser.PartitionKey, name); if (userCheck == null) { 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 CreateMonster( [ActivityTrigger] string username, [Table(nameof(Monster))] CloudTable table, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Create monster for user {user}", username); var client = table.AsClientFor <Monster>(); var monster = _monsterMaker.GetNewMonster(username); await client.InsertAsync(monster); await console.AddAsync($"Look out! {monster.Name} is now stalking {username}!"); logger.LogInformation("Created monster {monster} for user {user} successful", monster.Name, username); }
public static async Task CreateRoom( [ActivityTrigger] string username, [Table(nameof(Room))] CloudTable table, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Create room for user {user}", username); var client = table.AsClientFor <Room>(); var room = _roomMaker.GetNewRoom(username); await client.InsertAsync(room); await console.AddAsync($"{room.Name} has been prepared for {username}!"); logger.LogInformation("Creation of {room} for user {user} successful", room.Name, username); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "request")] HttpRequest req, [Table(nameof(Quote))] CloudTable quotes, [OrchestrationClient] DurableOrchestrationClient starter, ILogger log) { log.LogInformation("A new quote has been requested."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string description = data?.description; if (string.IsNullOrEmpty(description)) { log.LogError("A new quote has been requested but no description provided."); return(new BadRequestObjectResult("Description required.")); } DateTime dueDate = data?.dueDate; if (dueDate < DateTime.UtcNow) { log.LogError("A new quote has been requested but due time is in the past."); return(new BadRequestObjectResult("Due time must be set to future time.")); } var quotesClient = quotes.AsClientFor <Quote>(); var newQuote = new Quote { Description = description, DueDate = dueDate, Id = Guid.NewGuid().ToString(), Status = Status.AwaitingReview, }; await quotesClient.InsertAsync(newQuote); log.LogInformation($"A new quote has been created with id {newQuote.Id}"); var instanceId = await starter.StartNewAsync(nameof(QuoteRequestConfirmationWorkflow), newQuote); log.LogInformation($"Started {nameof(QuoteRequestConfirmationWorkflow)} orchestration with ID = '{instanceId}'."); return(new CreatedResult($"http://localhost:7071/api/quotes/{newQuote.Id}", newQuote)); }
public static async Task CreateUser( [ActivityTrigger] string username, [Table(nameof(User))] CloudTable table, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Create user: {user}", username); var client = table.AsClientFor <User>(); var user = new User { Name = username, IsAlive = true }; await client.InsertAsync(user); await console.AddAsync($"Successfully created user {username}"); logger.LogInformation("Create user {user} successful", username); }
public static async Task CreateInventory( [ActivityTrigger] string username, [Table(nameof(Inventory))] CloudTable table, [Queue(Global.QUEUE)] IAsyncCollector <string> console, ILogger logger) { logger.LogInformation("Create inventory for user {user}", username); var client = table.AsClientFor <Inventory>(); var inventory = _inventoryMaker.MakeNewInventory(username); await client.InsertManyAsync(inventory); await console.AddAsync($"A {inventory[0].Name} and {inventory[1].Name} have been added for user {username}!"); logger.LogInformation("Create {item} and {secondItem} for {user} successful", inventory[0].Name, inventory[1].Name, username); }
public static async Task <IActionResult> NewUser( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, [Queue(Global.QUEUE)] IAsyncCollector <string> console, [Table(nameof(User))] CloudTable table, [OrchestrationClient] DurableOrchestrationClient 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 client = table.AsClientFor <User>(); var tempUser = new User { Name = name }; var userCheck = await client.GetAsync(tempUser.PartitionKey, name); if (userCheck != null) { await console.AddAsync($"Attempt to add duplicate user {name} failed."); return(new BadRequestObjectResult("Duplicate username is not allowed.")); } 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> GameStatus( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "GameStatus/{username}")] HttpRequest req, string username, [Table(nameof(User))] CloudTable userTable, [Table(nameof(Room))] CloudTable roomTable, [Table(nameof(Monster))] CloudTable monsterTable, [Table(nameof(Inventory))] CloudTable inventoryTable, ILogger log) { if (string.IsNullOrWhiteSpace(username)) { log.LogWarning("No username passed."); return(new BadRequestObjectResult("Username is required.")); } var userClient = userTable.AsClientFor <User>(); var tempUser = new User { Name = username }; var userCheck = await userClient.GetAsync(tempUser.PartitionKey, username); if (userCheck == null) { log.LogWarning("Username {0} not found", username); return(new BadRequestObjectResult($"Username '{username}' does not exist.")); } var monsterList = await monsterTable.AsClientFor <Monster>().GetAllAsync(username); var inventoryList = await inventoryTable.AsClientFor <Inventory>().GetAllAsync(username); var roomList = await roomTable.AsClientFor <Room>().GetAllAsync(username); return(new OkObjectResult(new { user = userCheck, monster = monsterList[0], inventory = inventoryList, room = roomList[0] })); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "quotes/{quoteId}")] HttpRequest req, string quoteId, [Table(nameof(Quote))] CloudTable table, ILogger logger) { var quote = new Quote() { Id = quoteId }; var client = table.AsClientFor <Quote>(); var quoteFromDb = await client.GetAsync(quote.PartitionKey, quote.RowKey); if (quoteFromDb == null) { throw new Exception($"CancelQuote: Quote {quote.Id} not found!"); } return(new OkObjectResult(quoteFromDb)); }
public static async Task <bool> Run( [ActivityTrigger] Quote quote, [Table(nameof(Quote))] CloudTable table, ILogger logger) { logger.LogInformation($"Cancel Quote with id {quote.Id}"); var client = table.AsClientFor <Quote>(); var quoteFromDb = await client.GetAsync(quote.PartitionKey, quote.RowKey); if (quoteFromDb == null) { throw new Exception($"CancelQuote: Quote {quote.Id} not found!"); } quoteFromDb.Status = Status.Cancelled; await client.ReplaceAsync(quoteFromDb); logger.LogInformation($"Quote with id {quote.Id} cancelled successful"); return(true); }