Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for calls to the Services API that return a Future object #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/com/achimala/leaguelib/services/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

package com.achimala.leaguelib.services;

import com.achimala.leaguelib.connection.*;
import com.achimala.leaguelib.models.*;
import com.achimala.leaguelib.errors.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.achimala.leaguelib.connection.LeagueConnection;
import com.achimala.leaguelib.errors.LeagueErrorCode;
import com.achimala.leaguelib.errors.LeagueException;
import com.achimala.leaguelib.models.LeagueGame;
import com.achimala.leaguelib.models.LeagueSummoner;
import com.achimala.util.Callback;
import com.gvaneyck.rtmp.TypedObject;

Expand Down Expand Up @@ -68,4 +74,14 @@ public void onError(Exception ex) {
}
});
}

public Future<LeagueSummoner> fillActiveGameData(final LeagueSummoner summoner, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>() {
@Override
public LeagueSummoner call() throws Exception {
fillActiveGameData(summoner);
return summoner;
}
});
}
}
22 changes: 19 additions & 3 deletions src/com/achimala/leaguelib/services/LeaguesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

package com.achimala.leaguelib.services;

import com.achimala.leaguelib.connection.*;
import com.achimala.leaguelib.models.*;
import com.achimala.leaguelib.errors.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.achimala.leaguelib.connection.LeagueConnection;
import com.achimala.leaguelib.errors.LeagueException;
import com.achimala.leaguelib.models.LeagueMatchmakingQueue;
import com.achimala.leaguelib.models.LeagueSummoner;
import com.achimala.leaguelib.models.LeagueSummonerLeagueStats;
import com.achimala.util.Callback;
import com.gvaneyck.rtmp.TypedObject;

Expand Down Expand Up @@ -72,4 +78,14 @@ public void onError(Exception ex) {
}
});
}

public Future<LeagueSummoner> fillSoloQueueLeagueData(final LeagueSummoner summoner, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>(){
@Override
public LeagueSummoner call() throws Exception {
fillSoloQueueLeagueData(summoner);
return summoner;
}
});
}
}
36 changes: 30 additions & 6 deletions src/com/achimala/leaguelib/services/PlayerStatsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package com.achimala.leaguelib.services;

import com.achimala.leaguelib.connection.*;
import com.achimala.leaguelib.models.*;
import com.achimala.leaguelib.errors.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.achimala.leaguelib.connection.LeagueConnection;
import com.achimala.leaguelib.errors.LeagueException;
import com.achimala.leaguelib.models.LeagueCompetitiveSeason;
import com.achimala.leaguelib.models.LeagueSummoner;
import com.achimala.leaguelib.models.LeagueSummonerRankedStats;
import com.achimala.leaguelib.models.MatchHistoryEntry;
import com.achimala.util.Callback;
import com.gvaneyck.rtmp.TypedObject;

import java.util.List;
import java.util.ArrayList;

public class PlayerStatsService extends LeagueAbstractService {
private final String SUMMONERS_RIFT = "CLASSIC";

Expand Down Expand Up @@ -59,6 +65,15 @@ public void onError(Exception ex) {
});
}

public Future<LeagueSummoner> fillRankedStats(final LeagueSummoner summoner, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>(){
@Override
public LeagueSummoner call() throws Exception {
fillRankedStats(summoner);
return summoner;
}});
}

private List<MatchHistoryEntry> getMatchHistoryEntriesFromResult(TypedObject obj, LeagueSummoner summoner) {
Object[] games = obj.getTO("body").getArray("gameStatistics");
if(games == null || games.length == 0)
Expand Down Expand Up @@ -91,4 +106,13 @@ public void onError(Exception ex) {
}
});
}

public Future<LeagueSummoner> fillMatchHistroy(final LeagueSummoner summoner, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>(){
@Override
public LeagueSummoner call() throws Exception {
fillMatchHistory(summoner);
return summoner;
}});
}
}
20 changes: 20 additions & 0 deletions src/com/achimala/leaguelib/services/SummonerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.gvaneyck.rtmp.TypedObject;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class SummonerService extends LeagueAbstractService {
public SummonerService(LeagueConnection connection) {
Expand Down Expand Up @@ -107,6 +110,14 @@ public void onError(Exception ex) {
});
}

public Future<LeagueSummoner> getSummonerByName(final String name, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>(){
@Override
public LeagueSummoner call() throws Exception {
return getSummonerByName(name);
}});
}

public void fillPublicSummonerData(LeagueSummoner summoner) throws LeagueException {
TypedObject obj = call("getAllPublicSummonerDataByAccount", new Object[] { summoner.getAccountId() });
summoner.setProfileInfo(new LeagueSummonerProfileInfo(obj.getTO("body").getTO("summoner")));
Expand All @@ -127,4 +138,13 @@ public void onError(Exception ex) {
}
});
}

public Future<LeagueSummoner> fillPublicSummonerData(final LeagueSummoner summoner, ExecutorService executor){
return executor.submit(new Callable<LeagueSummoner>(){
@Override
public LeagueSummoner call() throws Exception {
fillPublicSummonerData(summoner);
return summoner;
}});
}
}
123 changes: 123 additions & 0 deletions src/com/achimala/leaguelib/tests/FuturesTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.achimala.leaguelib.tests;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.achimala.leaguelib.connection.LeagueAccount;
import com.achimala.leaguelib.connection.LeagueConnection;
import com.achimala.leaguelib.connection.LeagueServer;
import com.achimala.leaguelib.errors.LeagueException;
import com.achimala.leaguelib.models.LeagueChampion;
import com.achimala.leaguelib.models.LeagueGame;
import com.achimala.leaguelib.models.LeagueRankedStatType;
import com.achimala.leaguelib.models.LeagueSummoner;
import com.achimala.leaguelib.models.LeagueSummonerLeagueStats;

public class FuturesTests {

private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 30, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(30), new TestThreadFactory());

private static class TestThreadFactory implements ThreadFactory {

int count = 0;

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Test-Thread-" + count++);
t.setDaemon(true);
return t;
}

}

public static void main(String[] args) throws Exception {
final LeagueConnection c = new LeagueConnection(LeagueServer.NORTH_AMERICA);
c.getAccountQueue().addAccount(new LeagueAccount(LeagueServer.NORTH_AMERICA, "3.10.xx", args[0], args[1]));

Map<LeagueAccount, LeagueException> exceptions = c.getAccountQueue().connectAll();
if(exceptions != null) {
for(LeagueAccount account : exceptions.keySet())
System.out.println(account + " error: " + exceptions.get(account));
return;
}



Future<LeagueSummoner> initialSummoner = c.getSummonerService().getSummonerByName("Mazorlion", threadPool);
// Retrieve the initial summoner so more work can be done
LeagueSummoner summoner = initialSummoner.get();

System.out.println(summoner.getName() + ":");
System.out.println(" accountID: " + summoner.getAccountId());
System.out.println(" summonerID: " + summoner.getId());
System.out.println();

// Send out all the requests because each there's no reason they cant be running at the same time
Future<LeagueSummoner> summonerWithPublicData = c.getSummonerService().fillPublicSummonerData(summoner, threadPool);
Future<LeagueSummoner> summonerWithGameInfo = c.getGameService().fillActiveGameData(summoner, threadPool);
Future<LeagueSummoner> summonerWithRankedStats = c.getPlayerStatsService().fillRankedStats(summoner, threadPool);
Future<LeagueSummoner> summonerWithSoloQueue = c.getLeaguesService().fillSoloQueueLeagueData(summoner, threadPool);

summoner = summonerWithPublicData.get();
System.out.println("Profile:");
System.out.println(" S1: " + summoner.getProfileInfo().getSeasonOneTier());
System.out.println(" S2: " + summoner.getProfileInfo().getSeasonTwoTier());
System.out.println();

summoner = summonerWithSoloQueue.get();
LeagueSummonerLeagueStats stats = summoner.getLeagueStats();
if(stats != null) {
System.out.println("League:");
System.out.println(" Name: " + stats.getLeagueName());
System.out.println(" Tier: " + stats.getTier());
System.out.println(" Rank: " + stats.getRank());
System.out.println(" Wins: " + stats.getWins());
System.out.println(" ~Elo: " + stats.getApproximateElo());
} else {
System.out.println("NOT IN LEAGUE");
}

System.out.println();

summoner = summonerWithRankedStats.get();
LeagueChampion champ = LeagueChampion.getChampionWithName("Elise");
Map<LeagueRankedStatType, Integer> statsMap = summoner.getRankedStats().getAllStatsForChampion(champ);
if(stats == null) {
System.out.println("No stats for " + champ);
} else {
System.out.println("All stats for " + champ + ":");
for(LeagueRankedStatType type : LeagueRankedStatType.values())
System.out.println(" " + type + " = " + statsMap.get(type));
}

System.out.println();

summoner = summonerWithGameInfo.get();
if(summoner.getActiveGame() != null) {
LeagueGame game = summoner.getActiveGame();
System.out.println("PLAYER TEAM (" + game.getPlayerTeamType() + "):");
for(LeagueSummoner sum : summoner.getActiveGame().getPlayerTeam())
System.out.println(" " + sum);
System.out.println("ENEMY TEAM (" + game.getEnemyTeamType() + "):");
for(LeagueSummoner sum : summoner.getActiveGame().getEnemyTeam())
System.out.println(" " + sum);
System.out.println("PLAYER TEAM BANS:");
for(LeagueChampion champion : game.getBannedChampionsForTeam(game.getPlayerTeamType()))
System.out.println(" " + champion.getName());
System.out.println("ENEMY TEAM BANS:");
for(LeagueChampion champion : game.getBannedChampionsForTeam(game.getEnemyTeamType()))
System.out.println(" " + champion.getName());
} else {
System.out.println("NOT IN GAME");
}


System.out.println();
System.out.flush();
}
}