-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.java
More file actions
1540 lines (1321 loc) · 44.1 KB
/
Copy pathMyBot.java
File metadata and controls
1540 lines (1321 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import org.jibble.pircbot.*;
import org.jibble.jmegahal.*;
import java.util.Timer;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
//package com.javapapers.java;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.*;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class MyBot extends PircBot {
//VARIABLES//
public static final long DURATION = 1 * 15 * 1000;
private static String BRAIN = "C:\\Users\\Kevin\\Desktop\\bot\\sneetoswoman\\brain.ser";
private static String BRAIN2 = "C:\\Users\\Kevin\\Desktop\\bot\\sneetoswoman\\brain2.ser";
//final static Charset ENCODING = StandardCharsets.UTF_8;
public JMegaHal hal = new JMegaHal();
public JMegaHal hal2 = new JMegaHal();
Twitter twitter;
public String consumerKey = dbids.consumerKey;
public String consumerSecret = dbids.consumerSecret;
public String accessToken = dbids.accessToken;
public String accessSecret = dbids.accessSecret;
private Timer timer = new Timer(true);
public String TempString1;
public Boolean delay = false;
private Boolean talkedTo = false;
private Boolean talk = false;
private int talkCounter = 0;
private String talker;
private String talkString;
public String talkingTo = "";
public String talkingTo2 = "";
public String talkingTo3 = "";
public String lastTalkingTo1 = "null";
public String lastTalkingTo2 = "null";
public String lastTalkingTo3 = "null";
public boolean nameMentioned = false;
private Random random = new Random( );
private int WordTrigger;
private int WordGroup;
private int WordType;
private int mood = random.nextInt(6)+1;
private int moodchange = 0;
private int TempNum1;
private int TempNum2;
private int TempNum3;
private int TempNum4;
private int TempNum5;
private double[] speakChance = new double[100];
public String lastSpoke = "";
public String lastTalked1 = "";
public String lastTalked2 = "";
public String lastTalked3 = "";
public String lastTalked4 = "";
public String lastTalked5 = "";
public String lastSpokenSentence = "";
public String lastSpokenTrigger = "";
//variables for sneewo game
public ArrayList<String> channels = readFile("channels.txt");
public ArrayList<String> timers = initTimers();
public ArrayList<String> answers = initTimers();
public ArrayList<String> lastWords = new ArrayList<String>();;
//variables for sneewos brain
public ArrayList<String> memory = new ArrayList<String>();
public ArrayList<String> lastSaid = new ArrayList<String>();
public ArrayList<Integer> lastSaidIndexs = new ArrayList<Integer>();
public ArrayList<String> priorityWords = new ArrayList<String>();
public ArrayList<String> priorityWordsAmount = new ArrayList<String>();
public Boolean isMute = false;
public int gameTimer = 0;
private String TempString2;
private String TempString3;
private String TempString4;
private String TempString5;
private int trigger;
private String name;
private String TempWord1;
private String TempWord2;
private int lastmood;
private boolean isMod;
public int brainCounter = 0;
public String lastSaidSentence = "";
BufferedReader br = null;
List<String> timeoutList = new ArrayList<String>();
//List<int> timeoutListTime = new ArrayList<int>();
List<String> banList = new ArrayList<String>();
List<String> commandList = readCommands();
ArrayList<String> angryCommands = readFile("angryCommands.txt");
ArrayList<String> hypedCommands = readFile("hypedCommands.txt");
ArrayList<String> sadCommands = readFile("sadCommands.txt");
ArrayList<String> sarcasticCommands = readFile("sarcasticCommands.txt");
ArrayList<String> chillCommands = readFile("chillCommands.txt");
ArrayList<String> twitchyCommands = readFile("twitchyCommands.txt");
ArrayList<String> mods = readFile("mods.txt");
ArrayList<String> muteNames = readFile("channels.txt");
ArrayList<String> muteBooleans = readFile("muteBooleans.txt");
ArrayList<String> frequency = readFile("frequency.txt");
ArrayList<String> badWords = readFile("badWords.txt");
public String thisFrequency = "";
public MyBot() {
//INITIALIZE TWITTER
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessSecret);
TwitterFactory factory = new TwitterFactory(cb.build());
twitter = factory.getInstance();
/*
twitter.setOAuthConsumer(consumerKey, consumerSecret);
AccessToken accessToken = new AccessToken(accessToken, accessSecret);
twitter.setOAuthAccessToken(accessToken);*/
} catch (Exception te) {
te.printStackTrace();
}
//
//TWITTER DONE
//
ObjectInputStream in = null;
try {
File file = new File(BRAIN);
in = new ObjectInputStream(new FileInputStream(file));
hal = (JMegaHal) in.readObject();
} catch (FileNotFoundException e) {
//firstRun();
} catch (IOException e) {
//firstRun();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
try {
File file = new File(BRAIN2);
in = new ObjectInputStream(new FileInputStream(file));
hal2 = (JMegaHal) in.readObject();
} catch (FileNotFoundException e) {
//firstRun();
} catch (IOException e) {
//firstRun();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
this.setName("Sneewo");
//initialize update function
MyBotTimerTask3 timerTask3 = new MyBotTimerTask3(this);
timer.schedule(timerTask3, 0, 1000);
}
public void onMessage(String channel, String sender, String login, String hostname, String message) {
if (message.toLowerCase().contains("@sneeps")) {
ArrayList<String> local = new ArrayList<String>();
ArrayList<String> global = new ArrayList<String>();
ArrayList<String> tempArray = readFile("sneeps" + channel + ".txt");
ArrayList<String> tempNames = readFile("sneeps" + channel + "Names.txt");
local = getValues(tempNames, tempArray, sender);
tempArray = readFile("sneeps.txt");
tempNames = readFile("sneepsNames.txt");
global = getValues(tempNames, tempArray, sender);
sendMessage(channel, sender + ", Global Sneeps: (#" + global.get(0) + ") " + global.get(1) + " | Sneeps in " + channel + ": (#" + local.get(0) + ") " + local.get(1));
}
isMod = false;
if (mods.contains(sender)) {
isMod = true;
}
//System.out.println(timers.get(channels.indexOf(channel)));
if (!timers.get(channels.indexOf(channel)).equalsIgnoreCase("0")) {
//We are playing the snee game here
int channelIndex = channels.indexOf(channel);
if (message.toLowerCase().contains(answers.get(channelIndex).toLowerCase())) {
lastWords.add(answers.get(channelIndex).toLowerCase());
if (lastWords.size() > 5) {
lastWords.remove(lastWords.get(0));
}
timers.set(channelIndex, "0");
ArrayList<String> local = new ArrayList<String>();
ArrayList<String> global = new ArrayList<String>();
//System.out.print("1");
ArrayList<String> tempArray = readFile("sneeps" + channel + ".txt");
ArrayList<String> tempNames = readFile("sneeps" + channel + "Names.txt");
//System.out.print("2");
addOne(tempNames, tempArray, sender, 1);
//System.out.print("3");
writeFile(tempArray, "sneeps" + channel + ".txt");
writeFile(tempNames, "sneeps" + channel + "Names.txt");
//System.out.print("4");
local = getValues(tempNames, tempArray, sender);
//System.out.print("5");
tempArray = readFile("sneeps.txt");
tempNames = readFile("sneepsNames.txt");
addOne(tempNames, tempArray, sender, 1);
writeFile(tempArray, "sneeps.txt");
writeFile(tempNames, "sneepsNames.txt");
global = getValues(tempNames, tempArray, sender);
sendMessage(channel, "Congrats " + sender + "! You won! Sneeps: " + local.get(0));
}
} else
//if (true == true) {
if (!lastSpoke.equalsIgnoreCase(sender) || isMod == true) {
//initial variables
nameMentioned = false;
if (sender != "sneewo") {
isMute = false;
//Check to see if we are muted
for (int i = 0; i < muteBooleans.size(); i++) {
if (muteNames.get(i).equalsIgnoreCase(channel) && muteBooleans.get(i).equalsIgnoreCase("true")) {
isMute = true;
}
}
//Check to see frequency
for (int i = 0; i < frequency.size(); i++) {
if (muteNames.get(i).equalsIgnoreCase(channel)) {
thisFrequency = frequency.get(i);
}
}
if (isMod == true && (message.toLowerCase().contains("@fillinthesneewo") || message.toLowerCase().contains("@fits"))) {
int channelIndex = channels.indexOf(channel);
if (timers.get(channelIndex).equalsIgnoreCase("0")) {
//START SNEEWO MINI GAME
sendMessage(channel, "Fill in the sneewo started! Sneewo will talk about a random word for 2 minutes, type the word in chat to guess what she is talking about and win sneeps!");
timers.set(channelIndex,"60");
String answer = getAnswer();
answers.set(channelIndex, answer);
}
}
if (isMod == true && message.contains("@tweet ")) {
if (message.substring(0, 7).equalsIgnoreCase("@tweet ") && message.length() > 7) {
//Send the tweet
try {
//Send the update
twitter.updateStatus(message.substring(7));
System.out.println("Posted: " + message.substring(7));
} catch (TwitterException te) {
te.printStackTrace();
}
}
}
//test command
if (message.contains("@test") && sender.equalsIgnoreCase(channel.substring(1))) {
ArrayList<String> temp = new ArrayList<String>();
temp.add("first");
temp.add("second");
temp.add("third");
temp.add("fourth");
temp.add("fifth");
String tempStringTest = "";
for (int i = 0; i < temp.size(); i++) {
tempStringTest = tempStringTest + temp.get(i) + " ";
if (i == 1) {
temp.remove(i);
}
}
sendMessage(channel, "Testing: " + tempStringTest);
}
//set frequency command
if (message.contains("@frequency") && (isMod == true || sender.equalsIgnoreCase(channel.substring(1)))) {
if (message.substring(0, 11).contains("@frequency ")) {
for (int i = 0; i < frequency.size(); i++) {
if (muteNames.get(i).equalsIgnoreCase(channel) && (message.substring(11, message.length()).equalsIgnoreCase("low") || message.substring(11, message.length()).equalsIgnoreCase("medium") || message.substring(11, message.length()).equalsIgnoreCase("high"))) {
frequency.set(i, message.substring(11, message.length()));
writeFile(frequency, "frequency.txt");
sendMessage(channel, "Set frequency to " + message.substring(11, message.length()) + ".");
thisFrequency = message.substring(11, message.length());
}
}
}
}// end of check for frequency
//mute sneetoswoman command
if (message.contains("@muteSneetosWoman") && (isMod == true || sender.equalsIgnoreCase(channel.substring(1)))) {
if (message.substring(0, 17).contains("@muteSneetosWoman")) {
for (int i = 0; i < muteBooleans.size(); i++) {
if (muteNames.get(i).equalsIgnoreCase(channel) && (muteBooleans.get(i).equalsIgnoreCase("false"))) {
muteBooleans.set(i, "true");
writeFile(muteBooleans, "muteBooleans.txt");
sendMessage(channel, "Muted.");
}
}
}
}// end of check for mute
//unmute sneetoswoman command
if (message.contains("@unmuteSneetosWoman") && (isMod == true || sender.equalsIgnoreCase(channel.substring(1)))) {
if (message.substring(0, 19).contains("@unmuteSneetosWoman")) {
for (int i = 0; i < muteBooleans.size(); i++) {
if (muteNames.get(i).equalsIgnoreCase(channel) && muteBooleans.get(i).equalsIgnoreCase("true")) {
muteBooleans.set(i, "false");
writeFile(muteBooleans, "muteBooleans.txt");
sendMessage(channel, "Unmuted.");
}
}
}
}// end of check for unmute
if (isMute == false) {
//test code to read arrays
if (message.contains("@setMood ") && isMod == true) {
mood = Character.getNumericValue(message.charAt(9));
sendMessage(channel, "Mood set to: " + mood);
for (int i=0; i < sarcasticCommands.size(); i++) {
//sendMessage(channel, sarcasticCommands.get(i));
}
}
if (message.contains("@speak ") && isMod == true) {
speak1(channel, sender, login, hostname, hal2.getSentence(message.substring(7,message.length())));
}
//MOD COMMANDS END//
if ((message.toLowerCase().contains("sneetoswoman") || message.toLowerCase().contains("sneewo") || message.toLowerCase().contains("snee-chin") || message.toLowerCase().contains("snee-chan") || message.toLowerCase().contains("snee-chan") || message.toLowerCase().contains("sneetos woman") || message.toLowerCase().contains("sw, "))) {
if (talkingTo.isEmpty()) {
talkingTo = sender;
}
nameMentioned = true;
if (!talkingTo.equalsIgnoreCase(lastTalkingTo1)) {
//sendMessage("#moltov", "triggered" + talkingTo + talkingTo.equalsIgnoreCase(sender));
} else {
talkingTo = "";
}
}
if (random.nextInt(8) == 0 || talkingTo.equalsIgnoreCase(sender) || (nameMentioned == true && random.nextInt(8) <= 5)) {
String data = getMessage(channel, message);
/*data = "";
String newMessage = removeSymbols(message);
newMessage = removeStrings(newMessage, "smallWords.txt", true);
String aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
data = hal2.getSentence(aTrigger);
lastSpokenTrigger = aTrigger;
lastSpokenSentence = message;
if (!data.toLowerCase().contains(aTrigger.toLowerCase())) {
aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
data = hal2.getSentence(aTrigger);
if (!data.toLowerCase().contains(aTrigger.toLowerCase())) {
aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
data = hal2.getSentence(aTrigger);
if (!data.toLowerCase().contains(aTrigger.toLowerCase())) {
aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
data = hal2.getSentence(aTrigger);
if (!data.toLowerCase().contains(aTrigger.toLowerCase())) {
aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
data = hal2.getSentence(aTrigger);
}
}
}
}*/
if (!data.equalsIgnoreCase("null") && !sender.contains("sneewo")) {
//remove sneewos names before sending:
if (data.toLowerCase().contains("sneewo ")) {
if (data.toLowerCase().substring(0, 7).equalsIgnoreCase("sneewo ")) {
data = data.substring(7,data.length());
}
}
if (data.toLowerCase().contains("sneewo, ")) {
if (data.toLowerCase().substring(0, 8).equalsIgnoreCase("sneewo, ")) {
data = data.substring(8,data.length());
}
}
//remove sneewos names before sending:
if (data.toLowerCase().contains("sneetoswoman")) {
if (data.toLowerCase().substring(0, 12).equalsIgnoreCase("sneetoswoman")) {
data = data.substring(12,data.length());
}
}
//remove sneewos names before sending:
if (data.toLowerCase().contains("sneetos")) {
if (data.toLowerCase().substring(0, 7).equalsIgnoreCase("sneetos")) {
data = data.substring(6,data.length());
}
}
//remove sneewos names before sending:
if (data.toLowerCase().contains("snee-chin")) {
if (data.toLowerCase().substring(0, 9).equalsIgnoreCase("snee-chin")) {
data = data.substring(9,data.length());
}
}
//TESTING
if (data.toLowerCase().contains(" sneewo")) {
data = data.replaceAll(" sneewo", "");
data = data.replaceAll(" Sneewo", "");
}
if (data.toLowerCase().contains(" sneewo?")) {
data = data.replaceAll(" sneewo?", "?");
data = data.replaceAll(" Sneewo?", "?");
}
if (data.toLowerCase().contains(" sneetoswoman?")) {
data = data.replaceAll(" sneetoswoman?", "?");
data = data.replaceAll(" SneetosWoman?", "?");
data = data.replaceAll(" Sneetoswoman?", "?");
}
if (data.toLowerCase().contains(" snee-chin?")) {
data = data.replaceAll(" snee-chin?", "?");
data = data.replaceAll(" Snee-chin?", "?");
}
//remove space before sending:
if (data.toLowerCase().charAt(0) == ' ') {
data = data.substring(1);
}
//remove space before sending:
if (data.toLowerCase().charAt(0) == ',') {
data = data.substring(1);
}
//remove space before sending:
if (data.toLowerCase().charAt(0) == ':') {
data = data.substring(1);
}
//actually send it
if (!data.equalsIgnoreCase("null")) {
speak1(channel, sender, login, hostname, data);
}
}
}//end of delay*/
} // end of isMute check
//record the message:
if (!sender.contains("besson") && !sender.contains("sneetoswoman") && !sender.contains("sneewo") && !sender.contains("sneeto") && !sender.contains("snee-chin") && !sender.contains("sneetosman")&& !sender.equalsIgnoreCase("zeldobot") && message.length() < 100) {
boolean badWordsCheck = false;
for (int i = 0; i < badWords.size(); i++) {
if (message.toLowerCase().contains(badWords.get(i))) {
badWordsCheck = true;
}
}
if (badWordsCheck == false) {
//Add the message to the memory
addMemory(channel,message);
//Update the brain
hal.add(message);
hal2.add(message);
updateWordPriority(message, "generalSentence");
}
}
trigger = 0;
name = channel.substring(1);
if (brainCounter == 2000) {
//save the brain!
double startTime = System.currentTimeMillis();
try {
// save the new data
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(BRAIN));
out.writeObject(hal);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//save the brain!
try {
// save the new data
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(BRAIN2));
out.writeObject(hal2);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
brainCounter = 0;
double endTime = System.currentTimeMillis();
System.out.println("Time to start save:" + startTime);
System.out.println("Time to end save:" + endTime);
System.out.println("Time to get save:" + (endTime - startTime));
} else {
brainCounter++;
}
updatePriorityDatabase();
}
}
lastSpoke = sender;
}//end of on message
public ArrayList<String> getValues(ArrayList<String> names, ArrayList<String> array, String player) {
ArrayList<String> values = new ArrayList<String>();
values.add("" + names.size());
values.add("0");
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equalsIgnoreCase(player)) {
values.set(0, "" + (i+1));
values.set(1, array.get(i));
}
}
return values;
}
public void addOne(ArrayList<String> names, ArrayList<String> array, String player, int amount) {
boolean exists = false;
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equalsIgnoreCase(player)) {
exists = true;
int temp = Integer.parseInt(array.get(i)) + amount;
array.set(i, ""+temp);
i = names.size();
}
}
if (exists == false) {
names.add(player);
array.add("1");
}
sortAL(names,array);
}
public void sortAL(ArrayList<String> names, ArrayList<String> array) {
boolean done = true;
while (done != true) {
done = true;
for (int i = 0; i < names.size()-1; i++) {
if (Integer.parseInt(array.get(i)) < Integer.parseInt(array.get(i+1))) {
done = false;
String temp = names.get(i);
String temp2 = array.get(i);
names.set(i, names.get(i+1));
array.set(i, array.get(i+1));
names.set(i+1, temp);
array.set(i+1, temp2);
}
}
}
}
//update function that is called once every second
public void update() {
//update timers
for (int i = 0; i < timers.size(); i++) {
if (Integer.parseInt(timers.get(i)) % 15 == 10) {
String temp = hal2.getSentence(answers.get(i));
sendMessage(channels.get(i), addUnderscores(temp, answers.get(i)));
}
if (Integer.parseInt(timers.get(i)) == 1) {
sendMessage(channels.get(i), "Times up no one guessed the answer: " + answers.get(i));
}
if (Integer.parseInt(timers.get(i)) > 0) {
timers.set(i, "" + (Integer.parseInt(timers.get(i))-1));
System.out.println("" + (Integer.parseInt(timers.get(i))-1));
}
}
}
public String addUnderscores(String a, String b) {
String u = "_";
for (int i = 1; i < b.length(); i++) {
u = u + " _";
}
a = a.replaceAll(b, u);
return a;
}
public String getAnswer() {
String answer = "";
boolean done = false;
while (done == false) {
done = true;
String tempSentence = hal2.getSentence();
tempSentence = removeSymbols(tempSentence);
tempSentence = removeStrings(tempSentence, "smallWords.txt", true);
ArrayList<String> newTriggers = getTriggers(tempSentence);
int randomNumber = random.nextInt(newTriggers.size()-1);
answer = newTriggers.get(randomNumber);
for (int i = 0; i < lastWords.size(); i++) {
if (answer.equalsIgnoreCase(lastWords.get(i))) {
done = false;
}
}
if (answer.length() < 3) {
done = false;
}
}
return answer;
}
public String getMessage(String channel, String message) {
double startTime = System.currentTimeMillis();
//initialize variables
String data = "";
String highestSentence = "";
int highestScore = 0;
int maxTests = 50;
int index = 0;
boolean exists = false;
String newMessage = removeSymbols(message);
newMessage = removeStrings(newMessage, "smallWords.txt", true);
ArrayList<String> newTriggers = getTriggers(newMessage);
ArrayList<String> memoryTriggers = new ArrayList<String>();
for (int i = 0; i < memory.size(); i++) {
if (memory.get(i).equalsIgnoreCase("#" + channel)) {
exists = true;
index = i+1;
}
}
if (exists == true) {
//we found the channel loop through the memory and find the most suitable message
while (index < memory.size()) {
if (memory.get(index).charAt(0) == '#') {
//we hit a channel, set index to the end
index = memory.size();
} else {
String tempMessage = removeSymbols(memory.get(index).substring(1));
tempMessage = removeStrings(tempMessage, "smallWords.txt", true);
ArrayList<String> tempTriggers = getTriggers(tempMessage);
for (int i = 0; i < tempTriggers.size(); i++) {
memoryTriggers.add(tempTriggers.get(i));
}
}
index++;
}
//we have the triggers of the memory sentences
//loop through the main triggers and make our sentences based on the them
for (int i = 0; i < newTriggers.size(); i++) {
for (int j = 0; j < maxTests; j++) {
int value = 0;
//Make a sentence and check if it's better than the current sentence
String tempSentence = hal2.getSentence(newTriggers.get(i));
String tempSentence2 = removeSymbols(tempSentence);
tempSentence2 = removeStrings(tempSentence2, "smallWords.txt", true);
ArrayList<String> tempTriggers = getTriggers(tempSentence2);
ArrayList<String> tempWords = new ArrayList<String>();
//Compare all of the constructed sentences triggers against the main sentence and the memory senetences
for (int k = 0; k < newTriggers.size(); k++) {
for(int k2 = 0; k2 < tempTriggers.size(); k2++) {
if (newTriggers.get(k).equalsIgnoreCase(tempTriggers.get(k2))) {
tempWords.add(newTriggers.get(k));
int temp = checkInArray(tempWords, newTriggers.get(k));
if (temp == 1) {
value += 8;
} else if (temp == 2) {
value += 4;
} else if (temp > 5) {
value -= 6;
} else {
value += 0;
}
}
}
}
for (int k = 0; k < memoryTriggers.size(); k++) {
for(int k2 = 0; k2 < tempTriggers.size(); k2++) {
if (memoryTriggers.get(k).equalsIgnoreCase(tempTriggers.get(k2))) {
tempWords.add(memoryTriggers.get(k));
int temp = checkInArray(tempWords, memoryTriggers.get(k));
if (temp == 1) {
value += 4;
} else if (temp == 2) {
value += 2;
} else if (temp > 5) {
value -= 6;
} else {
value += 0;
}
}
}
}
if (value > highestScore) {
if (tempSentence.length() < 6) {
value += 20;
} else
if (tempSentence.length() < 10) {
value += 10;
} else if (tempSentence.length() < 20) {
value += 6;
} else if (tempSentence.length() < 30) {
value += 2;
}
System.out.println(value);
highestScore = value;
highestSentence = tempSentence;
}
}
}
} else {
//theres no memory of the channel
//Strip the message for triggers
String aTrigger = getTrigger(newMessage);
lastSpokenTrigger = aTrigger;
highestSentence = hal2.getSentence(aTrigger);
}
//Update the last spoken trigger and sentences
lastSpokenSentence = message;
//Debug time
double endTime = System.currentTimeMillis();
System.out.println("Time to start message:" + startTime);
System.out.println("Time to end message:" + endTime);
System.out.println("Time to get message:" + (endTime - startTime));
return highestSentence;
}
//checks how many times an item is found in an array
public int checkInArray(ArrayList <String> array, String keyWord) {
int checkHits = 0;
for (int i = 0; i < array.size(); i++) {
if (array.get(i).equalsIgnoreCase(keyWord)) {
checkHits++;
}
}
return checkHits;
}
public void addMemory(String channel, String message) {
//Store the message in short term memory
int maxSentences = 10;
boolean exists = false;
int index = 0;
for (int i = 0; i < memory.size(); i++) {
if (memory.get(0).equalsIgnoreCase("#" + channel)) {
exists = true;
index = i+1;
break;
}
}
if (exists == true) {
//channel exists:
//add the sentence
memory.add(index, "$" + message);
//find if the amount of sentences has exceeded the max.
boolean maxed = true;
for (int i = 0; i < maxSentences; i++) {
if ((index+i) >= memory.size()) {
//we reached the end
maxed = false;
break;
}
if (memory.get(index + i).charAt(0) == '#') {
//no increase in size
maxed = false;
break;
}
}
//were at the maximum sentences, remove the end sentence
if (maxed == true) {
if (index + maxSentences < memory.size()) {
if (memory.get(index+maxSentences).charAt(0) != '#') {
memory.remove(index+maxSentences);
}
}
}
} else {
//channel does not exist, add it!
memory.add("#" + channel);
memory.add("$" + message);
}
writeFile(memory, "memory.txt");
}
public void checkTriggers(ArrayList <String> checkCommands, String channel, String sender, String login, String hostname, String message) {
String sendMessage = "";
ArrayList<String> triggeredWords = new ArrayList<String>();
lastSaidIndexs.clear();
//get the triggered commands
for (int i=0; i < checkCommands.size()-1; i+=2) {
//check for period for case sensitive else just check without it.
if (checkCommands.get(i).charAt(0) == '.') {
if (message.contains(checkCommands.get(i).substring(1))) {
triggeredWords.add(checkCommands.get(i+1));
lastSaidIndexs.add(i);
}
} else {
if (message.toLowerCase().contains(checkCommands.get(i))) {
triggeredWords.add(checkCommands.get(i+1));
lastSaidIndexs.add(i);
}
}
}
//fix the triggered array to make sure it isn't using the same trigger multiple times
if (lastSaidIndexs.size() > 0) {
for (int i = 0; i < lastSaidIndexs.size(); i++) {
boolean test = false;
while (test == false && i < lastSaidIndexs.size()) {
if (lastSaid.contains(checkCommands.get(lastSaidIndexs.get(i)))) {
//sendMessage(channel, "removed " + checkCommands.get(lastSaidIndexs.get(i)+1));
triggeredWords.remove(checkCommands.get(lastSaidIndexs.get(i)+1));
lastSaidIndexs.remove(i);
} else {
test = true;
}
}
}
}
//decide if we're going to says something, if so decide what to say
if (triggeredWords.size() > 0) {
if (random.nextInt(4) <= 2) {
String data;
if (triggeredWords.size() == 1) {
sendMessage = triggeredWords.get(0);
if (random.nextInt(4) <= 2) {
data = hal2.getSentence(checkCommands.get(lastSaidIndexs.get(0)));
} else {
data = hal2.getSentence();
}
//Add this message to the lastSaid array and fix size if necessary.
lastSaid.add(checkCommands.get(lastSaidIndexs.get(0)));
if (lastSaid.size() > 10) {
lastSaid.remove(0);
}
} else {
//Find what to say
int randomNumber = random.nextInt(triggeredWords.size()-1);
sendMessage = triggeredWords.get(randomNumber);
//Add this message to the lastSaid array and fix size if necessary.
lastSaid.add(checkCommands.get(lastSaidIndexs.get(randomNumber)));
if (lastSaid.size() > 10) {
lastSaid.remove(0);
}
if (random.nextInt(4) <= 2) {
data = hal2.getSentence(checkCommands.get(lastSaidIndexs.get(randomNumber)));
} else {
data = hal2.getSentence();
}
}
for (int i = 0; i < sendMessage.length(); i++) {
if (sendMessage.charAt(i) == '$') {
String oldMessage = sendMessage;
sendMessage = oldMessage.substring(0, i) + sender + oldMessage.substring(i+1);
}
if (sendMessage.charAt(i) == '&') {
String oldMessage = sendMessage;
sendMessage = oldMessage.substring(0, i) + channel.substring(1) + oldMessage.substring(i+1);
}
}
speak1(channel, sender, login, hostname, data);
}
}
if (random.nextInt(20) == 0) {
talkingTo = "";
lastTalkingTo1 = sender;
//lastTalkingTo2 = lastTalkingTo1;
//lastTalkingTo3 = lastTalkingTo2;
}
}
//Updates word priorities based on a sentence given
public void updateWordPriority(String theSentence, String typeOfSentence) {
theSentence = removeStrings(theSentence, "smallWords.txt", true);
theSentence = removeSymbols(theSentence);
boolean done = false;
int amount = 0;
//Update the words priority in the sentence that she is responding to
if (typeOfSentence.equalsIgnoreCase("responding")) {
while (done != true) {