2015-10-01 18:58:04 +00:00
|
|
|
package com.habitrpg.android.habitica;
|
|
|
|
|
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.ApiService;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.ContentResult;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.QuestBoss;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.QuestContent;
|
2015-10-29 19:49:57 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.ItemData;
|
2015-10-01 18:58:04 +00:00
|
|
|
import com.raizlabs.android.dbflow.sql.builder.Condition;
|
|
|
|
|
import com.raizlabs.android.dbflow.sql.language.Select;
|
2015-10-29 19:49:57 +00:00
|
|
|
import com.raizlabs.android.dbflow.sql.language.Where;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
2015-10-01 18:58:04 +00:00
|
|
|
|
|
|
|
|
import retrofit.Callback;
|
|
|
|
|
import retrofit.RetrofitError;
|
|
|
|
|
import retrofit.client.Response;
|
|
|
|
|
|
2015-11-25 17:47:56 +00:00
|
|
|
|
2015-10-01 18:58:04 +00:00
|
|
|
public class ContentCache {
|
2015-10-29 19:49:57 +00:00
|
|
|
public interface GotContentEntryCallback<T extends Object> {
|
|
|
|
|
void GotObject(T obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface QuestContentCallback {
|
2015-10-01 18:58:04 +00:00
|
|
|
void GotQuest(QuestContent content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ApiService apiService;
|
|
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
public ContentCache(ApiService apiService) {
|
2015-10-01 18:58:04 +00:00
|
|
|
|
|
|
|
|
this.apiService = apiService;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
public void GetQuestContent(final String key, final QuestContentCallback cb) {
|
2015-10-01 18:58:04 +00:00
|
|
|
final QuestContent quest = new Select().from(QuestContent.class).where(Condition.column("key").eq(key)).querySingle();
|
|
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
if (quest != null) {
|
2015-11-08 19:04:31 +00:00
|
|
|
quest.boss = new Select().from(QuestBoss.class).where(Condition.column("key").eq(key)).querySingle();
|
2015-10-01 18:58:04 +00:00
|
|
|
cb.GotQuest(quest);
|
2015-10-29 19:49:57 +00:00
|
|
|
} else {
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
getContentAndSearchFor("quest", key, new GotContentEntryCallback<QuestContent>() {
|
2015-10-01 18:58:04 +00:00
|
|
|
@Override
|
2015-10-29 19:49:57 +00:00
|
|
|
public void GotObject(QuestContent obj) {
|
|
|
|
|
cb.GotQuest(obj);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetItemData(final String key, final GotContentEntryCallback<ItemData> gotEntry) {
|
|
|
|
|
final ItemData itemData = new Select().from(ItemData.class).where(Condition.column("key").eq(key)).querySingle();
|
|
|
|
|
|
|
|
|
|
if (itemData != null) {
|
|
|
|
|
gotEntry.GotObject(itemData);
|
|
|
|
|
} else {
|
|
|
|
|
getContentAndSearchFor("item", key, gotEntry);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
public void GetItemDataList(final List<String> keysToSearch, GotContentEntryCallback<List<ItemData>> gotEntries) {
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
Condition.In keyCondition = Condition.column("key").in("potion");
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
for (String item : keysToSearch) {
|
|
|
|
|
keyCondition = keyCondition.and(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Where<ItemData> query = new Select().from(ItemData.class).where(keyCondition);
|
|
|
|
|
|
|
|
|
|
List<ItemData> items = query.queryList();
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2016-01-19 14:23:53 +00:00
|
|
|
if (items != null && items.size() == keysToSearch.size()) {
|
2015-10-29 19:49:57 +00:00
|
|
|
gotEntries.GotObject(items);
|
|
|
|
|
} else {
|
|
|
|
|
getContentAndSearchForList("item", keysToSearch, gotEntries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private <T extends Object> void getContentAndSearchFor(final String typeOfSearch, final String searchKey, final GotContentEntryCallback<T> gotEntry) {
|
|
|
|
|
apiService.getContent(new Callback<ContentResult>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void success(ContentResult contentResult, Response response) {
|
|
|
|
|
switch (typeOfSearch) {
|
|
|
|
|
case "quest": {
|
2016-03-28 16:43:15 +00:00
|
|
|
Collection<QuestContent> questList = contentResult.quests;
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
for (QuestContent quest : questList) {
|
2016-03-28 16:43:15 +00:00
|
|
|
if (quest.getKey() == searchKey) {
|
2015-10-29 19:49:57 +00:00
|
|
|
gotEntry.GotObject((T) quest);
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
}
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
break;
|
2015-10-01 18:58:04 +00:00
|
|
|
}
|
2015-10-29 19:49:57 +00:00
|
|
|
case "item": {
|
|
|
|
|
T searchedItem = null;
|
|
|
|
|
|
|
|
|
|
if (searchKey == "potion") {
|
|
|
|
|
searchedItem = (T) contentResult.potion;
|
|
|
|
|
} else if (searchKey == "armoire") {
|
|
|
|
|
searchedItem = (T) contentResult.armoire;
|
|
|
|
|
} else {
|
2016-03-10 13:05:32 +00:00
|
|
|
Collection<ItemData> itemList = contentResult.gear.flat;
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
for (ItemData item : itemList) {
|
2016-03-10 13:05:32 +00:00
|
|
|
if (item.key.equals(searchKey)) {
|
2015-10-29 19:49:57 +00:00
|
|
|
searchedItem = (T) item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
gotEntry.GotObject((T) searchedItem);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
saveContentResultToDb(contentResult);
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void failure(RetrofitError error) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private <T extends Object> void getContentAndSearchForList(final String typeOfSearch, final List<String> searchKeys, final GotContentEntryCallback<List<T>> gotEntry) {
|
|
|
|
|
apiService.getContent(new Callback<ContentResult>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void success(ContentResult contentResult, Response response) {
|
|
|
|
|
|
|
|
|
|
switch (typeOfSearch) {
|
|
|
|
|
case "item": {
|
|
|
|
|
List<T> resultList = new ArrayList<T>();
|
|
|
|
|
|
2016-03-10 13:05:32 +00:00
|
|
|
List<ItemData> itemList = new ArrayList<ItemData>(contentResult.gear.flat);
|
2015-10-29 19:49:57 +00:00
|
|
|
itemList.add(contentResult.potion);
|
|
|
|
|
itemList.add(contentResult.armoire);
|
|
|
|
|
|
|
|
|
|
for (ItemData item : itemList) {
|
|
|
|
|
if (searchKeys.contains(item.key)) {
|
|
|
|
|
resultList.add((T) item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gotEntry.GotObject(resultList);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-10-01 18:58:04 +00:00
|
|
|
}
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
saveContentResultToDb(contentResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void failure(RetrofitError error) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveContentResultToDb(ContentResult contentResult) {
|
2016-03-28 16:43:15 +00:00
|
|
|
Collection<QuestContent> questList = contentResult.quests;
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
for (QuestContent quest : questList) {
|
|
|
|
|
quest.save();
|
|
|
|
|
|
|
|
|
|
if (quest.boss != null) {
|
2016-03-28 16:43:15 +00:00
|
|
|
quest.boss.key = quest.getKey();
|
2015-11-03 18:59:52 +00:00
|
|
|
quest.boss.async().save();
|
2015-10-29 19:49:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 13:05:32 +00:00
|
|
|
Collection<ItemData> itemList = new ArrayList<>(contentResult.gear.flat);
|
2015-11-03 18:59:52 +00:00
|
|
|
itemList.add(contentResult.armoire);
|
|
|
|
|
itemList.add(contentResult.potion);
|
2015-10-29 19:49:57 +00:00
|
|
|
|
|
|
|
|
for (ItemData item : itemList) {
|
2015-11-03 18:59:52 +00:00
|
|
|
item.async().save();
|
2015-10-01 18:58:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|