List<CommunityAccount> listCommunityAccount = new ArrayList<CommunityAccount>();
AllCommunityAccount myAllCommunityAccount = new AllCommunityAccount ();
myAllCommunityAccount.setListCommunityAccount(listCommunityAccount);
PersistenceManager pm =null;
pm = PMF.get().getPersistenceManager();
List<String> listIdUser = new ArrayList<String>();
try {
///Recuperation de l'encyclop�die des tanks ( n�cessaire pour connaitre le level de chaque char ) (pour calcul average level)
//=======================
if (tankEncyclopedia == null) {
String urlServer = urlServerEU +"/2.0/encyclopedia/tanks/?application_id=" + applicationIdEU ;
URL url = new URL(urlServer);
HttpURLConnection conn2 = (HttpURLConnection)url.openConnection();
conn2.setReadTimeout(20000);
conn2.setConnectTimeout(20000);
conn2.getInputStream();
BufferedReader readerUser = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
String lineUser = "";
String AllLinesUser = "";
while ((lineUser = readerUser.readLine()) != null) {
AllLinesUser = AllLinesUser + lineUser;
}
readerUser.close();
Gson gsonUser = new Gson();
tankEncyclopedia = gsonUser.fromJson(AllLinesUser, TankEncyclopedia.class);
}
//contr�le --------
if (tankEncyclopedia == null) {
log.severe("tankEncyclopedia is null" );
}
else {
log.warning("tankEncyclopedia is good" );
if (tankEncyclopedia.getData() ==null ) {
log.severe("tankEncyclopedia data is null" );
}
else {
Set<Entry<String, DataTankEncyclopedia>> set = tankEncyclopedia.getData().entrySet();
if (tankEncyclopedia.getData().get("6417") == null ){
log.severe("tankEncyclopedia data get tank id 6417 is null" );
}
}
}
//-----------
///
URL urlClan = null ;
// recup des membres du clan NVS
urlClan = null ;
if(lieu.equalsIgnoreCase("boulot")){ //on passe par 1 proxy
urlClan = new URL("https://pedro-proxy.appspot.com/api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id="+idClan);
}
else {
//500006074
urlClan = new URL("http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id="+idClan);
}
HttpURLConnection conn = (HttpURLConnection)urlClan.openConnection();
conn.setReadTimeout(60000);
conn.setConnectTimeout(60000);
conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
String AllLines = "";
while ((line = reader.readLine()) != null) {
AllLines = AllLines + line;
}
reader.close();
Gson gson = new Gson();
DaoCommunityClan2 daoCommunityClan = gson.fromJson(AllLines, DaoCommunityClan2.class);
daoCommunityClan.setIdClan(idClan);
daoCommunityClan.setDateCommunityClan(date);
//persist clan ?
CommunityClan communityClan = TransformDtoObject.TransformCommunityDaoCommunityClanToCommunityClan(daoCommunityClan);
if (communityClan != null) {
DataCommunityClan myDataCommunityClan = communityClan.getData();
List<DataCommunityClanMembers> listClanMembers = myDataCommunityClan.getMembers();
for (DataCommunityClanMembers dataClanMember : listClanMembers) {
for (DataCommunityMembers member : dataClanMember.getMembers()) {
log.warning("membermember " + member.getAccount_name() + " " + member.getAccount_id() );
String idUser = member.getAccount_id();
//log.warning("treatUser " + treatUser);
listIdUser.add(idUser);
}
}//for (DataCommunityClanMembers
} else {
log.severe("Erreur de parse");
}
////
String AllIdUser ="";
for(String idUser :listIdUser) {
AllIdUser = AllIdUser + "," + idUser;
}
URL url = null ;
String urlServer = urlServerEU +"/2.0/account/ratings/?application_id=" + applicationIdEU + "&account_id=";
//http://api.worldoftanks.ru/2.0/account/ratings/?application_id=171745d21f7f98fd8878771da1000a31&account_id=461
if(lieu.equalsIgnoreCase("boulot")){ //on passe par 1 proxy
url = new URL("https://pedro-proxy.appspot.com/"+urlServer.replaceAll("http://", "") + AllIdUser);
}
else {
url = new URL(urlServer + AllIdUser);
}
HttpURLConnection conn2 = (HttpURLConnection)url.openConnection();
conn2.setReadTimeout(20000);
conn2.setConnectTimeout(20000);
conn2.getInputStream();
BufferedReader readerUser = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
//BufferedReader readerUser = new BufferedReader(new InputStreamReader(url.openStream()));
String lineUser = "";
;
String AllLinesUser = "";
while ((lineUser = readerUser.readLine()) != null) {
AllLinesUser = AllLinesUser + lineUser;
}
log.warning(url + " --> " + AllLinesUser.substring(0, 400));
readerUser.close();
Gson gsonUser = new Gson();
PlayerRatings playerRatings = gsonUser.fromJson(AllLinesUser, PlayerRatings.class);
//Transform playerRatings en communityAccount (pour utiliser des types compatibles avec la s�rialisation (pas de MAP !!))
List<CommunityAccount> listCommunityAccount1 = TransformDtoObject.TransformPlayerRatingsToListCommunityAccount(playerRatings);
log.warning("TransformPlayerRatingsToListCommunityAccount done ");
////////////////////
//recup des stats de batailles par tank et par joueur (pour calcul average level) -- strong44 -- gexman47
//"http://api.worldoftanks.eu/2.0/account/tanks/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=506486576,506763437";
urlServer = urlServerEU +"/2.0/account/tanks/?application_id=" + applicationIdEU + "&account_id=";
if(lieu.equalsIgnoreCase("boulot")){ //on passe par 1 proxy
url = new URL("https://pedro-proxy.appspot.com/"+urlServer.replaceAll("http://", "") + AllIdUser);
}
else {
url = new URL(urlServer + AllIdUser);
}
//
conn2 = (HttpURLConnection)url.openConnection();
conn2.setReadTimeout(20000);
conn2.setConnectTimeout(20000);
conn2.getInputStream();
readerUser = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
lineUser = "";
AllLinesUser = "";
while ((lineUser = readerUser.readLine()) != null) {
AllLinesUser = AllLinesUser + lineUser;
}
log.warning(url + " --> " + AllLinesUser.substring(0, 400));
readerUser.close();
gsonUser = new Gson();
PlayerTankRatings playerTankRatings = gsonUser.fromJson(AllLinesUser, PlayerTankRatings.class);
//pb mapDataPlayerTankRatings is null !!!!!!
Map<String,List<DataPlayerTankRatings>> mapDataPlayerTankRatings = playerTankRatings.getData();
if(mapDataPlayerTankRatings != null )
log.warning("playerTankRatings.getData() done mapDataPlayerTankRatings is good");
else
log.warning("playerTankRatings.getData() done mapDataPlayerTankRatings is null !!!");
/////////////////////
for(CommunityAccount communityAccount : listCommunityAccount1) {
//Transform playerRatings en communityAccount (pour utiliser des types compatibles avec la s�rialisation (pas de MAP !!))
//communityAccount = TransformDtoObject.TransformPlayerRatingsToCommunityAccount(playerRatings);
//make some calculation of stats
//calcul average level
log.warning("communityAccount.getIdUser() " + communityAccount.getIdUser());
List<DataPlayerTankRatings> listPlayerTanksRatings = mapDataPlayerTankRatings.get(communityAccount.getIdUser());
//calcul du tier moyen
Double nbBattles = 0.0;
Double levelByBattles = 0.0 ;
Double averageLevelTank =0.0;
for (DataPlayerTankRatings dataPlayerTankRatings : listPlayerTanksRatings) {
int tankId= dataPlayerTankRatings.getTank_id() ;
int battles = dataPlayerTankRatings.getStatistics().getAll().getBattles();
//int wins = dataPlayerTankRatings.getStatistics().getAll().getWins();
//
//log.warning("tankId :" + tankId );
if (tankEncyclopedia.getData().get(String.valueOf(tankId)) == null )
log.severe ("tankEncyclopedia.getData().get(tankId) is null ");
else {
int levelTank = tankEncyclopedia.getData().get(String.valueOf(tankId)).getLevel();
//
nbBattles = nbBattles + battles;
levelByBattles =levelByBattles + levelTank * battles;
}
}//for
averageLevelTank = levelByBattles/nbBattles;
DataCommunityAccountRatings myDataCommunityAccountRatings = communityAccount.getData();
//average level tank
myDataCommunityAccountRatings.setAverageLevel(averageLevelTank);
//
//int battles = myDataCommunityAccountStats.getBattles();
//log.warning("userId :" + communityAccount.getIdUser() + " battles : " + battles);
if (true){
//pm = PMF.get().getPersistenceManager();
try {
//must transform before persist the objet clan
pm.currentTransaction().begin();
DaoCommunityAccount2 daoCommunityAccount = TransformDtoObject.TransformCommunityAccountToDaoCommunityAccount(communityAccount);
//pour eviter trop de donn�es en base 60 write OP
//daoCommunityAccount.getData().setAchievements(null);
daoCommunityAccount.setDateCommunityAccount(date);
//
pm.makePersistent(daoCommunityAccount);
pm.currentTransaction().commit();
//log.warning("vehicules daoCommunityAccount " + daoCommunityAccount.getData().statsVehicules.get(0).getName() + ":"+ daoCommunityAccount.getData().statsVehicules.get(0).getBattle_count() + ":"+ daoCommunityAccount.getData().statsVehicules.get(0).getWin_count());
listUsersPersisted.add(communityAccount.getIdUser());
}
catch(Exception e){
log.log(Level.SEVERE, "Exception while saving daoCommunityAccount", e);
pm.currentTransaction().rollback();
}
finally {
//pm.close();
}
}
}
// }//for (DataCommunityClanMembers
} catch (MalformedURLException e) {
// ...
log.throwing("Persist stats", "", e);
log.severe("MalformedURLException " + e.getLocalizedMessage());
e.printStackTrace();
} catch (IOException e) {
// ...
log.throwing("Persist stats", "", e);
log.severe("IOException " + e.getLocalizedMessage());
e.printStackTrace();
} catch (Exception e) {
// ...
log.throwing("Persist stats", "", e);
log.severe("Exception " + e.getLocalizedMessage());
StackTraceElement[] stack = e.getStackTrace();
for (StackTraceElement st : stack) {
log.severe(st.getMethodName()+":"+st.getLineNumber());
}
//e.printStackTrace();
}
finally {
if (pm != null)
pm.close();
}
return listUsersPersisted;
}