Bugfix: Wandering NPCs no longer ghost through the player.
[Pokemon.git] / src / com / dryerzinia / pokemon / PokemonServer.java
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
package com.dryerzinia.pokemon;
/*
 
By: ???
 
TODO:
Write CMD line args parser
Add port for remote control window via network
Add a CMD line option to open a control window
In control window enable observer mode
 
 */
 
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
 
import com.dryerzinia.pokemon.map.Direction;
import com.dryerzinia.pokemon.map.Level;
import com.dryerzinia.pokemon.map.Pose;
import com.dryerzinia.pokemon.net.ByteInputStream;
import com.dryerzinia.pokemon.net.DatagramSocketStreamer;
import com.dryerzinia.pokemon.net.Streamer;
import com.dryerzinia.pokemon.net.TCPStreamer;
import com.dryerzinia.pokemon.net.msg.client.CMLoad;
import com.dryerzinia.pokemon.net.msg.client.ClientMessage;
import com.dryerzinia.pokemon.net.msg.client.MessageClientMessage;
import com.dryerzinia.pokemon.net.msg.client.PlayerInfo;
import com.dryerzinia.pokemon.net.msg.client.PlayerMovement;
import com.dryerzinia.pokemon.net.msg.client.act.SendActMovedClientMessage;
import com.dryerzinia.pokemon.net.msg.client.fight.SendFightClientMessage;
import com.dryerzinia.pokemon.net.msg.server.ServerMessage;
import com.dryerzinia.pokemon.obj.GameState;
import com.dryerzinia.pokemon.obj.Player;
import com.dryerzinia.pokemon.obj.tiles.Person;
import com.dryerzinia.pokemon.ui.Fight;
import com.dryerzinia.pokemon.util.Database;
import com.dryerzinia.pokemon.util.ResourceLoader;
 
public class PokemonServer {
 
        /*
         * Number for Manhattan distances
         * 14 Max distance at which fights can be initiated
         * 50 Max distance at which people can hear each other in chat without whispering
         * 16 Distance at which other players and actors movements are no longer being told
         * 14 Distance at which other players and actors movements start being told
         * 2 width of the transition zone where clients are told either that a player is
         * coming into view or a player is going out of view
         * [16-14) going out of view
         * [14-12) Coming into view
         * 
         * TODO move values to configuration file
         */
        public static final int FIGHT_DISTANCE = 14;
        public static final int TALK_DISTANCE = 50;
        public static final int FOG_OF_WAR = 16;
        public static final int VISIBLE_DISTANCE = 14;
        public static final int TRANSITION_ZONE = 2;
 
        // TODO possibly multiple ports with per port mode settings in file
    public static final int PORT_NUM = 53879;
 
    public static final int CM_TCP = 0;
    public static final int CM_DATAGRAM = 1;
    public static final int CM_ALL = 2;
 
    private static int connectMode = CM_ALL;
 
    public static PokemonServer pokes;
 
    ServerSocket ssock;
 
    public static ConcurrentHashMap<Integer, PlayerInstanceData> players = new ConcurrentHashMap<Integer, PlayerInstanceData>();
 
    int playeridcount = 0;
 
    ArrayList<ObjectOutputStream> oos2;
 
    Timer server_tasks;
 
    /**
     * Entry point for Pokemon game server
     * @param args
     */
    public static void main(String args[]) {
        new PokemonServer();
    }
    
    public PokemonServer() {
        
        oos2 = new ArrayList<ObjectOutputStream>();
 
        // We have to pretend to have a head for the Pokemon
        // Game instance to be created
        System.setProperty("java.awt.headless", "false");
 
        // Probably do some other thing for this singleton
        pokes = this;
 
        // Don't need to load images for headless server
        ResourceLoader.setDoLoad(false);
 
        // Game Instance Data
        GameState.init();
 
        // Load Actors
        GameState.loadActors("Actors.json");
 
        System.out.println("Game World Instace Created");
 
        
        server_tasks = new Timer();
 
        server_tasks.schedule(new ActTask(), 0, 250);
        server_tasks.scheduleAtFixedRate(new SaveAllTask(), 0, 120000);
        server_tasks.scheduleAtFixedRate(new KickInactiveTask(), 0, 30000);
 
        listen();
 
    }
 
    /**
     * Saves all of the information of connected players every 2 minutes
     * in case server crashes
     */
    public class SaveAllTask extends TimerTask {
        public void run() {
                for (PlayerInstanceData pid : players.values())
                        Database.savePlayerData(pid.getPlayer());
        }
    }
    
    /**
     * Kicks any players who we have not received a message from in the last
     * 45 seconds.  Player should send pings every 15 seconds to insure they
     * stay connected.  This helps remove UDP Clients who have disconnected
     * without saying anything.  This takes runs once every 30 seconds so the
     * longest a player can be disconnected and not removed is 1:15
     */
    public class KickInactiveTask extends TimerTask {
        public void run() {
                kickInactive();
        }
    }
 
    /**
     * Removes players who have not sent a message in the last 45 seconds
     */
    public synchronized void kickInactive() {
 
        for(Entry<Integer, PlayerInstanceData> pidEntry : players.entrySet()) {
 
                PlayerInstanceData pid = pidEntry.getValue();
 
                /* Players who have not send a message in the last 45 seconds
                 * Are removed from the game, They should ping at least once
                 * every 30 seconds to show they are Connected particularly
                 * for UDP connections this is important.
                 */
            if (!pid.hasMessageLast45()) {
 
                System.out.println("Player " + pid.getPlayer().getName()
                        + " not responsive");
                
                Database.savePlayerData(pid.getPlayer());
 
                // Remove PlayerInstanceData from server
                players.remove(pidEntry.getKey());
 
                // TODO Proper disconnect and killing of all threads
                System.out.println("Player kicked!");
 
            }
        }
    }
 
    /**
     * Runs AI for all Actors in the game world and updates players near them
     * of their actions.  AI runs ~4 times per second.
     */
    public class ActTask extends TimerTask {
        public void run() {
 
                for(Person person: GameState.people.values())
                        sendPlayerActorUpdate(person, person.act());
 
        }
    }
 
    /**
     * Synchronized because PlayerInstanceData iteration
     * @param person Actor that changed and needs to be updated
     * TODO all actors are persons???
     */
    public synchronized static void sendPlayerActorUpdate(Person person, boolean changed){
 
        // TODO PER level search
                for(PlayerInstanceData pid : players.values()) {
            if(VISIBLE_DISTANCE > GameState.getMap().manhattanDistance(pid.getPlayer().getPose(), person.getPose()))
                                try {
 
                                        if(changed){
                                                pid.sendActor(person, Person.A_MOVED);
                                        }
 
                                } catch (IOException ioe) {
                                        System.err.println("Could not send actor update to player: " + ioe.getMessage());
                                        // TODO likely cause client is disconnected we should probably remove them here
                                }
        }
 
    }
 
    /**
     * Return true if the given tile is occupied by any player.
     * @param tileX
     * @param tileY
     * @param levelId
     */
    public synchronized static boolean isPlayerAtTile(
                int tileX, int tileY, int levelId) {
        
        Level level = GameState.getMap().getLevel(levelId);
        Iterator<Player> players = level.nearbyPlayerIterator();
        
        while (players.hasNext()) {
                Player player = players.next();
                Pose playerPose = player.getPose();
                
                int playerX = (int) playerPose.getX();
                int playerY = (int) playerPose.getY();
                int playerLvl = playerPose.getLevel();
                
                if (playerX == tileX && playerY == tileY && playerLvl == levelId) {
                        return true;
                }
        }
        
        return false;
    }
 
    /**
     * Binds TCP to the server socket and starts accepting
     * connections from clients
     */
    public void startTCPListen() {
 
        try {
 
            ssock = new ServerSocket();
            ssock.bind(new InetSocketAddress(PORT_NUM));
 
            (new TCPListener()).start();
 
        } catch (BindException be) {
 
                System.err.println("Unable to bind to port: " + be.getMessage());
                System.err.println("Not listening on TCP!");
 
        } catch (IOException ioe){
 
                ioe.printStackTrace();
        }
 
    }
 
    /**
     * Binds UDP to the Server Socket and starts listening for 
     * packets from clients
     */
    public void startDatagramListen() {
 
        try {
 
            (new DatagramListener(new DatagramSocket(PORT_NUM))).start();
 
        } catch (SocketException se) {
 
                System.err.println("Could not attach to UDP Socket: " + se.getMessage());
                System.err.println("Not listening on UDP!");
 
        }
 
    }
 
 
    /**
     * Thread for communicating with Client
     */
    public class MessageListener extends Thread {
 
        InputStream is;
        OutputStream os;
 
        Streamer streamer;
 
        PlayerInstanceData pid;
 
        boolean kill = false;
 
        public MessageListener(Streamer streamer, PlayerInstanceData pid) {
 
                this.streamer = streamer;
 
            try {
 
                this.is = streamer.getInputStream();
                this.os = streamer.getOutputStream();
 
            } catch (IOException ioe) {
 
                System.err.println("Could not open Stream to/from client.");
 
            }
 
            this.pid = pid;
 
        }
 
        /**
         * Closes the socket and ends this thread
         */
        public void stop_listening(){
 
                try {
 
                        streamer.close();
 
                } catch(IOException ioe){
 
                        System.err.println("Unable to close listening thread: " + ioe.getMessage());
 
                }
 
        }
        
        /**
         * Run loop for listening to messages from client and processing them
         */
        public void run() {
 
            try (ObjectInputStream ois = new ObjectInputStream(is)) {
 
                while(true){
 
                        ServerMessage receivedMessage = (ServerMessage) ois.readObject();
                    pid.recivedMessage();
                    receivedMessage.proccess(ois, pid);
 
                }
 
            } catch(SocketException se) {
 
                // Safe disconnect should be an socket close interupt
 
            } catch (EOFException eofe) {
 
                System.out.println("EOF Disconnect, thats not very nice!");
 
            } catch (IOException ioe) {
 
                System.err.println("IOException occured!");
                System.err.println("Client " + pid.getPlayer().getName() + "disconnected!");
 
                ioe.printStackTrace();
 
            } catch (ClassNotFoundException cnfe) { 
 
                System.err.println("Received unknown message from Client:");
 
                cnfe.printStackTrace();
 
            } finally {
 
                System.out.println("Client " + pid.getPlayer().getName() + " disconnected");
                
                // Remove player from list of connected players
                if (pid.isLoggedIn())
                        // Save player
                    remove(pid);
                else
                        // Don't save anything for players who never logged in successfully
                    removeNoSave(pid);
            }
        }
    }
    
    /**
     * This thread waits for a socket connections on TCP
     * then splits off a new socket acceptor and starts handling
     * messages from the client
     */
    public class TCPListener extends Thread {
 
        Streamer streamer;
 
        public TCPListener() {
        }
 
        public void run() {
                
                try {
 
                Socket sock = ssock.accept();
 
                Thread tcp_client_listener = new TCPListener();
                tcp_client_listener.start();
 
                streamer = new TCPStreamer(sock);
 
                try {
 
                    /* Create's new player instance data and sets a player object
                         * for it and the output stream to send messages on
                         */
                    PlayerInstanceData pid = new PlayerInstanceData();
                    pid.setPlayer(getNextPlayer());
                    pid.setOutputStream(new ObjectOutputStream(streamer.getOutputStream()));
 
                    /* Starts listening for messages from the client
                     */
                    MessageListener message_listener = new MessageListener(streamer, pid);
                    message_listener.start();
 
                    /* sets a reference to the message listener so it can be
                     * killed when we need to remove this player
                     */
                    pid.setListener(message_listener);
                    
                } catch (IOException ioe) {
                    /* We likely lost connection to player during accept
                     * player is added during login messages so no need to
                     * clean player up
                     */
                    System.err.println("Failed to open streams to new client: " + ioe.getMessage());
                }
                
            } catch(IOException ioe){
 
                System.err.println("Error occured while waiting for connection.");
                System.err.println("We are no longer listening on TCP.");
 
            } catch(SecurityException se){
 
                System.err.println("Not allowed to accept on this socket.");
                System.err.println("We are no longer listening on TCP.");
 
            }
 
        }
 
    }
 
    /**
     * Thread listens for datagram packets and routes them to the
     * ObjectInputStream of the appropriate client
     * TODO clients can easily hijack other datagram clients connections
     * crashing them.  Add some kind of authentication
     */
    public class DatagramListener extends Thread {
 
        /* Socket to listen for UPD packets on
         * TODO why public?
         */
        public DatagramSocket ds;
 
        /* HashMap of connected clients for routing incoming packets
         */
        private HashMap<Integer, DatagramSocketStreamer> dss = new HashMap<Integer, DatagramSocketStreamer>();
 
        public DatagramListener(DatagramSocket ds) {
 
                this.ds = ds;
 
        }
 
        public void run() {
 
                DatagramPacket dp = new DatagramPacket(new byte[10000], 10000);
 
                while(true) {
 
                        try {
                        
                                ds.receive(dp);
 
                                try (ByteInputStream bis = new ByteInputStream(dp.getData())) {
        
                                        int id = bis.readInt();
                            int len = bis.readInt();
        
                            byte[] data = new byte[len];
        
                            for (int i = 0; i < len; i++)
                                data[i] = (byte) bis.read();
        
                            DatagramSocketStreamer dssi = dss.get(new Integer(id));
        
                            if (dssi == null) {
        
                                InetAddress from = dp.getAddress();
        
                                int fport = dp.getPort();
                                Player p = getNextPlayer();
                                int id2 = p.getID();
                                PlayerInstanceData pid = new PlayerInstanceData();
                                pid.setPlayer(p);
        
                                dssi = new DatagramSocketStreamer(ds,
                                        new InetSocketAddress(from, fport), id2);
        
                                ObjectOutputStream oos = new ObjectOutputStream(dssi.getOutputStream());
        
                                pid.setOutputStream(oos);
                                pid.setSockID(id2);
        
                                sendID(dssi.getID(), oos);
        
                                MessageListener message_listener = new MessageListener(dssi, pid);
                                message_listener.start();
                                pid.setListener(message_listener);
        
                                dss.put(new Integer(id2), dssi);
        
                            } else {
                             
                                dssi.addToByteArray(data);
                            
                                        }
                                
                                } catch(IOException ioe){
                                        
                                        System.err.println("Error while reading Packet: " + ioe.getMessage());
        
                                }
 
                        } catch(IOException ioe){
 
                                System.err.println("Failed to read datagram packet: " + ioe.getMessage());
                                // TODO we may have lost the socket here and need to do some resetting
 
                        }
                }
        }
        
        /**
         * sends the client its Assigned ID when it first connects
         * @param id ID of new client
         * @param oos Output stream to send ID over
         * @throws IOException
         */
        public void sendID(int id, ObjectOutputStream oos) throws IOException {
            oos.writeInt(id);
            oos.flush();
        }
 
    }
 
    /**
     * starts listening for connections to server
     */
    public void listen() {
 
        switch (connectMode) {
        case CM_TCP:
            startTCPListen();
            break;
        case CM_DATAGRAM:
            startDatagramListen();
            break;
        case CM_ALL:
            startTCPListen();
            startDatagramListen();
            break;
        }
 
    }
 
    /**
     * Creates new player with instance ID auto incremented
     * but dosen't add to master list
     */
    public synchronized Player getNextPlayer() {
        playeridcount++;
        return new Player(playeridcount, new Pose(5, 5, 9, Direction.UP), "Player");
    }
    
    /**
     * Adds player instance data to master list for client management
     * @param pid PlayerInstanceData of new player to add to master list
     */
    public synchronized void addPlayer(PlayerInstanceData pid) {
        players.put(pid.getPlayer().getID(), pid);
    }
 
    /**
         * Calculates the Manhattan Distance between 2 players
         * 
     * @param p1 first player
     * @param p2 second player
     * @return Manhattan Distance between players
     */
    public static int distance(Player p1, Player p2) {
 
        return GameState.getMap().manhattanDistance(p1.getPose(), p2.getPose());
 
    }
 
    /**
     * Thread safe method called from MesageServerMessage to send 
     * any messages to the appropriate players based on there relative
     * locations and the message type 
     * @param s the message
     * @param p2id the player who sent the message
     */
    public synchronized void sendMessage(String s, PlayerInstanceData p2id) {
 
        Player p2 = p2id.getPlayer();
 
        if(s.charAt(0) == '/') {
 
                switch (s.charAt(1)) {
            case 'w':
                try {
                    int sp = s.indexOf(' ', 3);
                    String name = s.substring(3, sp);
                    String message = s.substring(sp, s.length());
                    for(PlayerInstanceData pid : players.values()) {
                        Player p = pid.getPlayer();
                        if (p.name.equals(name)) {
                            pid.sendMessage(p2.name + " whispers: " + message);
                        }
                    }
                } catch (Exception x) {
                }
                break;
 
            case 'f':
                try {
 
                    String name = s.substring(3, s.length());
                    if (name.equals(p2.getName()))
                        break;
 
                    for(PlayerInstanceData pid : players.values()) {
 
                        Player p = pid.getPlayer();
                        if (p.name.equals(name) && FIGHT_DISTANCE > distance(p, p2)){
 
                            Fight f;
 
                            // Confirming/Starting Fight
                            if ((f = pid.getFight()) != null) {
                                pid.isChallenger = true;
                                p2id.isChallenger = false;
 
                                // Set up server part of the fight
                                f.currentPlayer = p2id.getPlayer();
                                f.enemyPlayer = pid.getPlayer();
                                f.setOutPokemon(p2id.getPlayer().getFirstOut());
                                f.setEnemyPokemon(pid.getPlayer().getFirstOut());
 
                                f.activePokemonE = f.enemyPlayer.poke
                                        .getFirstHealthy();
                                f.activePokemonC = f.currentPlayer.poke
                                        .getFirstHealthy();
 
                                p2id.getPlayer()
                                        .getFirstOut()
                                        .getBase();
                                pid.getPlayer()
                                        .getFirstOut()
                                        .getBase();
 
                                pid.sendFightStart(p2id.getPlayer());
                                p2id.sendFightStart(pid.getPlayer());
 
                                // Asking to fight
                            } else {
 
                                f = new Fight();
                                p2id.setFight(f);
                                pid.setFight(f);
 
                                pid.sendMessage(p2.name + " wants to fight...");
 
                            }
                        }
                    }
                } catch (Exception x) {
                    x.printStackTrace();
                    System.err.println("Failed to get player to Fight...");
                }
                break;
            }
 
        } else {
 
            for(PlayerInstanceData pid : players.values()) {
 
                Player p = pid.getPlayer();
                if(p.id != p2.id && TALK_DISTANCE > distance(p, p2)){
                    try {
                        pid.sendMessage(p2.name + ": " + s);
                    } catch (IOException x) {
                        System.out.println("Failed to send Message");
                    }
                }
            }
 
        }
    }
 
    /**
     * Checks to see if a player is already logged in via
     * username comparison
     * @param p
     * @return
     */
    public synchronized boolean isLoggedIn(Player logged_in) {
        for(PlayerInstanceData pid : players.values()) {
                if(pid.getPlayer().equals(logged_in))
                return true;
        }
        return false;
    }
 
    /**
     * Removes kicked or disconnected player from master list
     * @param toRemove PlayerInstance Data of player to remove from master list
     */
    public synchronized void remove(PlayerInstanceData toRemove) {
 
        Player playerToRemove = toRemove.getPlayer();
 
        /* If the player is not logged in their level will be -1
         * so we check for this, we only want to save changes if
         * they where logged in, also we only need to update nearby
         * people if they where logged in
         */
        boolean loggedIn = false;
        if(playerToRemove.getPose().getLevel() != -1)
                loggedIn = true;
 
        /*
         * Remove the player from the level list he is in
         */
        GameState.getMap().getLevel(playerToRemove.getPose().getLevel()).removePlayer(playerToRemove);
 
        /*
         * Save player if they where logged in
         */
        if(loggedIn)
                Database.savePlayerData(playerToRemove);
 
        /*
         * Tell any near by players this one is gone and remove this players
         * Instance data from the list
         * TODO don't iterate WHOLE LIST LOCALS ONLY!
         * TODO every once in a while the client should remove nowhere land players
         */
        for(Entry<Integer, PlayerInstanceData> pidEntry : players.entrySet()){
 
                PlayerInstanceData pid = pidEntry.getValue();
 
                Player player = pid.getPlayer();
 
                /* If the ID's are equal we found the player we are
                 * removing from the master list
                 */
                if(pid == toRemove)
                players.remove(pidEntry.getKey());
 
                /*
                 * If this player was near the player to remove tell them he is in
                 * nowhere land
                 */
                else if(VISIBLE_DISTANCE < distance(playerToRemove, player))
                        pid.writeClientMessage(new PlayerMovement(player.getID(), Pose.NOWHERE_LAND));
 
        }
    }
    
    /**
     * Removes a player from the master list without saving there status
     * @param pid player to remove
     */
    public synchronized void removeNoSave(PlayerInstanceData pid) {
 
        for(Entry<Integer, PlayerInstanceData> pidEntry : players.entrySet()) {
        
                PlayerInstanceData pid2 = pidEntry.getValue();
 
                // Removes the PlayerInstanceData for the player
            if (pid == pid2) {
                players.remove(pidEntry.getKey());
                break;
            }
 
        }
    }
    
    /**
     * Contains everything the server needs to keep track of players and
     * send messages to them
     */
    public static class PlayerInstanceData {
 
        public Player p;
        private int sockID = -1;
        private long lastRecivedMessageTime = 0;
        private MessageListener message_listener;
        private ObjectOutputStream oos;
        private boolean loggedIn = false;
        private Fight f = null;
        private boolean isChallenger = false;
 
        public PlayerInstanceData() {
        }
 
        public PlayerInstanceData(Player player, long last_message_recived_time, MessageListener message_listener) {
            this.p = player;
            this.lastRecivedMessageTime = last_message_recived_time;
            this.message_listener = message_listener;
        }
 
        boolean hasMessageLast45() {
            if (System.currentTimeMillis() - 45000 > lastRecivedMessageTime)
                return false;
            return true;
        }
 
        public void stop() {
            message_listener.stop_listening();
        }
 
        public Player getPlayer() {
            return p;
        }
 
        public void setPlayer(Player p) {
            this.p = p;
        }
 
        public boolean isLoggedIn() {
            return loggedIn;
        }
 
        public boolean isChallenger() {
            return isChallenger;
        }
 
        public void setSockID(int id) {
            sockID = id;
        }
 
        public int getSockID() {
            return sockID;
        }
 
        public void setFight(Fight f) {
            this.f = f;
        }
 
        public Fight getFight() {
            return f;
        }
 
        public void setListener(MessageListener message_listener) {
            this.message_listener = message_listener;
        }
 
        public void setLoggedIn(boolean li) {
            loggedIn = li;
        }
 
        public void recivedMessage() {
            lastRecivedMessageTime = System.currentTimeMillis();
        }
 
        public void setOutputStream(ObjectOutputStream oos) {
            this.oos = oos;
        }
 
        public void setIsChallenger(boolean chal) {
            isChallenger = chal;
        }
 
        public synchronized void writeClientMessage(ClientMessage cm) {
            try {
                oos.writeObject(cm);
                oos.flush();
                oos.reset();
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
 
        public synchronized void sendActor(Person p, int activity)
                throws IOException {
                // TODO redo player talking to
            //if (activity == Person.A_TALKING_TO)
            //    oos.writeObject(new SendActTalkingToClientMessage(p.id, (int)p.x,
            //                  (int)p.y, p.dir, p.level, p.onClick.getActive()));
            //else
 
            oos.writeObject(new SendActMovedClientMessage(p.id, p.getPose()));
            oos.flush();
            oos.reset();
        }
 
        public synchronized void sendPlayerUpdate(Player p, boolean self)
                throws IOException {
            oos.writeObject(new PlayerInfo(p, self));
            oos.flush();
            oos.reset();
        }
 
        public synchronized void sendMessage(String message) throws IOException {
            oos.writeObject(new MessageClientMessage(message));
            oos.flush();
            oos.reset();
        }
 
        public synchronized void sendCommand(int command) throws IOException {
            oos.writeInt(command);
            oos.flush();
        }
 
        public synchronized void sendLoad() throws IOException {
            int ids[] = new int[6];
            for (int i = 0; i < 6; i++) {
                if (p.poke.belt[i] != null)
                    ids[i] = p.poke.belt[i].idNo;
                else
                    ids[i] = -1;
            }
 
            oos.writeObject(new CMLoad(ids, p.poke.box, p.items));
            oos.flush();
            oos.reset();
 
        }
 
        public synchronized void sendFightStart(Player enemyPlayer)
                throws IOException {
 
            // Create a new fight
            Fight f = new Fight();
 
            // Set the other guy to the enemy player
            f.enemyPlayer = enemyPlayer;
 
            // set the number of pokemon and the enemy
            f.pokemonCountE = 0;
            f.activePokemonE = -1;
            // Count pokemon
            for (int i = 0; i < 6; i++) {
                if (enemyPlayer.poke.belt[i] == null)
                    break;
                if (f.activePokemonE == -1
                        && enemyPlayer.poke.belt[i].currentHP > 0) {
                    f.activePokemonE = i; // Set first slot
                    f.enemy = enemyPlayer.poke.belt[i]; // Set Enemy Pokemon
                }
                f.pokemonCountE++;
            }
 
            // Set self to Well self
            f.currentPlayer = p;
 
            // Send the fight to the opponent
            oos.writeObject(new SendFightClientMessage(f));
 
            oos.flush();
            oos.reset();
        }
 
    }
}