NPE hotfix
[troll.git] / src / main / java / com / codingame / game / Referee.java
index 4b1315b..7efe7b3 100644 (file)
@@ -21,6 +21,8 @@ public class Referee extends AbstractReferee {
     @Inject private MultiplayerGameManager<Player> gameManager;
     @Inject private GraphicEntityModule graphicEntityModule;
 
+    Random random;
+
     int roadLength;
     int initialStones;
     int trollPosition;
@@ -30,7 +32,8 @@ public class Referee extends AbstractReferee {
     
     @Override
     public void init() {
-        switch (new Random(gameManager.getSeed()).nextInt(4)) {
+        random = new Random(gameManager.getSeed());
+        switch (random.nextInt(4)) {
         case 0:
             roadLength = 7;
             initialStones = 15;
@@ -104,15 +107,7 @@ public class Referee extends AbstractReferee {
                     .setLineWidth(0)
                     .setFillColor(0xffffff);
 
-            Text text = graphicEntityModule.createText(player.getNicknameToken())
-                    .setX(x)
-                    .setY(y + 120)
-                    .setZIndex(20)
-                    .setFontSize(40)
-                    .setFillColor(0x7f3f00)
-                    .setAnchor(0.5);
-
-            Sprite avatar = graphicEntityModule.createSprite()
+            Sprite avatarSprite = graphicEntityModule.createSprite()
                     .setX(x)
                     .setY(y)
                     .setZIndex(20)
@@ -121,7 +116,17 @@ public class Referee extends AbstractReferee {
                     .setBaseHeight(116)
                     .setBaseWidth(116);
 
-            Text stoneCounter = graphicEntityModule.createText("S")
+            player.avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite);
+
+            Text text = graphicEntityModule.createText(player.getNicknameToken())
+                    .setX(x)
+                    .setY(y + 120)
+                    .setZIndex(20)
+                    .setFontSize(40)
+                    .setFillColor(0x7f3f00)
+                    .setAnchor(0.5);
+
+            player.stoneCounter = graphicEntityModule.createText("S")
                 .setX(x)
                 .setY(y+200)
                 .setZIndex(20)
@@ -129,13 +134,25 @@ public class Referee extends AbstractReferee {
                 .setFillColor(0x7f3f00)
                 .setAnchor(0.5);
 
+            player.message = graphicEntityModule.createText()
+                .setX(p0 ? 15 : 1920-15)
+                .setY(680)
+                .setZIndex(1)
+                .setFontSize(40)
+                .setStrokeColor(0x000000)
+                .setFillColor(0xffbf7f)
+                .setAnchorX(p0 ? 0 : 1)
+                .setAnchorY(1);
+
             player.castle = graphicEntityModule.createSprite()
                 .setImage("castle.png")
+                .setTint(player.getColorToken())
                 .setX(p0 ? 160 : 1920-160)
                 .setY(p0 ? 890 : 880)
                 .setZIndex(1)
                 .setAnchorX(0.5)
-                .setAnchorY(1);
+                .setAnchorY(1)
+                .setScaleX(p0 ? 1 : -1);
 
             player.stone = graphicEntityModule.createText()
                 .setZIndex(3)
@@ -143,9 +160,6 @@ public class Referee extends AbstractReferee {
                 .setFillColor(0x12322a)
                 .setAnchor(0.5)
                 .setAlpha(0);
-
-            player.hud = graphicEntityModule.createGroup(border1, border2, text, avatar, stoneCounter, player.castle);
-            player.stoneCounter = stoneCounter;
         }
     }
 
@@ -203,12 +217,27 @@ public class Referee extends AbstractReferee {
         int delta = 0;
         for (Player player : gameManager.getActivePlayers()) {
             try {
-                final int stones = player.getAction();
+                int stones = player.getAction();
+                if (stones == 0 && player.getStones() > 0) {
+                    if (random.nextInt(10) > 0) {
+                        gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones.  Fixing that for them because I'm in a good mood today."));
+                        stones = 1;
+                    }
+                    else {
+                        throw new InvalidAction("tried not throwing any stone.  They were then eaten by a grue.");
+                    }
+                }
                 player.consumeStones(stones);
                 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
                 delta += player.getMultiplier() * stones;
 
-                if (stones > 0) {
+                if (stones < 0) {
+                    player.deactivate(player.getNicknameToken() + " CHEAT");
+                    gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated.  Banning account."));
+                    player.setScore(-1);
+                    endGame();
+                }
+                else if (stones > 0) {
                     player.stone.setX(player.castle.getX());
                     player.stone.setY(player.castle.getY() - 100);
                     player.stone.setText(new Integer(stones).toString());
@@ -246,6 +275,11 @@ public class Referee extends AbstractReferee {
                 player.setScore(-1);
                 endGame();
             }
+
+            player.message
+                .setText(player.getMessageString());
+            //  .setAnchorX(/*player == p0 ? 0 : */ 1);
+            graphicEntityModule.commitEntityState(0, player.message);
         }
 
         if (delta > 0) {
@@ -269,23 +303,40 @@ public class Referee extends AbstractReferee {
                 endGame();
             }
         }
+
+        if (p0.getStones() <= 0 && p1.getStones() <= 0) {
+            endGame();
+        }
+    }
+
+    private void destroyPlayer(Player player) {
+        player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
+
+        graphicEntityModule.commitEntityState(0.5, player.castle);
+        player.castle.setX(player.castle.getX(), Curve.ELASTIC);
+        player.castle.setScaleY(-0.2, Curve.EASE_IN);
     }
 
     private void endGame() {
-        gameManager.endGame();
+        if (! gameManager.isGameEnd()) {
+            gameManager.endGame();
 
-        if (p0.getScore() > p1.getScore()) {
-            gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
-            p1.hud.setAlpha(0.3);
-        }
-        else if (p0.getScore() < p1.getScore()) {
-            gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
-            p0.hud.setAlpha(0.3);
-        }
-        else {
-            gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
-            p0.hud.setAlpha(0.3);
-            p1.hud.setAlpha(0.3);
+            if (p0.getScore() > p1.getScore()) {
+                gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
+                destroyPlayer(p1);
+            }
+            else if (p0.getScore() < p1.getScore()) {
+                gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
+                destroyPlayer(p0);
+            }
+            else if (p0.getScore() < 0) {
+                gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
+                destroyPlayer(p0);
+                destroyPlayer(p1);
+            }
+            else {
+                gameManager.addToGameSummary("Draw.");
+            }
         }
     }
 }