Skip to content

Commit

Permalink
fixed craft test
Browse files Browse the repository at this point in the history
  • Loading branch information
blksmithchain committed Jun 22, 2023
1 parent 8c888c3 commit 9cbc2bf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
50 changes: 40 additions & 10 deletions Assets/Scripts/UI/CraftingUI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void DisplayNextRollSprite()
currentRollSpriteIndex = (currentRollSpriteIndex + 1) % rollResults.rollSprites.Length;
Transform nftImage = rollSlots[0].transform.Find("NFT_Image");
nftImage.GetComponent<Image>().sprite = rollResults.rollSprites[currentRollSpriteIndex];
rollNameText.text = rollResults.rollNames[currentRollSpriteIndex];
SetRollNameText(rollResults.rollNames[currentRollSpriteIndex]);
float rollPercentage = (float)rollResults.rollPercentageRolls[currentRollSpriteIndex] / rollResults.totalOdds * 100;
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -98,7 +99,7 @@ public void DisplayPreviousRollSprite()
}
Transform nftImage = rollSlots[0].transform.Find("NFT_Image");
nftImage.GetComponent<Image>().sprite = rollResults.rollSprites[currentRollSpriteIndex];
rollNameText.text = rollResults.rollNames[currentRollSpriteIndex];
SetRollNameText(rollResults.rollNames[currentRollSpriteIndex]);
float rollPercentage = ((float)rollResults.rollPercentageRolls[currentRollSpriteIndex] / rollResults.totalOdds * 100);
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -117,7 +118,7 @@ public void DisplayRollData(RollResult rollResult)

if (rollResult.rollSprites != null && rollResult.rollSprites.Length > 0)
{
rollNameText.text = rollResults.rollNames[0];
SetRollNameText(rollResults.rollNames[0]);
float rollPercentage = ((float)rollResults.rollPercentageRolls[0] / rollResults.totalOdds * 100);
rollPercentage = (float)Math.Round(rollPercentage, 1);
SetRollPercentageText(rollPercentage.ToString());
Expand All @@ -135,7 +136,7 @@ public async void DisplayRollImage(RollResult rollResult)

}
}
public void DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, IndexIngredientAssetsResult indexIngredientAssetsResult)
public async Task DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, IndexIngredientAssetsResult indexIngredientAssetsResult)
{
int totalRequiredAssets = requiredAssetResult.requiredAssetAmount.Sum();
InstantiateRequirementSlots(totalRequiredAssets);
Expand All @@ -159,17 +160,25 @@ public void DisplayRequirementsData(RequiredAssetsResult requiredAssetResult, In

}
}
SortAndSelectAssetsInRequirementSlots(requirementSlots,indexIngredientAssetsResult);
var sortedRequirementSlots = SortAssets(requirementSlots);
SetupRequirements(sortedRequirementSlots, indexIngredientAssetsResult);
await LoadRequirementSlotImages(sortedRequirementSlots, indexIngredientAssetsResult);

uIController.ChangePrefabColor();


}

// Sort and select the assets in the requirement slots based on priority.
public async void SortAndSelectAssetsInRequirementSlots(GameObject[] requirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult)
// Method for sorting the assets based on priority.
public List<GameObject> SortAssets(GameObject[] requirementSlots)
{
string[] priorityOrder = { "TEMPLATE_INGREDIENT", "ATTRIBUTE_INGREDIENT", "SCHEMA_INGREDIENT", "COLLECTION_INGREDIENT" };
var sortedRequirementSlots = requirementSlots.OrderBy(slot => Array.IndexOf(priorityOrder, slot.GetComponent<TemplateNFT>().GetRequirementType())).ToList();
return sortedRequirementSlots;
}

public void SetupRequirements(List<GameObject> sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult)
{
List<string> selectedAssetIds = new List<string>();
foreach (var requirementSlot in sortedRequirementSlots)
{
Expand All @@ -183,7 +192,6 @@ public async void SortAndSelectAssetsInRequirementSlots(GameObject[] requirement
{
selectedAssetIds.Add(indexIngredientAssetsResult.assetIds[i]);
requirementSlot.GetComponent<RequirementUIElementController>().selectedAssetId = indexIngredientAssetsResult.assetIds[i];
requirementSlot.transform.Find("NFT_Image").GetComponent<Image>().sprite = await craftingFetcher.GetImageLoaderSpriteAsync(indexIngredientAssetsResult.ingredientSpriteHashes[i]); ;
requirementSlot.transform.Find("Selected_Ingredient_Background/SelectedIngredient").GetComponent<TextMeshProUGUI>().text = "Autoselected: # " + indexIngredientAssetsResult.mintNumbers[i];
break;
}
Expand All @@ -193,6 +201,28 @@ public async void SortAndSelectAssetsInRequirementSlots(GameObject[] requirement
}
}

public async Task LoadRequirementSlotImages(List<GameObject> sortedRequirementSlots, IndexIngredientAssetsResult indexIngredientAssetsResult)
{
List<string> selectedAssetIds = new List<string>();
foreach (var requirementSlot in sortedRequirementSlots)
{
if (requirementSlot.GetComponent<TemplateNFT>().GetRequirementType() != "FT_INGREDIENT")
{
for (var i = 0; i < indexIngredientAssetsResult.assetIds.Count; i++)
{
if (requirementSlot.GetComponent<TemplateNFT>().GetBlendIngredientIndex() == indexIngredientAssetsResult.indexId[i] && !selectedAssetIds.Contains(indexIngredientAssetsResult.assetIds[i]))
{
selectedAssetIds.Add(indexIngredientAssetsResult.assetIds[i]);
Sprite sprite = await craftingFetcher.GetImageLoaderSpriteAsync(indexIngredientAssetsResult.ingredientSpriteHashes[i]);
requirementSlot.transform.Find("NFT_Image").GetComponent<Image>().sprite = sprite;
break;
}
}
}
}
}


private void DisplayFTIngredientRequirement(RequiredAssetsResult requiredAssetResult, int currentRequirementSlotIndex, int i)
{
for (int j = 0; j < requiredAssetResult.requiredAssetAmount[i]; j++)
Expand Down Expand Up @@ -226,11 +256,11 @@ private void DisplayNFTIngredientRequirement(RequiredAssetsResult requiredAssetR
}
}

public void DisplayAssetImages(RequiredAssetsResult requiredAssetResult,IndexIngredientAssetsResult indexIngredientAssetsResult,RollResult rollResult,int securityId)
public async Task DisplayAssetImages(RequiredAssetsResult requiredAssetResult,IndexIngredientAssetsResult indexIngredientAssetsResult,RollResult rollResult,int securityId)
{
LoadDefaultRollImages(rollResult);
DisplayRollData(rollResult);
DisplayRequirementsData(requiredAssetResult, indexIngredientAssetsResult);
await DisplayRequirementsData(requiredAssetResult, indexIngredientAssetsResult);
DisplayRollPaginationArrows(rollResult.rollSprites);
DisplayBlendProtection(securityId);
LoadRollImages(rollResult);
Expand Down
32 changes: 21 additions & 11 deletions Assets/Tests/EditModeTests/CraftingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,39 @@ public void TearDown()
Object.DestroyImmediate(craftingUI.gameObject);
}

/* [Test]
[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithUnique_AssetIds()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
AssertRequirements(new string[] { "Asset1", "Asset2", "Asset3", "Asset4", "Asset5" });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithNoMatchingIngredients_ShouldNotSelectAnyAssets()
{
SetUpRequirements(new int[] { 6, 7, 8, 9, 10 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 5));
AssertRequirements(new string[] { null, null, null, null, null });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_WithMultipleMatchingIngredients_ShouldPrioritizeTemplateIngredient()
{
SetUpRequirements(new int[] { 1, 3, 4, 2, 0 }, new string[] { "ATTRIBUTE_INGREDIENT", "SCHEMA_INGREDIENT", "FT_INGREDIENT", "SCHEMA_INGREDIENT", "TEMPLATE_INGREDIENT" });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, new IndexIngredientAssetsResult()
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, new IndexIngredientAssetsResult()
{
assetIds = new List<string>() { "Asset1", "Asset2", "Asset3", "Asset4", "Asset5", "Asset1" },
indexId = new List<int>() { 0, 2, 3, 4, 0, 1 },
mintNumbers = new List<int>() { 0,0,0,0,0,0 }
mintNumbers = new List<int>() { 0, 0, 0, 0, 0, 0 }

});
AssertRequirements(new string[] { null, "Asset3", null, "Asset2", "Asset1" });
}

[Test]
public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpty()
{
Expand All @@ -71,7 +75,8 @@ public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpt
requirementSlots[0].GetComponent<TemplateNFT>().SetBlendIngredientIndex(0);

// Act
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, indexIngredientAssetsResult);
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, indexIngredientAssetsResult);

// Assert
Assert.IsNull(requirementSlots[0].transform.Find("Selected_Ingredient_Background/SelectedIngredient").GetComponent<TextMeshProUGUI>().text);
Expand All @@ -81,7 +86,8 @@ public void TestSortAndSelectAssetsInRequirementSlots_FT_INGREDIENT_ShouldBeEmpt
public void TestSortAndSelectAssetsInRequirementSlots_WithEmptyIngredient()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, new IndexIngredientAssetsResult()
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, new IndexIngredientAssetsResult()
{
assetIds = new List<string>() { },
indexId = new List<int>() { 1, 2, 3, 4, 5 },
Expand All @@ -95,9 +101,10 @@ public void TestSortAndSelectAssetsInRequirementSlots_WithEmptyIngredient()
public void TestSortAndSelectAssetsInRequirementSlots_WithLessIngredients_ShouldSelectAssetsForMatchingIndexes()
{
SetUpRequirements(new int[] { 1, 2, 3, 4, 5 });
craftingUI.SortAndSelectAssetsInRequirementSlots(requirementSlots, CreateIndexIngredientAssetsResult("Asset", 3));
var sortedRequirementSlots = craftingUI.SortAssets(requirementSlots);
craftingUI.SetupRequirements(sortedRequirementSlots, CreateIndexIngredientAssetsResult("Asset", 3));
AssertRequirements(new string[] { "Asset1", "Asset2", "Asset3", null, null });
}*/
}

private GameObject[] CreateSlots(int number)
{
Expand Down Expand Up @@ -144,18 +151,21 @@ private void AssertRequirements(string[] expectedTexts)

private IndexIngredientAssetsResult CreateIndexIngredientAssetsResult(string baseAssetName, int number)
{
IndexIngredientAssetsResult result = new IndexIngredientAssetsResult()
var result = new IndexIngredientAssetsResult()
{
assetIds = new List<string>(),
indexId = new List<int>(),
mintNumbers = new List<int>()
mintNumbers = new List<int>(),
ingredientSpriteHashes = new List<string>()

};

for (int i = 0; i < number; i++)
{
result.assetIds.Add($"{baseAssetName}{i + 1}");
result.indexId.Add(i + 1);
result.mintNumbers.Add(i);
result.ingredientSpriteHashes.Add("fakeimghash");
}

return result;
Expand Down

0 comments on commit 9cbc2bf

Please sign in to comment.