diff --git a/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/MiningSettingsDAOBean.java b/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/MiningSettingsDAOBean.java
index 57047b9..e2585f3 100644
--- a/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/MiningSettingsDAOBean.java
+++ b/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/MiningSettingsDAOBean.java
@@ -27,12 +27,18 @@ class MiningSettingsDAOBean
/** emp.mset_update_start(INT)
stored procedure */
private StoredProc pStartEmpireUpdate;
+ /** emp.mset_update_start(INT,INT)
stored procedure */
+ private StoredProc pStartPlanetUpdate;
+
/** emp.mset_update_set(TEXT,INT)
stored procedure */
private StoredProc pSetMiningPriority;
/** emp.mset_update_apply()
stored procedure */
private StoredProc pApplyUpdate;
+ /** emp.mset_toggle_source(INT,INT)
stored procedure */
+ private StoredProc pToggleSource;
+
/**
* Dependency injector that sets the data source
@@ -47,6 +53,11 @@ class MiningSettingsDAOBean
this.pStartEmpireUpdate.addParameter( "_empire" , Types.INTEGER );
this.pStartEmpireUpdate.addOutput( "_success" , Types.BOOLEAN );
+ this.pStartPlanetUpdate = new StoredProc( dataSource , "emp" , "mset_update_start" );
+ this.pStartPlanetUpdate.addParameter( "_empire" , Types.INTEGER );
+ this.pStartPlanetUpdate.addParameter( "_planet" , Types.INTEGER );
+ this.pStartPlanetUpdate.addOutput( "_success" , Types.BOOLEAN );
+
this.pSetMiningPriority = new StoredProc( dataSource , "emp" , "mset_update_set" );
this.pSetMiningPriority.addParameter( "_resource" , Types.VARCHAR );
this.pSetMiningPriority.addParameter( "_priority" , Types.INTEGER );
@@ -54,6 +65,10 @@ class MiningSettingsDAOBean
this.pApplyUpdate = new StoredProc( dataSource , "emp" , "mset_update_apply" );
this.pApplyUpdate.addOutput( "_success" , Types.BOOLEAN );
+
+ this.pToggleSource = new StoredProc( dataSource , "emp" , "mset_toggle_source" );
+ this.pToggleSource.addParameter( "_empire" , Types.INTEGER );
+ this.pToggleSource.addParameter( "_planet" , Types.INTEGER );
}
@@ -65,6 +80,14 @@ class MiningSettingsDAOBean
}
+ /* Documented in interface */
+ @Override
+ public boolean startUpdate( int empire , int planet )
+ {
+ return (Boolean) this.pStartPlanetUpdate.execute( empire , planet ).get( "_success" );
+ }
+
+
/* Documented in interface */
@Override
public boolean setNewPriority( String resource , int priority )
@@ -80,4 +103,12 @@ class MiningSettingsDAOBean
return (Boolean) this.pApplyUpdate.execute( ).get( "_success" );
}
+
+ /* Documented in interface */
+ @Override
+ public void togglePlanet( int empire , int planet )
+ {
+ this.pToggleSource.execute( empire , planet );
+ }
+
}
diff --git a/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/ResourcesControllerBean.java b/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/ResourcesControllerBean.java
index a2b8040..1829713 100644
--- a/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/ResourcesControllerBean.java
+++ b/legacyworlds-server-beans-resources/src/main/java/com/deepclone/lw/beans/game/resources/ResourcesControllerBean.java
@@ -52,12 +52,51 @@ class ResourcesControllerBean
@Override
public void updateEmpireSettings( int empireId , Map< String , Integer > settings )
{
- if ( !this.settings.startUpdate( empireId ) ) {
- return;
+ if ( this.settings.startUpdate( empireId ) ) {
+ this.sendMiningSettings( settings );
}
+ }
+
+ /* Documented in interface */
+ @Override
+ public void togglePlanet( int empire , int planet )
+ {
+ this.settings.togglePlanet( empire , planet );
+ }
+
+
+ /**
+ * Update a planet's mining settings
+ *
+ *
+ * Start a mining settings update for the specified planet / empire combination, then set each + * resource-specific priority, then apply the update. Exit whenever something goes wrong. + */ + @Override + public void updatePlanetSettings( int empire , int planet , Map< String , Integer > settings ) + { + if ( this.settings.startUpdate( empire , planet ) ) { + this.sendMiningSettings( settings ); + } + } + + + /** + * Send mining settings to the database + * + *
+ * This method is called once a mining settings update has been started (whether it's a
+ * planet-specific or empire-wide update). It sends all settings to the database server, and
+ * aborts if anything goes wrong.
+ *
+ * @param settings
+ * the settings to upload
+ */
+ private void sendMiningSettings( Map< String , Integer > settings )
+ {
for ( Map.Entry< String , Integer > entry : settings.entrySet( ) ) {
- if ( ! this.settings.setNewPriority( entry.getKey( ) , entry.getValue( ) ) ) {
+ if ( !this.settings.setNewPriority( entry.getKey( ) , entry.getValue( ) ) ) {
return;
}
}
diff --git a/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetDAOBean.java b/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetDAOBean.java
index 8deb049..e9a1cf9 100644
--- a/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetDAOBean.java
+++ b/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetDAOBean.java
@@ -101,6 +101,7 @@ public class PlanetDAOBean
info.setRenamePossible( rs.getBoolean( "can_rename" ) );
info.setAbandonPossible( rs.getBoolean( "can_abandon" ) );
info.setAbandonTime( rs.getInt( "abandon_time" ) );
+ info.setOwnSettings( rs.getBoolean( "specific_mining_settings" ) );
return info;
}
};
diff --git a/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetsManagementBean.java b/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetsManagementBean.java
index 0e364ca..8d5bf13 100644
--- a/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetsManagementBean.java
+++ b/legacyworlds-server-beans-simple/src/main/java/com/deepclone/lw/beans/map/PlanetsManagementBean.java
@@ -109,6 +109,7 @@ public class PlanetsManagementBean
view.sethChange( data.gethChange( ) );
view.setIncome( data.getIncome( ) );
view.setUpkeep( data.getUpkeep( ) );
+ view.setOwnMiningSettings( data.isOwnSettings( ) );
view.setCivQueue( this.planetDao.getConstructionQueue( planetId ) );
view.setMilQueue( this.planetDao.getMilitaryQueue( planetId ) );
diff --git a/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/ToggleMiningSettingsCommandDelegateBean.java b/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/ToggleMiningSettingsCommandDelegateBean.java
new file mode 100644
index 0000000..f4a951a
--- /dev/null
+++ b/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/ToggleMiningSettingsCommandDelegateBean.java
@@ -0,0 +1,73 @@
+package com.deepclone.lw.beans.user.player.game.planets;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.deepclone.lw.beans.user.abst.AutowiredCommandDelegate;
+import com.deepclone.lw.beans.user.abst.SessionCommandHandler;
+import com.deepclone.lw.beans.user.player.GameSubTypeBean;
+import com.deepclone.lw.cmd.player.planets.ToggleMiningSettingsCommand;
+import com.deepclone.lw.cmd.player.planets.ViewPlanetCommand;
+import com.deepclone.lw.interfaces.game.resources.ResourcesController;
+import com.deepclone.lw.interfaces.session.ServerSession;
+import com.deepclone.lw.session.Command;
+import com.deepclone.lw.session.CommandResponse;
+import com.deepclone.lw.session.NullResponse;
+
+
+
+/**
+ * Command handler for {@link ToggleMiningSettingsCommand}
+ *
+ * @author E. Benoît
+ */
+class ToggleMiningSettingsCommandDelegateBean
+ implements AutowiredCommandDelegate
+{
+
+ /** The resources controller */
+ private ResourcesController resourcesController;
+
+
+ /**
+ * Dependency injector that sets the resources controller
+ *
+ * @param resourcesController
+ * the resources controller
+ */
+ @Autowired( required = true )
+ public void setResourcesController( ResourcesController resourcesController )
+ {
+ this.resourcesController = resourcesController;
+ }
+
+
+ /** This class handles {@link ToggleMiningSettingsCommand} instances */
+ @Override
+ public Class< ? extends Command > getType( )
+ {
+ return ToggleMiningSettingsCommand.class;
+ }
+
+
+ /** This class is enabled for the {@link GameSubTypeBean} session type */
+ @Override
+ public Class< ? extends SessionCommandHandler > getCommandHandler( )
+ {
+ return GameSubTypeBean.class;
+ }
+
+
+ /** If the account is not in vacation mode, try to toggle the planet's settings */
+ @Override
+ public CommandResponse execute( ServerSession session , Command command )
+ {
+ if ( !session.get( "vacation" , Boolean.class ) ) {
+ int empireId = session.get( "empireId" , Integer.class );
+ ViewPlanetCommand planet = (ViewPlanetCommand) command;
+ this.resourcesController.togglePlanet( empireId , planet.getId( ) );
+ }
+ return new NullResponse( );
+ }
+
+}
diff --git a/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/UpdatePlanetMiningSettingsCommandDelegateBean.java b/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/UpdatePlanetMiningSettingsCommandDelegateBean.java
new file mode 100644
index 0000000..1a7d954
--- /dev/null
+++ b/legacyworlds-server-beans-user/src/main/java/com/deepclone/lw/beans/user/player/game/planets/UpdatePlanetMiningSettingsCommandDelegateBean.java
@@ -0,0 +1,72 @@
+package com.deepclone.lw.beans.user.player.game.planets;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.deepclone.lw.beans.user.abst.AutowiredCommandDelegate;
+import com.deepclone.lw.beans.user.abst.SessionCommandHandler;
+import com.deepclone.lw.beans.user.player.GameSubTypeBean;
+import com.deepclone.lw.cmd.player.planets.UpdatePlanetMiningSettingsCommand;
+import com.deepclone.lw.interfaces.game.resources.ResourcesController;
+import com.deepclone.lw.interfaces.session.ServerSession;
+import com.deepclone.lw.session.Command;
+import com.deepclone.lw.session.CommandResponse;
+import com.deepclone.lw.session.NullResponse;
+
+
+
+/**
+ * Command handler for {@link UpdatePlanetMiningSettingsCommand}
+ *
+ * @author E. Benoît
+ */
+class UpdatePlanetMiningSettingsCommandDelegateBean
+ implements AutowiredCommandDelegate
+{
+
+ /** The resources controller */
+ private ResourcesController resourcesController;
+
+
+ /**
+ * Dependency injector that sets the resources controller
+ *
+ * @param resourcesController
+ * the resources controller
+ */
+ @Autowired( required = true )
+ public void setResourcesController( ResourcesController resourcesController )
+ {
+ this.resourcesController = resourcesController;
+ }
+
+
+ /** This class handles {@link UpdatePlanetMiningSettingsCommand} instances */
+ @Override
+ public Class< ? extends Command > getType( )
+ {
+ return UpdatePlanetMiningSettingsCommand.class;
+ }
+
+
+ /** This class is enabled for the {@link GameSubTypeBean} session type */
+ @Override
+ public Class< ? extends SessionCommandHandler > getCommandHandler( )
+ {
+ return GameSubTypeBean.class;
+ }
+
+
+ /** If the empire is not in vacation mode, try updating the planet's mining priorities */
+ @Override
+ public CommandResponse execute( ServerSession session , Command command )
+ {
+ if ( !session.get( "vacation" , Boolean.class ) ) {
+ UpdatePlanetMiningSettingsCommand settings = (UpdatePlanetMiningSettingsCommand) command;
+ int empireId = session.get( "empireId" , Integer.class );
+ this.resourcesController.updatePlanetSettings( empireId , settings.getId( ) , settings.getSettings( ) );
+ }
+ return new NullResponse( );
+ }
+
+}
diff --git a/legacyworlds-server-beans-user/src/main/resources/configuration/session-types/player.xml b/legacyworlds-server-beans-user/src/main/resources/configuration/session-types/player.xml
index 203a345..ff9a382 100644
--- a/legacyworlds-server-beans-user/src/main/resources/configuration/session-types/player.xml
+++ b/legacyworlds-server-beans-user/src/main/resources/configuration/session-types/player.xml
@@ -61,6 +61,8 @@
emp.mset_update_start(INT)
stored
* procedure.
*
- * @param empireId
+ * @param empire
* the identifier of the empire whose settings will be updated
*
* @return true
on success, false
on failure
*/
- public boolean startUpdate( int empireId );
+ public boolean startUpdate( int empire );
+
+
+ /**
+ * Start a planet settings update
+ *
+ *
+ * Start a planet settings update by calling the emp.mset_update_start(INT,INT)
+ * stored procedure.
+ *
+ * @param empire
+ * the identifier of the empire who supposedly owns the planet
+ * @param planet
+ * the identifier of the planet whose settings will be changed
+ *
+ * @return true
if the planet belongs to the specified empire and has specific
+ * mining settings
+ */
+ public boolean startUpdate( int empire , int planet );
/**
@@ -57,4 +75,19 @@ public interface MiningSettingsDAO
* invalid.
*/
public boolean applyUpdate( );
+
+
+ /**
+ * Toggle the source of a planet's mining settings
+ *
+ *
+ * Call the emp.mset_toggle_source(INT,INT)
stored procedure to toggle the source
+ * of a planet's mining priorities.
+ *
+ * @param empire
+ * the empire's identifier
+ * @param planet
+ * the planet's identifier
+ */
+ public void togglePlanet( int empire , int planet );
}
diff --git a/legacyworlds-server-interfaces/src/main/java/com/deepclone/lw/interfaces/game/resources/ResourcesController.java b/legacyworlds-server-interfaces/src/main/java/com/deepclone/lw/interfaces/game/resources/ResourcesController.java
index 2444da7..940dc45 100644
--- a/legacyworlds-server-interfaces/src/main/java/com/deepclone/lw/interfaces/game/resources/ResourcesController.java
+++ b/legacyworlds-server-interfaces/src/main/java/com/deepclone/lw/interfaces/game/resources/ResourcesController.java
@@ -27,4 +27,36 @@ public interface ResourcesController
*/
public void updateEmpireSettings( int empireId , Map< String , Integer > settings );
+
+ /**
+ * Toggle the source of a planet's mining priorities
+ *
+ *
+ * If the planet is currently using specific priorities, it will revert to empire-wide settings, + * and vice-versa. The planet must belong to the specified empire. + * + * @param empire + * the empire's identifier + * @param planet + * the planet's identifier + */ + public void togglePlanet( int empire , int planet ); + + + /** + * Update a planet's mining settings + * + *
+ * If the planet is using specific mining priorities and belongs to the specified empire, update
+ * its mining priorities.
+ *
+ * @param empire
+ * the empire's identifier
+ * @param planet
+ * the planet's identifier
+ * @param settings
+ * the new priorities
+ */
+ public void updatePlanetSettings( int empire , int planet , Map< String , Integer > settings );
+
}
diff --git a/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/MockMiningSettingsDAO.java b/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/MockMiningSettingsDAO.java
index 0b46dce..330c0f2 100644
--- a/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/MockMiningSettingsDAO.java
+++ b/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/MockMiningSettingsDAO.java
@@ -15,15 +15,27 @@ import com.deepclone.lw.interfaces.game.resources.MiningSettingsDAO;
public class MockMiningSettingsDAO
implements MiningSettingsDAO
{
- /** The empire identifier with which {@link #startUpdate(int)} was called */
- private Integer updateEmpire = null;
+ /**
+ * The empire identifier with which {@link #startUpdate(int)} or {@link #togglePlanet(int, int)}
+ * was called
+ */
+ private Integer empire = null;
+
+ /** The planet identifier with which {@link #togglePlanet(int, int)} was called */
+ private Integer planet = null;
/** The amount of calls to {@link #setNewPriority(String, int)} */
private int callsToSet = 0;
+ /** Whether {@link #startUpdate(int)} was called */
+ private boolean startUpdateCalled = false;
+
/** Whether {@link #applyUpdate()} was called */
private boolean applyCalled = false;
+ /** Whether {@link #togglePlanet(int, int)} was called */
+ private boolean togglePlanetCalled = false;
+
/** Whether {@link #startUpdate(int)} will succeed or fail */
private boolean startUpdateSucceeds = true;
@@ -37,10 +49,24 @@ public class MockMiningSettingsDAO
private Integer maxSetCalls = null;
- /** @return the empire identifier to update */
- public Integer getUpdateEmpire( )
+ /** @return the empire identifier */
+ public Integer getEmpire( )
{
- return this.updateEmpire;
+ return this.empire;
+ }
+
+
+ /** @return the planet identifier */
+ public Integer getPlanet( )
+ {
+ return this.planet;
+ }
+
+
+ /** @return true
if {@link #startUpdate(int)()} was called */
+ public boolean wasStartUpdateCalled( )
+ {
+ return this.startUpdateCalled;
}
@@ -58,6 +84,13 @@ public class MockMiningSettingsDAO
}
+ /** @return true
if {@link #togglePlanet(int, int)} was called */
+ public boolean wasTogllePlanetCalled( )
+ {
+ return this.togglePlanetCalled;
+ }
+
+
/**
* Determine whether calls to {@link #startUpdate(int)} will succeed
*
@@ -98,7 +131,18 @@ public class MockMiningSettingsDAO
@Override
public boolean startUpdate( int empireId )
{
- this.updateEmpire = empireId;
+ this.startUpdateCalled = true;
+ this.empire = empireId;
+ return this.startUpdateSucceeds;
+ }
+
+
+ @Override
+ public boolean startUpdate( int empire , int planet )
+ {
+ this.startUpdateCalled = true;
+ this.empire = empire;
+ this.planet = planet;
return this.startUpdateSucceeds;
}
@@ -118,4 +162,13 @@ public class MockMiningSettingsDAO
return this.applyUpdateSucceeds;
}
+
+ @Override
+ public void togglePlanet( int empire , int planet )
+ {
+ this.togglePlanetCalled = true;
+ this.empire = empire;
+ this.planet = planet;
+ }
+
}
diff --git a/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/TestResourcesControllerBean.java b/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/TestResourcesControllerBean.java
index b836ab9..393b577 100644
--- a/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/TestResourcesControllerBean.java
+++ b/legacyworlds-server-tests/src/test/java/com/deepclone/lw/beans/game/resources/TestResourcesControllerBean.java
@@ -26,6 +26,9 @@ public class TestResourcesControllerBean
/** Empire identifier used in the tests */
private static final Integer EMPIRE_ID = 42;
+ /** Planet identifier used in the tests */
+ private static final Integer PLANET_ID = 43;
+
/** Mining settings used in the tests */
private static final Map< String , Integer > MINING_SETTINGS;
@@ -70,7 +73,25 @@ public class TestResourcesControllerBean
{
this.miningSettingsDAO.setStartUpdateSucceeds( false );
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
- assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
+ assertTrue( this.miningSettingsDAO.wasStartUpdateCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
+ assertEquals( 0 , this.miningSettingsDAO.getCallsToSet( ) );
+ assertFalse( this.miningSettingsDAO.wasApplyCalled( ) );
+ }
+
+
+ /**
+ * When calling {@link MiningSettingsDAO#startUpdate(int,int)} fails, the planet settings update
+ * is interrupted.
+ */
+ @Test
+ public void testPlanetStartUpdateFails( )
+ {
+ this.miningSettingsDAO.setStartUpdateSucceeds( false );
+ this.ctrl.updatePlanetSettings( EMPIRE_ID , PLANET_ID , MINING_SETTINGS );
+ assertTrue( this.miningSettingsDAO.wasStartUpdateCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
+ assertEquals( PLANET_ID , this.miningSettingsDAO.getPlanet( ) );
assertEquals( 0 , this.miningSettingsDAO.getCallsToSet( ) );
assertFalse( this.miningSettingsDAO.wasApplyCalled( ) );
}
@@ -85,7 +106,8 @@ public class TestResourcesControllerBean
{
this.miningSettingsDAO.setMaxSetCalls( 2 );
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
- assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
+ assertTrue( this.miningSettingsDAO.wasStartUpdateCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
assertEquals( 3 , this.miningSettingsDAO.getCallsToSet( ) );
assertFalse( this.miningSettingsDAO.wasApplyCalled( ) );
}
@@ -100,7 +122,8 @@ public class TestResourcesControllerBean
public void testSettingsSuccess( )
{
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
- assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
+ assertTrue( this.miningSettingsDAO.wasStartUpdateCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
assertEquals( 4 , this.miningSettingsDAO.getCallsToSet( ) );
assertTrue( this.miningSettingsDAO.wasApplyCalled( ) );
}
@@ -114,8 +137,21 @@ public class TestResourcesControllerBean
{
this.miningSettingsDAO.setApplyUpdateSucceeds( false );
this.ctrl.updateEmpireSettings( EMPIRE_ID , MINING_SETTINGS );
- assertEquals( EMPIRE_ID , this.miningSettingsDAO.getUpdateEmpire( ) );
+ assertTrue( this.miningSettingsDAO.wasStartUpdateCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
assertEquals( 4 , this.miningSettingsDAO.getCallsToSet( ) );
assertTrue( this.miningSettingsDAO.wasApplyCalled( ) );
}
+
+ /**
+ * "Toggle planet source" calls {@link MiningSettingsDAO#togglePlanet(int, int)}
+ */
+ @Test
+ public void tesTogglePlanet()
+ {
+ this.ctrl.togglePlanet( EMPIRE_ID , PLANET_ID );
+ assertTrue( this.miningSettingsDAO.wasTogllePlanetCalled( ) );
+ assertEquals( EMPIRE_ID , this.miningSettingsDAO.getEmpire( ) );
+ assertEquals( PLANET_ID , this.miningSettingsDAO.getPlanet( ) );
+ }
}
diff --git a/legacyworlds-session/src/main/java/com/deepclone/lw/cmd/player/planets/ToggleMiningSettingsCommand.java b/legacyworlds-session/src/main/java/com/deepclone/lw/cmd/player/planets/ToggleMiningSettingsCommand.java
new file mode 100644
index 0000000..c15dc0e
--- /dev/null
+++ b/legacyworlds-session/src/main/java/com/deepclone/lw/cmd/player/planets/ToggleMiningSettingsCommand.java
@@ -0,0 +1,38 @@
+package com.deepclone.lw.cmd.player.planets;
+
+
+/**
+ * Command sent to toggle between empire-wide and planet-specific mining settings
+ *
+ *
+ * Sending this command to the server will try to switch between empire-wide and planet-specific + * settings on a given planet. The planet must be owned by the empire sending the command. + * + * @author E. Benoît + */ +public class ToggleMiningSettingsCommand + extends ViewPlanetCommand +{ + + /** + * Serialisation version identifier + * + *
+ * This command must be sent with the identifier of a planet that belongs to the current empire. If + * the planet doesn't exist, if its ownership changed, or if it's configured to use empire-wide + * settings, the command will be ignored. + * + * @author E. Benoît + */ +public class UpdatePlanetMiningSettingsCommand + extends ViewPlanetCommand +{ + + /** + * Serialisation version identifier + * + *
+ * This method handles all mining settings commands, including both switching between empire-
+ * and planet-specific settings and updating all priorities.
+ *
+ * @param request
+ * the HTTP request
+ * @param model
+ * the model
+ * @param planetId
+ * the planet's identifier string from the path
+ * @param toggle
+ * this parameter from the form's toggle-settings
value will be set if
+ * the "Switch to empire-wide/planet-specific settings" button was clicked.
+ *
+ * @return a redirect to the overview page if the planet identifier was invalid, or a redirect
+ * to the planet page's natural resources tab otherwise.
+ *
+ * @throws SessionException
+ * if some error occurs on the server
+ * @throws SessionServerException
+ * if the server is unreachable
+ * @throws SessionMaintenanceException
+ * if the game is under maintenance
+ */
+ @RequestMapping( value = "/planet-{planetId}-update-mset.action" , method = RequestMethod.POST )
+ public String updateMiningSettings( HttpServletRequest request , Model model , @PathVariable String planetId ,
+ @RequestParam( value = "toggle-settings" , required = false ) String toggle )
+ throws SessionException , SessionServerException , SessionMaintenanceException
+ {
+ int pId;
+ try {
+ pId = Integer.parseInt( planetId );
+ } catch ( NumberFormatException e ) {
+ return this.redirect( "overview" );
+ }
+
+ PlayerSession session = this.getSession( PlayerSession.class , request );
+ if ( toggle == null ) {
+ Map< String , Integer > settings = this.getMiningSettings( request );
+ if ( settings != null ) {
+ session.updatePlanetMiningSettings( pId , settings );
+ }
+ } else {
+ session.toggleMiningSettingsFor( pId );
+ }
+
+ return this.redirect( "planet-" + Integer.toString( pId ) + "#natres" );
+ }
+
+
+ /**
+ * Extract mining priorities from the HTTP request
+ *
+ *
+ * Look for all submitted fields that begin with "pms-" then try to extract their values into a
+ * map that associates resource identifiers to priorities.
+ *
+ * @param request
+ * the HTTP request
+ *
+ * @return the map containing the submitted mining settings, or null
if one of the
+ * values was incorrect.
+ */
+ private Map< String , Integer > getMiningSettings( HttpServletRequest request )
+ {
+ Map< String , Object > input = this.getInput( request );
+ Map< String , Integer > miningSettings = new HashMap< String , Integer >( );
+ for ( Entry< String , Object > entry : input.entrySet( ) ) {
+ // Ignore items which are not mining settings
+ String name = entry.getKey( );
+ if ( !name.startsWith( "pms-" ) ) {
+ continue;
+ }
+ name = name.substring( 4 );
+
+ // Get values
+ if ( ! ( entry.getValue( ) instanceof String[] ) ) {
+ continue;
+ }
+ String[] values = (String[]) entry.getValue( );
+ if ( values.length < 1 ) {
+ continue;
+ }
+
+ // Pre-validate them
+ int value;
+ try {
+ value = Integer.parseInt( values[ 0 ] );
+ } catch ( NumberFormatException e ) {
+ value = -1;
+ }
+ if ( value < 0 || value > 4 ) {
+ return null;
+ }
+
+ miningSettings.put( name , value );
+ }
+ return miningSettings;
+ }
}