Skip to content

Commit

Permalink
Use loop instead of linq
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Jul 31, 2023
1 parent 720ebc7 commit e788fa8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Lib9c/Battle/StageSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ private void SetWave(StageSheet.Row stageRow, StageWaveSheet.Row stageWaveRow)
{
var enemyStatModifiers = stageRow.EnemyInitialStatModifiers;
var waves = stageWaveRow.Waves;
foreach (var wave in waves
.Select(e => SpawnWave(e, enemyStatModifiers)))
foreach (var waveData in waves)
{
var wave = SpawnWave(waveData, enemyStatModifiers);
_waves.Add(wave);
}
}
Expand Down
10 changes: 7 additions & 3 deletions Lib9c/Battle/WeightedSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public IEnumerable<T> Select(int count)
var result = new List<T>();
var weight = 0m;
var rnd = _random.Next(1, 100001) * 0.00001m;
var sum = _items.Sum(i => i.Weight);
decimal sum = 0;
foreach (var item in _items)
{
sum += item.Weight;
}
var ratio = 1.0m / sum;
while (result.Count < count)
{
foreach (var item in _items.OrderBy(i => i.Weight).ToList())
foreach (var item in _items.OrderBy(i => i.Weight))
{
weight += (item.Weight * ratio);
weight += item.Weight * ratio;

if (rnd <= weight)
{
Expand Down
26 changes: 15 additions & 11 deletions Lib9c/Model/Quest/QuestList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,34 @@ public void UpdateTradeQuest(TradeType type, FungibleAssetValue price)

public void UpdateStageQuest(CollectionMap stageMap)
{
var stageQuests = _quests.OfType<WorldQuest>();
foreach (var quest in stageQuests)
foreach (var quest in _quests)
{
quest.Update(stageMap);
if (quest is WorldQuest worldQuest)
{
worldQuest.Update(stageMap);
}
}
}

public void UpdateMonsterQuest(CollectionMap monsterMap)
{
var monsterQuests = _quests.OfType<MonsterQuest>();
foreach (var quest in monsterQuests)
foreach (var quest in _quests)
{
quest.Update(monsterMap);
if (quest is MonsterQuest monsterQuest)
{
monsterQuest.Update(monsterMap);
}
}
}

public void UpdateCollectQuest(CollectionMap itemMap)
{
var collectQuests = _quests.OfType<CollectQuest>();
foreach (var quest in collectQuests)
foreach (var quest in _quests)
{
quest.Update(itemMap);
if (quest is CollectQuest collectQuest)
{
collectQuest.Update(itemMap);
}
}
}

Expand Down Expand Up @@ -326,10 +332,8 @@ public IValue Serialize()
return Dictionary.Empty
.SetItem(ListVersionKey, _listVersion.Serialize())
.SetItem(QuestsKey, (IValue) new List(_quests
.OrderBy(i => i.Id)
.Select(q => q.Serialize())))
.SetItem(CompletedQuestIdsKey, (IValue) new List(completedQuestIds
.OrderBy(i => i)
.Select(i => i.Serialize())));
}

Expand Down
16 changes: 7 additions & 9 deletions Lib9c/Model/State/AvatarState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,18 +523,16 @@ public void UpdateFromQuestReward2(Quest.Quest quest, MaterialItemSheet material

public void UpdateQuestRewards(MaterialItemSheet materialItemSheet)
{
var completedQuests = questList
.Where(quest => quest.Complete && !quest.IsPaidInAction)
.ToList();
// 완료되었지만 보상을 받지 않은 퀘스트를 return 문에서 Select 하지 않고 미리 저장하는 이유는
// 지연된 실행에 의해, return 시점에서 이미 모든 퀘스트의 보상 처리가 완료된 상태에서
// completed를 호출 시 where문의 predicate가 평가되어 컬렉션이 텅 비기 때문이다.
var completedQuestIds = completedQuests.Select(quest => quest.Id).ToList();
foreach (var quest in completedQuests)
var completedQuestIds = new List<int>();
foreach (var quest in questList)
{
if (!quest.Complete || quest.IsPaidInAction)
{
continue;
}
completedQuestIds.Add(quest.Id);
UpdateFromQuestReward(quest, materialItemSheet);
}

questList.completedQuestIds = completedQuestIds;
}

Expand Down

0 comments on commit e788fa8

Please sign in to comment.