如何优化并使其更具可读性
How to optimize and do this more readeable
大家好,我有一个关于如何使它更干净、可重用和可读的问题。我有一些数据模型(遗物),这具有稀有性和水平。根据级别和稀有性,必须对其进行分组,以便稍后将其应用于某些数学计算。出于这个原因,我需要解析我所有的遗物并检查等级和稀有度并存储在 var 计数器中。
public double TotalGlobalBonus
{
get
{
float commonRarityMultiplier = 20;
float rareRarityMultiplier = 120;
float epicRarityMultiplier = 320;
float legendaryRarityMultiplier = 540;
int rarityCommonLevel1 = 0;
int rarityCommonLevel2 = 0;
int rarityCommonLevel3 = 0;
int rarityCommonLevel4 = 0;
int rarityCommonLevel5 = 0;
int rarityRareLevel1 = 0;
int rarityRareLevel2 = 0;
int rarityRareLevel3 = 0;
int rarityRareLevel4 = 0;
int rarityRareLevel5 = 0;
int rarityEpicLevel1 = 0;
int rarityEpicLevel2 = 0;
int rarityEpicLevel3 = 0;
int rarityEpicLevel4 = 0;
int rarityEpicLevel5 = 0;
int rarityLegendaryLevel1 = 0;
int rarityLegendaryLevel2 = 0;
int rarityLegendaryLevel3 = 0;
int rarityLegendaryLevel4 = 0;
int rarityLegendaryLevel5 = 0;
foreach (RelicModel relic in this.equipedRelics)
{
switch (relic.rarity)
{
case RarityType.COMMON:
switch (relic.Level)
{
case 1:
rarityCommonLevel1++;
break;
case 2:
rarityCommonLevel2++;
break;
case 3:
rarityCommonLevel3++;
break;
case 4:
rarityCommonLevel4++;
break;
case 5:
rarityCommonLevel5++;
break;
}
break;
case RarityType.RARE:
switch (relic.Level)
{
case 1:
rarityRareLevel1++;
break;
case 2:
rarityRareLevel2++;
break;
case 3:
rarityRareLevel3++;
break;
case 4:
rarityRareLevel4++;
break;
case 5:
rarityRareLevel5++;
break;
}
break;
case RarityType.EPIC:
switch (relic.Level)
{
case 1:
rarityEpicLevel1++;
break;
case 2:
rarityEpicLevel2++;
break;
case 3:
rarityEpicLevel3++;
break;
case 4:
rarityEpicLevel4++;
break;
case 5:
rarityEpicLevel5++;
break;
}
break;
case RarityType.LEGENDARY:
{
switch (relic.Level)
{
case 1:
rarityLegendaryLevel1++;
break;
case 2:
rarityLegendaryLevel2++;
break;
case 3:
rarityLegendaryLevel3++;
break;
case 4:
rarityLegendaryLevel4++;
break;
case 5:
rarityLegendaryLevel5++;
break;
}
break;
}
}
}
double common = (commonRarityMultiplier / 100) * (rarityCommonLevel1 * 1 + rarityCommonLevel2 * 5 +
rarityCommonLevel3 * 10 + rarityCommonLevel4 * 20 +
rarityCommonLevel5 * 40);
double rare = (rareRarityMultiplier / 100) * (rarityRareLevel1 * 1 + rarityRareLevel2 * 5 +
rarityRareLevel3 * 10 + rarityRareLevel4 * 20 +
rarityRareLevel5 * 40);
double epic = (epicRarityMultiplier / 100) * (rarityEpicLevel1 * 1 + rarityEpicLevel2 * 5 +
rarityEpicLevel3 * 10 + rarityEpicLevel4 * 20 +
rarityEpicLevel5 * 40);
double legendary = (legendaryRarityMultiplier / 100) * (rarityLegendaryLevel1 * 1 + rarityLegendaryLevel2 * 5 +
rarityLegendaryLevel3 * 10 + rarityLegendaryLevel4 * 20 +
rarityLegendaryLevel5 * 40);
double final = common + rare + epic + legendary;
return final;
}
}
它真的很长 属性 并且会随着关卡数量的增加而增加,所以这不是精确可扩展的
您需要将所有这些值放在 Dictionary
中,键类型为 RarityType
,值类型为 int[]
.
var rarity = new Dictionary<RarityType, int[]>();
rarity[RarityType.COMMON] = new int[6];
rarity[RarityType.RARE] = new int[6];
rarity[RarityType.EPIC] = new int[6];
rarity[RarityType.LEGENDARY] = new int[6];
foreach (RelicModel relic in this.equipedRelics)
{
rarity[relic.rarity][relic.Level]++;
}
每个数组的第一个元素,即索引 = 0 的元素,将不被使用。
大家好,我有一个关于如何使它更干净、可重用和可读的问题。我有一些数据模型(遗物),这具有稀有性和水平。根据级别和稀有性,必须对其进行分组,以便稍后将其应用于某些数学计算。出于这个原因,我需要解析我所有的遗物并检查等级和稀有度并存储在 var 计数器中。
public double TotalGlobalBonus
{
get
{
float commonRarityMultiplier = 20;
float rareRarityMultiplier = 120;
float epicRarityMultiplier = 320;
float legendaryRarityMultiplier = 540;
int rarityCommonLevel1 = 0;
int rarityCommonLevel2 = 0;
int rarityCommonLevel3 = 0;
int rarityCommonLevel4 = 0;
int rarityCommonLevel5 = 0;
int rarityRareLevel1 = 0;
int rarityRareLevel2 = 0;
int rarityRareLevel3 = 0;
int rarityRareLevel4 = 0;
int rarityRareLevel5 = 0;
int rarityEpicLevel1 = 0;
int rarityEpicLevel2 = 0;
int rarityEpicLevel3 = 0;
int rarityEpicLevel4 = 0;
int rarityEpicLevel5 = 0;
int rarityLegendaryLevel1 = 0;
int rarityLegendaryLevel2 = 0;
int rarityLegendaryLevel3 = 0;
int rarityLegendaryLevel4 = 0;
int rarityLegendaryLevel5 = 0;
foreach (RelicModel relic in this.equipedRelics)
{
switch (relic.rarity)
{
case RarityType.COMMON:
switch (relic.Level)
{
case 1:
rarityCommonLevel1++;
break;
case 2:
rarityCommonLevel2++;
break;
case 3:
rarityCommonLevel3++;
break;
case 4:
rarityCommonLevel4++;
break;
case 5:
rarityCommonLevel5++;
break;
}
break;
case RarityType.RARE:
switch (relic.Level)
{
case 1:
rarityRareLevel1++;
break;
case 2:
rarityRareLevel2++;
break;
case 3:
rarityRareLevel3++;
break;
case 4:
rarityRareLevel4++;
break;
case 5:
rarityRareLevel5++;
break;
}
break;
case RarityType.EPIC:
switch (relic.Level)
{
case 1:
rarityEpicLevel1++;
break;
case 2:
rarityEpicLevel2++;
break;
case 3:
rarityEpicLevel3++;
break;
case 4:
rarityEpicLevel4++;
break;
case 5:
rarityEpicLevel5++;
break;
}
break;
case RarityType.LEGENDARY:
{
switch (relic.Level)
{
case 1:
rarityLegendaryLevel1++;
break;
case 2:
rarityLegendaryLevel2++;
break;
case 3:
rarityLegendaryLevel3++;
break;
case 4:
rarityLegendaryLevel4++;
break;
case 5:
rarityLegendaryLevel5++;
break;
}
break;
}
}
}
double common = (commonRarityMultiplier / 100) * (rarityCommonLevel1 * 1 + rarityCommonLevel2 * 5 +
rarityCommonLevel3 * 10 + rarityCommonLevel4 * 20 +
rarityCommonLevel5 * 40);
double rare = (rareRarityMultiplier / 100) * (rarityRareLevel1 * 1 + rarityRareLevel2 * 5 +
rarityRareLevel3 * 10 + rarityRareLevel4 * 20 +
rarityRareLevel5 * 40);
double epic = (epicRarityMultiplier / 100) * (rarityEpicLevel1 * 1 + rarityEpicLevel2 * 5 +
rarityEpicLevel3 * 10 + rarityEpicLevel4 * 20 +
rarityEpicLevel5 * 40);
double legendary = (legendaryRarityMultiplier / 100) * (rarityLegendaryLevel1 * 1 + rarityLegendaryLevel2 * 5 +
rarityLegendaryLevel3 * 10 + rarityLegendaryLevel4 * 20 +
rarityLegendaryLevel5 * 40);
double final = common + rare + epic + legendary;
return final;
}
}
它真的很长 属性 并且会随着关卡数量的增加而增加,所以这不是精确可扩展的
您需要将所有这些值放在 Dictionary
中,键类型为 RarityType
,值类型为 int[]
.
var rarity = new Dictionary<RarityType, int[]>();
rarity[RarityType.COMMON] = new int[6];
rarity[RarityType.RARE] = new int[6];
rarity[RarityType.EPIC] = new int[6];
rarity[RarityType.LEGENDARY] = new int[6];
foreach (RelicModel relic in this.equipedRelics)
{
rarity[relic.rarity][relic.Level]++;
}
每个数组的第一个元素,即索引 = 0 的元素,将不被使用。