/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program 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 2 of the License, or (at
your option) any later version.

This program 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.server.core;

import java.util.Iterator;

/**
 *  This class represents an event handler for a unit. All event handlers should
 *  be subclasses of this class. An event handler should take care of the
 *  occasions when an event is sent to a unit, for example, when the unit stands
 *  on an automaton that affects unit in some way.
 *
 * @author     Henrik Bäärnhielm, Karl Hasselström, Henrik Hägeström.
 * @created    April 30, 2001
 */
public abstract class EventHandler {

    /**
     *  Constructor for the EventHandler object
     */
    public EventHandler() {
    }


    /**
     *  The method that is called by the game engine when a unit of a unit type
     *  with this event handler receives a message. Most of the code of the
     *  event handler will be in this method.
     *
     * @param  event  The event.
     * @param  unit   The unit affected by the event.
     */
    public abstract void handleEvent(UnitEvent event, Unit unit);


    /**
     *  Sends a message to all observers of a given (as well as to all roles
     *  with status as god's eye).
     *
     * @param  unit         The unit.
     * @param  unitMessage  The message to be sent.
     */
    protected void sendUnitMessage(Unit unit, String unitMessage) {
        Client ci = ReferenceHolder.getComClient();
        int handle = unit.getHandle();
        // Send to all observers!
        Iterator it = unit.getObservers().iterator();
        while (it.hasNext()) {
            String role = (String) it.next();
            ci.unitMessage(role, handle, unitMessage);
        }
        // Send to all gods eye roles!
        it = ReferenceHolder.getUnitManager().getGodsEyeRoles().iterator();
        while (it.hasNext()) {
            String role = (String) it.next();
            ci.unitMessage(role, handle, unitMessage);
        }
    }

}
