View Javadoc
1   package de.aikiit.game.kaiser;
2   
3   import org.apache.commons.lang3.StringUtils;
4   
5   import java.math.BigDecimal;
6   
7   /**
8    * This class contains static helper methods to print
9    * results or calculation or status information of the currently running game.
10   */
11  public class KaiserEnginePrinter {
12  
13      // inspired by https://talyian.github.io/ansicolors/
14      /**
15       * Reset anything in console.
16       */
17      public static final String ANSI_RESET = "\u001B[0m";
18      /**
19       * Colour BLACK.
20       */
21      public static final String ANSI_BLACK = "\u001B[30m";
22      /**
23       * Colour RED.
24       */
25      public static final String ANSI_RED = "\u001B[31m";
26      /**
27       * Colour GREEN.
28       */
29      public static final String ANSI_GREEN = "\u001B[32m";
30      /**
31       * Colour YELLOW.
32       */
33      public static final String ANSI_YELLOW = "\u001B[33m";
34      /**
35       * Colour BLUE.
36       */
37      public static final String ANSI_BLUE = "\u001B[34m";
38      /**
39       * Colour PURPLE.
40       */
41      public static final String ANSI_PURPLE = "\u001B[35m";
42      /**
43       * Colour CYAN.
44       */
45      public static final String ANSI_CYAN = "\u001B[36m";
46      /**
47       * Colour WHITE.
48       */
49      public static final String ANSI_WHITE = "\u001B[37m";
50      /**
51       * Colour ORANGE.
52       */
53      public static final String ORANGE = "\u001B[38;5;202m";
54  
55      private final KaiserEngine engine;
56  
57      public KaiserEnginePrinter(KaiserEngine engine) {
58          this.engine = engine;
59      }
60  
61      /**
62       * Show status information of the underlying game engine for the given round.
63       *
64       * @param round current round/year of the game.
65       * @return colour-encoded status.
66       */
67      public String getStatus(int round) {
68          StringBuilder status = new StringBuilder();
69          status.append(ANSI_GREEN).append("### STATUS nach Runde ").append(round).append(ANSI_RESET).append(System.lineSeparator());
70          status.append(ANSI_CYAN).append("Die Einwohnerzahl beträgt jetzt   ").append(StringUtils.leftPad(String.valueOf(this.engine.getPopulation()), 10)).append(ANSI_RESET).append(System.lineSeparator());
71          status.append(ANSI_CYAN).append("Die Stadt besitzt an Land (Hektar)").append(StringUtils.leftPad(String.valueOf(this.engine.getLandArea()), 10)).append(ANSI_RESET).append(System.lineSeparator());
72          status.append(ANSI_CYAN).append("Die Ernte betrug (dt/Hektar)      ").append(StringUtils.leftPad(String.valueOf(this.engine.getHarvestYield()), 10)).append(ANSI_RESET).append(System.lineSeparator());
73          status.append(ANSI_RED).append("Ratten haben gefressen (dt)       ").append(StringUtils.leftPad(String.valueOf(this.engine.getRatLoss()), 10)).append(ANSI_RESET).append(System.lineSeparator());
74          status.append(ANSI_RED).append("Eure Vorräte betragen (dt)        ").append(StringUtils.leftPad(String.valueOf(this.engine.getGrainStocks()), 10)).append(ANSI_RESET).append(System.lineSeparator());
75          return status.toString();
76      }
77  
78      /**
79       * Show population result of the current round.
80       *
81       * @param round current round/year of the game.
82       * @return colour-encoded population statistics.
83       */
84      public String getYearResult(int round) {
85          return ANSI_GREEN + "Im Jahr " + round + ANSI_RESET + System.lineSeparator() +
86                  ANSI_CYAN + "Es starben         " + StringUtils.leftPad(String.valueOf(this.engine.getStarvationDeaths()), 10) + " Einwohner" + ANSI_RESET + System.lineSeparator() +
87                  ANSI_CYAN + "hinzugekommen sind " + StringUtils.leftPad(String.valueOf(this.engine.getImmigrants()), 10) + " Einwohner" + ANSI_RESET + System.lineSeparator();
88      }
89  
90      /**
91       * After the game ends and the maximum number of years is reached an overall statistics is printed out.
92       *
93       * @return colour-encoded overall statistics.
94       */
95      public String getResults() {
96          return "In Ihrer " + KaiserGame.MAX_ROUNDS + "-jährigen Amtszeit sind durchschnittlich " +
97                  format(this.engine.getAverageStarvationPercentage()) + " % im Jahr verstorben." + System.lineSeparator() +
98                  "Insgesamt sind damit " + this.engine.getTotalStarvationDeaths() + " Einwohner verstorben." + System.lineSeparator() +
99                  System.lineSeparator() +
100                 "Anfangs hatten Sie 10 Hektar pro Einwohner, jetzt haben Sie " + format(this.engine.getLandPerPerson()) + " Hektar/Einwohner." + System.lineSeparator();
101     }
102 
103     /**
104      * Depending on global rules the player's legacy is described/classified depending on the engine's results.
105      *
106      * @return colour-encoded legacy/regency output.
107      */
108     public String evaluateRegency() {
109         StringBuilder result = new StringBuilder();
110         result.append(ANSI_BLUE).append(">>>> Bewertung Ihrer Herrschaft <<<<").append(System.lineSeparator());
111 
112         switch (engine.getRegency()) {
113             case DISASTROUS -> result.append(dismissalMessage());
114             case TYRANNICAL -> result.append("Ihre hartherzige Regierungsmethode erinnert an Nero und Iwan den Schrecklichen.")
115                     .append(System.lineSeparator())
116                     .append("Die (verbliebenen) Einwohner würden Sie gerne zum Teufel jagen!");
117             case MEDIOCRE -> result.append("Sie hätten glücklicher agieren können, aber es war nicht übel.")
118                     .append(System.lineSeparator())
119                     .append(engine.countDiscontentedResidents())
120                     .append(" Einwohner möchten Sie zwar hängen sehen, aber kleine Probleme hat ja jeder.");
121             case EXCELLENT -> result.append("Eine wunderbare Regierungszeit! Karl, Konrad und Willi (die Großen) hätten es nicht besser machen können.");
122         }
123 
124         result.append(ANSI_RESET);
125         result.append(System.lineSeparator());
126         result.append(System.lineSeparator());
127         return result.toString();
128     }
129 
130     public String getDismissal() {
131         return ANSI_YELLOW + "Sie haben " + engine.getStarvationDeaths()
132                 + " Menschen in nur einem Jahr verhungern lassen!" + System.lineSeparator()
133                 + dismissalMessage() + ANSI_RESET;
134     }
135 
136     private static String dismissalMessage() {
137         return "Auf Grund dieser extremen Misswirtschaft werden Sie nicht nur aus Amt und Würden gejagt,"
138                 + System.lineSeparator() + "sondern auch zum Versager des Jahres erklärt.";
139     }
140 
141     private static String format(BigDecimal value) {
142         return value.stripTrailingZeros().toPlainString();
143     }
144 }