@@ -18,34 +18,38 @@ public class NoteTrigger : BaseTrigger
1818
1919 public NoteTrigger ( TriggerType type , string name , TriggerOptionsBase options ) : base ( type , name , options )
2020 {
21- Options . NoteTriggerOptions . Notes = new Dictionary < SteamID , Dictionary < string , Note > > ( ) ;
22- saveNoteTimer = new Timer ( 1000 * 60 * 5 ) ;
21+ saveNoteTimer = new Timer ( options . NoteTriggerOptions . SaveTimer ) ;
2322 saveNoteTimer . Elapsed += SaveNoteTimer_Elapsed ;
2423 }
2524
2625 private void SaveNoteTimer_Elapsed ( object sender , ElapsedEventArgs e )
2726 {
2827 string json = JsonConvert . SerializeObject ( Options . NoteTriggerOptions . Notes ) ;
29- File . WriteAllText ( Bot . username + "/notes.json" , json ) ;
28+ File . WriteAllText ( Options . NoteTriggerOptions . NoteFile , json ) ;
29+ Log . Instance . Silly ( "{0}/{1}: Wrote notes to {0}/notes.json" , Bot . username , Name ) ;
3030 }
3131
3232 public override bool OnLoggedOn ( )
3333 {
3434 try
3535 {
36- string notes = File . ReadAllText ( Bot . username + "/notes.json" ) ;
37- Options . NoteTriggerOptions . Notes = ( Dictionary < SteamID , Dictionary < string , Note > > ) JsonConvert . DeserializeObject ( notes ) ;
36+ Options . NoteTriggerOptions . Notes = JsonConvert . DeserializeObject < Dictionary < ulong , Dictionary < string , Note > > > ( File . ReadAllText ( Options . NoteTriggerOptions . NoteFile ) ) ;
37+ Log . Instance . Silly ( Bot . username + "/" + Name + ": Loaded notes from " + Options . NoteTriggerOptions . NoteFile ) ;
3838 }
39- catch ( FileNotFoundException fnfe )
39+ catch ( FileNotFoundException fnfe )
4040 {
41- File . Create ( Bot . username + "/notes.json" ) ;
41+ File . Create ( Options . NoteTriggerOptions . NoteFile ) ;
4242
4343 }
44- catch ( Exception e )
44+ catch ( Exception e )
4545 {
46- Log . Instance . Error ( e . StackTrace ) ;
46+ Log . Instance . Error ( e . Message + ": " + e . StackTrace ) ;
47+ }
48+
49+ if ( Options . NoteTriggerOptions . Notes == null )
50+ {
51+ Options . NoteTriggerOptions . Notes = new Dictionary < ulong , Dictionary < string , Note > > ( ) ;
4752 }
48-
4953
5054 saveNoteTimer . Start ( ) ;
5155
@@ -59,18 +63,80 @@ public override bool respondToChatMessage(SteamID roomID, SteamID chatterId, str
5963
6064 private bool Respond ( SteamID roomID , SteamID userID , string message )
6165 {
62- string [ ] query = StripCommand ( message , Options . NoteTriggerOptions . NoteCommand ) ;
66+ Dictionary < string , Note > db ;
67+ ulong room = roomID . ConvertToUInt64 ( ) ;
68+
69+ if ( ! Options . NoteTriggerOptions . Notes . ContainsKey ( room ) )
70+ {
71+ Options . NoteTriggerOptions . Notes [ room ] = new Dictionary < string , Note > ( ) ;
72+ db = Options . NoteTriggerOptions . Notes [ room ] ;
73+ }
74+ else
75+ {
76+ db = Options . NoteTriggerOptions . Notes [ room ] ;
77+ }
78+
79+ string [ ] query = StripCommand ( message , Options . NoteTriggerOptions . NotesCommand ) ;
80+ if ( query != null && query . Length == 1 )
81+ {
82+ List < string > _notes = new List < string > ( ) ;
83+ string notes = "" ;
84+ foreach ( string note in db . Keys )
85+ {
86+ _notes . Add ( note ) ;
87+ }
88+ notes = string . Join ( ", " , _notes . ToArray ( ) ) ;
89+ SendMessageAfterDelay ( roomID , "Please see your personal chat for a list of all the notes in this chat room" , true ) ;
90+ SendMessageAfterDelay ( userID , "Notes in " + roomID . ConvertToUInt64 ( ) + ": " + notes , false ) ;
91+ return true ;
92+ }
93+
94+ query = StripCommand ( message , Options . NoteTriggerOptions . DeleteCommand ) ;
95+ if ( query != null && query . Length == 2 )
96+ {
97+ string name = query [ 1 ] ;
98+ if ( ! db . ContainsKey ( name ) )
99+ {
100+ SendMessageAfterDelay ( roomID , string . Format ( "The note \" {0}\" does not exist. Use \" {1}\" to create it." , name , Options . NoteTriggerOptions . NoteCommand + " " + name + " <definition>" ) , true ) ;
101+ return true ;
102+ }
103+ else
104+ {
105+ db . Remove ( name ) ;
106+ SendMessageAfterDelay ( roomID , string . Format ( "Note \" {0}\" deleted" , name ) , true ) ;
107+ return true ;
108+ }
109+ }
110+
111+ query = StripCommand ( message , Options . NoteTriggerOptions . InfoCommand ) ;
112+ if ( query != null && query . Length == 2 )
113+ {
114+ string name = query [ 1 ] ;
115+ if ( ! db . ContainsKey ( name ) )
116+ {
117+ SendMessageAfterDelay ( roomID , string . Format ( "The note \" {0}\" does not exist. Use \" {1}\" to create it." , name , Options . NoteTriggerOptions . NoteCommand + " " + name + " <definition>" ) , true ) ;
118+ return true ;
119+ }
120+ else
121+ {
122+ Note note = db [ name ] ;
123+ SendMessageAfterDelay ( roomID , string . Format ( "The note \" {0}\" with definition \" {1}\" was last modified by {2} on {3}" , name , note . Definition , note . ModifiedBy , note . ModifiedWhen ) , true ) ;
124+ return true ;
125+ }
126+ }
127+
128+ query = StripCommand ( message , Options . NoteTriggerOptions . NoteCommand ) ;
63129 if ( query != null && query . Length == 2 )
64130 {
65131 string name = query [ 1 ] ;
66- Note note = Options . NoteTriggerOptions . Notes [ roomID ] [ name ] ;
67- if ( note == null )
132+ if ( ! db . ContainsKey ( name ) )
68133 {
69134 SendMessageAfterDelay ( roomID , string . Format ( "The note \" {0}\" does not exist. Use \" {1}\" to create it." , name , Options . NoteTriggerOptions . NoteCommand + " " + name + " <definition>" ) , true ) ;
70135 return true ;
71136 }
72137 else
73138 {
139+ Note note = db [ name ] ;
74140 SendMessageAfterDelay ( roomID , "\" " + note . Definition + "\" " , true ) ;
75141 return true ;
76142 }
@@ -90,38 +156,23 @@ private bool Respond(SteamID roomID, SteamID userID, string message)
90156 string definition = string . Join ( " " , def ) ;
91157
92158 note . Definition = definition ;
93- note . ModifiedBy = Bot . steamFriends . GetFriendPersonaName ( userID ) + " < " + userID . ConvertToUInt64 ( ) . ToString ( ) + "> " ;
159+ note . ModifiedBy = Bot . steamFriends . GetFriendPersonaName ( userID ) + " ( " + userID . ConvertToUInt64 ( ) . ToString ( ) + ") " ;
94160 note . ModifiedWhen = DateTime . Now . ToString ( ) ;
95161
96- Dictionary < string , Note > innerNote = new Dictionary < string , Note > ( ) ;
97- innerNote . Add ( name , note ) ;
98-
99- if ( Options . NoteTriggerOptions . Notes == null || Options . NoteTriggerOptions . Notes [ roomID ] == null || Options . NoteTriggerOptions . Notes [ roomID ] [ name ] == null )
162+ if ( ! db . ContainsKey ( name ) )
100163 {
101- Options . NoteTriggerOptions . Notes = new Dictionary < SteamID , Dictionary < string , Note > > ( ) ;
102- Options . NoteTriggerOptions . Notes . Add ( roomID , innerNote ) ;
164+ db . Add ( name , note ) ;
103165 }
104166 else
105167 {
106- Options . NoteTriggerOptions . Notes . Remove ( roomID ) ;
107- Options . NoteTriggerOptions . Notes . Add ( roomID , innerNote ) ;
168+ db . Remove ( name ) ;
169+ db . Add ( name , note ) ;
108170 }
109171
110172 SendMessageAfterDelay ( roomID , string . Format ( "Note \" {0}\" saved" , name ) , true ) ;
111173 return true ;
112174 }
113-
114- query = StripCommand ( message , Options . NoteTriggerOptions . DeleteCommand ) ;
115- if ( query != null && query . Length == 2 )
116- {
117- string name = query [ 1 ] ;
118- Options . NoteTriggerOptions . Notes [ roomID ] . Remove ( name ) ;
119- SendMessageAfterDelay ( roomID , string . Format ( "Note \" {0}\" deleted" , name ) , true ) ;
120- return true ;
121- }
122175 return false ;
123-
124-
125176 }
126177 }
127- }
178+ }
0 commit comments