home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 28.4 KB | 894 lines |
- package borland.samples.apps.chess.server;
-
- import java.net.*;
- import java.io.*;
- import java.util.*;
- import borland.samples.apps.chess.client.ServerMessage;
- import borland.samples.apps.chess.client.PersistString;
-
- class SendThread extends Thread {
- ServerSocket serverSocket = null;
- String socketnum;
- int portnum;
- PlayerId name;
- GameLibrary library = null;
- String outputLine;
- private Vector msgque = null;
- Vector suspendList = null;
- Game game = null;
- boolean isPlaying = false;
- //PrintStream os ;
- PrintWriter os ;
- SendThread opponent = null;
- SendThread token = null;
-
- SendThread(int port) {
- outputLine = "?";
- try {
- portnum = port;
- socketnum = String.valueOf(port);
- serverSocket = new ServerSocket(port);
- msgque = new Vector(5,5);
- //System.out.println("MHello1");
- }
- catch (IOException e) {
- System.out.println("Could not listen on port: " + 4444 + ", " + e);
- System.exit(1);
- }
- }
-
- void handleBye(ServerMessage smsg) {
- if (game == null) {
- if (name != null ) {
- System.out.println(name.user + "was not playing") ;
- ChessServer.playerlist.remove(name.user);
- }
- }
- else {
- if (isPlaying)
- SuspendGame();
- if (game.RemoveObserver(this)) //if last observer of the game
- ChessServer.gamelist.remove(game.getPlayers()); //remove yourself
- if (opponent == null) {
- System.out.println(name.user + "was playing, but game over/suspended") ;
- if (token != null)
- token.addMsg(portnum,"Token",name.user + ": parting");
- }
- else
- opponent.addMsg(portnum,"Token",name.user + ": parting...");
-
- ChessServer.playerlist.remove(name.user);
- game = null;
- token = null;
- isPlaying = false;
- }
- }
- void addMsg(int portnum,String msgid,String msg) {
- addMsg(new ServerMessage(portnum,msgid,msg));
- }
- synchronized void addMsg(ServerMessage msg) {
- msgque.addElement(msg);
- notify();
- }
- void handleMove(ServerMessage smsg) {
- smsg.msgid = "Moved";
- if (opponent != null) {
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- }
- game.AddMove(smsg);
- }
-
- void handleInfo(ServerMessage smsg) {
- PlayerId p = (PlayerId) PlayerId.usersByUser.get(smsg.msg);
- if (p != null) {
- String temp = p.password;
- if (smsg.msgid.equals("Info"))
- p.password = " ";
- os.println(smsg.msgid + " " + p.Output());
- os.flush();
- p.password = temp;
- }
- else
- System.out.println("Don't know " + smsg.msg);
- }
-
- void handleUpdateInfo(ServerMessage smsg) {
- String oldId = name.id;
- String oldUser = name.user;
- int oldRating = name.rating;
- int oldGamesPlayed = name.gamesPlayed;
- name.read(smsg.msg);
- name.id = oldId;
- name.user = oldUser;
- name.rating = oldRating;
- name.gamesPlayed = oldGamesPlayed;
- name.isDirty();
- }
-
- void handleSuspend(ServerMessage smsg) {
- SuspendGame();
- if (opponent != null)
- opponent.addMsg(portnum,"Token",name.user + ": parted");
- //synchronized (opponent) {
- // opponent.msgque.addElement((new ServerMessage(portnum,"Token",name.user + ": parted")));
- // opponent.notify();
- //}
- opponent = null;
- token = null;
- isPlaying = false;
- }
-
- //Offer draw or abort
- void handleOffers(ServerMessage smsg) {
- if (smsg.port != portnum) {
- os.println(smsg.msgid);
- os.flush();
- }
- else {
- if (opponent != null) {
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- }
- System.out.println("remembering the offer until it is refused");
- //gameover takes care of itself
- int offer = Game.DrawOffer;
- if (smsg.msg.equals("OfferAbort"))
- offer = Game.AbortOffer;
- game.setOffer(name.user,offer);
- }
- }
- void handleNote(ServerMessage smsg) {
- handleNote(PersistString.parse(smsg.msg));
- }
- void handleNote(String msgData[]) {
- System.out.println("handle note: is " + msgData[0] + "logged on?");
- SendThread recipient = null;
- if (ChessServer.playerlist.containsKey(msgData[0]))
- recipient = (SendThread)ChessServer.playerlist.get(msgData[0]);
- if (recipient != null) {
- recipient.addMsg(portnum,"Chat",msgData[1]);
- //synchronized (recipient) {
- // recipient.msgque.addElement(new ServerMessage(portnum,"Chat",msgData[1]));
- // recipient.notify();
- //}
- }
- else {
- System.out.println("look in dataset for " + msgData[0]);
- PlayerId toPlayer = (PlayerId)PlayerId.usersByUser.get(msgData[0]);
- if (toPlayer == null)
- System.out.println(msgData[0] + "is not in the hashtable??");
- else {
- System.out.println("add message to player " + toPlayer.name);
- toPlayer.addNote(msgData[1]);
-
- }
- }
- }
- void handleChat(ServerMessage smsg) {
- if (smsg.port != portnum) {
- System.out.println("ST report Chat" );
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- else
- if (isPlaying) {
- if (opponent == null) {
- System.out.println("ST opponent Note" );
- if (game != null) {
- String [] msgData = new String[2];
- msgData[0] = game.white;
- if (game.white.equals(name.user))
- msgData[0] = game.black;
- msgData[1] = smsg.msg;
- handleNote(msgData);
- }
- else
- System.out.println("handleChat: isPlaying but game is null??");
- }
- else {
- System.out.println("ST opponent Chat" );
- os.println(smsg.msgid+" "+smsg.msg);
- os.flush();
- opponent.addMsg(smsg);
- // synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- }
- }
- else {
- if (game != null) {
- System.out.println("ST group Chat" );
- game.Chat(smsg);
- }
- else {
- System.out.println("Broadcast chat?");
- Enumeration e = ChessServer.playerlist.elements();
- while (e.hasMoreElements()) {
- SendThread t = (SendThread) e.nextElement();
- if (!t.isPlaying && t != this)
- t.addMsg(smsg);
- //synchronized (t) {
- // t.msgque.addElement(smsg);
- // t.notify();
- //}
- }
- os.println(smsg.msgid+" "+smsg.msg);
- os.flush();
- }
- }
- }
-
- void handleResign(ServerMessage smsg) {
- if (smsg.port != portnum) {
- os.println(smsg.msgid + " " +smsg.msg);
- os.flush();
- }
- else {
- smsg.msgid= ("Resigned");
- if (opponent != null)
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- // }
- RecordResult(smsg.msg);
- }
- }
-
- void handleTokenMove(ServerMessage smsg) {
- if (smsg.port != portnum) {
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- else
- if (token != null)
- token.addMsg(smsg);
- //synchronized (token) {
- // token.msgque.addElement(smsg);
- // token.notify();
- //}
- }
-
- void handleGameAccept(ServerMessage smsg) {
- //PlayerId temp = null;
- if (game != null)
- if (game.RemoveObserver(this))
- ChessServer.gamelist.remove(game.getPlayers());
- isPlaying = true;
- if (smsg.msgid.equals("GameAccept")) {
- if (token != null && token != opponent)
- token.addMsg(portnum,"Token",name.user + ": parted.");
- //synchronized (token) {
- // token.msgque.addElement((new ServerMessage(portnum,"Token",name.user + ": parted.")));
- // token.notify();
- //}
- token = opponent;
- if (opponent.token != null && opponent.token != this)
- opponent.addMsg(portnum,"Token",name.user + ": parted..");
- // synchronized (opponent.token) {
- // opponent.token.msgque.addElement((new ServerMessage(portnum,"Token",name.user + ": parted..")));
- // opponent.token.notify();
- //}
- opponent.token = this;
- String [] parm = PersistString.parse(smsg.msg);
- //int index = smsg.msg.indexOf(' ');
- //String color = smsg.msg.substring(0,index);
- String color = parm[0];
- if (color.equals("white")) {
- //iswhite = true;
- game = new Game(name.user,opponent.name.user);
- }
- else {
- //iswhite = false;
- game = new Game(opponent.name.user,name.user);
- }
- game.moveTime = parm[1];
- ChessServer.gamelist.put(game.getPlayers(),game);
- smsg.msgid= ("GameAccepted");
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- }
- else { //tell the criginator the good news
- game = opponent.game;//both reference the same game
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- }
-
- void handleAccepts(ServerMessage smsg) {
- if (smsg.port != portnum) {
- os.println(smsg.msgid);
- os.flush();
- }
- else {
- if (opponent != null)
- opponent.addMsg(smsg);
- // synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- // }
- RecordResult(smsg.msg);
- }
- }
-
- void handleName(ServerMessage smsg) {
- System.out.println("MS" + socketnum + " Name " + smsg.msg);
- if (isGoodLogon(smsg.msg)) {
- os.println("Welcome");
- os.flush();
- //deliver messages , if any
- int count = name.message.size();
- if (count > 0) {
- for (int i=0;i<count;i++) {
- os.println("Chat " + name.message.elementAt(i));
- os.flush();
- }
- name.message.removeAllElements();
- name.isDirty();
- }
- }
- else {
- os.println("Status Password did not match - try again");
- os.flush();
- }
- }
-
- void handleChallenges(ServerMessage smsg) {
- if (smsg.msgid.equals("Challenge")) {
- String msgData[] = PersistString.parse(smsg.msg);
- if (!isPlaying) { //is someone challenging you?
- isPlaying = true; // so nobody else will challenge them
- //int index = smsg.msg.indexOf(' ');
- //index = smsg.msg.indexOf(' ',index+1);
- //String key = smsg.msg.substring(index+1);
- //PlayerId opp = (PlayerId)PlayerId.usersByUser.get(key);
- opponent = (SendThread)ChessServer.playerlist.get(msgData[2]);
- //key = smsg.msg.substring(0,index+1);
- if (opponent.isPlaying) { //did someone else get them first?
- System.out.println(name + "Challenged " + msgData[2] + ",but they are not available");
- os.println("Status Not Available");
- os.flush();
- }
- else {
- opponent.isPlaying = true;
- os.println("Status Challenged " + opponent.name.user);
- os.flush();
- String[] parameter = new String[6];
- parameter[0] = msgData[0]; //time per game
- parameter[1] = msgData[1]; //added time per move
- parameter[2] = name.user; //challenger
- parameter[3] = String.valueOf(name.rating); //challenger rating
- parameter[4] = opponent.name.user; //challengee
- parameter[5] = String.valueOf(opponent.name.rating); //challengee rating
- smsg.msg = PersistString.concat(parameter);
- smsg.msgid = "Challenged";
- //smsg.msg = key + name.user;
- opponent.opponent = this;
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- //opponent.msgque.addElement(smsg);
- //opponent.opponent = this;
- //opponent.notify();
- //}
- }
- }
- //else do nothing - you are about to be challenged
- }
- else {
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- }
-
- void handleRefuseChallenge(ServerMessage smsg) {
- smsg.msgid = "RefusedChallenge";
- isPlaying = false;
- if (opponent != null)
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- opponent = null;
- }
-
- void handleRefused(ServerMessage smsg) {
- game.setOffer(name.user,0);
- if (smsg.port != portnum) {
- os.println(smsg.msgid);
- os.flush();
- }
- else {
- if (opponent != null)
- opponent.addMsg(smsg);
- //synchronized (opponent) {
- // opponent.msgque.addElement(smsg);
- // opponent.notify();
- //}
- }
- }
-
- void handleRefusedChallenge(ServerMessage smsg) {
- //ChessServer.playerlist.put(name.user,this);
- isPlaying = false;
- opponent = null;
- os.println("Refused " + smsg.msg);
- os.flush();
- System.out.println("refused");
- }
-
- void handleResume(ServerMessage smsg) {
- int index = Integer.parseInt(smsg.msg);
- game = (Game)suspendList.elementAt(index);
- if (game == null)
- os.println("Status Can't find the game?!");
- else {
- game.GameResumed();
-
- if (game.isOver()) {
- game.Kibitzer.addElement(this);
- isPlaying = false;
- }
- else
- isPlaying = true;
- String opp = game.white;
- System.out.println("Resuming " + game);
- if (game.white.equals(name.user))
- opp = game.black;
- SendThread myopp = (SendThread) ChessServer.playerlist.get(opp);
- if (myopp != null) {
- if ( myopp.game == game){
- //if ( myopp.opponent == this) {
- myopp.opponent = this;
- opponent = myopp;
- System.out.println("joining the suspended game");
- //}
- //else
- // System.out.println("Other player is still suspended" + myopp.opponent);
- }
- else
- System.out.println("Other player is looking at game " + myopp.game + " not " + game);
- }
- else
- System.out.println("Other player is not logged on") ;
- os.println("Resume " + game.getMoves());
- os.flush();
- System.out.println("Resume " + game.getMoves());
- String offer;
- if ((offer = game.getOffer(name.user)) != null) {
- os.println(offer);
- os.flush();
- }
- if (token != null && token != opponent)
- token.addMsg(portnum,"Token",name.user + ": parted...");
- //synchronized (token) {
- // token.msgque.addElement((new ServerMessage(portnum,"Token",name.user + ": parted...")));
- // token.notify();
- //}
- token = null;
- if (opponent == null) {
- if (ChessServer.gamelist.get(game.getPlayers())== null)
- ChessServer.gamelist.put(game.getPlayers(),game);
- }
- else {
- token = opponent;
- opponent.opponent = this;
- opponent.token = this;
- os.println("Chat " + opponent.name.user + ": joins");
- os.flush();
- opponent.addMsg(portnum,"Chat",name.user + ": joins");
- //synchronized (opponent) {
- // opponent.msgque.addElement((new ServerMessage(portnum,"Chat",name.user + ": joins")));
- // opponent.notify();
- //}
- }
- }
- }
-
- void handleWatching(ServerMessage smsg) {
- if (game != null)
- if (game.RemoveObserver(this))
- ChessServer.gamelist.remove(game.getPlayers());
- System.out.println("MS process Watching");
- game = (Game)ChessServer.gamelist.get(smsg.msg);
- if (game != null)
- game.AddObserver(this);
- else
- System.out.println("MS game is null for" + name.user);
- }
-
- public void run() {
- System.out.println("MS running" + socketnum);
- if (serverSocket == null)
- return;
- Socket clientSocket = null;
- try {
- clientSocket = serverSocket.accept();
- }
- catch (IOException e) {
- System.out.println("Accept failed: " + 1957 + ", " + e);
- System.exit(1);
- }
- try {
- //System.out.println("set up data streams");
- new ListenThread(clientSocket,this).start();
- os = new PrintWriter(clientSocket.getOutputStream());
- //os = new PrintStream(new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
-
- suspendList = new Vector(10);
- String inputLine;
- ServerMessage smsg;
- String oldname;
- System.out.println("ST Waiting for Stuff to do" + socketnum);
- if (msgque == null)
- System.out.println("ST null msgque?? - crash coming");
- alive: {
- while (true) {
- synchronized (this) {
- if (msgque.isEmpty()) {
- try {
- System.out.println("ST waiting");
- wait();
- }
- catch (InterruptedException e){System.out.println("ST woke up");}
- }
- else
- System.out.println("ST queued up");
- smsg = (ServerMessage) msgque.firstElement();
- System.out.println("ST msg" + String.valueOf(portnum) + String.valueOf(smsg.port) + smsg.msgid + " " + smsg.msg);
- msgque.removeElementAt(0);
- }
- if (smsg.msgid.equals("Bye") || smsg.msgid.equals("dead")) {
- handleBye(smsg);
- if (smsg.msgid.equals("dead"))
- break alive; //if they said bye, they can logon again
- }
- else
- if (smsg.msgid.equals("Move"))
- handleMove(smsg);
- else
- if (smsg.msgid.equals("Moved")) {
- if (smsg.port != portnum) {
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- }
- else
- if (smsg.msgid.startsWith("Info"))
- handleInfo(smsg) ;
- else
- if (smsg.msgid.equals("UpdateInfo"))
- handleUpdateInfo(smsg) ;
- else
- if (smsg.msgid.equals("MoveList")) {
- os.println(smsg.msgid + " " + smsg.msg);
- os.flush();
- }
- else
- if (smsg.msgid.equals("TokenMove"))
- handleTokenMove(smsg) ;
- else
- if (smsg.msgid.equals("Suspend"))
- handleSuspend(smsg) ;
- else
- if (smsg.msgid.equals("LibraryList")){
- library = GameLibrary.getLibrary(null,null);
- os.println("LibraryList " + library.getList());
- os.flush();
- }
- else
- if (smsg.msgid.equals("LogonList")){
- os.println("PlayerList " + getInfoList());
- os.flush();
- }
- else
- if (smsg.msgid.equals("PlayerList")){
-
- //os.println("PlayerList " +
- getPlayerList();
- //os.flush();
- }
- else
- if (smsg.msgid.equals("GetLibraryGame"))
- getLibraryGame(smsg.msg);
- else
- if (smsg.msgid.equals("Token"))
- OpponentParted(smsg);
- else
- if (smsg.msgid.equals("Name"))
- handleName(smsg);
- else
- if (smsg.msgid.equals("List")) {
- os.println(GetList());
- os.flush();
- System.out.println("MS" + socketnum + " ListF:" );
- }
- else
- if (smsg.msgid.startsWith("Offer"))
- handleOffers(smsg);
- else
- if (smsg.msgid.equals("Chat"))
- handleChat(smsg) ;
- else
- if (smsg.msgid.equals("Note"))
- handleNote(smsg) ;
- else
- if (smsg.msgid.startsWith("Resign"))
- handleResign(smsg) ;
- else
- if (smsg.msgid.startsWith("GameAccept"))
- handleGameAccept(smsg);
- else
- if (smsg.msgid.startsWith("Accept"))
- handleAccepts(smsg);
- else
- if (smsg.msgid.startsWith("Challenge"))
- handleChallenges(smsg);
- else
- if (smsg.msgid.equals("RefuseChallenge"))
- handleRefuseChallenge(smsg);
- else
- if (smsg.msgid.equals("Refused"))
- handleRefused(smsg) ;
- else
- if (smsg.msgid.equals("RefusedChallenge"))
- handleRefusedChallenge(smsg) ;
- else
- if (smsg.msgid.equals("Resume"))
- handleResume(smsg) ;
- else
- if (smsg.msgid.equals("Watching"))
- handleWatching(smsg) ;
- else
- if (smsg.msgid.equals("UpdateUser"))
- UpdateUser(smsg.msg);
- else
- System.out.println("MS ignored " + smsg.msgid);
- }
- }
- System.out.println("closing socket " + portnum);
- if (name != null)
- ChessServer.playerlist.remove(name.user) ; //should be gone by now anyway
- os.close();
- clientSocket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- ChessServer.portIdle(portnum);
- if (serverSocket != null) {
- try {
- serverSocket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- serverSocket = null;
- }
- }
- }
-
- void SuspendGame() {
- //Write the game to disk if on move 2 or more (need blacks move to get his time remaining)
- if (game.GameSuspended())
- ChessServer.gamelist.remove(game.getPlayers());
- if (game.movenum > 1) {
- if (game.filename == null) {
- System.out.println("adding to suspend list: " + game);
- suspendList.addElement(game);
- //if (opponent != null)
- // opponent.suspendList.addElement(game);
- }
- PlayerId.SuspendGame(game);
- }
- game = null;
- }
-
- String getInfoList() {
- int playerCount = ChessServer.playerlist.size();
- System.out.println("getInfoList -playercount = " + playerCount);
- String[] dataArray = new String[playerCount * 3] ;
- int i = 0;
- for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
- SendThread np = (SendThread)e.nextElement();
- //PlayerId player = (PlayerId)e.nextElement();
- dataArray[i++] = String.valueOf(np.name.rating);
- dataArray[i++] = np.name.user;
- dataArray[i++] = np.name.name;
- }
- return PersistString.concat(dataArray);
- }
-
- void getPlayerList() {
- int userCount = PlayerId.usersByUser.size();
- System.out.println("getPlayerList playerCount=" + userCount);
-
- Enumeration e = PlayerId.usersByUser.elements();
- while (e.hasMoreElements()) {
- int count = userCount;
- if (userCount > 30)
- count = 30;
- String[] dataArray = new String[count * 3] ;
- int i = 0;
- for ( ; e.hasMoreElements() && i < 30;) {
- PlayerId player = (PlayerId)e.nextElement();
- dataArray[i++] = String.valueOf(player.rating);
- dataArray[i++] = player.user;
- dataArray[i++] = player.name;
- userCount--;
- }
- os.println("PlayerList " + PersistString.concat(dataArray));
- os.flush();
- yield();
- }
- }
-
- String GetList() {
- String outputLine = "List ";
- SendThread np;
- Game g;
- for (Enumeration e = suspendList.elements() ; e.hasMoreElements() ;) {
- g = (Game)e.nextElement();
- outputLine = outputLine + 'G' + g.toString() + '?';
- }
- //System.out.println("MS" + socketnum + " ListA:" );
- for (Enumeration e = ChessServer.playerlist.elements() ; e.hasMoreElements() ;) {
- np = (SendThread)e.nextElement();
- if (np.isPlaying == false)
- outputLine = outputLine + 'P' + np.name.user + " " + np.name.rating + '?';
- }
- //System.out.println("MS" + socketnum + " ListA:" );
- for (Enumeration e = ChessServer.gamelist.elements() ; e.hasMoreElements() ;) {
- g = (Game)e.nextElement();
- //if (np != null ) {
- if (g.white.equals(name.user) || g.black.equals(name.user));
- else
- outputLine = outputLine + 'A' + g.toString() + '?';
- //}
- }
- return outputLine;
- }
-
- void getLibraryGame(String games) {
- if (game != null)
- if (game.RemoveObserver(this))
- ChessServer.gamelist.remove(game.getPlayers());
- int index = 0;
- int endindex = games.lastIndexOf('?');
- GameLibrary temp = library;
- while (index < endindex && temp == library) {
- int nextindex = games.indexOf('?',index);
- if (nextindex == -1)
- nextindex = endindex;
- String gameName = games.substring(index,nextindex);
- temp = GameLibrary.getLibrary(gameName,library);
- System.out.println("getting game " + index + " " + nextindex + " " + gameName);
- if (temp == library)
- os.println("LibraryGame " + gameName + "?" + temp.getGame(gameName));
- else
- os.println("LibraryList " + temp.getList());
- os.flush();
- index = nextindex + 1;
- }
- library = temp;
- }
-
- protected void OpponentParted(ServerMessage msg) {
- os.println(msg.msgid );
- os.flush();
- os.println("Chat " + msg.msg);
- os.flush();
- //String oppName = token.name.user;
- System.out.println("OpponentParted " + msg.msg ) ;
- opponent = null;
- token = null;
- game.Chat(new ServerMessage(msg.port,"Chat",msg.msg));
- }
-
- protected void RecordResult(String result) {
- if (game != null) {
- game.GameOver(result);
- ServerMessage msg = new ServerMessage(portnum,"Chat",name.user + " joins");
- game.Chat(msg);
- game.Kibitzer.addElement(this);
-
- if (opponent != null) {
- ServerMessage smsg = new ServerMessage(portnum,"Chat",opponent.name.user + " joins");
- game.Chat(smsg);
- game.Kibitzer.addElement(opponent) ;
- opponent.isPlaying = false;
- //opponent.iswhite = false;
- opponent.opponent = null;
- if (game.filename != null)
- opponent.suspendList.removeElement(game);
- }
- PlayerId.RecordResult(game,result) ;
- if (game.filename != null)
- suspendList.removeElement(game);
- opponent = null;
- isPlaying = false;
- }
- else
- System.out.println("ST tried to record result");
- }
-
- void UpdateUser(String msg) {
- int index = msg.indexOf('[');
- String password = msg.substring(0,index);
- int index1 = msg.indexOf(index+1,'[');
- String email = msg.substring(index+1,index1);
- index = msg.indexOf(index1+1,'[');
- String name = msg.substring(index1+1,index);
- index1 = msg.indexOf(index+1,'[');
- String location = msg.substring(index+1,index1);
- String extra = msg.substring(index1+1);
- this.name.Update(password,email,name,location,extra);
- }
-
- boolean isGoodLogon(String msg){
- int index = msg.indexOf('[');
- String password = msg.substring(index +1).trim();
- String user = msg.substring(0,index).trim();
- PlayerId p = (PlayerId) PlayerId.usersByUser.get(user);
- if (p == null) {
- System.out.println("Adding " + user + " " + password);
- name = new PlayerId( user,password);
- name.addPlayer();
- }
- else
- if (p.password.equals(password)) {
- name = p;
- for ( Enumeration e = p.partialGame.elements();e.hasMoreElements();){
- String filename = (String) e.nextElement();
- if (!UseLoggedOnOpponentsGame(filename)) {
- System.out.println("adding to " + name.user + " suspended list " + filename);
- Game g = p.getGame(filename);
- if (g != null)
- suspendList.addElement(g);
- }
- }
- }
- else
- return false;
- ChessServer.playerlist.put((Object)user,(Object)this);
- return true;
- }
-
- boolean UseLoggedOnOpponentsGame(String filename) {
- PlayerId opp = (PlayerId) PlayerId.usersById.get(filename.substring(0,4));
- if (opp != null)
- if (opp.user.equals(name.user))
- opp = (PlayerId) PlayerId.usersById.get(filename.substring(4,8));
- if (opp == null) {
- System.out.println("Somebody disappeared from the user list!");
- return false ; //can't fix it ,show game to get a clue
- }
- SendThread pastOpponent = (SendThread) ChessServer.playerlist.get(opp.user);
- if (pastOpponent == null)
- return false;
- Game g;
- for (Enumeration e = pastOpponent.suspendList.elements() ; e.hasMoreElements() ;) {
- g = (Game)e.nextElement();
- if (g.filename.equals(filename)) {
- suspendList.addElement(g);
- System.out.println("used game from " + pastOpponent.name.user + "'s suspended game list for " + name.user);
- return true;
- }
- }
- System.out.println("Found the player,but did not find the game?");
- return false;
- }
- } //end of class
-