Skip to content

Commit

Permalink
releaseb dont allow char updates before loading everything from db
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammers21 committed Oct 1, 2023
1 parent 3ae3679 commit 8382581
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/io/github/sammers/pla/logic/CharUpdater.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.sammers.pla.logic;

import io.github.sammers.pla.Main;
import io.github.sammers.pla.blizzard.*;
import io.github.sammers.pla.blizzard.BlizzardAPI;
import io.github.sammers.pla.blizzard.PvpBracket;
import io.github.sammers.pla.blizzard.WowAPICharacter;
import io.github.sammers.pla.db.Character;
import io.github.sammers.pla.db.DB;
import io.github.sammers.pla.db.Snapshot;
Expand All @@ -14,28 +16,28 @@
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import static io.github.sammers.pla.logic.Conts.*;
import static io.github.sammers.pla.logic.Ladder.*;
import static io.github.sammers.pla.logic.Refs.bucketRef;

public class CharUpdater {

private static final Logger log = LoggerFactory.getLogger(CharUpdater.class);
private final BlizzardAPI api;
private final CharacterCache characterCache;
private final NickNameSearchIndex charSearchIndex;
private final AtomicBoolean charsLoaded;
private final DB db;
private final Refs refs;

public CharUpdater(BlizzardAPI api,
CharacterCache characterCache,
Refs refs,
AtomicBoolean charsLoaded, Refs refs,
NickNameSearchIndex charSearchIndex,
DB db) {
this.api = api;
this.charsLoaded = charsLoaded;
this.refs = refs;
this.characterCache = characterCache;
this.charSearchIndex = charSearchIndex;
Expand Down Expand Up @@ -109,11 +111,18 @@ public Completable updateChars(List<String> nickNames, String region) {
}

public Completable updateChar(String region, String nickName) {
return Completable.defer(() -> api.character(region, nickName).flatMapCompletable(wowAPICharacter -> {
characterCache.upsert(wowAPICharacter);
charSearchIndex.insertNickNames(new SearchResult(nickName, region, wowAPICharacter.clazz()));
return db.upsertCharacter(wowAPICharacter).ignoreElement();
}).onErrorComplete()
return Completable.defer(() -> {
if (charsLoaded.get()) {
return api.character(region, nickName).flatMapCompletable(wowAPICharacter -> {
characterCache.upsert(wowAPICharacter);
charSearchIndex.insertNickNames(new SearchResult(nickName, region, wowAPICharacter.clazz()));
return db.upsertCharacter(wowAPICharacter).ignoreElement();
}).onErrorComplete();
} else {
log.warn("Not allowing char updates before char load from db");
return Completable.complete();
}
}
);
}
}
13 changes: 12 additions & 1 deletion src/io/github/sammers/pla/logic/Ladder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -50,19 +51,22 @@ public class Ladder {
private final DB db;
private final BlizzardAPI blizzardAPI;

private final AtomicBoolean charsLoaded = new AtomicBoolean(false);

public Ladder(WebClient web, DB db, BlizzardAPI blizzardAPI, CharacterCache characterCache, Refs refs, Map<String, Cutoffs> regionCutoff) {
this.web = web;
this.db = db;
this.blizzardAPI = blizzardAPI;
this.refs = refs;
this.characterCache = characterCache;
this.regionCutoff = regionCutoff;
this.charUpdater = new CharUpdater(blizzardAPI, characterCache, refs, charSearchIndex, db);
this.charUpdater = new CharUpdater(blizzardAPI, characterCache, charsLoaded, refs, charSearchIndex, db);
}

public void start() {
loadRegionData(EU)
.andThen(loadRegionData(US))
.andThen(charsAreLoaded())
.andThen(
runDataUpdater(US,
runDataUpdater(EU,
Expand All @@ -80,6 +84,13 @@ public void start() {
.subscribe();
}

private Completable charsAreLoaded() {
return Completable.fromAction(() -> {
charsLoaded.set(true);
log.info("Chars are loaded. Updates are allowed now");
});
}

private <R> Observable<Snapshot> runDataUpdater(String region, Observable<R> tickObservable) {
return tickObservable
.flatMapSingle(tick -> {
Expand Down

0 comments on commit 8382581

Please sign in to comment.