Loading...
 

PogamutUT2004


Custom Player/Bot Observer in 3.1

Hello!

Here I will show you, how to create basic player observer in Pogamut 3.1. Note that Observer class in Pogamut is a stub and does not provide much help for the programmer, but listeners work and you can send commands to the server, so basically it is fully functional.

Start by creating normal Pogamut Java Bot through New Project in NetBeans.

Then add following two classes to the package:

PogamutObserver

package observerconnection;

import com.google.inject.Inject;
import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
import cz.cuni.amis.pogamut.base.communication.command.IAct;
import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverFactory;

import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
import cz.cuni.amis.utils.exception.PogamutException;


/**
 * Custom basic observer class.
 *
 * @author Michal Bida
 */
public class ObserverConnection extends UT2004Observer {

    /** Boolean holding the info that we have already started to observer someone */
    boolean bSetObservedPlayer = false;

    /**
     * Listener for players - we will receive here all players on the server in handshake
     * and then all the players our observed player/bot can see
     */
    IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playerListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
        public void notify(WorldObjectUpdatedEvent<Player> event) {
            if (!bSetObservedPlayer) {
                //observer first player we get 
                getAct().act(new Initialize().setName(event.getObject().getName()));
                bSetObservedPlayer = true;
                System.out.println("Observing: " + event.getObject().getName());
            }

            System.out.println(event.getObject().toString());
        }
        
    };

    /**
     * Self message of our observed player/bot.
     */
    IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>> selfListener = new IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>>() {

        public void notify(WorldObjectUpdatedEvent<Self> event) {
            System.out.println(event.getObject().toString());
        }
        
    };

    @Inject
    public ObserverConnection(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
        super(params, bus, agentLogger, worldView, act);
    }

    /**
     * We call this method manually - we register listeners here.
     */
    public void initialize() {

        //initialize listeners
        getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerListener);
        getWorldView().addObjectListener(Self.class, WorldObjectUpdatedEvent.class, selfListener);

        System.out.println("!!!!!!! Initialize");
    }

    /**
     * This method is called when the bot is started either from IDE or from command line.
     * It connects the bot to the game server.
     * @param args
     */
    public static void main(String args[]) throws PogamutException {

        //creating agent parameters - setting module name and connection adress
        UT2004AgentParameters params = new UT2004AgentParameters();
        params.setAgentId(new AgentId("ObserverConnection"));
        params.setWorldAddress(new SocketConnectionAddress("127.0.0.1", 3002));

        //create module that tells guice it should instantiate OUR (this) class
        ObserverConnectionModule module = new ObserverConnectionModule();

        //creating pogamut factory
        UT2004ObserverFactory fac = new UT2004ObserverFactory(module);
        ObserverConnection obs = (ObserverConnection) fac.newAgent(params);

        //starting the connection - connecting to the server
        obs.start();
        //launching our custom method
        obs.initialize();
    }
}

and

CustomPogamutObserverModule
(CustomPogamutObserverModule is important because it is telling guice to instantiate our Observer class and not default UT2004Observer.)

package observerconnection;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import cz.cuni.amis.pogamut.base.agent.IAgent;
import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
import cz.cuni.amis.pogamut.ut2004.communication.translator.observer.ObserverFSM;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverModule;
import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;

/**
 * Class telling guice it should instantiate our custom observer class.
 *
 * @author Michal Bida
 */
public class ObserverConnectionModule extends UT2004ObserverModule {

	@Override
	protected void configureModules() {
		super.configureModules();
		addModule(new AbstractModule() {

			@Override
			public void configure() {
				bind(IWorldMessageTranslator.class).to(ObserverFSM.class);
				bind(IWorldView.class).to(IVisionWorldView.class);
				bind(IVisionWorldView.class).to(UT2004WorldView.class);
				bind(ComponentDependencies.class).annotatedWith(Names.named(UT2004WorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
				bind(IAgent.class).to(IUT2004Observer.class);
				bind(IUT2004Observer.class).to(ObserverConnection.class); //THIS tells guice it should instantiate our class and not default one
			}

		});
	}
}


Run observer by running PogamutObserver class. It will connect by default to observer port 3002 and start observing player named Shadow.
You can choose to observe some different player by reacting in playerListener listener to Player objects that you will receive in the handshake. But don't forget to erase the line trying to observer player Shadow from Initialize() method.

Best,
Michal
Hmmm, i've some doubts about it...

While the "14-custom-control-server-archetype" is working displaying the GameBots messages of an EmptyBot running in a lan game, this example doesn't display anything, just !!!!!Initialize.

And how can I retrieve the information about a particolar player? I mean how I can observe a particular player?

Sorry but i'm a bit confused... :-) I don't know where is the problem...

> Hello!
>
> Here I will show you, how to create basic player observer in Pogamut 3.1. Note that Observer class in Pogamut is a stub and does not provide much help for the programmer, but listeners work and you can send commands to the server, so basically it is fully functional.
>
> Start by creating normal Pogamut Java Bot through New Project in NetBeans.
>
> Then add following two classes to the package:
>
> PogamutObserver
>
>
> package observerconnection;
> 
> import com.google.inject.Inject;
> import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
> import cz.cuni.amis.pogamut.base.communication.command.IAct;
> import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
> import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
> import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
> import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
> import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
> import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
> import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
> import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverFactory;
> 
> import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
> import cz.cuni.amis.utils.exception.PogamutException;
> 
> 
> /**
>  * Custom basic observer class.
>  *
>  * @author Michal Bida
>  */
> public class ObserverConnection extends UT2004Observer {
> 
>     /** Boolean holding the info that we have already started to observer someone */
>     boolean bSetObservedPlayer = false;
> 
>     /**
>      * Listener for players - we will receive here all players on the server in handshake
>      * and then all the players our observed player/bot can see
>      */
>     IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playerListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
>         public void notify(WorldObjectUpdatedEvent<Player> event) {
>             if (!bSetObservedPlayer) {
>                 //observer first player we get 
>                 getAct().act(new Initialize().setName(event.getObject().getName()));
>                 bSetObservedPlayer = true;
>                 System.out.println("Observing: " + event.getObject().getName());
>             }
> 
>             System.out.println(event.getObject().toString());
>         }
>         
>     };
> 
>     /**
>      * Self message of our observed player/bot.
>      */
>     IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>> selfListener = new IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>>() {
> 
>         public void notify(WorldObjectUpdatedEvent<Self> event) {
>             System.out.println(event.getObject().toString());
>         }
>         
>     };
> 
>     @Inject
>     public ObserverConnection(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
>         super(params, bus, agentLogger, worldView, act);
>     }
> 
>     /**
>      * We call this method manually - we register listeners here.
>      */
>     public void initialize() {
> 
>         //initialize listeners
>         getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerListener);
>         getWorldView().addObjectListener(Self.class, WorldObjectUpdatedEvent.class, selfListener);
> 
>         System.out.println("!!!!!!! Initialize");
>     }
> 
>     /**
>      * This method is called when the bot is started either from IDE or from command line.
>      * It connects the bot to the game server.
>      * @param args
>      */
>     public static void main(String args[]) throws PogamutException {
> 
>         //creating agent parameters - setting module name and connection adress
>         UT2004AgentParameters params = new UT2004AgentParameters();
>         params.setAgentId(new AgentId("ObserverConnection"));
>         params.setWorldAddress(new SocketConnectionAddress("127.0.0.1", 3002));
> 
>         //create module that tells guice it should instantiate OUR (this) class
>         ObserverConnectionModule module = new ObserverConnectionModule();
> 
>         //creating pogamut factory
>         UT2004ObserverFactory fac = new UT2004ObserverFactory(module);
>         ObserverConnection obs = (ObserverConnection) fac.newAgent(params);
> 
>         //starting the connection - connecting to the server
>         obs.start();
>         //launching our custom method
>         obs.initialize();
>     }
> }
>

> and
>
> CustomPogamutObserverModule
> (CustomPogamutObserverModule is important because it is telling guice to instantiate our Observer class and not default UT2004Observer.)
>
>
> package observerconnection;
> 
> import com.google.inject.AbstractModule;
> import com.google.inject.name.Names;
> 
> import cz.cuni.amis.pogamut.base.agent.IAgent;
> import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
> import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
> import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
> import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
> import cz.cuni.amis.pogamut.ut2004.communication.translator.observer.ObserverFSM;
> import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
> import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverModule;
> import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;
> 
> /**
>  * Class telling guice it should instantiate our custom observer class.
>  *
>  * @author Michal Bida
>  */
> public class ObserverConnectionModule extends UT2004ObserverModule {
> 
> 	@Override
> 	protected void configureModules() {
> 		super.configureModules();
> 		addModule(new AbstractModule() {
> 
> 			@Override
> 			public void configure() {
> 				bind(IWorldMessageTranslator.class).to(ObserverFSM.class);
> 				bind(IWorldView.class).to(IVisionWorldView.class);
> 				bind(IVisionWorldView.class).to(UT2004WorldView.class);
> 				bind(ComponentDependencies.class).annotatedWith(Names.named(UT2004WorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
> 				bind(IAgent.class).to(IUT2004Observer.class);
> 				bind(IUT2004Observer.class).to(ObserverConnection.class); //THIS tells guice it should instantiate our class and not default one
> 			}
> 
> 		});
> 	}
> }
>

>
> Run observer by running PogamutObserver class. It will connect by default to observer port 3002 and start observing player named Shadow.
> You can choose to observe some different player by reacting in playerListener listener to Player objects that you will receive in the handshake. But don't forget to erase the line trying to observer player Shadow from Initialize() method.
>
> Best,
> Michal
Hi!

I'm going to create another example/archetype right away that will feature the observer.

Jakub
Thanks a lot, I really appreaciate it

> Hello!
>
> Here I will show you, how to create basic player observer in Pogamut 3.1. Note that Observer class in Pogamut is a stub and does not provide much help for the programmer, but listeners work and you can send commands to the server, so basically it is fully functional.
>
> Start by creating normal Pogamut Java Bot through New Project in NetBeans.
>
> Then add following two classes to the package:
>
> PogamutObserver
>
>
> package observerconnection;
> 
> import com.google.inject.Inject;
> import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
> import cz.cuni.amis.pogamut.base.communication.command.IAct;
> import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
> import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
> import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
> import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
> import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
> import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
> import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
> import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
> import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverFactory;
> 
> import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
> import cz.cuni.amis.utils.exception.PogamutException;
> 
> 
> /**
>  * Custom basic observer class.
>  *
>  * @author Michal Bida
>  */
> public class ObserverConnection extends UT2004Observer {
> 
>     /** Boolean holding the info that we have already started to observer someone */
>     boolean bSetObservedPlayer = false;
> 
>     /**
>      * Listener for players - we will receive here all players on the server in handshake
>      * and then all the players our observed player/bot can see
>      */
>     IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playerListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
>         public void notify(WorldObjectUpdatedEvent<Player> event) {
>             if (!bSetObservedPlayer) {
>                 //observer first player we get 
>                 getAct().act(new Initialize().setName(event.getObject().getName()));
>                 bSetObservedPlayer = true;
>                 System.out.println("Observing: " + event.getObject().getName());
>             }
> 
>             System.out.println(event.getObject().toString());
>         }
>         
>     };
> 
>     /**
>      * Self message of our observed player/bot.
>      */
>     IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>> selfListener = new IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>>() {
> 
>         public void notify(WorldObjectUpdatedEvent<Self> event) {
>             System.out.println(event.getObject().toString());
>         }
>         
>     };
> 
>     @Inject
>     public ObserverConnection(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
>         super(params, bus, agentLogger, worldView, act);
>     }
> 
>     /**
>      * We call this method manually - we register listeners here.
>      */
>     public void initialize() {
> 
>         //initialize listeners
>         getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerListener);
>         getWorldView().addObjectListener(Self.class, WorldObjectUpdatedEvent.class, selfListener);
> 
>         System.out.println("!!!!!!! Initialize");
>     }
> 
>     /**
>      * This method is called when the bot is started either from IDE or from command line.
>      * It connects the bot to the game server.
>      * @param args
>      */
>     public static void main(String args[]) throws PogamutException {
> 
>         //creating agent parameters - setting module name and connection adress
>         UT2004AgentParameters params = new UT2004AgentParameters();
>         params.setAgentId(new AgentId("ObserverConnection"));
>         params.setWorldAddress(new SocketConnectionAddress("127.0.0.1", 3002));
> 
>         //create module that tells guice it should instantiate OUR (this) class
>         ObserverConnectionModule module = new ObserverConnectionModule();
> 
>         //creating pogamut factory
>         UT2004ObserverFactory fac = new UT2004ObserverFactory(module);
>         ObserverConnection obs = (ObserverConnection) fac.newAgent(params);
> 
>         //starting the connection - connecting to the server
>         obs.start();
>         //launching our custom method
>         obs.initialize();
>     }
> }
>

> and
>
> CustomPogamutObserverModule
> (CustomPogamutObserverModule is important because it is telling guice to instantiate our Observer class and not default UT2004Observer.)
>
>
> package observerconnection;
> 
> import com.google.inject.AbstractModule;
> import com.google.inject.name.Names;
> 
> import cz.cuni.amis.pogamut.base.agent.IAgent;
> import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
> import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
> import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
> import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
> import cz.cuni.amis.pogamut.ut2004.communication.translator.observer.ObserverFSM;
> import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
> import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverModule;
> import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;
> 
> /**
>  * Class telling guice it should instantiate our custom observer class.
>  *
>  * @author Michal Bida
>  */
> public class ObserverConnectionModule extends UT2004ObserverModule {
> 
> 	@Override
> 	protected void configureModules() {
> 		super.configureModules();
> 		addModule(new AbstractModule() {
> 
> 			@Override
> 			public void configure() {
> 				bind(IWorldMessageTranslator.class).to(ObserverFSM.class);
> 				bind(IWorldView.class).to(IVisionWorldView.class);
> 				bind(IVisionWorldView.class).to(UT2004WorldView.class);
> 				bind(ComponentDependencies.class).annotatedWith(Names.named(UT2004WorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
> 				bind(IAgent.class).to(IUT2004Observer.class);
> 				bind(IUT2004Observer.class).to(ObserverConnection.class); //THIS tells guice it should instantiate our class and not default one
> 			}
> 
> 		});
> 	}
> }
>

>
> Run observer by running PogamutObserver class. It will connect by default to observer port 3002 and start observing player named Shadow.
> You can choose to observe some different player by reacting in playerListener listener to Player objects that you will receive in the handshake. But don't forget to erase the line trying to observer player Shadow from Initialize() method.
>
> Best,
> Michal
I've realised that my UTserver is running on localhost:3001... could this be a problem, since the observerConnection is listening on the port 3002 ?
 

News

News RSS RSS feed for News link



Pogamut

Quarterly RSS RSS feed for quarterly reports

Acknowledgement

This work is supported by GA UK 1053/2007/A-INF/MFF (2007-8), GA UK 351/2006/A-INF/MFF (2006-8), the Ministry of Education of the Czech Republic (grant MSM0021620838) (2008-9), by the Program "Information Society" under project 1ET100300517 (2006-9), and the project Integration of IT Tools into Education of Humanities (2006-8) and by the project CZ.2.17/3.1.00/31162, which are financed by the European Social Fund, the state budget of the Czech Republic, and by the budget of Municipal House Prague.