/src/test/java/com/jcloisterzone/integration/IntegrationTest.java

http://github.com/farin/JCloisterZone · Java · 80 lines · 69 code · 11 blank · 0 comment · 6 complexity · 90a0859bb13e8a799abfae1e05688ed5 MD5 · raw file

  1. package com.jcloisterzone.integration;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.net.URISyntaxException;
  6. import java.net.URL;
  7. import com.google.gson.stream.JsonReader;
  8. import com.jcloisterzone.config.Config;
  9. import com.jcloisterzone.game.GameSetup;
  10. import com.jcloisterzone.game.GameStatePhaseReducer;
  11. import com.jcloisterzone.game.PlayerSlot;
  12. import com.jcloisterzone.game.PlayerSlot.SlotState;
  13. import com.jcloisterzone.game.phase.Phase;
  14. import com.jcloisterzone.game.save.SavedGame;
  15. import com.jcloisterzone.game.save.SavedGame.SavedGamePlayerSlot;
  16. import com.jcloisterzone.game.save.SavedGameParser;
  17. import com.jcloisterzone.game.state.GameState;
  18. import com.jcloisterzone.game.state.GameStateBuilder;
  19. import com.jcloisterzone.wsio.message.WsReplayableMessage;
  20. import com.jcloisterzone.wsio.message.WsSaltMessage;
  21. public class IntegrationTest {
  22. private PlayerSlot[] createPlayerSlots(SavedGame savedGame) {
  23. PlayerSlot[] slots = new PlayerSlot[savedGame.getSlots().size()];
  24. int idx = 0;
  25. for (SavedGamePlayerSlot sgSlot : savedGame.getSlots()) {
  26. slots[idx] = new PlayerSlot(idx);
  27. slots[idx].setNickname(sgSlot.getNickname());
  28. slots[idx].setSerial(sgSlot.getSerial());
  29. slots[idx].setAiClassName(sgSlot.getAiClassName());
  30. slots[idx].setState(SlotState.OWN);
  31. idx++;
  32. }
  33. return slots;
  34. }
  35. protected GameState createGameState(String savedGameFile) {
  36. Config config = new Config();
  37. SavedGameParser parser = new SavedGameParser();
  38. JsonReader reader;
  39. try {
  40. URL resource = getClass().getClassLoader().getResource(savedGameFile);
  41. if (resource == null) {
  42. return null;
  43. }
  44. else {
  45. reader = new JsonReader(new FileReader(new File(resource.toURI())));
  46. }
  47. } catch (FileNotFoundException | URISyntaxException e1) {
  48. throw new RuntimeException(e1);
  49. }
  50. SavedGame sg = parser.fromJson(reader);
  51. GameSetup setup = sg.getSetup().asGameSetup();
  52. PlayerSlot[] slots = createPlayerSlots(sg);
  53. GameStatePhaseReducer phaseReducer = new GameStatePhaseReducer(setup, sg.getInitialSeed());
  54. GameStateBuilder builder = new GameStateBuilder(setup, slots, config);
  55. builder.setGameAnnotations(sg.getAnnotations());
  56. GameState state = builder.createInitialState();
  57. Phase firstPhase = phaseReducer.getFirstPhase();
  58. state = builder.createReadyState(state);
  59. state = state.setPhase(firstPhase.getClass());
  60. state = phaseReducer.applyStepResult(firstPhase.enter(state));
  61. for (WsReplayableMessage msg : sg.getReplay()) {
  62. if (msg instanceof WsSaltMessage) {
  63. phaseReducer.getRandom().setSalt(((WsSaltMessage) msg).getSalt());
  64. }
  65. state = phaseReducer.apply(state, msg);
  66. }
  67. return state;
  68. }
  69. }