Jriver/
Jriver/src/org/jriver/core/
Jriver/src/org/jriver/core/interactive/
Jriver/src/org/jriver/lib/
Jriver/src/org/jriver/lib/daemon/
Jriver/src/org/jriver/lib/events/
Jriver/src/org/jriver/lib/interfaces/
Jriver/src/org/jriver/lib/std/
Jriver/src/org/jriver/telnet/
Jriver/src/org/jriver/telnet/.svn/
Jriver/src/org/jriver/telnet/.svn/prop-base/
Jriver/src/org/jriver/telnet/.svn/text-base/
/*
 * Copyright 2007 Kevin Roe, Daniel McCarney
 * This file is part of Jriver.
 *
 * Jriver is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jriver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jriver.core;

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

import javax.swing.tree.DefaultMutableTreeNode;

import org.jriver.lib.Player;
import org.jriver.telnet.ConnectionThread;


public class MudDriver
{
	private final String CONFIG 		= "config.properties";
	private ThreadGroup 		tg		= new ThreadGroup("connections");
	private ArrayList<Player>   players = new ArrayList<Player>();
	private DefaultMutableTreeNode root;
	private static MudDriver driver;
	public MudObject loginRoom;
	public HeartBeat heartBeat;
	public Properties configuration = new Properties(); //Our driver configuration

	public static void main(String[] args) throws IOException
	{			
		getMudDriver();
	}
	
	public void addObject(DefaultMutableTreeNode object)
	{
		root.add(object);
		System.out.print("Added object: ");
		System.out.println(((MudObject)object.getUserObject()).queryName());
	}
	
    public MudObject getLoginRoom() {
        return loginRoom;
    }

	public static MudDriver getMudDriver() {
		if(driver == null) driver = new MudDriver();
		return driver;
	}

	private MudDriver ()
	{
		Socket socket;
		ServerSocket serverSocket = null;
		boolean listening = true;
		String portString = "";
			
		if(driver == null) driver = this;
		
		//Load our configuration properties file from the classpath and use it to 
		//fill a properties object.
		try
		{
			configuration.load(new FileInputStream(CONFIG));
		} catch(FileNotFoundException e) {
			System.err.println("No configuration file found...");
			System.exit(-1);
		} catch(IOException e) {
			System.err.println("Error loading configuration properties file. "+ e);
			System.exit(-1);
		}
		
		portString = configuration.getProperty("port", "4444");
		
		try
		{
			serverSocket = new ServerSocket(Integer.parseInt(portString));
		} catch(NumberFormatException e) {
			System.err.println("Invalid port specified in configuration.");
			System.exit(-1);
		} catch (IOException e) {
			System.err.println("Could not listen on port: "+ portString);
			System.exit(-1);
		}

		System.out.println("Jriver ["+ 
				configuration.getProperty("mudname", "Unknown MUD") 
						+"] v0.1");
		
		System.out.println("Now accepting connections on port "+ portString);
		
		new Thread(heartBeat = HeartBeat.getHeartBeat()).start();
                root = new DefaultMutableTreeNode(this);
                loginRoom = new MudObject();
		loginRoom.name = "Room1";
		loginRoom.shortDesc = "The world's first room";
		loginRoom.longDesc = 
"This is the central gathering room.  The room is fairly\r\n"+
"empty, except for a large table in the center of the room\r\n"+
"that has blueprints and design drawings all over it.  The\r\n"+
"floor is made from a smooth wood planking.  Two large iron\r\n"+
"braziers hang on the wall which hold lit torches giving the\r\n"+
"room light.\n\r";

		while (listening)
		{
			try
			{
				new Thread(tg, 
						new ConnectionThread(
								socket = serverSocket.accept())).start();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		try
		{
			serverSocket.close();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public void registerPlayer(Player p)
	{
		players.add(p);
	}
	
	public void removePlayer(Player p)
	{
		players.remove(p);
	}
	
	public ArrayList<Player> getUsers()
	{
		//Don't want people manipulating the actual arraylist
		return new ArrayList<Player>(players);
	}
	
	public Object toObject(String hashCode)
	{
		Object toReturn = null;
		Enumeration treeEnumeration = root.breadthFirstEnumeration();
		
		while(treeEnumeration.hasMoreElements())
		{
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeEnumeration.nextElement();
			MudObject obj = (MudObject) node.getUserObject();
			
			if(obj.getID().equals(hashCode))
				toReturn = obj;
		}
			
		return toReturn;
	}
	
	public Player findPlayer(String name)
	{
		Iterator<Player> playerIterator = players.iterator();
		Player found = null;
		
		name = name.toLowerCase();
		
		while(playerIterator.hasNext())
		{
			Player tmp = playerIterator.next();
			if(tmp.queryName() != null 
				&& tmp.queryName().toLowerCase().equals(name))
				found = tmp;
		}
		
		return found;		
	}
}