All pastes #814274 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-13 03:51 UTC
rendered paste body
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class Server extends Thread {
 public GameMap map;
 public Map<InetAddress, Player> players = new HashMap<InetAddress,Player>();
 public Map<Player, Object[]> adds = new HashMap<Player,Object[]>();
 DatagramSocket s;
 InetAddress group;
 MapThread mt;
 boolean maplock = false;
 public HashMap<Integer, Boolean> playerNumsMap = new HashMap<Integer,Boolean>();
 ByteArrayOutputStream byteArr;
 ObjectOutputStream out;
 GZIPOutputStream gzip;
 SpawnQueue spawn;
 ScoreThread scores;
 ClientList clientList;
 public Server(String mapFile) throws SocketException, UnknownHostException, IOException {
  map = new GameMap(mapFile);
  group = InetAddress.getByName("228.5.6.7");
  s =  new DatagramSocket(27055);
  //s.joinGroup(group);
  s.setSoTimeout(10);
  Thread t = new Thread(this);
  t.start();
  spawn = new SpawnQueue(this);
  scores = new ScoreThread(this);
  for(int i = 2; i < 12; i++) {
   playerNumsMap.put(i,false);
  }
  clientList = new ClientList(this);
  byteArr = new ByteArrayOutputStream();
  gzip = new GZIPOutputStream(byteArr);
  out = new ObjectOutputStream(gzip);
  mt = new MapThread(this);
 }
 
 public void run() {
  byte[] buffer = new byte[10240];
  DatagramPacket r = new DatagramPacket(buffer, buffer.length);
  System.out.println("SERVER STARTED");
  while(true) {
   r = new DatagramPacket(buffer, buffer.length);
   //System.out.println(map);
   //try { handleAmmo(); } catch (IOException e) { e.printStackTrace(System.err); }
   try { s.receive(r); } catch (SocketException e) { r = null; }
   catch (IOException e) { /* e.printStackTrace(System.err); */ r = null; }
   try { handleRequests(r); } catch (IOException e) { e.printStackTrace(System.err); }
   catch (ClassNotFoundException e) { e.printStackTrace(System.err); }
   catch (InterruptedException e) {}
  }
 }
 
 public void sendMap() throws IOException {
 Iterator i = adds.keySet().iterator();
 Object tmp;
 while(i.hasNext()) {
  tmp = i.next();
  maplock = true;
    sendData((InetAddress)adds.get(tmp)[0], (Integer)adds.get(tmp)[1], (Object)map);
  maplock = false;
 }
 }

 public void sendtoAll(Object o) throws IOException {
 	Iterator i = adds.keySet().iterator();
	Object tmp;
	while(i.hasNext()) {
		tmp = i.next();
		sendData((InetAddress)adds.get(tmp)[0],(Integer)adds.get(tmp)[1],o);
	}
 }

 public void sendLocation(InetAddress i, int port, int[] location) throws IOException {
  sendData(i,port,(Object)location);
 }

 public void sendDamage(InetAddress i, int port, Ammunition a) throws IOException { System.out.println(a.damage);
  sendData(i,port,(Object)a);
 }

 public void sendData(InetAddress i, int port, Object o) throws IOException {
  byteArr = new ByteArrayOutputStream();
  gzip = new GZIPOutputStream(byteArr);
  out = new ObjectOutputStream(gzip); 
  out.writeObject(o);  
  gzip.finish();
  DatagramPacket reply = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, i, port);
  s.send(reply);
  out.flush();
 }

 public void handleRequests(DatagramPacket r) throws IOException, ClassNotFoundException, InterruptedException {
  if(r == null) return;
  ObjectInputStream input;
  input = new ObjectInputStream(new ByteArrayInputStream(r.getData()));
  Object o;
  o = input.readObject();
  //System.out.println(o.getClass());
  if(o instanceof String) {
   if(o.equals("GETMAPNUM")) {
 Iterator i = playerNumsMap.keySet().iterator();
 for(Object tmp = i.next(); i.hasNext(); tmp = i.next()) {
  if(!playerNumsMap.get(tmp)) {
       System.out.println("playerno: " + tmp);
   sendData(r.getAddress(), r.getPort(), tmp);
   playerNumsMap.put((Integer)tmp,new Boolean(true));
   return;
     }
 }
  }
  if(o.equals("HELO")) {
   clientList.clients.put(r.getAddress(),new Object[]{ players.get(r.getAddress()), System.currentTimeMillis() });
    System.out.println(r.getAddress().toString() + o.toString());
 }
  }
  if(o.getClass().isArray()) {  //System.out.println("gotarray");
        int[] xy = (int[]) o;
        int x = xy[0]; int y = xy[1];
        if (map.isValid(x,y)&&map.isEmpty(x,y))
        {
             Player p = players.get(r.getAddress()); System.out.println("movefrom "+r.getAddress());
      while(maplock) { Thread.sleep(1); }
             map.setCell(p.getx(),p.gety(),null); map.setCell(x,y,p);
             p.setLocation(x,y); 
      sendLocation( r.getAddress(), r.getPort(), new int[]{x,y});
        }
   }
  if( o instanceof Player) {
       if(!scores.scoreBoard.containsKey((Player) o)) scores.scoreBoard.put((Player) o,0);
       players.put(r.getAddress(), (Player) o); System.out.println("added "+r.getAddress());
       Object[] duplet = { r.getAddress(), r.getPort() };
       adds.put((Player) o,duplet);
       Object[] triplet = { o, r.getAddress(), r.getPort() };
       spawn.spawnQueue.add(triplet); 
  }
  if (o instanceof Ammunition) {
   while(maplock) { Thread.sleep(1); }
   map.addAmmo((Ammunition) o);
  }
 }

 public void handleAmmo() throws IOException, InterruptedException {
  Iterator<Ammunition> i = map.getAmmo().iterator();
  Ammunition a;
  while(i.hasNext()) {
   a = i.next();
   a.step();
   if(a.dead || !map.isValid(a.getTile()[0],a.getTile()[1]) || map.getCell(a.getTile()[0],a.getTile()[1]) instanceof Wall) {
       while(maplock) { Thread.sleep(1); }
    i.remove();
    continue;
   }
   for(Player p : adds.keySet()) {
    if(p.getx()==a.getTile()[0] && p.gety()==a.getTile()[1] && !(a instanceof Bullet 
    && p.equals(a.getOwner()))) { System.out.println(a.damage);
     if(p.healthDrain(a)) { System.out.println("killed");
          while(maplock) { Thread.sleep(1); }
      map.setCell(p.oldx,p.oldy,null);
      scores.scoreBoard.put(a.getOwner(),scores.scoreBoard.get(a.getOwner())+1);
      scores.scoreBoard.put(p,scores.scoreBoard.get(p)-1);
     }
     else sendDamage((InetAddress)adds.get(p)[0], (Integer)adds.get(p)[1], a);
        while(maplock) { Thread.sleep(1); }
     i.remove();
     System.out.println("hit by "+a.getOwner());
     break;
    }
  }
  }
 }

 public static void main(String args[]) throws IOException, SocketException {
  if(args.length > 0) { Server serv = new Server(args[0]); }
  else {
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter the map you want to play: ");
   String map = in.readLine();
   Server serv = new Server(map);
  }
 }
}