2016-04-30 3 views
0

私はAndroidのゲームで作業しています。マルチスロットクラウドキューに予定アイテムを追加

ゲームのエリアには、一度に作成できるアイテムの数を決定するためのスロットがあります。利用可能なスロットがない場合、そのアイテムには、スロットが利用可能になる時点と相関する予定された開始日が与えられる。

私が遭遇している問題は、現在のコードでは、最初のスロットが利用可能になるときだけであり、スロットが存在しないときに考慮されるということです。

予定のアイテムの追加:スロットが使用可能な時間を取得する

long timeSlotAvailable = getTimeSlotAvailable(location); 

Pending_Inventory newScheduledItem = new Pending_Inventory(itemId, state, timeSlotAvailable, quantity, craftTime, location); 

を:

public static long getTimeSlotAvailable(Long location) { 
    List<Pending_Inventory> pendingItems = getPendingItems(location, true); 
    int locationSlots = Slot.getUnlockedSlots(location); 
    long timeAvailable = System.currentTimeMillis(); 

    // Works for single slots, not for multi though. Needs to consider slot count. 
    for (Pending_Inventory pending_inventory : pendingItems) { 
     long finishTime = pending_inventory.getTimeCreated() + pending_inventory.getCraftTime(); 
     if (finishTime > timeAvailable) { 
      timeAvailable = finishTime; 
     } 
    } 

    return timeAvailable; 
} 

コードが現在作るために作られた、またはスケジュールされたすべての項目を見て、時間を取得することにより動作します最後のものは終了する。

locationSlotsは現在使用されていませんが、スロットが利用可能になる正確な時間を計算する必要があると思います。

私はいくつかのアプローチを試みました(配列にすべての仕上げ時間を追加すると、n値を得ることは約束されましたが、私の周りに頭を上げることはできませんでした)。

ありがとうございます!

答えて

0

最終的にアレイアプローチでもう一度取り組み、成功しました。

public static long getTimeSlotAvailable(Long location) { 
    List<Pending_Inventory> pendingItems = getPendingItems(location, true); 
    int numSlots = Slot.getUnlockedSlots(location); 

    // Add all of the times a slot will become available to a list 
    List<Long> finishTimes = new ArrayList<>(); 
    for (Pending_Inventory pending_inventory : pendingItems) { 
     long finishTime = pending_inventory.getTimeCreated() + pending_inventory.getCraftTime(); 
     finishTimes.add(finishTime); 
    } 

    // Sort these times so the latest time is first 
    Collections.sort(finishTimes, Collections.<Long>reverseOrder()); 

    if (finishTimes.size() >= numSlots) { 
     // If we're all full up, get the first time a slot will become available 
     return finishTimes.get(numSlots-1); 
    } else { 
     // Otherwise, it can go in now 
     return System.currentTimeMillis(); 
    } 
} 

これは、今後同様の問題が発生した場合に役立ちます。

関連する問題