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.util.*;
22  import erland.game.*;
23  
24  
25  public class TetrisServer extends GameServer implements GameServerEnvironmentInterface {
26      private HighScore highScore;
27      private ParameterValueStorageExInterface storage;
28      private FileStorage file;
29      private boolean bEndGame;
30  
31      public static void main(String[] args) {
32          TetrisServer me = new TetrisServer();
33          me.run(1123);
34      }
35  
36      protected int getMaxPlayers() {
37          return 2;
38      }
39  
40      protected GamePlayerInterface createPlayer() {
41          return new TetrisPlayer(this);
42      }
43  
44      public void initPlayer(GamePlayerInterface player) {
45          sendConnectionStatus(player);
46          updateConnectionStatus(player,"CONNECTED");
47      }
48  
49      public void disconnected(GamePlayerInterface player) {
50          updateConnectionStatus(player,"DISCONNECTED");
51      }
52  
53      public void command(GamePlayerInterface player, String message) {
54              ((TetrisPlayer)player).doCommand(message);
55      }
56  
57      public boolean startGame() {
58  
59          bEndGame = false;
60          BlockFactory.getInstance().reset();
61          return true;
62      }
63  
64      protected void startPlayer(GamePlayerInterface player) {
65          ((TetrisPlayer)player).startNewGame();
66      }
67  
68      public void sendConnectionStatus(GamePlayerInterface me) {
69  
70          GamePlayerInterface[] players = getPlayers();
71          for(int i = 0; i < players.length;i++) {
72              TetrisPlayer player = (TetrisPlayer) players[i];
73              if(player==me) {
74                  me.getConnection().write("1"+player.toString()+"CONNECTED");
75              }else {
76                  me.getConnection().write("2"+player.toString()+"CONNECTED");
77              }
78          }
79      }
80  
81      public void updateConnectionStatus(GamePlayerInterface me, String status) {
82          GamePlayerInterface[] players = getPlayers();
83          for (int i = 0; i < players.length;i++) {
84              TetrisPlayer player = (TetrisPlayer) players[i];
85              if(player!=me) {
86                  player.getConnection().write("2"+me.toString()+status);
87              }
88          }
89      }
90  
91      public void sendUpdate(TetrisPlayer me, String message) {
92          GamePlayerInterface[] players = getPlayers();
93          for (int i = 0; i < players.length;i++) {
94              TetrisPlayer player = (TetrisPlayer) players[i];
95              if(player==me) {
96                  player.getConnection().write("1"+message);
97              }else {
98                  player.getConnection().write("2"+message);
99              }
100         }
101     }
102 
103 
104 
105     protected boolean isEndGame() {
106         return bEndGame;
107     }
108 
109     protected void updatePlayer(GamePlayerInterface player) {
110         TetrisPlayer p = (TetrisPlayer) player;
111         if(!bEndGame && !p.isGameOver()) {
112             if(p.update() || p.needUpdate()) {
113                 String s = player.toString();
114                 if(p.isGameOver()) {
115                     if(p.isCompleted()) {
116                         sendUpdate(p,s+"COMPLETE");
117                     }else {
118                         sendUpdate(p,s+"GAMEOVER");
119                     }
120                 }else {
121                     sendUpdate(p,s+"RUNNING");
122                 }
123             }
124         }
125     }
126     protected void updateGame() {
127         boolean bGameOver = true;
128         GamePlayerInterface[] players = getPlayers();
129         for(int i = 0; i < players.length;i++) {
130             TetrisPlayer player = (TetrisPlayer) players[i];
131             if(!player.isGameOver()) {
132                 bGameOver = false;
133             }
134         }
135         if(bGameOver) {
136             bEndGame = true;
137         }
138     }
139 
140 
141     public ParameterValueStorageExInterface getStorage() {
142         if(storage==null) {
143             file = new FileStorage("tetris.xml");
144             storage = new ParameterStorageString(file,null,"tetris");
145         }
146         return storage;
147     }
148 
149     public HighScoreInterface getHighScore() {
150         if(highScore==null) {
151             highScore = new HighScore(getStorage());
152             highScore.load();
153         }
154         return highScore;
155     }
156 
157     public HighScoreListInterface getHighScoreList() {
158         return null;
159     }
160 }