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 package com.buckosoft.fibs.BuckoFIBS;
312
313 import java.util.Date;
314 import java.util.Timer;
315 import java.util.TimerTask;
316
317 import com.buckosoft.fibs.BuckoFIBS.AudioManager.Cue;
318 import com.buckosoft.fibs.BuckoFIBS.gui.MainDialog;
319 import com.buckosoft.fibs.BuckoFIBS.gui.SystemMessagesTextPane;
320 import com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.ChatPane;
321 import com.buckosoft.fibs.domain.CookieString;
322 import com.buckosoft.fibs.domain.FinishedMatch;
323 import com.buckosoft.fibs.domain.Player;
324 import com.buckosoft.fibs.net.ClientAdapter;
325 import com.buckosoft.fibs.net.ClientConnection;
326 import com.buckosoft.fibs.net.FIBSMessages;
327
328
329
330
331
332
333
334
335 public class ClientReceiveParser implements FIBSMessages, ClientAdapter {
336 private final static boolean DEBUG = false;
337
338
339
340 public enum Mode {
341
342 Run,
343
344 Register
345 }
346 private MainDialog mainDialog;
347 private CommandDispatcher commandDispatcher = null;
348 private BFProperties properties = null;
349 private ClientConnection clientConnection = null;
350 private Mode mode = Mode.Run;
351 private final static String eol = "\r\n";
352
353 private String lastInviter = null;
354 private String lastMessage0;
355 private String lastMessage1;
356 private int lastCookie0;
357 private int lastCookie1;
358
359 private boolean whoVirgin = true;
360 private boolean needSavedGames = true;
361 private Date matchStartTime = null;
362
363 private boolean matchResumingYourTurn = false;
364
365 private FinishedMatch finishedMatch = null;
366
367
368
369
370 public void setMainDialog(MainDialog mainDialog) {
371 this.mainDialog = mainDialog;
372 }
373
374
375
376
377 public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
378 this.commandDispatcher = commandDispatcher;
379 }
380
381
382
383
384 public void setClientConnection(ClientConnection clientConnection) {
385 this.clientConnection = clientConnection;
386 }
387
388
389
390
391 public void setProperties(BFProperties properties) {
392 this.properties = properties;
393 }
394
395
396
397
398
399 public void setMode(Mode mode) {
400 this.mode = mode;
401 clientConnection.resetFIBSCookieMonster();
402 }
403
404
405
406 public void shutdown() {
407 if (this.myTimer != null) {
408 this.myTimer.cancel();
409 this.myTimer = null;
410 }
411 if (this.myTimerSavedGames != null) {
412 this.myTimerSavedGames.cancel();
413 this.myTimerSavedGames = null;
414 }
415 if (this.myTimerTaskSavedGames != null) {
416 this.myTimerTaskSavedGames.cancel();
417 this.myTimerTaskSavedGames = null;
418 }
419 }
420
421 private void dispatchMessage(int cookie, String s) {
422 CookieString gl;
423 lastMessage1 = lastMessage0;
424 lastMessage0 = s;
425 lastCookie1 = lastCookie0;
426 lastCookie0 = cookie;
427 if (DEBUG)
428 System.out.println("cookie = " + cookie);
429 if (this.properties.isDisplayRecv())
430 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
431 if (mode == Mode.Register) {
432 parseMessageRegister(s, cookie);
433 return;
434 }
435 switch (cookie) {
436
437 case CLIP_MOTD_END:
438 case FIBS_BoardstyleSetTo:
439 case FIBS_SavedMatchesHeader:
440 case FIBS_Empty:
441 case FIBS_UsersHeardYou:
442 case FIBS_YouAlreadyRolled:
443 break;
444 case CLIP_OWN_INFO:
445 this.commandDispatcher.dispatch(CommandDispatcher.Command.OWN_INFO, s);
446 break;
447 case CLIP_WHO_INFO:
448 parseWhoInfo(s);
449 break;
450 case CLIP_WHO_END:
451 handleWhoEnd();
452 break;
453 case FIBS_SavedMatchPlaying:
454 case FIBS_SavedMatchReady:
455 case FIBS_SavedMatch:
456
457 System.out.println(s);
458 this.commandDispatcher.dispatch(CommandDispatcher.Command.SAVED_MATCH, s);
459 break;
460 case FIBS_NoSavedMatch:
461 showErrorMessage(s);
462 this.commandDispatcher.dispatch(CommandDispatcher.Command.SAVED_MATCH, s);
463 break;
464 case FIBS_LoginPrompt:
465 whoVirgin = true;
466 this.commandDispatcher.writeSystemMessageln(s);
467 this.mainDialog.getDB().setActiveProfile(this.properties.getSelectedProfile());
468 this.mainDialog.updateRatingPanel();
469 this.mainDialog.setPlayerListTabVisible();
470 clientConnection.sendLogin();
471 break;
472 case CLIP_LOGIN:
473 parsePlayerLoggedIn(s);
474 break;
475 case CLIP_LOGOUT:
476 parsePlayerLoggedOut(s);
477 break;
478 case FIBS_OpponentLogsOut:
479 case FIBS_OpponentLeftGame:
480 parseAbortMatch(s);
481 break;
482 case FIBS_Goodbye:
483 disconnectFromServer();
484 break;
485 case FIBS_Timeout:
486 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
487 disconnectFromServer();
488 break;
489 case CLIP_WELCOME:
490 this.commandDispatcher.dispatch(CommandDispatcher.Command.NETWORK_CONNECTED);
491 break;
492 case FIBS_YouAreWatching:
493 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
494 this.commandDispatcher.dispatch(CommandDispatcher.Command.WATCHING);
495 break;
496 case CLIP_SAYS:
497 if (s.startsWith("12 RepBotNG ")) {
498 this.mainDialog.getPlayerReportPane().receiveLine(s, cookie);
499 return;
500 }
501 parseChatMessage(s, cookie);
502 break;
503 case CLIP_WHISPERS:
504 case CLIP_KIBITZES:
505 parseChatMessage(s, cookie);
506 break;
507 case CLIP_SHOUTS:
508 if (s.startsWith("13 MissManners"))
509 this.commandDispatcher.dispatch(CommandDispatcher.Command.MISS_MANNERS, s);
510 if(!properties.isMsgIgnoreShouts())
511 parseChatMessage(s, cookie);
512 break;
513 case CLIP_YOU_SAY:
514 parseChatMessage(s, cookie);
515 break;
516 case CLIP_YOU_SHOUT:
517 case CLIP_YOU_WHISPER:
518 case CLIP_YOU_KIBITZ:
519 parseYouChatMessage(s, cookie);
520 break;
521 case FIBS_NobodyHeardYou:
522 this.commandDispatcher.writeChatMessageln(null, cookie, s);
523 case FIBS_Waves:
524 case FIBS_WavesAgain:
525 if(!properties.isMsgIgnoreShouts())
526 parseWavesMessage(s);
527 break;
528 case FIBS_NewMatchRequest:
529 case FIBS_ResumeMatchRequest:
530 parseInvite(s);
531 break;
532 case FIBS_CantInviteSelf:
533 case FIBS_CantWatchSelf:
534 case FIBS_NotInteresting:
535 case FIBS_PlayerNotPlaying:
536 case FIBS_PlayerRefusingGames:
537 case FIBS_NotYourTurnToRoll:
538 case FIBS_NotYourTurnToMove:
539 case FIBS_PlayerBannedWatch:
540 case FIBS_DoesntWantYouToWatch:
541 showErrorMessage(s);
542 break;
543 case FIBS_ResumeMatchAck0:
544 case FIBS_ResumeMatchAck5:
545 displayGameMessage(s);
546 startNewGame(true);
547
548 break;
549 case FIBS_Turn:
550 displayGameMessage(s);
551 parseResumeMatchTurn(s, cookie);
552 break;
553 case FIBS_MatchLength:
554 displayGameMessage(s);
555 parseResumeMatchLength(s);
556 break;
557 case FIBS_Board:
558 if (DEBUG)
559 System.out.println(s);
560 this.mainDialog.getDocument().getBoard().parseFibsBoard(s);
561 gl = new CookieString(cookie, s);
562 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, gl);
563 if (this.matchResumingYourTurn) {
564 if (this.mainDialog.getDocument().getBoard().isYouMayDouble()) {
565 if (this.mainDialog.getDocument().getBoard().getDice()[0][0] == 0) {
566 this.commandDispatcher.dispatch(CommandDispatcher.Command.ROLL_OR_DOUBLE);
567 }
568 }
569 this.matchResumingYourTurn = false;
570 }
571 doAutoGreedyBearoff();
572 break;
573 case FIBS_NewMatchAck10:
574 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
575 this.mainDialog.setGameMessagesTabVisible();
576 break;
577 case FIBS_NewMatchAck2:
578 case FIBS_FirstRoll:
579 case FIBS_MakesFirstMove:
580 case FIBS_PlayerRolls:
581 case FIBS_YouRoll:
582 case FIBS_PlayerMoves:
583 case FIBS_CantMove:
584 case FIBS_OnlyPossibleMove:
585 case FIBS_PleaseMove:
586 case FIBS_YourTurnToMove:
587 case FIBS_AcceptRejectDouble:
588 case FIBS_WatchResign:
589 case FIBS_ResignRefused:
590 case FIBS_AcceptWins:
591 case FIBS_PlayerWantsToResign:
592 case FIBS_YouCantMove:
593 case FIBS_BearingOff:
594 case FIBS_YouAcceptAndWin:
595 case FIBS_YouResign:
596 case FIBS_YouReject:
597 case FIBS_YouDouble:
598 displayGameMessage(s);
599 gl = new CookieString(cookie, s);
600 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, gl);
601 break;
602 case FIBS_StartingNewGame:
603 startNewGame(false);
604 displayGameMessage(s);
605 gl = new CookieString(cookie, s);
606 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, gl);
607 break;
608 case FIBS_YouWinMatch:
609 case FIBS_PlayerWinsMatch:
610 parseMatchOverMessage(s, cookie);
611 displayGameMessage(s);
612 break;
613 case FIBS_ReadyTrue:
614 case FIBS_ReadyFalse:
615 parseReadyMessage(cookie);
616 break;
617
618
619
620 case FIBS_JoinNextGame:
621 this.commandDispatcher.writeNetworkMessageln("join");
622 break;
623 case FIBS_PlayerWinsGame:
624 case FIBS_ScoreUpdate:
625 case FIBS_ResignWins:
626 case FIBS_PlayerStartsWatching:
627 case FIBS_PointsFor:
628 case FIBS_YouWinGame:
629 case FIBS_CantMoveFirstMove:
630 case FIBS_RollBeforeMove:
631 case FIBS_Done:
632 case FIBS_GreedyTrue:
633 case FIBS_GreedyFalse:
634 case FIBS_ResignYouWin:
635 case FIBS_PleaseWaitForJoin:
636 case FIBS_YouGiveUp:
637 case FIBS_MustMove:
638 displayGameMessage(s);
639 break;
640 case FIBS_PlayerAcceptsDouble:
641 gl = new CookieString(cookie, s);
642 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, gl);
643 this.mainDialog.getDocument().setYouDouble(false);
644 displayGameMessage(s);
645 break;
646 case FIBS_DidntDoubleOrResign:
647 displayGameError(s);
648 break;
649 case FIBS_PlayersStartingMatch:
650 case FIBS_PlayerStopsWatching:
651 case FIBS_PlayersStartingUnlimitedMatch:
652 case FIBS_ResumingLimitedMatch:
653 case FIBS_ResumingUnlimitedMatch:
654 case FIBS_MatchResult:
655 parseOtherMatchMessage(s);
656 break;
657 case FIBS_PlayerInfoStart:
658 case FIBS_LastLogin:
659 case FIBS_StillLoggedIn:
660 case FIBS_NotLoggedIn:
661 case FIBS_ReadyWatchingPlaying:
662 case FIBS_RatingExperience:
663 case FIBS_NoEmail:
664 case FIBS_EmailAddress:
665 case FIBS_IsPlayingWith:
666 case FIBS_HasSavedGames:
667 case FIBS_HasNoSavedGames:
668 case FIBS_NoInfo:
669 this.mainDialog.getPlayerReportPane().receiveLine(s, cookie);
670
671 break;
672
673 case FIBS_PreLogin:
674 case CLIP_MOTD_BEGIN:
675 case FIBS_TypeJoin:
676 case FIBS_YouStopWatching:
677 case FIBS_YouAlreadyPlaying:
678 case FIBS_AlreadyPlaying:
679 case FIBS_LastLogout:
680 case FIBS_NotPlaying:
681 case FIBS_NoUser:
682 case FIBS_EchoJunk:
683 case FIBS_WaitForLastInvitation:
684 case FIBS_WaitForAcceptResign:
685 case FIBS_YourEmailChanged:
686 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
687 break;
688 case FIBS_DidntInvite:
689 case FIBS_NotWatchingPlaying:
690 case FIBS_NoSavedGames:
691 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NETWORKMSG, s);
692 this.mainDialog.setSystemMessagesTabVisible();
693 break;
694 case FIBS_WARNINGSavedMatch:
695 parseInviteWarning(s);
696 break;
697 case FIBS_DoubleTrue:
698 parseDoublesOnOff(true);
699 displayGameMessage(s);
700 break;
701 case FIBS_DoubleFalse:
702 parseDoublesOnOff(false);
703 displayGameMessage(s);
704 break;
705 case FIBS_Doubles:
706 parseDoubles(s);
707 gl = new CookieString(cookie, s);
708 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, gl);
709 displayGameMessage(s);
710 break;
711 case FIBS_YouAcceptDouble:
712 displayGameMessage(s);
713 this.mainDialog.getDocument().setYouDouble(false);
714 this.mainDialog.updateBoard();
715 break;
716 case FIBS_RollOrDouble:
717 this.commandDispatcher.dispatch(CommandDispatcher.Command.ROLL_OR_DOUBLE);
718 break;
719
720
721
722 case FIBS_YouInvited:
723 parseYouInvited(s);
724 break;
725
726
727
728
729
730 case FIBS_ATTENTION:
731 case FIBS_ShuttingDown:
732 case FIBS_Rebooting:
733 this.mainDialog.playSound(Cue.FibsAttention);
734 case FIBS_GamesWillBeSaved:
735 case FIBS_UnknownCommand:
736 case FIBS_GameWasSaved:
737 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, s);
738 break;
739 case FIBS_Unknown:
740 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, "Unknown message from FIBS: '" + s + "'");
741 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, "First char is " + new Integer(s.charAt(0)));
742 break;
743 default:
744 if (this.commandDispatcher.getProperties().isDEBUG_ShowUnhandledCookies()) {
745 String t = "Cookie? " + cookie + " '" + s + "'";
746 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, t);
747 System.out.println(t);
748 t = "Last message was " + lastCookie1 + " '" + lastMessage1 + "'";
749 if (cookie == FIBS_Unknown) {
750 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, t);
751 System.out.println(t);
752 }
753 }
754 break;
755 }
756 }
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775 private enum RegState {
776 name,
777 password,
778 password1,
779 done
780 }
781 private RegState regState = RegState.name;
782
783 private void parseMessageRegister(String s, int fibsCookie) {
784 int profileId = this.commandDispatcher.getProperties().getSelectedProfile();
785 String password;
786 switch (fibsCookie) {
787 case FIBS_LoginPrompt:
788 this.commandDispatcher.writeRegisterUserMessage("Logging in...");
789 this.commandDispatcher.writeSystemMessageln(s);
790 clientConnection.sendMessage("guest" + eol);
791 break;
792 case FIBS_OneUserPerPerson:
793 String userName = this.commandDispatcher.getProperties().getUserName(profileId);
794 this.commandDispatcher.writeRegisterUserMessage("Registering user " + userName);
795 this.clientConnection.sendMessage("name " + userName + eol);
796 regState = RegState.password;
797 break;
798
799 case FIBS_GivePassword:
800 password = this.commandDispatcher.getProperties().getPassword(profileId);
801 this.clientConnection.sendMessage(password + eol);
802 break;
803 case FIBS_RetypePassword:
804 password = this.commandDispatcher.getProperties().getPassword(profileId);
805 this.clientConnection.sendMessage(password + eol);
806 break;
807 case FIBS_YouAreRegistered:
808 this.commandDispatcher.writeRegisterUserMessage("Success!");
809
810 this.commandDispatcher.dispatch(CommandDispatcher.Command.CONNECT_TO_SERVER);
811 break;
812 case FIBS_UseAnotherName:
813 this.commandDispatcher.dispatch(CommandDispatcher.Command.BAD_NEW_USER, s);
814 break;
815 case CLIP_MOTD_BEGIN:
816 if (regState == RegState.password) {
817 password = this.commandDispatcher.getProperties().getPassword(profileId);
818 this.clientConnection.sendMessage(password + eol);
819 regState = RegState.password1;
820 break;
821 }
822 if (regState == RegState.password1) {
823 password = this.commandDispatcher.getProperties().getPassword(profileId);
824 this.clientConnection.sendMessage(password + eol);
825 regState = RegState.done;
826 break;
827 }
828 }
829 }
830
831 private void showErrorMessage(String s) {
832 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, s);
833 }
834
835 private void disconnectFromServer() {
836 this.commandDispatcher.dispatch(CommandDispatcher.Command.SHUTTING_DOWN);
837 this.clientConnection.resetFIBSCookieMonster();
838 }
839
840 private void parseWhoInfo(String s) {
841 Player p = new Player();
842 p.parsePlayer(s);
843 if (this.finishedMatch != null && p.getName().equals(this.mainDialog.getDocument().getName())) {
844 this.finishedMatch.setRatingAfterMatch(p.getRating());
845 this.mainDialog.getDB().store(this.finishedMatch);
846 this.finishedMatch = null;
847 this.mainDialog.updateRatingPanel();
848 this.mainDialog.getDB().updateOpponent(p);
849 this.mainDialog.getPlayerListTab().getPlayerTableModel().playerChanged(p);
850 }
851 this.commandDispatcher.dispatch(CommandDispatcher.Command.PLAYER_CHANGED, p);
852 }
853
854 private void handleWhoEnd() {
855 if (this.whoVirgin) {
856 sendDelayedWhoCommand();
857 } else if (this.needSavedGames) {
858 sendDelayedSavedGamesCommand();
859 this.needSavedGames = false;
860 }
861 }
862
863 private void parsePlayerLoggedOut(String s) {
864 String[] ss = s.split(" ");
865 this.commandDispatcher.dispatch(CommandDispatcher.Command.PLAYER_GONE, ss[1]);
866 if (this.properties.isMsgLoginsAndOuts())
867 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NORMAL, ss[2] + " " + ss[3] + " " + ss[4]);
868 }
869
870 private void parseAbortMatch(String s) {
871 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, s);
872 this.mainDialog.playSound(Cue.MatchAborted);
873 this.commandDispatcher.writeNetworkMessageln("show saved");
874 }
875
876
877
878
879
880 private void parsePlayerLoggedIn(String s) {
881 String[] ss = s.split(" ");
882 if (this.properties.isMsgLoginsAndOuts())
883 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NORMAL, ss[2] + " " + ss[3] + " " + ss[4]);
884 }
885
886 private void parseOtherMatchMessage(String s) {
887 if (this.properties.isMsgOtherMatchInfo())
888 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NORMAL, s);
889 }
890
891 private void parseInvite(String s) {
892 String[] ss = s.split(" ");
893 this.commandDispatcher.dispatch(CommandDispatcher.Command.INVITED,
894 ss[0],
895 ss[3].equals("resume") ? ss[3] : ss[5]);
896 lastInviter = ss[0];
897 }
898
899 private void parseInviteWarning(String s) {
900 if (lastInviter != null) {
901 this.commandDispatcher.dispatch(CommandDispatcher.Command.INVITE_WARNING, lastInviter, s);
902 }
903 }
904
905 private void parseYouInvited(String s) {
906 String[] ss = s.split(" ");
907 this.commandDispatcher.dispatch(CommandDispatcher.Command.YOU_INVITED, ss[3]);
908 this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.NORMAL, s);
909 }
910
911 private void parseDoubles(String s) {
912 this.commandDispatcher.writeNetworkMessageln("board");
913 }
914
915 private void startNewGame(boolean resume) {
916 if (this.matchStartTime == null)
917 this.matchStartTime = new Date();
918 if (!resume)
919 this.commandDispatcher.dispatch(CommandDispatcher.Command.START_GAME);
920 else
921 this.commandDispatcher.dispatch(CommandDispatcher.Command.RESUME_GAME);
922
923 }
924
925 private void displayGameMessage(String s) {
926 this.commandDispatcher.writeGameMessageln(s);
927 }
928 private void displayGameError(String s) {
929 this.commandDispatcher.writeGameErrorln(s);
930 }
931
932 private void parseMatchOverMessage(String s, int cookie) {
933 String[] ss = s.split(" ");
934 this.commandDispatcher.dispatch(CommandDispatcher.Command.MATCH_OVER, ss[0], ss[6]);
935 String opponentName = this.mainDialog.getDocument().getBoard().getPlayerName()[1];
936 int[] scores = this.mainDialog.getDocument().getBoard().getMatchScore();
937 if (this.mainDialog.getDocument().getBoard().isYouPlaying()) {
938 this.finishedMatch = new FinishedMatch();
939 this.finishedMatch.setMatchPoints(Integer.parseInt(ss[3]));
940 this.finishedMatch.setYourScore(scores[0]);
941 this.finishedMatch.setOpponentScore(scores[1]);
942 Date endDate = new Date();
943 this.finishedMatch.setDate(endDate);
944 this.finishedMatch.setDuration((int)((endDate.getTime()-this.matchStartTime.getTime())/1000));
945 this.matchStartTime = null;
946 this.mainDialog.getDocument().removeSavedMatch(opponentName);
947 Player op = this.mainDialog.getDB().getPlayer(opponentName);
948 if (op == null) {
949 op = this.mainDialog.getPlayerTableModel().getPlayer(opponentName);
950 this.mainDialog.getDB().store(op);
951 }
952 op.setSavedMatch(null);
953 this.finishedMatch.setOpponentId(op.getId());
954 this.commandDispatcher.dispatch(CommandDispatcher.Command.PLAYER_CHANGED, op);
955
956 this.mainDialog.getChatPane().setupComboBoxCommand(ChatPane.Command.Tell);
957 this.mainDialog.getChatPane().maybeAddPlayer(opponentName);
958 }
959 }
960
961 private void parseDoublesOnOff(boolean onoff) {
962 this.commandDispatcher.dispatch(CommandDispatcher.Command.TOGGLE_DOUBLE, new Boolean(onoff));
963
964 }
965
966 private void parseReadyMessage(int cookie) {
967 boolean b = false;
968 if (cookie == FIBS_ReadyTrue)
969 b = true;
970 this.commandDispatcher.dispatch(CommandDispatcher.Command.READY_TO_PLAY, new Boolean(b));
971 }
972
973 private void parseChatMessage(String s, int cookie) {
974 int i = s.indexOf(' ', 3);
975 if (i == -1)
976 return;
977 String name = s.substring(3, i);
978 String text = s.substring(i+1);
979 this.mainDialog.playSound(Cue.ChatReceived);
980 this.commandDispatcher.writeChatMessageln(name, cookie, text);
981 }
982
983 private void parseYouChatMessage(String s, int cookie) {
984 this.mainDialog.playSound(Cue.ChatReceived);
985 this.commandDispatcher.writeChatMessageln(null, cookie, s.substring(3));
986 }
987
988 private void parseWavesMessage(String s) {
989 this.commandDispatcher.writeSystemMessageln(s);
990 }
991
992 private void parseResumeMatchTurn(String s, int cookie) {
993 String t = s.substring(6, s.length()-1);
994 if (this.mainDialog.getDocument().getName().equals(t)) {
995 this.matchResumingYourTurn = true;
996 CookieString cs = new CookieString(cookie, s);
997 this.commandDispatcher.dispatch(CommandDispatcher.Command.GAME_EVENT, cs);
998 }
999 }
1000
1001 private void parseResumeMatchLength(String s) {
1002
1003 }
1004
1005
1006
1007
1008
1009 private void doAutoGreedyBearoff() {
1010 if (!this.mainDialog.getDocument().getBoard().isYouPlaying())
1011 return;
1012 if (!this.properties.isAutoGreedyBearOff())
1013 return;
1014 if (this.mainDialog.getDocument().isGreedy())
1015 return;
1016 if (this.mainDialog.getDocument().getBoard().isRace())
1017 this.commandDispatcher.dispatch(CommandDispatcher.Command.TOGGLE_GREEDY_BEAROFF);
1018 }
1019
1020
1021
1022
1023 @Override
1024 public void dispatch(int cookie, String s) {
1025 this.dispatchMessage(cookie, s);
1026 }
1027
1028
1029
1030
1031 @Override
1032 public void writeSystemMessageln(MessageRoute route, String s) {
1033 this.writeSystemMessage(route, s + eol);
1034 }
1035
1036
1037
1038
1039 @Override
1040 public void writeSystemMessage(MessageRoute route, String s) {
1041 if (route == ClientAdapter.MessageRoute.NETWORKOUT)
1042 this.commandDispatcher.writeSystemMessage(SystemMessagesTextPane.NETWORKOUT, s);
1043 else if (route == ClientAdapter.MessageRoute.DEBUG)
1044 this.commandDispatcher.writeSystemMessage(SystemMessagesTextPane.DEBUG, s);
1045 else if (route == ClientAdapter.MessageRoute.SYSTEM)
1046 this.commandDispatcher.writeSystemMessage(SystemMessagesTextPane.NORMAL, s);
1047 else if (route == ClientAdapter.MessageRoute.ERROR)
1048 this.commandDispatcher.writeSystemMessage(SystemMessagesTextPane.ERROR, s);
1049 }
1050
1051
1052 @Override
1053 public void connectionAborted() {
1054 this.commandDispatcher.dispatch(CommandDispatcher.Command.DISCONNECT_FROM_NETWORK);
1055 }
1056
1057
1058
1059
1060
1061 private Timer myTimer = null;
1062 private TimerTask myTimerTask = null;
1063
1064 private void sendDelayedWhoCommand() {
1065 if (myTimer != null) {
1066 return;
1067 }
1068 myTimer = new Timer();
1069 try {
1070 myTimerTask = new MyTimerTask(this);
1071 myTimer.schedule(myTimerTask, 2000);
1072 } catch (IllegalStateException e) {
1073 e.printStackTrace();
1074 }
1075 }
1076
1077 private class MyTimerTask extends TimerTask {
1078 private ClientReceiveParser crp;
1079
1080 MyTimerTask(ClientReceiveParser crp) {
1081 super();
1082 this.crp = crp;
1083 }
1084
1085
1086
1087 @Override
1088 public void run() {
1089 this.crp.commandDispatcher.writeNetworkMessageln("who");
1090 this.crp.whoVirgin = false;
1091 this.crp.myTimer.cancel();
1092 this.crp.myTimer = null;
1093 }
1094 }
1095
1096
1097
1098
1099 private Timer myTimerSavedGames = null;
1100 private TimerTask myTimerTaskSavedGames = null;
1101
1102 private void sendDelayedSavedGamesCommand() {
1103 if (myTimerSavedGames != null) {
1104 return;
1105 }
1106 myTimerSavedGames = new Timer();
1107 try {
1108 myTimerTaskSavedGames = new MyTimerTaskSavedGames(this);
1109 myTimerSavedGames.schedule(myTimerTaskSavedGames, 1000);
1110 } catch (IllegalStateException e) {
1111 e.printStackTrace();
1112 }
1113 }
1114
1115 private class MyTimerTaskSavedGames extends TimerTask {
1116 private ClientReceiveParser crp;
1117
1118 MyTimerTaskSavedGames(ClientReceiveParser crp) {
1119 super();
1120 this.crp = crp;
1121 }
1122
1123
1124
1125 @Override
1126 public void run() {
1127 this.crp.commandDispatcher.writeNetworkMessageln("show saved");
1128 this.crp.myTimerSavedGames.cancel();
1129 this.crp.myTimerSavedGames = null;
1130 this.crp.myTimerTaskSavedGames.cancel();
1131 this.crp.myTimerTaskSavedGames = null;
1132 }
1133 }
1134
1135 }