Skip to content

Commit

Permalink
Fixed #94 : Added support for CardID
Browse files Browse the repository at this point in the history
  • Loading branch information
Neraud committed Feb 23, 2016
1 parent c4fbaa2 commit ed5d58f
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ChooseSyncInitHelper {
private ChooseSyncModel mChooseSyncModel = null;
private boolean mHasDataToSync = false;
private boolean mHasChosenDataToSync = false;
private int nbElementsToSync = 0;

public ChooseSyncInitHelper(Context context, ComputeSyncResultModel syncResult) {
super();
Expand All @@ -44,6 +45,8 @@ public ChooseSyncModel filterSyncResult() {
fillMaterials();
fillMonsters();

MyLog.info("nbElementsToSync : " + nbElementsToSync);

MyLog.exit();
return mChooseSyncModel;
}
Expand All @@ -56,6 +59,7 @@ private void fillUserInfo() {
syncedUserInfoToUpdate.setModel(mSyncResult.getSyncedUserInfo());

mChooseSyncModel.setSyncedUserInfoToUpdate(syncedUserInfoToUpdate);
nbElementsToSync++;

MyLog.exit();
}
Expand All @@ -74,6 +78,7 @@ private void fillMaterials() {
container.setChosen(mPrefHelper.isChooseSyncPreselectMaterialsUpdated());
container.setModel(material);
syncedMaterialsToUpdate.add(container);
nbElementsToSync++;
flagDataToSync(container);
}
}
Expand All @@ -85,9 +90,9 @@ private void fillMaterials() {
private void fillMonsters() {
MyLog.entry();

final List<ChooseModelContainer<SyncedMonsterModel>> syncedMonstersToUpdate = new ArrayList<ChooseModelContainer<SyncedMonsterModel>>();
final List<ChooseModelContainer<SyncedMonsterModel>> syncedMonstersToCreate = new ArrayList<ChooseModelContainer<SyncedMonsterModel>>();
final List<ChooseModelContainer<SyncedMonsterModel>> syncedMonstersToDelete = new ArrayList<ChooseModelContainer<SyncedMonsterModel>>();
mChooseSyncModel.setSyncedMonsters(SyncMode.UPDATED, new ArrayList<ChooseModelContainer<SyncedMonsterModel>>());
mChooseSyncModel.setSyncedMonsters(SyncMode.CREATED, new ArrayList<ChooseModelContainer<SyncedMonsterModel>>());
mChooseSyncModel.setSyncedMonsters(SyncMode.DELETED, new ArrayList<ChooseModelContainer<SyncedMonsterModel>>());

final Set<Integer> ignoredIds = mPrefHelper.getMonsterIgnoreList();

Expand All @@ -101,40 +106,39 @@ private void fillMonsters() {
MyLog.debug("ignoring deleted monster : " + monster);
} else {
MyLog.debug("adding deleted monster : " + monster);
container.setChosen(mPrefHelper.isChooseSyncPreselectMonsters(SyncMode.DELETED));
syncedMonstersToDelete.add(container);
flagDataToSync(container);
addMonsterToSync(SyncMode.DELETED, container);
}
} else if (monster.getPadherderInfo() == null) {
boolean ignored = mPrefHelper.isChooseSyncUseIgnoreListForMonsters(SyncMode.CREATED) && ignoredIds.contains(monster.getDisplayedMonsterInfo().getIdJP());
if (ignored) {
MyLog.debug("ignoring created monster : " + monster);
} else {
MyLog.debug("adding created monster : " + monster);
container.setChosen(mPrefHelper.isChooseSyncPreselectMonsters(SyncMode.CREATED));
syncedMonstersToCreate.add(container);
flagDataToSync(container);
addMonsterToSync(SyncMode.CREATED, container);
}
} else {
boolean ignored = mPrefHelper.isChooseSyncUseIgnoreListForMonsters(SyncMode.UPDATED) && ignoredIds.contains(monster.getDisplayedMonsterInfo().getIdJP());
if (ignored) {
MyLog.debug("ignoring updated monster : " + monster);
} else {
MyLog.debug("adding updated monster : " + monster);
container.setChosen(mPrefHelper.isChooseSyncPreselectMonsters(SyncMode.UPDATED));
syncedMonstersToUpdate.add(container);
flagDataToSync(container);
addMonsterToSync(SyncMode.UPDATED, container);
}
}
}
mChooseSyncModel.setSyncedMonsters(SyncMode.UPDATED, syncedMonstersToUpdate);
mChooseSyncModel.setSyncedMonsters(SyncMode.CREATED, syncedMonstersToCreate);
mChooseSyncModel.setSyncedMonsters(SyncMode.DELETED, syncedMonstersToDelete);


MyLog.exit();
}

private void addMonsterToSync(SyncMode mode, ChooseModelContainer<SyncedMonsterModel> container) {
container.setChosen(mPrefHelper.isChooseSyncPreselectMonsters(mode));
mChooseSyncModel.getSyncedMonsters(mode).add(container);
flagDataToSync(container);
}

private void flagDataToSync(ChooseModelContainer container) {
nbElementsToSync++;
mHasDataToSync = true;
if (container.isChosen()) {
mHasChosenDataToSync = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MonsterComparatorHelper {

public static boolean areMonstersEqual(MonsterModel a, MonsterModel b) {
return a.getIdJp() == b.getIdJp() &&
a.getCardId() == b.getCardId() &&
a.getExp() == b.getExp() &&
a.getSkillLevel() == b.getSkillLevel() &&
a.getPlusHp() == b.getPlusHp() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ private void pushMonsterToUpdate(SyncedMonsterModel model) throws JSONException,
final JSONObject json = new JSONObject();
json.put("monster", model.getCapturedInfo().getIdJp());

if (model.getCapturedInfo().getCardId() != null && !model.getCapturedInfo().getCardId().equals(model.getPadherderInfo().getCardId())) {
json.put("pad_id", model.getCapturedInfo().getCardId());
}

if (model.getCapturedInfo().getIdJp() != model.getPadherderInfo().getIdJp()) {
// Monster evolution, reset the target to the max
json.put("target_level", model.getCapturedMonsterInfo().getMaxLevel());
Expand Down Expand Up @@ -140,6 +144,9 @@ private void pushMonsterToCreate(SyncedMonsterModel model) throws JSONException,
*/
final JSONObject json = new JSONObject();
json.put("monster", model.getDisplayedMonsterInfo().getIdJP());
if (model.getCapturedInfo().getCardId() != null) {
json.put("pad_id", model.getCapturedInfo().getCardId());
}
json.put("current_xp", model.getCapturedInfo().getExp());
json.put("current_skill", model.getCapturedInfo().getSkillLevel());
json.put("current_awakening", model.getCapturedInfo().getAwakenings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -375,6 +376,39 @@ private List<SyncedMonsterModel> syncMonstersForOneId(Integer monsterId, List<Mo

final Iterator<MonsterModel> capturedIter = capturedMonstersWork.iterator();

// Matching monsters on CARD_ID
capturedLoop:
while (capturedIter.hasNext()) {
final MonsterModel captured = capturedIter.next();
if (captured.getCardId() != null) {
final Iterator<UserInfoMonsterModel> parherderIter = padherderMonstersWork.iterator();
while (parherderIter.hasNext()) {
final UserInfoMonsterModel padherder = parherderIter.next();
if (captured.getCardId().equals(padherder.getCardId())) {
if (MonsterComparatorHelper.areMonstersEqual(captured, padherder)) {
MyLog.debug("matching ids and equal, removing : " + captured);
capturedIter.remove();
parherderIter.remove();
continue capturedLoop;
} else {
try {
final SyncedMonsterModel model = fillUpdatedSyncedMonsterModel(padherder, captured);
syncedMonsters.add(model);
MyLog.debug("matching ids and different, updating : " + model);
} catch (UnknownMonsterException e) {
// Should not happen as monsters are checked before during the reorg phase
MyLog.warn("missing monster info for id = " + e.getMonsterId());
hasEncounteredUnknownMonster = true;
}
capturedIter.remove();
parherderIter.remove();
continue capturedLoop;
}
}
}
}
}

// Filter "equals" monsters
capturedLoop:
while (capturedIter.hasNext()) {
Expand All @@ -383,7 +417,7 @@ private List<SyncedMonsterModel> syncMonstersForOneId(Integer monsterId, List<Mo
while (parherderIter.hasNext()) {
final UserInfoMonsterModel padherder = parherderIter.next();
if (MonsterComparatorHelper.areMonstersEqual(captured, padherder)) {
MyLog.debug("equals, removing : " + captured);
MyLog.debug("equal, removing : " + captured);
capturedIter.remove();
parherderIter.remove();
continue capturedLoop;
Expand All @@ -395,46 +429,19 @@ private List<SyncedMonsterModel> syncMonstersForOneId(Integer monsterId, List<Mo
final int nbPadherder = padherderMonstersWork.size();

for (int i = 0; i < Math.max(nbCaptured, nbPadherder); i++) {
final SyncedMonsterModel model = new SyncedMonsterModel();
final SyncedMonsterModel model;
try {
if (i < nbCaptured && i < nbPadherder) {
// Update while we have enough monsters in each list
model.setPadherderId(padherderMonstersWork.get(i).getPadherderId());

final UserInfoMonsterModel padherder = padherderMonstersWork.get(i);
final MonsterModel captured = capturedMonstersWork.get(i);

model.setPadherderMonsterInfo(monsterInfoHelper.getMonsterInfo(padherder.getIdJp()));
model.setCapturedMonsterInfo(monsterInfoHelper.getMonsterInfo(captured.getIdJp()));

// Keep priority and note
captured.setPriority(padherder.getPriority());
captured.setNote(padherder.getNote());

model.setCapturedInfo(captured);
model.setPadherderInfo(padherder);
model = fillUpdatedSyncedMonsterModel(padherderMonstersWork.get(i), capturedMonstersWork.get(i));
MyLog.debug("updated : " + model);
} else if (i < nbCaptured) {
// more captured -> creating
final MonsterModel captured = capturedMonstersWork.get(i);

model.setCapturedMonsterInfo(monsterInfoHelper.getMonsterInfo(captured.getIdJp()));

// default create priority
captured.setPriority(helper.getDefaultMonsterCreatePriority());
model.setCapturedInfo(capturedMonstersWork.get(i));

model.setPadherderInfo(null);
model = fillCreatedSyncedMonsterModel(capturedMonstersWork.get(i));
MyLog.debug("added : " + model);
} else {
// not enough captured -> deleting
final UserInfoMonsterModel padherder = padherderMonstersWork.get(i);

model.setPadherderMonsterInfo(monsterInfoHelper.getMonsterInfo(padherder.getIdJp()));

model.setPadherderId(padherder.getPadherderId());
model.setCapturedInfo(null);
model.setPadherderInfo(padherderMonstersWork.get(i));
model = fillDeletedSyncedMonsterModel(padherderMonstersWork.get(i));
MyLog.debug("deleted : " + model);
}

Expand All @@ -449,4 +456,45 @@ private List<SyncedMonsterModel> syncMonstersForOneId(Integer monsterId, List<Mo
MyLog.exit();
return syncedMonsters;
}

@NonNull
private SyncedMonsterModel fillUpdatedSyncedMonsterModel(UserInfoMonsterModel padherder, MonsterModel captured) throws UnknownMonsterException {
final SyncedMonsterModel model = new SyncedMonsterModel();
model.setPadherderId(padherder.getPadherderId());
model.setPadherderMonsterInfo(monsterInfoHelper.getMonsterInfo(padherder.getIdJp()));
model.setCapturedMonsterInfo(monsterInfoHelper.getMonsterInfo(captured.getIdJp()));

// Keep priority and note
captured.setPriority(padherder.getPriority());
captured.setNote(padherder.getNote());

model.setCapturedInfo(captured);
model.setPadherderInfo(padherder);
return model;
}

@NonNull
private SyncedMonsterModel fillCreatedSyncedMonsterModel(MonsterModel captured) throws UnknownMonsterException {
final SyncedMonsterModel model = new SyncedMonsterModel();
model.setCapturedMonsterInfo(monsterInfoHelper.getMonsterInfo(captured.getIdJp()));

// default create priority
captured.setPriority(helper.getDefaultMonsterCreatePriority());
model.setCapturedInfo(captured);

model.setPadherderInfo(null);
return model;
}


@NonNull
private SyncedMonsterModel fillDeletedSyncedMonsterModel(UserInfoMonsterModel padherder) throws UnknownMonsterException {
final SyncedMonsterModel model = new SyncedMonsterModel();
model.setPadherderMonsterInfo(monsterInfoHelper.getMonsterInfo(padherder.getIdJp()));

model.setPadherderId(padherder.getPadherderId());
model.setCapturedInfo(null);
model.setPadherderInfo(padherder);
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private MonsterModel parseMonsterFromArray(final JSONArray cardResult) throws JS

//[8,190542,30,1,0,1655,0,0,0,0,0]
final MonsterModel monster = new MonsterModel();
monster.setCardId(cardResult.getLong(0));
monster.setExp(cardResult.getLong(1));
monster.setLevel(cardResult.getInt(2));
monster.setSkillLevel(cardResult.getInt(3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ private UserInfoMonsterModel parseOneMonster(JSONObject monsterJson) throws JSON
"target_evolution": 480,
"plus_hp": 3,
"plus_atk": 3,
"plus_rcv": 0
"plus_rcv": 0,
"pad_id": 784
*/

final UserInfoMonsterModel monster = new UserInfoMonsterModel();

if (monsterJson.has("pad_id")) {
monster.setCardId(monsterJson.getLong("pad_id"));
}
monster.setPadherderId(monsterJson.getLong("id"));
monster.setIdJp(monsterJson.getInt("monster"));
monster.setNote(monsterJson.getString("note"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
public class MonsterModel extends BaseMonsterStatsModel {

private static final long serialVersionUID = 1L;
private Long cardId;
private MonsterPriority priority;
private String note;

public Long getCardId() {
return cardId;
}

public void setCardId(Long cardId) {
this.cardId = cardId;
}

public MonsterPriority getPriority() {
return priority;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getContentType() {
*/
public enum Fields implements IField {

FAKE_ID(BaseColumns._ID),
CARD_ID(BaseColumns._ID),
MONSTER_ID_JP("monster_id_jp"),
EXP("exp"),
LEVEL("level"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CapturedPlayerMonsterProviderHelper extends BaseProviderHelper {
public static MonsterModel cursorToModel(Cursor cursor) {
final MonsterModel model = new MonsterModel();

model.setCardId(getLong(cursor, CapturedPlayerMonsterDescriptor.Fields.CARD_ID));
model.setIdJp(getInt(cursor, CapturedPlayerMonsterDescriptor.Fields.MONSTER_ID_JP));
model.setExp(getInt(cursor, CapturedPlayerMonsterDescriptor.Fields.EXP));
model.setLevel(getInt(cursor, CapturedPlayerMonsterDescriptor.Fields.LEVEL));
Expand Down Expand Up @@ -56,6 +57,7 @@ public static CapturedMonsterFullInfoModel cursorWithInfoToModel(Cursor cursor)
public static ContentValues modelToValues(MonsterModel model) {
final ContentValues values = new ContentValues();

putValue(values, CapturedPlayerMonsterDescriptor.Fields.CARD_ID, model.getCardId());
putValue(values, CapturedPlayerMonsterDescriptor.Fields.MONSTER_ID_JP, model.getIdJp());
putValue(values, CapturedPlayerMonsterDescriptor.Fields.EXP, model.getExp());
putValue(values, CapturedPlayerMonsterDescriptor.Fields.LEVEL, model.getLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CapturedPlayerMonsterTable implements ITable {
@Override
public String createTable() {
return "CREATE TABLE " + CapturedPlayerMonsterDescriptor.TABLE_NAME + " (" +
/* */CapturedPlayerMonsterDescriptor.Fields.FAKE_ID.getColName() + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
/* */CapturedPlayerMonsterDescriptor.Fields.CARD_ID.getColName() + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
/* */CapturedPlayerMonsterDescriptor.Fields.MONSTER_ID_JP.getColName() + " INTEGER," +
/* */CapturedPlayerMonsterDescriptor.Fields.EXP.getColName() + " INTEGER NOT NULL," +
/* */CapturedPlayerMonsterDescriptor.Fields.LEVEL.getColName() + " INTEGER NOT NULL," +
Expand Down
1 change: 1 addition & 0 deletions PADListenerApp/src/main/res/xml/changelog_master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<change>Fixed #81 : Fixed friends broken by the new Best Friend flag (thanks QuasiTD !)</change>
<change>Fixed #90 : repaired Manual capture for M (thanks olifozzy !)</change>
<change>Fixed #91 : Tapping on "PAD data captured" notification dismisses it</change>
<change>Fixed #94 : Added support for CardID</change>
</release>
<release
version="2.1.8" versioncode="56">
Expand Down

0 comments on commit ed5d58f

Please sign in to comment.