View Javadoc

1   package erland.game.tetris;
2   /*
3    * Copyright (C) 2003 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  import erland.network.NetworkConnectionInterface;
22  import erland.network.NetworkClientListenerInterface;
23  import erland.network.NetworkClient;
24  import erland.game.GameEnvironmentInterface;
25  
26  public class TetrisModelNetworked implements TetrisModelInterface,NetworkClientListenerInterface {
27      private BlockContainerData mainContainer;
28      private Block nextBlock;
29      private BlockContainerData opponentMainContainer;
30      private boolean bEnd;
31      private boolean bStarted;
32      private boolean bCompleted;
33      private String highScore;
34      private String score;
35      private String opponentScore;
36      private String level;
37      private String opponentLevel;
38      private boolean bOpponentConnected;
39      private NetworkConnectionInterface connection;
40      private String hostname;
41      private String username;
42      private String password;
43  
44      public TetrisModelNetworked(String hostname,String username,String password) {
45          this.hostname = hostname;
46          this.username = username;
47          this.password = password;
48      }
49  
50      public BlockContainerData getMainContainer() {
51          return mainContainer;
52      }
53  
54  
55      public Block getNextBlock() {
56          return nextBlock;
57      }
58  
59      public BlockContainerData getOpponentMainContainer() {
60          return opponentMainContainer;
61      }
62  
63      public boolean isEnd() {
64          return bEnd;
65      }
66  
67      public boolean isStarted() {
68          return bStarted;
69      }
70  
71      public boolean isCompleted() {
72          return bCompleted;
73      }
74  
75      public String getHighScore() {
76          return highScore!=null?highScore:"";
77      }
78  
79      public String getScore() {
80          return score!=null?score:"";
81      }
82  
83      public String getOpponentScore() {
84          return opponentScore!=null?opponentScore:"";
85      }
86  
87      public String getLevel() {
88          return level!=null?level:"";
89      }
90  
91      public String getOpponentLevel() {
92          return opponentLevel!=null?opponentLevel:"";
93      }
94  
95      public boolean isMultiplayer() {
96          return true;
97      }
98  
99      public boolean isOpponentConnected() {
100         return bOpponentConnected;
101     }
102 
103     public void init(GameEnvironmentInterface environment) {
104         mainContainer = new BlockContainerData(10,30);
105         opponentMainContainer = new BlockContainerData(10,30);
106         bEnd = false;
107         bCompleted = false;
108         bStarted=false;
109         bOpponentConnected=false;
110         System.out.println("Please wait, connecting to "+hostname+"...");
111         connection = NetworkClient.connect(hostname,1123,5,username,password,this);
112         connection.write("INIT");
113     }
114     protected void initNewGame() {
115         bEnd = false;
116         bStarted=true;
117         bCompleted = false;
118     }
119 
120     public void moveLeft() {
121         connection.write("LEFT");
122     }
123 
124     public void moveRight() {
125         connection.write("RIGHT");
126     }
127 
128     public void rotate() {
129         connection.write("ROTATE");
130     }
131 
132     public void moveDown() {
133         connection.write("DOWN");
134     }
135 
136     public void startGame() {
137         connection.write("START");
138     }
139 
140     public void update() {
141         // Do nothing, the model update is done on the server
142     }
143 
144     public void connected(NetworkConnectionInterface connection) {
145         System.out.println("Tetris.connected");
146     }
147 
148     public void disconnected(NetworkConnectionInterface connection) {
149         System.out.println("Tetris.disconnected");
150     }
151 
152     public void message(NetworkConnectionInterface connection, String message) {
153         if(message.charAt(0)=='1') {
154             String status = message.substring(327);
155             if(status.equals("CONNECTED")) {
156                 score = message.substring(313,313+6);
157                 highScore = message.substring(319,319+6);
158                 level = message.substring(325,325+2);
159             }else {
160                 nextBlock = getBlockFromString(nextBlock,message.substring(7,12));
161                 mainContainer.fromString(message.substring(13));
162                 score = message.substring(313,313+6);
163                 highScore = message.substring(319,319+6);
164                 level = message.substring(325,325+2);
165                 //System.out.println("message = " + message);
166                 if(status.equals("GAMEOVER")) {
167                     bEnd = true;
168                 }else if(status.equals("COMPLETE")) {
169                     bEnd = true;
170                     bCompleted = true;
171                 }
172             }
173         }else if(message.charAt(0)=='2') {
174             opponentMainContainer.fromString(message.substring(13));
175             opponentScore = message.substring(313,313+6);
176             opponentLevel = message.substring(325,325+2);
177             String status = message.substring(327);
178             if(status.equals("CONNECTED")) {
179                 bOpponentConnected = true;
180             }else if(status.equals("DISCONNECTED")) {
181                 bOpponentConnected = false;
182             }
183         }else if(message.equals("STARTED")) {
184             initNewGame();
185         }else if(message.equals("ENDED") || message.equals("STOPPED")) {
186             bStarted = false;
187         }
188     }
189     protected Block getBlockFromString(Block oldBlock, String message) {
190         char c = message.charAt(0);
191         Block block = oldBlock;
192         if(c=='0') {
193             return null;
194         }else if(c=='1') {
195             if(!(oldBlock instanceof Block1)) {
196                 block = new Block1();
197             }
198         }else if(c=='2') {
199             if(!(oldBlock instanceof Block2)) {
200                 block = new Block2();
201             }
202         }else if(c=='3') {
203             if(!(oldBlock instanceof Block3)) {
204                 block = new Block3();
205             }
206         }else if(c=='4') {
207             if(!(oldBlock instanceof Block4)) {
208                 block = new Block4();
209             }
210         }else if(c=='5') {
211             if(!(oldBlock instanceof Block5)) {
212                 block = new Block5();
213             }
214         }else if(c=='6') {
215             if(!(oldBlock instanceof Block6)) {
216                 block = new Block6();
217             }
218         }else if(c=='7') {
219             if(!(oldBlock instanceof Block7)) {
220                 block = new Block7();
221             }
222         }
223         block.init(Integer.parseInt(message.substring(2,3)),Integer.parseInt(message.substring(2,3)),(c-'0')*90);
224         return block;
225     }
226 }