BotCommandFactory.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using sera_slackbot.Commands;
  3. namespace sera_slackbot.Library
  4. {
  5. public class BotCommandFactory
  6. {
  7. private JsonFileReader reader;
  8. private JsonFileSaver saver;
  9. public BotCommandFactory(JsonFileReader reader, JsonFileSaver saver)
  10. {
  11. this.reader = reader;
  12. this.saver = saver;
  13. }
  14. public IBotCommand CreateCommand(string commandString)
  15. {
  16. var typeString = "sera_slackbot.Commands." + commandString + "BotCommand";
  17. try
  18. {
  19. return (IBotCommand)Activator.CreateInstance(Type.GetType(typeString), this.reader, this.saver);
  20. }
  21. catch (Exception)
  22. {
  23. return new InvalidBotCommand(commandString);
  24. }
  25. }
  26. public IBotInteraction CreateInteraction(string interactionString)
  27. {
  28. var commandString = interactionString;
  29. switch (interactionString)
  30. {
  31. case "pointingpoker":
  32. commandString = "PointingPoker";
  33. break;
  34. }
  35. var typeString = "sera_slackbot.Commands." + commandString + "BotCommand";
  36. try
  37. {
  38. return (IBotInteraction)Activator.CreateInstance(Type.GetType(typeString), this.reader, this.saver);
  39. }
  40. catch (Exception)
  41. {
  42. return new InvalidBotCommand(commandString);
  43. }
  44. }
  45. }
  46. }