// Takes ItemIds of available objects, and itemId of target object // If targetItem can be made, return corresponding GameObjectEntry public GameObjectEntry GetBlueprint(List <RecipeElement> availableItems, int targetItemId) { // Obtain blueprint from targetItemId GameObjectEntry goe = GameObjs.items.Find(item => item.item_id == targetItemId); // ForEach item in the blueprint, check the required items are available foreach (RecipeElement item in goe.blueprint) { // Find recipe element in provided available elements RecipeElement available = availableItems.Find(itemToFind => itemToFind.item_id == item.item_id); // If item is present if (available != null) { // Is the required quantity available? if (available.quantity < item.quantity) { return(null); } } else { // If item is not present return(null); } } return(goe); }
public void TestSerializeSingleItem() { using (StreamReader r = new StreamReader(getFilepath(singleItemJson))) { string json = r.ReadToEnd(); GameObjectEntry entry = JsonUtility.FromJson <GameObjectEntry>(json); // Assert fields are correct Assert.That(entry.item_id, Is.EqualTo(1)); Assert.That(entry.name, Is.EqualTo("wood")); Assert.That(entry.type, Is.EqualTo(1)); } }
public void TestSerializeSingleItemWithList() { using (StreamReader r = new StreamReader(getFilepath(singleItemListJson))) { string json = r.ReadToEnd(); GameObjectEntry entry = JsonUtility.FromJson <GameObjectEntry>(json); // Assert fields are correct Assert.That(entry.item_id, Is.EqualTo(8)); Assert.That(entry.name, Is.EqualTo("steel")); Assert.That(entry.type, Is.EqualTo(4)); // Asserts recipe entry is correct Assert.That(entry.recipe[0].item_id, Is.EqualTo(4)); Assert.That(entry.recipe[0].quantity, Is.EqualTo(1)); } }
public void TestGetRecipePass() { // Serialize schema GameObjectsHandler goh = GameObjectsHandler.WithFilepath(getFilepath(itemSchemaV1)); // Create valid list of available objects List <RecipeElement> availables = new List <RecipeElement>(); availables.Add(new RecipeElement(4, 1)); availables.Add(new RecipeElement(5, 1)); // Obtain valid output GameObjectEntry goe = goh.GetRecipe(availables, 7); // Asserts Assert.That(goe.name, Is.EqualTo("steel")); }
public void TestGetSingleElementBlueprintFail() { // Serialize schema GameObjectsHandler goh = GameObjectsHandler.WithFilepath(getFilepath(itemSchemaV1)); // Create list of available objects, with too few values List <RecipeElement> availables = new List <RecipeElement>(); availables.Add(new RecipeElement(2, 6)); GameObjectEntry goe = goh.GetBlueprint(availables, 7); if (goe != null) { // Failure case // GetBlueprint returns non-null object where null expected Assert.Fail(); } }
public void TestGetSingleElementBlueprintPass() { // Serialize schema GameObjectsHandler goh = GameObjectsHandler.WithFilepath(getFilepath(itemSchemaV1)); // Create list of available objects List <RecipeElement> availables = new List <RecipeElement>(); availables.Add(new RecipeElement(2, 8)); try { GameObjectEntry goe = goh.GetBlueprint(availables, 7); //Assert values are correct Assert.That(goe.name, Is.EqualTo("furnace")); } catch (InvalidDataException e) { // Exception thrown, failure case Assert.Fail(); } }
public void TestGetRecipeFail() { // Serialize schema GameObjectsHandler goh = GameObjectsHandler.WithFilepath(getFilepath(itemSchemaV1)); // Create valid list of available objects List <RecipeElement> availables = new List <RecipeElement>(); availables.Add(new RecipeElement(5, 1)); // Obtain valid output GameObjectEntry goe = goh.GetRecipe(availables, 7); // Asserts if (goe != null) { // Failure case, no objects are valid. Hence goe should be null Assert.Fail(); } }