private async Task<PieBotData> GetOrCreatePiesAsync(IStore blobStore) { PieBotData pies; if (!await blobStore.ExistsAsync(this.GeneralContainer, this.PieBlob).ConfigureAwait(false)) { pies = new PieBotData() { KnownPies = new List<Pie>(), CreatedPies = new Dictionary<DateTime, string>(), }; } else { pies = await blobStore.GetAsync<PieBotData>(this.GeneralContainer, this.PieBlob).ConfigureAwait(false); } return pies; }
private DataUpdateResponse AddPieHistoryAction(PieBotData pies, IEnumerable<string> arguments) { if (arguments.Count() < 2) { return new DataUpdateResponse(false, "Did not find enough elements to add historical pie information!"); } string pieName; string newPieName = string.Join(" ", arguments.Skip(1)); DateTime pieCreationTime; bool parsedCreationTime = DateTime.TryParse(arguments.First(), out pieCreationTime); if (!parsedCreationTime) { return new DataUpdateResponse(false, "Could not parse when the historical pie was created!"); } else if (pies.CreatedPies.TryGetValue(pieCreationTime, out pieName)) { return new DataUpdateResponse(false, $"The {pieName} pie was create on this date. This conflicts with new pie {newPieName}. Please choose a slightly different time."); } pies.CreatedPies.Add(pieCreationTime, newPieName); return new DataUpdateResponse(true, $"The {newPieName} historical pie was added."); }
public async Task<ActivityResponse> ProcessActivityAsync(IStore blobStore, ActivityRequest request) { PieBotData pies = await this.GetOrCreatePiesAsync(blobStore).ConfigureAwait(false); // Preprocess input text string[] input = request.SanitizedText.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (input.Length == 0) { return new ActivityResponse("I could not understand the action to take on the pies."); } string command = input.First().ToLowerInvariant(); // All the commands are in ASCII, so we can use ToLower instead of ToUpper IEnumerable<string> arguments = input.Skip(1); // Perform the requested command DataUpdateResponse response; switch (command) { case "add": response = AddPieAction(pies.KnownPies, arguments); break; case "vote": case "upvote": response = VotePieAction(pies.KnownPies, request.From, "upvote", (pie) => pie.Upvotes, arguments); break; case "dvote": case "downvote": response = VotePieAction(pies.KnownPies, request.From, "downvote", (pie) => pie.Downvotes, arguments); break; case "clearvote": response = ClearVoteAction(pies.KnownPies, arguments); break; case "rank": response = new DataUpdateResponse(false, RankAction(pies.KnownPies, input)); break; case "list": // List out pies and how many were created. response = new DataUpdateResponse(false, "**Pies**:\n\n" + string.Join("\n\n", pies.KnownPies.OrderBy(pie => pie.Name).Select(pie => $"{pie.Name}: {pies.CreatedPies.Count(item => pie.Name.Equals(item.Value, StringComparison.OrdinalIgnoreCase))}"))); break; case "addhistory": response = AddPieHistoryAction(pies, arguments); break; case "history": response = new DataUpdateResponse(false, "**Created Pies**:\n\n" + string.Join("\n\n", pies.CreatedPies.OrderByDescending(pie => pie.Key).Select(pie => $"{pie.Key.ToString("MM/dd/yyyy")}: {pie.Value}"))); break; case "help": response = new DataUpdateResponse(false, HelpAction()); break; default: response = new DataUpdateResponse(false, "I did not understand the action you want me to take on pies!"); break; } // Save if necessary and return if (response.UpdateData) { await blobStore.CreateOrUpdateAsync(this.GeneralContainer, this.PieBlob, pies).ConfigureAwait(false); } return new ActivityResponse(response.Response); }