@BeforeMethod public void setUpMethod() throws Exception { itemBean = new ItemBean(); setField(ItemBean.class, "logBean", itemBean, logBean); itemDef = new ItemDefinition(); itemDef.setId(1); itemDef.setName("ring"); itemDef.setAdject1("nice"); itemDef.setAdject2("golden"); itemDef.setAdject3("friendship"); itemDef.setCopper(5); ring = new NormalItem(); ring.setItemDefinition(itemDef); room1 = new Room(); room1.setId(1); room1.setContents("You are in a small room."); karn = new User(); karn.setName("Karn"); karn.setRoom(room1); File file = new File(Constants.getMudfilepath() + File.separator + "Karn.log"); PrintWriter writer = new PrintWriter(file); writer.close(); }
@Override public DisplayInterface run(String command, User aUser) throws MudException { for (Wielding position : Wielding.values()) { aUser.wield(null, position); } aUser.getRoom().sendMessageExcl(aUser, "%SNAME disarms %SHIMHERself completely.<br/>\r\n"); aUser.writeMessage("You disarm yourself completely.<br/>\r\n"); return aUser.getRoom(); }
@Test public void buyOk() throws NamingException { Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); setField(Person.class, "copper", karn, 1111); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); new Expectations() // an "expectation block" { { commandRunner.getItemBean(); result = itemBean; } }; DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat( log, equalTo("You buy a nice, golden, friendship ring from Karcas for 5 copper coins.<br />\n")); }
@Test public void buyItemNotWorthAnything() throws NamingException { ring.getItemDefinition().setCopper(0); Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); new Expectations() // an "expectation block" { { commandRunner.getItemBean(); result = itemBean; } }; DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat( log, equalTo( "Karcas says [to you] : That item is not worth anything.<br />\nYou did not buy anything.<br />\n")); }
@Test public void buyIllegalAmount() { User karcas = new User(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy -2 ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("That is an illegal amount.<br />\n")); }
@Test public void buyToUnknownShopkeeper() { HashSet<Item> items = new HashSet<>(); items.add(ring); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("Unable to locate shopkeeper.<br />\n")); }
@Test public void buyItemShopkeeperDoesNotHave() { Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy brush from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("The shopkeeper doesn't have that.<br />\n")); }
/** * Tries out the sell command. There are a couple of requirements that need to be met, before a * successful sale takes place. * * <ol> * <li>command struct. should be "<I>sell [<amount>] <item> to * <character></I>", for example: "<I>sell gold ring to Karcas</I>". * <li>keeper buying the item should * <ol> * <li>exist, * <li>be in the same room and * <li>have a god==4 to indicate a "keeper" and * <li>has enough money * </ol> * <li>the customer should have the item * <li>the item is not being wielded * <li>the item is not being worn * <li>the item itself should <I>NOT</I> have a attribute called "notsellable". * <li>the item should not contain any items * </ol> * * A best effort is tried, this means the following sequence of events: * * <ol> * <li>the item is transferred into the inventory of the keeper * <li>money is transferred into the inventory of the customer * <li>continue with next item * </ol> * * @param command the command entered. * @param aUser the character doing the selling. * @return a simple displayinterface, in our case the room. */ @Override public DisplayInterface run(String command, User aUser) throws MudException { List<String> parsed = new ArrayList<>(Arrays.asList(parseCommand(command))); parsed.remove(0); // remove "sell" String shopkeeperName = parsed.get(parsed.size() - 1); parsed.remove(parsed.size() - 1); // remove shopkeeper parsed.remove(parsed.size() - 1); // remove "to" int amount = 1; try { amount = Integer.parseInt(parsed.get(0)); parsed.remove(0); } catch (NumberFormatException e) { // do nothing here, we assume we need to drop only one item. } if (amount <= 0) { aUser.writeMessage("That is an illegal amount.<br/>\n"); return aUser.getRoom(); } // find the item on ourselves List<Item> itemsFound = aUser.findItems(parsed); if (itemsFound.isEmpty()) { aUser.writeMessage("You don't have that.<br/>\n"); return aUser.getRoom(); } if (itemsFound.size() < amount) { aUser.writeMessage("You do not have that many items in your inventory.<br/>\r\n"); return aUser.getRoom(); } Person keeper = aUser.getRoom().getPerson(shopkeeperName); if (keeper == null) { aUser.writeMessage("Unable to locate shopkeeper.<br/>\r\n"); return aUser.getRoom(); } if (keeper.getGod() != God.SHOPKEEPER) { aUser.writeMessage("That's not a shopkeeper!<br/>\r\n"); return aUser.getRoom(); } Shopkeeper shopkeeper = (Shopkeeper) keeper; boolean sold = false; ItemBean itemBean = getItemBean(); for (Item item : itemsFound) { // item is not used. if (item.getCopper() <= 1) { String message = "That item is not worth anything."; aUser .getRoom() .sendMessage( shopkeeper, aUser, "%SNAME say%VERB2 [to %TNAME] : " + message + "<br/>\r\n"); continue; } if (shopkeeper.getCopper() < item.getCopper()) { aUser.writeMessage( shopkeeper.getName() + " mutters something about not having enough money.<br/>\r\n"); break; } if (!aUser.unused(item)) { aUser.writeMessage("You are wearing or wielding this item.<BR>\r\n"); continue; } if (!item.isSellable()) { aUser.writeMessage("You cannot sell that item.<BR>\r\n"); continue; } Integer moneyPaid = itemBean.sell(item, aUser, shopkeeper); if (moneyPaid == null) { continue; } aUser .getRoom() .sendMessage( aUser, "%SNAME sold%VERB2 " + item.getDescription() + " to " + shopkeeper.getName() + " for " + Constants.getDescriptionOfMoney(moneyPaid) + ".<br/>\r\n"); sold = true; amount--; if (amount == 0) { return aUser.getRoom(); } } if (!sold) { aUser.writeMessage("You did not sell anything.<br/>\r\n"); } else { aUser.writeMessage("You sold some of the items.<br/>\r\n"); } return aUser.getRoom(); }