Permalink
Browse files

WIP - observability check rewrite.

  • Loading branch information...
1 parent 709f632 commit d79c73fc2dcb1bef75af0adee39f8f63784ea817 @lhunath lhunath committed Feb 25, 2014
Showing with 295 additions and 150 deletions.
  1. +44 −0 .idea/codeStyleSettings.xml
  2. +1 −0 .idea/dictionaries/lhunath.xml
  3. +5 −5 .idea/inspectionProfiles/Lhunath.xml
  4. +21 −0 omicron-api/src/main/java/com/lyndir/omicron/api/model/GameObservable.java
  5. +4 −11 omicron-api/src/main/java/com/lyndir/omicron/api/model/GameObserver.java
  6. +1 −5 omicron-api/src/main/java/com/lyndir/omicron/api/model/IGameObject.java
  7. +0 −4 omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayer.java
  8. +1 −1 omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayerController.java
  9. +2 −2 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGameObject.java
  10. +20 −17 omicron-api/src/main/java/com/lyndir/omicron/api/model/Security.java
  11. +9 −0 omicron-api/src/main/java/com/lyndir/omicron/api/util/Maybe.java
  12. +17 −0 omicron-api/src/main/java/com/lyndir/omicron/api/util/Maybool.java
  13. +1 −3 omicron-cli/src/main/java/com/lyndir/omicron/cli/command/ListCommand.java
  14. +39 −14 omicron-core/src/main/java/com/lyndir/omicron/api/model/BaseModule.java
  15. +25 −17 omicron-core/src/main/java/com/lyndir/omicron/api/model/ConstructorModule.java
  16. +26 −27 omicron-core/src/main/java/com/lyndir/omicron/api/model/CoreUtils.java
  17. +25 −4 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameController.java
  18. +2 −2 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObject.java
  19. +3 −3 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObjectController.java
  20. +2 −1 omicron-core/src/main/java/com/lyndir/omicron/api/model/ModuleType.java
  21. +14 −12 omicron-core/src/main/java/com/lyndir/omicron/api/model/Player.java
  22. +33 −22 omicron-core/src/main/java/com/lyndir/omicron/api/model/PlayerController.java
@@ -77,6 +77,25 @@
<option name="XML_SPACE_INSIDE_EMPTY_TAG" value="true" />
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
+ <codeStyleSettings language="CFML">
+ <option name="KEEP_LINE_BREAKS" value="false" />
+ <option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
+ <option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
+ <option name="WHILE_ON_NEW_LINE" value="true" />
+ <option name="CATCH_ON_NEW_LINE" value="true" />
+ <option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" />
+ <option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
+ <option name="SPACE_WITHIN_METHOD_CALL_PARENTHESES" value="true" />
+ <option name="SPACE_BEFORE_QUEST" value="false" />
+ <option name="SPACE_BEFORE_COLON" value="false" />
+ <option name="CALL_PARAMETERS_WRAP" value="1" />
+ <option name="METHOD_PARAMETERS_WRAP" value="1" />
+ <option name="BINARY_OPERATION_WRAP" value="1" />
+ <option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
+ <option name="TERNARY_OPERATION_WRAP" value="1" />
+ <option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
+ <option name="PARENT_SETTINGS_INSTALLED" value="true" />
+ </codeStyleSettings>
<codeStyleSettings language="CoffeeScript">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
@@ -88,6 +107,31 @@
<option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
<option name="PARENT_SETTINGS_INSTALLED" value="true" />
</codeStyleSettings>
+ <codeStyleSettings language="ECMA Script Level 4">
+ <option name="KEEP_LINE_BREAKS" value="false" />
+ <option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
+ <option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
+ <option name="BLANK_LINES_AFTER_IMPORTS" value="2" />
+ <option name="WHILE_ON_NEW_LINE" value="true" />
+ <option name="CATCH_ON_NEW_LINE" value="true" />
+ <option name="FINALLY_ON_NEW_LINE" value="true" />
+ <option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" />
+ <option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
+ <option name="SPACE_WITHIN_METHOD_CALL_PARENTHESES" value="true" />
+ <option name="SPACE_BEFORE_QUEST" value="false" />
+ <option name="SPACE_BEFORE_COLON" value="false" />
+ <option name="CALL_PARAMETERS_WRAP" value="1" />
+ <option name="METHOD_PARAMETERS_WRAP" value="1" />
+ <option name="EXTENDS_LIST_WRAP" value="1" />
+ <option name="EXTENDS_KEYWORD_WRAP" value="1" />
+ <option name="BINARY_OPERATION_WRAP" value="1" />
+ <option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
+ <option name="TERNARY_OPERATION_WRAP" value="1" />
+ <option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
+ <option name="ARRAY_INITIALIZER_WRAP" value="1" />
+ <option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
+ <option name="PARENT_SETTINGS_INSTALLED" value="true" />
+ </codeStyleSettings>
<codeStyleSettings language="Groovy">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
@@ -42,6 +42,7 @@
<w>minable</w>
<w>msgs</w>
<w>natively</w>
+ <w>observable's</w>
<w>omnicron</w>
<w>online</w>
<w>parameterized</w>
@@ -483,7 +483,7 @@
<option name="ignoreWithoutParameters" value="true" />
</inspection_tool>
<inspection_tool class="NonAtomicOperationOnVolatileField" enabled="true" level="WARNING" enabled_by_default="true" />
- <inspection_tool class="NonBooleanMethodNameMayNotStartWithQuestion" enabled="true" level="WARNING" enabled_by_default="true">
+ <inspection_tool class="NonBooleanMethodNameMayNotStartWithQuestion" enabled="false" level="WARNING" enabled_by_default="false">
<option name="questionString" value="is,can,has,should,could,will,shall,contains,equals,startsWith,endsWith" />
<option name="ignoreBooleanMethods" value="false" />
<option name="onlyWarnOnBaseMethods" value="true" />
@@ -521,10 +521,10 @@
<inspection_tool class="NullThrown" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="NumericToString" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="ObjectAllocationInLoop" enabled="true" level="INFO" enabled_by_default="true" />
- <inspection_tool class="ObjectEquality" enabled="true" level="WARNING" enabled_by_default="true">
+ <inspection_tool class="ObjectEquality" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="m_ignoreEnums" value="true" />
<option name="m_ignoreClassObjects" value="true" />
- <option name="m_ignorePrivateConstructors" value="false" />
+ <option name="m_ignorePrivateConstructors" value="true" />
</inspection_tool>
<inspection_tool class="ObjectNotify" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="ObsoleteCollection" enabled="true" level="WARNING" enabled_by_default="true">
@@ -548,11 +548,11 @@
<option name="m_ignoreForPropertySetters" value="true" />
<option name="m_ignoreForAbstractMethods" value="true" />
</inspection_tool>
- <inspection_tool class="ParameterNameDiffersFromOverriddenParameter" enabled="true" level="WARNING" enabled_by_default="true">
+ <inspection_tool class="ParameterNameDiffersFromOverriddenParameter" enabled="true" level="BUGGY WARNING" enabled_by_default="true">
<option name="m_ignoreSingleCharacterNames" value="true" />
<option name="m_ignoreOverridesOfLibraryMethods" value="true" />
</inspection_tool>
- <inspection_tool class="ParameterNamingConvention" enabled="true" level="WARNING" enabled_by_default="true">
+ <inspection_tool class="ParameterNamingConvention" enabled="false" level="WARNING" enabled_by_default="false">
<option name="m_regex" value="[a-z][A-Za-z\d]*" />
<option name="m_minLength" value="1" />
<option name="m_maxLength" value="32" />
@@ -0,0 +1,21 @@
+package com.lyndir.omicron.api.model;
+
+import com.lyndir.omicron.api.util.Maybe;
+
+
+/**
+ * @author lhunath, 2/25/2014
+ */
+public interface GameObservable {
+
+ /**
+ * @return The player that has control over this observable, if any.
+ */
+ Maybe<? extends IPlayer> checkOwner();
+
+ /**
+ * @return The observable's location or {@link Maybe#unknown()} if the current player doesn't own the observable and none of its game objects can observe it.
+ */
+ Maybe<? extends ITile> checkLocation()
+ throws Security.NotAuthenticatedException;
+}
@@ -15,14 +15,14 @@
public interface GameObserver {
/**
- * Check whether the current object can observe the tile at the given location.
+ * Check whether the current object can observe given observable.
*
- * @param location The tile that this observer is trying to see.
+ * @param observable The observable that this observer is trying to see.
*
- * @return true if the current player is allowed to know and the given tile is visible to this observer.
+ * @return true if the current player is able and allowed to observe the target.
*/
@Authenticated
- Maybool canObserve(@Nonnull ITile location)
+ Maybool canObserve(@Nonnull GameObservable observable)
throws Security.NotAuthenticatedException;
/**
@@ -34,11 +34,4 @@ Maybool canObserve(@Nonnull ITile location)
@Authenticated
Iterable<? extends ITile> listObservableTiles()
throws Security.NotAuthenticatedException, Security.NotObservableException;
-
- /**
- * @return The player that has control over this observer, if any.
- */
- @Nonnull
- // TODO: This should return a Maybe to avoid being able to detect ownership changes on objects that are not observable.
- Optional<? extends IPlayer> getOwner();
}
@@ -5,7 +5,6 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableCollection;
import com.lyndir.lhunath.opal.system.util.PredicateNN;
-import com.lyndir.omicron.api.util.Maybe;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -16,7 +15,7 @@
*
* @author lhunath
*/
-public interface IGameObject extends GameObserver {
+public interface IGameObject extends GameObserver, GameObservable {
@Nonnull
IGameObjectController<? extends IGameObject> getController();
@@ -28,9 +27,6 @@ boolean isOwnedByCurrentPlayer()
IGame getGame();
- Maybe<? extends ITile> checkLocation()
- throws NotAuthenticatedException;
-
IUnitType getType();
/**
@@ -14,10 +14,6 @@
@Nonnull
IPlayerController getController();
- @Override
- Maybool canObserve(@Nonnull ITile location)
- throws Security.NotAuthenticatedException;
-
int getPlayerID();
boolean hasKey(PlayerKey playerKey);
@@ -19,7 +19,7 @@
*
* @return A list of game objects owned by this controller's player.
*/
- ImmutableCollection<? extends IGameObject> listObjects()
+ ImmutableSet<? extends IGameObject> listObjects()
throws NotAuthenticatedException;
/**
@@ -51,8 +51,8 @@ public boolean equals(@Nullable final Object obj) {
@Nonnull
@Override
- public Optional<? extends IPlayer> getOwner() {
- return core.getOwner();
+ public Optional<? extends IPlayer> checkOwner() {
+ return core.checkOwner();
}
@Override
@@ -16,8 +16,10 @@
package com.lyndir.omicron.api.model;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.lyndir.omicron.api.model.error.ExceptionUtils.*;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.lyndir.lhunath.opal.system.util.Job;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
@@ -76,13 +78,13 @@ static void playerRun(final IPlayer jobPlayer, final Runnable job) {
}
public static void authenticate(final IPlayer currentPlayer, final PlayerKey playerKey) {
- Preconditions.checkArgument( currentPlayer.hasKey( playerKey ), "Cannot authenticate, key does not match player: ", currentPlayer );
+ checkArgument( currentPlayer.hasKey( playerKey ), "Cannot authenticate, key does not match player: ", currentPlayer );
currentPlayerTL.set( currentPlayer );
}
public static void authenticatedRun(final IPlayer currentPlayer, final PlayerKey playerKey, final Runnable job) {
- Preconditions.checkArgument( currentPlayer.hasKey( playerKey ), "Cannot authenticate, key does not match player: ", currentPlayer );
+ checkArgument( currentPlayer.hasKey( playerKey ), "Cannot authenticate, key does not match player: ", currentPlayer );
playerRun( currentPlayer, job );
}
@@ -95,6 +97,15 @@ static boolean isGod() {
return !godTL.get().isEmpty() && godTL.get().peek();
}
+ /**
+ * @return true if the current player is god or can observe the target.
+ *
+ * @see IPlayer#canObserve(GameObservable)
+ */
+ static boolean currentPlayerCanObserve(final GameObservable gameObservable) {
+ return isGod() || currentPlayer().canObserve( gameObservable ).isTrue();
+ }
+
@Nonnull
static IPlayer currentPlayer()
throws NotAuthenticatedException {
@@ -109,31 +120,23 @@ static IPlayer currentPlayer()
return currentPlayer;
}
- public static void assertOwned(final GameObserver observer)
+ public static void assertOwned(final GameObservable observable)
throws NotAuthenticatedException, NotOwnedException {
if (isGod())
return;
- assertSecure( observer.getOwner().isPresent() && ObjectUtils.equals( observer.getOwner().get(), currentPlayer() ), //
- NotOwnedException.class, observer );
- }
-
- public static void assertObservable(final ITile location)
- throws NotAuthenticatedException, NotObservableException {
- if (isGod())
- return;
-
- assertSecure( currentPlayer().canObserve( location ).isTrue(), //
- NotObservableException.class, location );
+ Maybe<? extends IPlayer> owner = observable.checkOwner();
+ assertSecure( owner.presence() == Maybe.Presence.PRESENT && ObjectUtils.equals( owner.get(), currentPlayer() ), //
+ NotOwnedException.class, observable );
}
- public static void assertObservable(final IGameObject gameObject)
+ public static void assertObservable(final GameObservable observable)
throws NotAuthenticatedException, NotObservableException {
if (isGod())
return;
- assertSecure( gameObject.checkLocation().presence() == Maybe.Presence.PRESENT, //
- NotObservableException.class, gameObject );
+ assertSecure( currentPlayer().canObserve( observable ).isTrue(), //
+ NotObservableException.class, observable );
}
public static class NotAuthenticatedException extends OmicronSecurityException {
@@ -36,14 +36,23 @@
return new Unknown<>();
}
+ /**
+ * @return {@link Presence#ABSENT} if reference is null, otherwise {@link Presence#PRESENT}.
+ */
public static <T> Maybe<T> fromNullable(@Nullable final T reference) {
return reference == null? Maybe.<T>absent(): new Present<>( reference );
}
+ /**
+ * @return {@link Presence#ABSENT} if reference is {@link Optional#absent()}, otherwise {@link Presence#PRESENT}.
+ */
public static <T> Maybe<T> fromOptional(final Optional<T> reference) {
return reference.isPresent()? new Present<>( reference.get() ): Maybe.<T>absent();
}
+ /**
+ * @return Always {@link Presence#PRESENT}.
+ */
public static <T> Maybe<T> of(@Nonnull final T reference) {
return new Present<>( reference );
}
@@ -1,5 +1,8 @@
package com.lyndir.omicron.api.util;
+import javax.annotation.Nullable;
+
+
/**
* @author lhunath, 2013-08-17
*/
@@ -40,4 +43,18 @@ public boolean isKnown() {
public abstract boolean isTrue();
public abstract boolean isKnown();
+
+ /**
+ * @return YES if value is true, NO if value is false.
+ */
+ public static Maybool from(final boolean value) {
+ return value? YES: NO;
+ }
+
+ /**
+ * @return UNKNOWN if value is null, YES if value is true, NO if value is false.
+ */
+ public static Maybool fromNullable(@Nullable final Boolean value) {
+ return value == null? UNKNOWN : value? YES: NO;
+ }
}
@@ -1,7 +1,5 @@
package com.lyndir.omicron.cli.command;
-import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*;
-
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.lyndir.omicron.api.model.*;
@@ -70,7 +68,7 @@ public void objects(final Iterator<String> tokens) {
for (final IGameObject gameObject : gameObjectBuilder.build()) {
ITile location = gameObject.checkLocation().get();
inf( "%5s | %20s | (%7s: %3d, %3d) | %s", //
- gameObject.getObjectID(), ifNotNullElse( IPlayer.class, gameObject.getOwner().orNull(), "-" ).getName(),
+ gameObject.getObjectID(), ifNotNullElse( IPlayer.class, gameObject.checkOwner().orNull(), "-" ).getName(),
location.getLevel().getType().getName(), location.getPosition().getX(), location.getPosition().getY(),
gameObject.getType().getTypeName() );
}
Oops, something went wrong.

0 comments on commit d79c73f

Please sign in to comment.