ezpersonality.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {
  10. $Log: 21834: EZPersonality.pas
  11. Rev 1.0 2003.07.13 12:12:04 AM czhower
  12. Initial checkin
  13. Rev 1.0 2003.05.19 2:54:06 PM czhower
  14. }
  15. unit ezpersonality;
  16. interface
  17. {$ifdef fpc}
  18. {$mode objfpc}{$H+}
  19. {$endif}
  20. uses
  21. Classes;
  22. type
  23. TEZPersonalityAttributes = record
  24. Name: string;
  25. Description: string;
  26. end;
  27. TEZPersonality = class;
  28. TEZPersonalityClass = class of TEZPersonality;
  29. TEZPersonality = class(TCollection)
  30. protected
  31. FKeywords: TStrings;
  32. //
  33. procedure AddReply(const AKeywords: array of string;
  34. const AReplies: array of string); overload;
  35. procedure AddReply(const AKeywords: array of string;
  36. const AReplies: array of string; const ASounds: array of string); overload;
  37. procedure InitReplies; virtual; abstract;
  38. public
  39. class function Attributes: TEZPersonalityAttributes; virtual; abstract;
  40. class function ConstructPersonality(const AName: string): TEZPersonality;
  41. constructor Create; virtual;
  42. destructor Destroy; override;
  43. class procedure PersonalityList(AStrings: TStrings);
  44. class procedure RegisterPersonality;
  45. //
  46. property Keywords: TStrings read FKeywords;
  47. end;
  48. TEZReply = class(TCollectionItem)
  49. protected
  50. FIndex: Integer;
  51. FSound: string;
  52. FSounds: TStrings;
  53. FTexts: TStrings;
  54. public
  55. procedure AddText(const AText: string; const ASound: string = '');
  56. constructor Create(AOwner: TCollection); override;
  57. destructor Destroy; override;
  58. function NextText: string;
  59. //
  60. property Sound: string read FSound;
  61. end;
  62. implementation
  63. uses
  64. SysUtils;
  65. var
  66. GPersonalities: TStringList;
  67. { TEZReply }
  68. procedure TEZReply.AddText(const AText: string; const ASound: string = '');
  69. begin
  70. FTexts.Add(AText);
  71. FSounds.Add(ASound);
  72. end;
  73. constructor TEZReply.Create(AOwner: TCollection);
  74. begin
  75. inherited;
  76. FSounds := TStringList.Create;
  77. FTexts := TStringList.Create;
  78. end;
  79. destructor TEZReply.Destroy;
  80. begin
  81. FreeAndNil(FTexts);
  82. FreeAndNil(FSounds);
  83. inherited;
  84. end;
  85. function TEZReply.NextText: string;
  86. begin
  87. Result := FTexts[FIndex];
  88. FSound := FSounds[FIndex];
  89. Inc(FIndex);
  90. if FIndex = FTexts.Count then begin
  91. FIndex := 0;
  92. end;
  93. end;
  94. { TEZPersonality }
  95. procedure TEZPersonality.AddReply(const AKeywords, AReplies: array of string;
  96. const ASounds: array of string);
  97. var
  98. i: integer;
  99. LReply: TEZReply;
  100. begin
  101. LReply := TEZReply.Create(Self);
  102. for i := Low(AReplies) to High(AReplies) do begin
  103. if i <= High(ASounds) then begin
  104. LReply.AddText(AReplies[i], ASounds[i]);
  105. end else begin
  106. LReply.AddText(AReplies[i]);
  107. end;
  108. end;
  109. for i := Low(AKeywords) to High(AKeywords) do begin
  110. FKeywords.AddObject(Uppercase(AKeywords[i]), LReply);
  111. end;
  112. end;
  113. procedure TEZPersonality.AddReply(const AKeywords, AReplies: array of string);
  114. begin
  115. AddReply(AKeywords, AReplies, []);
  116. end;
  117. class function TEZPersonality.ConstructPersonality(
  118. const AName: string): TEZPersonality;
  119. var
  120. i: Integer;
  121. begin
  122. i := GPersonalities.IndexOf(AName);
  123. if i = -1 then begin
  124. raise Exception.Create('Personality not found.');
  125. end;
  126. Result := TEZPersonalityClass(GPersonalities.Objects[i]).Create;
  127. end;
  128. constructor TEZPersonality.Create;
  129. begin
  130. inherited Create(TEZReply);
  131. FKeywords := TStringList.Create;
  132. InitReplies;
  133. end;
  134. destructor TEZPersonality.Destroy;
  135. begin
  136. FreeAndNil(FKeywords);
  137. inherited;
  138. end;
  139. class procedure TEZPersonality.PersonalityList(AStrings: TStrings);
  140. begin
  141. AStrings.AddStrings(GPersonalities);
  142. end;
  143. class procedure TEZPersonality.RegisterPersonality;
  144. begin
  145. GPersonalities.AddObject(Self.Attributes.Name, TObject(Self));
  146. end;
  147. initialization
  148. GPersonalities := TStringList.Create;
  149. GPersonalities.Sorted := True;
  150. finalization
  151. FreeAndNil(GPersonalities)
  152. end.