Break stone counter. FIX AVATAR DEFEAT ANIMATION AT LAST!!!cos0!!
[troll.git] / src / main / java / com / codingame / game / Referee.java
1 package com.codingame.game;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Random;
7
8 import com.codingame.gameengine.core.AbstractPlayer.TimeoutException;
9 import com.codingame.gameengine.core.AbstractReferee;
10 import com.codingame.gameengine.core.GameManager;
11 import com.codingame.gameengine.core.MultiplayerGameManager;
12 import com.codingame.gameengine.module.entities.GraphicEntityModule;
13 import com.codingame.gameengine.module.entities.Rectangle;
14 import com.codingame.gameengine.module.entities.Sprite;
15 import com.codingame.gameengine.module.entities.Text;
16 import com.codingame.gameengine.module.entities.Curve;
17 import com.google.inject.Inject;
18 import com.google.inject.Provider;
19
20 public class Referee extends AbstractReferee {
21     @Inject private MultiplayerGameManager<Player> gameManager;
22     @Inject private GraphicEntityModule graphicEntityModule;
23
24     @Inject private View view;
25     @Inject private Model model;
26
27     @Override
28     public void init() {
29         model.init(gameManager.getSeed());
30         gameManager.getPlayer(0).model = model.p0;
31         gameManager.getPlayer(1).model = model.p1;
32
33         for (Player p : gameManager.getPlayers()) {
34             p.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
35         }
36
37         view.init(model);
38         gameManager.getPlayer(0).view = view.p0;
39         gameManager.getPlayer(1).view = view.p1;
40
41         gameManager.setFrameDuration(2000); // XXX
42     }
43
44     @Override
45     public void gameTurn(int turn) {
46         // System.err.println("Starting turn " + turn);
47
48         boolean disqual = false;
49         boolean victory = false;
50         boolean exhausted = false;
51
52         int delta = 0;
53         for (Player player : gameManager.getActivePlayers()) {
54             Model.Player p = player.model;
55             {
56                 int trollDistance = p.getTrollDistance();
57                 int stones = p.getStones();
58                 int oppStones = p.getOppStones();
59                 player.sendInputLine(String.format("%d %d %d", trollDistance, stones, oppStones));
60             }
61             player.execute();
62         }
63
64         for (Player player : gameManager.getActivePlayers()) {
65             Model.Player p = player.model;
66
67             try {
68                 int stones = player.getAction();
69                 if (stones == 0 && p.getStones() > 0) {
70                     if (model.random.nextInt(10) > 0) {
71                         gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones.  Fixing that for them because I'm in a good mood today."));
72                         stones = 1;
73                     }
74                     else {
75                         throw new InvalidAction("tried not throwing any stone.  They were then eaten by a grue.");
76                     }
77                 }
78                 p.consumeStones(stones);
79                 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
80                 delta += player.model.getMultiplier() * stones;
81
82                 if (stones < 0) {
83                     player.deactivate(player.getNicknameToken() + " CHEAT");
84                     gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated.  Banning account."));
85                     player.setScore(-1);
86                     disqual = true;
87                 }
88                 else if (stones > 0) {
89                     player.view.animateStones(stones);
90                 }
91             }
92             catch (InvalidAction e) {
93                 player.deactivate(player.getNicknameToken() + " INVALID");
94                 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
95                 player.setScore(-1);
96                 disqual = true;
97             }
98             catch (NumberFormatException e) {
99                 player.deactivate(player.getNicknameToken() + " ERROR");
100                 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
101                 player.setScore(-1);
102                 disqual = true;
103             }
104             catch (TimeoutException e) {
105                 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
106                 player.deactivate(player.getNicknameToken() + " T/O");
107                 player.setScore(-1);
108                 disqual = true;
109             }
110
111             player.view.displayMessage(player.getMessageString());
112         }
113
114         if (! disqual) {
115             if (delta > 0) {
116                 gameManager.addToGameSummary("Troll walks right.");
117                 model.trollPosition++;
118             }
119             else if (delta < 0) {
120                 gameManager.addToGameSummary("Troll walks left.");
121                 model.trollPosition--;
122             }
123             else {
124                 gameManager.addToGameSummary("Troll stands still.");
125                 // XXX animate
126             }
127             view.moveTroll();
128
129             for (Player player : gameManager.getActivePlayers()) {
130                 player.model.adjustScore(model.trollPosition);
131             }
132
133             if (model.haveWinner()) {
134                 int winner = model.getWinner();
135                 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + gameManager.getPlayer(winner).getNicknameToken()));
136                 victory = true;
137             }
138             else if (model.exhausted()) exhausted = true;
139         }
140
141         if (disqual || victory || exhausted) endGame();
142     }
143
144     private void endGame() {
145         gameManager.endGame();
146
147         Player p0 = gameManager.getPlayer(0);
148         Player p1 = gameManager.getPlayer(1);
149
150         int s0 = p0.getScore();
151         int s1 = p1.getScore();
152
153         if (s0 > s1) {
154             gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
155             p1.view.destroy();
156         }
157         else if (s0 < s1) {
158             gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
159             p0.view.destroy();
160         }
161         else if (s0 < 0) {
162             gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
163             p0.view.destroy();
164             p1.view.destroy();
165         }
166         else {
167             gameManager.addToGameSummary("Draw.");
168         }
169     }
170 }