Skip to content

Commit

Permalink
Merge pull request #3 from Cristian-Sknz/feature_multipleinstances
Browse files Browse the repository at this point in the history
Criar múltiplas instâncias da classe Paladins utilizando o PaladinsBuilder
  • Loading branch information
Cristian-Sknz authored Feb 21, 2022
2 parents 64a26c5 + 20d1be6 commit aaf7467
Show file tree
Hide file tree
Showing 23 changed files with 680 additions and 295 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ version '2021.0417.3'
repositories {
maven { url 'https://jitpack.io' }
jcenter()
mavenCentral()
}

dependencies {
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.9.0'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.9.0'
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.36'
testImplementation(platform('org.junit:junit-bom:5.7.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
}
Expand All @@ -39,5 +40,4 @@ shadowJar{

test {
useJUnitPlatform()
}

}
100 changes: 15 additions & 85 deletions src/main/java/me/skiincraft/api/paladins/Paladins.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.skiincraft.api.paladins.entity.champions.Champions;
import me.skiincraft.api.paladins.entity.champions.objects.Cards;
import me.skiincraft.api.paladins.entity.champions.objects.Skins;
import me.skiincraft.api.paladins.entity.match.Match;
import me.skiincraft.api.paladins.exceptions.ContextException;
import me.skiincraft.api.paladins.exceptions.RequestException;
import me.skiincraft.api.paladins.impl.paladins.SessionImpl;
import me.skiincraft.api.paladins.impl.storage.PaladinsStorageImpl;
import me.skiincraft.api.paladins.impl.storage.StorageImpl;
import me.skiincraft.api.paladins.internal.logging.PaladinsLogger;
import me.skiincraft.api.paladins.internal.requests.APIRequest;
import me.skiincraft.api.paladins.internal.requests.impl.DefaultAPIRequest;
import me.skiincraft.api.paladins.internal.requests.impl.FakeAPIRequest;
import me.skiincraft.api.paladins.internal.session.Session;
import me.skiincraft.api.paladins.json.SessionJsonAdapter;
import me.skiincraft.api.paladins.objects.AccessUtils;
import me.skiincraft.api.paladins.objects.miscellany.DataUsed;
import me.skiincraft.api.paladins.storage.PaladinsStorage;
import me.skiincraft.api.paladins.storage.impl.PaladinsStorageImpl;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;

Expand All @@ -31,6 +24,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static me.skiincraft.api.paladins.objects.HirezAPIUtils.checkResponse;

/**
* <h1>Paladins</h1>
Expand All @@ -41,51 +35,19 @@
*/
public class Paladins {

private static Paladins instance;
private final AccessUtils accessUtils;
private final PaladinsStorage storage;
private final List<Session> sessions;
private OkHttpClient client;
private final Logger logger;

private int devId;
private String authkey;
private final int devId;
private final String authkey;

private Paladins() {
this.accessUtils = new AccessUtils(this);
protected Paladins(int devId, String authkey) {
this.devId = devId;
this.authkey = authkey;
this.sessions = new ArrayList<>();
this.client = new OkHttpClient();
this.logger = PaladinsLogger.getLogger(Paladins.class);
this.storage = new PaladinsStorageImpl(
new StorageImpl<Champions>(new Champions[0]) {
public Champions getById(long id) {
return getAsList().stream().filter(i -> i.getLanguage().getLanguagecode() == id).findFirst().orElse(null);
}
}, new StorageImpl<Match>(new Match[0]) {
public Match getById(long id) {
return getAsList().stream().filter(i -> i.getMatchId() == id).findFirst().orElse(null);
}
}, new StorageImpl<Cards>(new Cards[0]) {
public Cards getById(long id) {
return getAsList().stream().filter(i -> i.getChampionCardId() == id).findFirst().orElse(null);
}
}, new StorageImpl<Skins>(new Skins[0]) {
public Skins getById(long id) {
return getAsList().stream().filter(i -> i.get(0).getChampionId() == id).findFirst().orElse(null);
}
});
}

/**
* <h1>Instance</h1>
* <p>Method to get the instance of Paladins API</p>
*/
public static Paladins getInstance() {
if (instance == null) {
PaladinsLogger.getLogger(Paladins.class).debug("Creating a new instance of the Paladins class");
instance = new Paladins();
}
return instance;
}

public Logger getLogger() {
Expand Down Expand Up @@ -133,12 +95,12 @@ public synchronized APIRequest<Boolean> testSession(String sessionId) {
return new DefaultAPIRequest<>("testsession", sessionId, null, (response) -> {
try {
String json = Objects.requireNonNull(response.body(), "response is null").string();
if (AccessUtils.checkResponse(json)) {
if (checkResponse(json)) {
logger.info("TestSession: Session {} is still valid", sessionId);
return true;
}
Stream<Session> activeSessions = sessions.stream().filter((session) -> session.getSessionId().equalsIgnoreCase(sessionId));
if (activeSessions.count() >= 1) {
if (activeSessions.findAny().isPresent()) {
logger.warn("[{}] Sessions have been removed for being invalid.", activeSessions.count());
logger.debug("Session [{}] is invalid", sessionId);
sessions.removeAll(activeSessions.collect(Collectors.toList()));
Expand Down Expand Up @@ -180,7 +142,7 @@ public synchronized APIRequest<DataUsed> getDataUsed(String sessionId) {
return new DefaultAPIRequest<>("getdataused", sessionId, null, (response) -> {
try {
String json = Objects.requireNonNull(response.body(), "response is null").string();
if (AccessUtils.checkResponse(json)) {
if (checkResponse(json)) {
return new Gson().fromJson(json, DataUsed[].class)[0];
}
throw new RequestException(json, json);
Expand All @@ -206,14 +168,7 @@ public synchronized APIRequest<DataUsed> getDataUsed(Session session) {
* @return The storage such as Matches, Champions and Cards.
*/
public PaladinsStorage getStorage() {
return storage;
}

/**
* Are the utils used in the API, this class does not interfere negatively in the functioning of the API
*/
public AccessUtils getAccessUtils() {
return accessUtils;
return PaladinsStorageImpl.getInstance();
}

/**
Expand All @@ -231,42 +186,13 @@ public int getDevId() {
return devId;
}

/**
* <p>Define DevId in this instance</p>
* <p>It cannot be changed if there is an active session</p>
*
* @param devId The Developer Id
* @throws ContextException If you have an active session.
*/
public Paladins setDevId(int devId) {
if (sessions.size() != 0)
throw new ContextException("You cannot change the data after a session has already been created!");
this.devId = devId;
return this;
}

/**
* @return The API authentication key present in this instance.
*/
public String getAuthkey() {
return authkey;
}

/**
* <p>Define Authkey in this instance</p>
* <p>It cannot be changed if there is an active session</p>
*
* @param authkey The API access token
* @throws ContextException If you have an active session.
*/
public Paladins setAuthkey(String authkey) {
if (sessions.size() != 0) {
throw new ContextException("You cannot change the data after a session has already been created!");
}
this.authkey = authkey;
return this;
}

public OkHttpClient getClient() {
return client;
}
Expand All @@ -276,6 +202,10 @@ public Paladins setClient(@Nonnull OkHttpClient client) {
return this;
}

public static PaladinsBuilder builder() {
return new PaladinsBuilder();
}

@Override
public String toString() {
return "Paladins{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public PaladinsBuilder setAuthKey(String authKey) {
}

public Paladins build() {
return Paladins.getInstance()
.setAuthkey(authKey)
.setDevId(devId);
return new Paladins(devId, authKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -53,4 +54,17 @@ public String toString() {
", language=" + language +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Champions champions = (Champions) o;
return language == champions.language;
}

@Override
public int hashCode() {
return Objects.hash(language);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -68,4 +69,17 @@ public String toString() {
", language=" + language +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cards cards = (Cards) o;
return championId == cards.championId && language == cards.language;
}

@Override
public int hashCode() {
return Objects.hash(championId, language);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -21,26 +22,19 @@
public class Skins implements CustomList<ChampionSkin> {

private final ChampionSkin[] championSkins;
private final long championId;
private final Language language;

public Skins(List<ChampionSkin> ChampionSkin, Language language) {
public Skins(List<ChampionSkin> ChampionSkin, long championId, Language language) {
this.championSkins = new ChampionSkin[ChampionSkin.size()];
this.championId = championId;
this.language = language;
AtomicInteger integer = new AtomicInteger();
for (ChampionSkin item : ChampionSkin) {
championSkins[integer.getAndIncrement()] = item;
}
}

public Skins(List<ChampionSkin> ChampionSkin) {
this.championSkins = new ChampionSkin[ChampionSkin.size()];
this.language = ChampionSkin.get(0).getLanguage();
AtomicInteger integer = new AtomicInteger();
for (ChampionSkin item : ChampionSkin) {
championSkins[integer.getAndIncrement()] = item;
}
}

@Nonnull
public Iterator<ChampionSkin> iterator() {
return Arrays.stream(championSkins).iterator();
Expand All @@ -59,7 +53,7 @@ public ChampionSkin getById(long id) {
}

public long getChampionId() {
return championSkins[0].getChampionId();
return championId;
}

public Language getLanguage() {
Expand All @@ -69,9 +63,22 @@ public Language getLanguage() {
@Override
public String toString() {
return "Skins{" +
"championSkins=" + championSkins.length +
"championId=" + getChampionId() +
"championId=" + championId +
", championSkins=" + championSkins.length +
", language=" + language +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Skins that = (Skins) o;
return championId == that.championId && language == that.language;
}

@Override
public int hashCode() {
return Objects.hash(championId, language);
}
}
14 changes: 14 additions & 0 deletions src/main/java/me/skiincraft/api/paladins/impl/match/MatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class MatchImpl implements Match {
Expand Down Expand Up @@ -194,4 +195,17 @@ public String toString() {
", isRanked=" + isRanked() +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MatchImpl match = (MatchImpl) o;
return matchId == match.matchId && queueId == match.queueId;
}

@Override
public int hashCode() {
return Objects.hash(matchId, queueId);
}
}
Loading

0 comments on commit aaf7467

Please sign in to comment.