A set of classes for parsing, evaluating, and formatting die roll strings.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // SA_DiceBot.h
  3. // RPGBot
  4. //
  5. // Created by Sandy Achmiz on 12/30/15.
  6. //
  7. //
  8. /*
  9. The SA_DiceBot class hierarchy works like this:
  10. SA_DiceBot is the root class. A user of the SA_DiceBot package usually only
  11. needs to create and manage objects of this class. An SA_DiceBot creates and
  12. manages all other objects and classes in the package (directly or through its
  13. other members), as needed.
  14. An SA_DiceBot owns a single SA_DiceRoller and a single SA_DiceFormatter.
  15. The objects owned by an SA_DiceBot are for the SA_DiceBot's internal use
  16. only, and not accessible to users of the package. (But see SA_DiceRoller.h
  17. and SA_DiceFormatter.h for information on standalone use.)
  18. An SA_DiceRoller owns an SA_DiceParser and an SA_DiceEvaluator. An
  19. SA_DiceEvaluator owns an SA_DiceBag. (See Figure 1, below.)
  20. *--------------------------------------------------------------------------*
  21. | Fig. 1. SA_DiceBot class hierarchy diagram. |
  22. | |
  23. | |
  24. | SA_DiceBot |
  25. | / \ |
  26. | SA_DiceFormatter SA_DiceRoller |
  27. | / \ |
  28. | SA_DiceParser SA_DiceEvaluator |
  29. | | |
  30. | SA_DiceBag |
  31. | |
  32. *--------------------------------------------------------------------------*
  33. When the SA_DiceBot receives a recognized command that involves rolling some
  34. dice or otherwise evaluating some sort of string of semantically meaningful
  35. text (like just adding some numbers), it extracts that string (which may
  36. optionally include some text label) from the rest of the message body, and
  37. passes it to the SA_DiceRoller.
  38. The SA_DiceRoller strips off the label (if any), and passes the 'pure' string
  39. to the SA_DiceParser.
  40. The SA_DiceParser parses the string and produces an 'expression tree'
  41. representation of the string. An expression tree is an NSDictionary with a
  42. certain structure (which is described in SA_DiceExpressionStringConstants.h).
  43. The SA_DiceRoller then passes the expression tree to the SA_DiceEvaluator.
  44. The evaluator recursively traverses the expression tree in a depth-first
  45. manner, computing the results of each subtree, and storing those results in
  46. (a mutable copy of) the expression tree itself. Once the entire tree has been
  47. evaluated, (a mutable copy of) it is returned to the SA_DiceRoller. The
  48. SA_DiceRoller reattaches the stripped-off label (inserting it into the top
  49. level of the expresion tree) and returns the evaluated expression tree
  50. (now called a result) to the SA_DiceBot.
  51. The SA_DiceBot passes the evaluated result tree to its SA_DiceFormatter.
  52. The formatter traverses the tree, constructing a human-readable string form of
  53. the results, with whatever formatting it (the formatter) has been configured
  54. to provide. The formatter then returns this formatted result string to the
  55. SA_DiceBot.
  56. The SA_DiceBot then incorporates the formatted result string into some
  57. appropriate reply message or messages, and sends the reply(ies) back to its
  58. delegate, for transmission to the appropriate endpoint.
  59. */
  60. #import <Foundation/Foundation.h>
  61. #import "SA_Bot.h"
  62. @class SA_CommandResponder;
  63. /*
  64. ######################################
  65. #### SA_DiceBot Usage Information ####
  66. ######################################
  67. I. SUPPORTED COMMANDS & PARSER MODES
  68. The set of commands, and the syntax and behavior of those commands, that an
  69. SA_DiceBot supports depends on its currently set parser behavior mode (and, in
  70. some cases, also on the currently set formatter behavior mode). (Read more
  71. about parser behavior modes in SA_DiceParser.h, and about formatter behavior
  72. modes in SA_DiceFormatter.h.)
  73. Below is a list of available parser modes, along with the commands supported in
  74. each mode. (See section II for a list of commands that are supported in all
  75. parser modes.)
  76. NOTE: Commands are not case-sensitive; e.g., 'roll', 'ROLL', and 'rOLl' all
  77. work equally well.
  78. 1. DEFAULT mode
  79. "Default" mode is an alias for whatever default behavior is currently set for
  80. new SA_DiceParser instances. (The "default default" behavior for the current
  81. implementation is "legacy".)
  82. 2. LEGACY mode
  83. "Legacy" mode (mostly) emulates DiceBot by Sabin (and Dawn by xthemage before
  84. it). The following commands are available in legacy mode.
  85. 1. ROLL command.
  86. Takes 1 or more whitespace-delimited roll strings as parameters. The roll
  87. command is executed once for every parameter, and each execution generates
  88. a separate result string. One reply message is sent for each result string
  89. (or, if the simple formatter is being used, the results may optionally be
  90. collapsed into a single reply message).
  91. Each roll string may optionally be suffixed with with a (configurable)
  92. delimiter character (such as ';'), which may be followed with an arbitrary
  93. text label (which may not contain whitespace, however). That label may then
  94. be included in the result string (depending on the selected formatter
  95. behavior mode and other formatter settings).
  96. The body of the roll string (up to the label delimiter, if any) is simply
  97. parsed and evaluated.
  98. EXAMPLES (assuming legacy formatter behavior):
  99. Obormot: !roll 1d20
  100. SA_DiceBot: 1d20 < 14 = 14 > = 14
  101. Obormot: .roll 2+4-5
  102. SA_DiceBot: 2 + 4 - 5 = 1
  103. Obormot: /roll 2d4 1d20+19 4d10
  104. SA_DiceBot: 2d4 < 3 1 = 4 > = 4
  105. SA_DiceBot: 1d20 < 5 = 5 > + 19 = 24
  106. SA_DiceBot: 4d10 < 2 2 1 2 = 7 > = 7
  107. Obormot: !roll 1d20+4;fort_save
  108. SA_DiceBot: (fort_save) 1d20 < 8 = 8 > + 4 = 12
  109. Obormot: SA_DiceBot: 1d8+6;longsword 1d6+3;shortsword
  110. SA_DiceBot: (longsword) 1d8 < 4 = 4 > + 6 = 10
  111. SA_DiceBot: (shortsword) 1d6 < 2 = 2 > + 3 = 5
  112. 2. TRY command.
  113. Takes 1 or more whitespace-delimited roll strings. Prepends "1d20+" to each
  114. roll string, and otherwise behaves in the same way as the ROLL command.
  115. 3. CHAR command.
  116. II. OTHER COMMANDS
  117. III. CONFIGURATION
  118. */
  119. /****************************************/
  120. #pragma mark SA_DiceBot class declaration
  121. /****************************************/
  122. @interface SA_DiceBot : SA_Bot
  123. /************************/
  124. #pragma mark - Properties
  125. /************************/
  126. @property (strong) SA_CommandResponder *botCommandresponder;
  127. @property (strong) SA_CommandResponder *legacyCommandResponder;
  128. @property (strong) SA_CommandResponder *currentCommandResponder;
  129. @property (copy) NSString *commandDesignatorCharacters;
  130. /****************************/
  131. #pragma mark - Public methods
  132. /****************************/
  133. - (void)message:(NSString *)messageBody withInfo:(NSDictionary *)messageInfo;
  134. /****************************/
  135. #pragma mark - Helper methods
  136. /****************************/
  137. - (NSArray <NSDictionary *> *)repliesForCommandString:(NSString *)commandString messageInfo:(NSDictionary *)messageInfo byName:(BOOL)byName;
  138. - (void)loadDefaultCommandResponders;
  139. @end