Skip to content

Commit

Permalink
#56 Started to do NewsFactory to create actual news into game.
Browse files Browse the repository at this point in the history
Adds also JUnit for it.t
  • Loading branch information
tuomount committed Jul 24, 2017
1 parent 06d606d commit 7b768f0
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.openRealmOfStars.starMap.newsCorp;

import org.openRealmOfStars.player.PlayerInfo;
import org.openRealmOfStars.starMap.planet.Planet;
import org.openRealmOfStars.utilities.DiceGenerator;

/**
*
* Open Realm of Stars game project
* Copyright (C) 2017 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/
*
*
* News Factory to create news data about special events in game.
*
*/
public final class NewsFactory {

/**
* Just hiding the constructor
*/
private NewsFactory() {
// Nothing to do here
}

/**
* Make War news. Aggressor makes war declaration to defender.
* This diplomatic meeting happened in meeting place which
* can be planet or fleet.
* @param aggressor Player who is declaring the war
* @param defender Player who is defending
* @param meetingPlace Where meeting happened, fleet or planet
* @return NewsData
*/
public static NewsData makeWarNews(final PlayerInfo aggressor,
final PlayerInfo defender, final Object meetingPlace) {
NewsData news = new NewsData();
ImageInstruction instructions = new ImageInstruction();
instructions.addBackground(ImageInstruction.BACKGROUND_NEBULAE);
if (meetingPlace instanceof Planet) {
Planet planet = (Planet) meetingPlace;
instructions.addPlanet(ImageInstruction.POSITION_CENTER,
Planet.PLANET_NEWS_INSTRUCTIONS[planet.getPlanetImageIndex()],
ImageInstruction.SIZE_FULL);
}
switch (DiceGenerator.getRandom(2)) {
case 0:
default: {
instructions.addText("WAR DECLARATION!");
break;
}
case 1: {
instructions.addText("WAR!");
break;
}
case 2: {
instructions.addText("WAR BEGINS!");
break;
}
}
instructions.addText(aggressor.getEmpireName());
instructions.addRelationSymbol(ImageInstruction.WAR);
instructions.addText(defender.getEmpireName());
news.setImageInstructions(instructions.build());
return news;
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/openRealmOfStars/starMap/planet/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openRealmOfStars.player.ship.Ship;
import org.openRealmOfStars.player.ship.ShipStat;
import org.openRealmOfStars.starMap.Coordinate;
import org.openRealmOfStars.starMap.newsCorp.ImageInstruction;
import org.openRealmOfStars.starMap.planet.construction.Building;
import org.openRealmOfStars.starMap.planet.construction.BuildingType;
import org.openRealmOfStars.starMap.planet.construction.Construction;
Expand Down Expand Up @@ -69,6 +70,13 @@ public class Planet {
GuiStatics.BIG_PLANET_ROCK1, GuiStatics.BIG_PLANET_WATERWORLD1,
GuiStatics.BIG_PLANET_WATERWORLD2, GuiStatics.BIG_PLANET_IRONPLANET1,
GuiStatics.BIG_PLANET_IRONPLANET2 };
/**
* List of planet image instructions used in news.
*/
public static final String[] PLANET_NEWS_INSTRUCTIONS = {
ImageInstruction.PLANET_ROCK1, ImageInstruction.PLANET_WATERWORLD1,
ImageInstruction.PLANET_WATERWORLD2, ImageInstruction.PLANET_IRONWORLD1,
ImageInstruction.PLANET_IRONWORLD2};

/**
* List of big planet images
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openRealmOfStars.starMap.newsCorp;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
import org.openRealmOfStars.player.PlayerInfo;

/**
*
* Open Realm of Stars game project
* Copyright (C) 2017 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/
*
*
* News Factory Tests
*
*/
public class NewsFactoryTest {

@Test
@Category(org.openRealmOfStars.UnitTest.class)
public void testWar() {
PlayerInfo aggressor = Mockito.mock(PlayerInfo.class);
Mockito.when(aggressor.getEmpireName()).thenReturn("Empire of Test");
PlayerInfo defender = Mockito.mock(PlayerInfo.class);
Mockito.when(defender.getEmpireName()).thenReturn("Democracy of Defender");
NewsData news = NewsFactory.makeWarNews(aggressor, defender, null);
assertEquals(true, news.getImageInstructions().contains(
aggressor.getEmpireName()));
assertEquals(true, news.getImageInstructions().contains(
defender.getEmpireName()));
}

}

0 comments on commit 7b768f0

Please sign in to comment.