package edu.miamioh.cse283.htw;

import java.net.*;
import java.util.*;

public class CaveServer {

	/** Port base for this cave server. */
	protected int portBase;

	/** Socket for accepting connections from players. */
	protected ServerSocket clientSocket;

	/** Proxy to the CaveSystemServer. */
	protected CaveSystemServerProxy caveSystem;

	/** Rooms in this CaveServer. */
	protected ArrayList<Room> rooms;

	/** Constructor. */
	public CaveServer(CaveSystemServerProxy caveSystem, int portBase) {
		this.caveSystem = caveSystem;
		this.portBase = portBase;
		rooms = new ArrayList<Room>();
		for (int i = 0; i < 20; ++i) {
			rooms.add(new Room());
		}
	}

	/** Returns the port number to use for accepting client connections. */
	public int getClientPort() {
		return portBase;
	}

	/** This is the thread that handles a single client connection. */
	public class ClientThread implements Runnable {
		/**
		 * This is our "client" (actually, a proxy to the network-connected
		 * client).
		 */
		protected ClientProxy client;

		/** Constructor. */
		public ClientThread(ClientProxy client) {
			this.client = client;
		}

		/**
		 * Play the game with this client.
		 */
		public void run() {
			try {
				client.message("Welcome!");

				// put the player in a room (any room is fine)

				// now, in a loop while the player is alive:
				// -- send the player their "senses":
				client.senses("You are in an empty room.");
				// -- and retrieve their action:
				String action = client.getAction();
				// -- and perform the action

			} catch (Exception ex) {
				// If an exception is thrown, we can't fix it here -- Crash.
				ex.printStackTrace();
				System.exit(1);
			}
		}
	}

	/** Runs the CaveSystemServer. */
	public void run() {
		try {
			clientSocket = new ServerSocket(getClientPort());
			caveSystem.register(clientSocket);

			while (true) {
				// and now loop forever, accepting client connections:
				while (true) {
					ClientProxy client = new ClientProxy(clientSocket.accept());
					(new Thread(new ClientThread(client))).start();
				}
			}
		} catch (Exception ex) {
			// If an exception is thrown, we can't fix it here -- Crash.
			ex.printStackTrace();
			System.exit(1);
		}
	}

	/** Main method (run the CaveServer). */
	public static void main(String[] args) throws Exception {
		// first, we need our proxy object to the CaveSystemServer:
		CaveSystemServerProxy caveSystem = new CaveSystemServerProxy(
				new Socket(InetAddress.getByName(args[0]),
						Integer.parseInt(args[1]) + 1));

		// now construct this cave server, and run it:
		CaveServer cs = new CaveServer(caveSystem, Integer.parseInt(args[2]));
		cs.run();
	}
}

@@ -66,16 +66,236 @@
 til.*;%0A%0A
+/**%0A * The CaveServer class takes the following command-line parameters:%0A * %0A * %3CHostname of CaveSystemServer%3E %3Cport number of CaveSystemServer%3E %3Cport number%0A * of this CaveServer%3E%0A * %0A * E.g., %22localhost 1234 2000%22%0A */%0A
 public c
@@ -2538,16 +2538,288 @@
 ption %7B%0A
+%09%09InetAddress addr = InetAddress.getByName(%22localhost%22);%0A%09%09int cssPortBase = 1234;%0A%09%09int cavePortBase = 2000;%0A%0A%09%09if (args.length %3E 0) %7B%0A%09%09%09addr = InetAddress.getByName(args%5B0%5D);%0A%09%09%09cssPortBase = Integer.parseInt(args%5B1%5D);%0A%09%09%09cavePortBase = Integer.parseInt(args%5B2%5D);%0A%09%09%7D%0A%0A
 %09%09// fir
@@ -2955,71 +2955,25 @@
 ket(
-InetAddress.getByName(args%5B0%5D),%0A%09%09%09%09%09%09Integer.parseInt(args%5B1%5D)
+addr, cssPortBase
  + 1
@@ -3075,33 +3075,20 @@
 em, 
-Integer.parseInt(args%5B2%5D)
+cavePortBase
 );%0A%09

add comments and default arguments to CaveServer