View Javadoc

1   package nl.tudelft.goal.ut2004.server;
2   
3   import nl.tudelft.goal.ut2004.messages.UnrealIdOrLocation;
4   
5   import com.google.inject.Inject;
6   
7   import cz.cuni.amis.pogamut.base.communication.command.IAct;
8   import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnection;
9   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
10  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
11  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
12  import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
13  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
14  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
15  import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
16  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
17  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
18  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Group;
19  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddInventory;
20  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ChangeTeam;
21  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Pause;
22  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Respawn;
23  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SetGameSpeed;
24  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SpawnActor;
25  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
26  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
27  import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
28  import cz.cuni.amis.pogamut.ut2004.server.impl.UT2004Server;
29  import cz.cuni.amis.utils.exception.PogamutException;
30  import eis.eis2java.annotation.AsAction;
31  
32  public class EnvironmentControllerServer extends UT2004Server {
33  
34  	private IWorldEventListener<GameInfo> pausedListener = new IWorldEventListener<GameInfo>() {
35  
36  		@Override
37  		public void notify(GameInfo event) {
38  
39  		}
40  	};
41  
42  	@Inject
43  	public EnvironmentControllerServer(UT2004AgentParameters params, IAgentLogger agentLogger, IComponentBus bus,
44  			SocketConnection connection, UT2004WorldView worldView, IAct act) {
45  		super(params, agentLogger, bus, connection, worldView, act);
46  
47  		worldView.addEventListener(GameInfo.class, pausedListener);
48  
49  	}
50  
51  	public void sendResumeGame() {
52  		Pause resume = new Pause(false, false);
53  		getAct().act(resume);
54  	}
55  
56  	public void sendPausegame() {
57  		Pause pause = new Pause(true, false);
58  		getAct().act(pause);
59  
60  	}
61  	
62  
63  	@AsAction(name = "addInventory")
64  	public void addInventory(UnrealId id, Category category, Group group) {
65  		for (ItemType item : group.getTypes()) {
66  			if (item.getCategory() == category) {
67  				AddInventory addInventory = new AddInventory(id, item.getName());
68  				getAct().act(addInventory);
69  				return;
70  			}
71  		}
72  
73  		String message = String.format("%s was not a ItemType in the %s category.", group, category);
74  		throw new PogamutException(message, this);
75  	}
76  
77  	@AsAction(name = "respawn")
78  	public void respawn(UnrealId id, UnrealIdOrLocation unrealIdOrLocation, Rotation rotation) {
79  
80  		ILocated location = unrealIdOrLocation.toILocated(getWorldView());
81  		
82  		if(location == null){
83  			String message = String.format("Could not resolve %s to a location", unrealIdOrLocation);
84  			throw new PogamutException(message, this);
85  		}
86  
87  		getAct().act(new Respawn(id, location.getLocation(), rotation));
88  
89  	}
90  
91  	@AsAction(name = "changeTeam")
92  	public void changeTeam(UnrealId id) {
93  
94  		Player player = getWorldView().get(id, Player.class);
95  
96  		if (player == null) {
97  			// TODO: Throw exception
98  			return;
99  		}
100 
101 		getAct().act(new ChangeTeam(id, 1- player.getTeam()));
102 
103 	}
104 	
105 	
106 	@AsAction(name = "setGameSpeed")
107 	public void setGameSpeed(Double speed) {
108 		getAct().act(new SetGameSpeed(speed));
109 	}
110 	
111 	@AsAction(name = "spawnItem")
112 	public void spawnItem(UnrealIdOrLocation unrealIdOrLocation, Category category, Group group) {
113 		
114 		ILocated location = unrealIdOrLocation.toILocated(getWorldView());
115 		
116 		if(location == null){
117 			String message = String.format("Could not resolve %s to a location", unrealIdOrLocation);
118 			throw new PogamutException(message, this);
119 		}
120 		
121 		for (ItemType item : group.getTypes()) {
122 			if (item.getCategory() == category) {
123 				SpawnActor spawn = new SpawnActor(location.getLocation(), Rotation.ZERO, item.getName());
124 				getAct().act(spawn);
125 				return;
126 			}
127 		}
128 
129 		String message = String.format("%s was not a ItemType in the %s category.", group, category);
130 		throw new PogamutException(message, this);
131 		
132 	}
133 	
134 }