2016-04-30 108 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(); 
    } 
} 

希望能幫助某人在未來出現類似問題。