log.warning("========lancement cronPersistAllStats : " + date + ":" + idClan + " :============== " );
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)
//=======================
generateTankEncyclopedia();
//-----------recup�ration des stats moyennes par TANK sur Noobmeter ---------
// n�cessaire pour le calcul du wn8
// url : http://www.wnefficiency.net/exp/expected_tank_values_latest.json
// retour de l'URL
/*
* {"header":{"version":14},
* "data":[{"IDNum":"3089","expFrag":"2.11","expDamage":"278.00","expSpot":"2.35","expDef":"1.84","expWinRate":"59.54"},
* {"IDNum":"3329","expFrag":"2.10","expDamage":"270.00","expSpot":"1.55","expDef":"1.81","expWinRate":"60.46"},
* {"IDNum":"577","expFrag":"2.01","expDamage":"268.00","expSpot":"2.12","expDef":"2.17","expWinRate":"60.24"},
* {"IDNum":"1329","expFrag":"2.05","expDamage":"274.00","expSpot":"1.51","expDef":"2.10","expWinRate":"60.00"},
*/
generateWnEfficientyTank();
//construction de la liste des id des joueurs du clan (s�parateur la ,)
//String AllIdUser = generateAllIdUsers(idClan, date);
//Tous les joueurs du clan sont stockés dans cette hashMap <hMIdUser> : id joueur / nom joueur
HashMap<String , String> hMIdUser = generateHMAllIdUsers(idClan, date);
String AllIdUser ="";
for(String idUser :hMIdUser.keySet()) {
if ("".equalsIgnoreCase(AllIdUser))
AllIdUser = idUser;
else
AllIdUser = AllIdUser + "," + idUser;
}
//=== recup des stats des joueurs ==========
URL url = null ;
String urlServer = urlServerEU +"/wot/account/info/?application_id=" + applicationIdEU + "&account_id=";
//http://api.worldoftanks.ru/2.0/account/ratings/?application_id=171745d21f7f98fd8878771da1000a31&account_id=506486576
//nouvelle URL car ancienne obsolète
// https://api.worldoftanks.eu/wot/account/info/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=506486576
if(WotServiceImpl.lieu.equalsIgnoreCase("boulot")){ //on passe par 1 proxy
url = new URL(WotServiceImpl.proxy + urlServer + 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();
PlayersInfos playersInfos = gsonUser.fromJson(AllLinesUser, PlayersInfos.class);
//Transform playerRatings en communityAccount (pour utiliser des types compatibles avec la s�rialisation (pas de MAP !!))
List<DataPlayerInfos> listPlayerInfos = TransformDtoObject.TransformPlayerInfosToListDataPlayerInfos(playersInfos);
////////////////////
//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(WotServiceImpl.lieu.equalsIgnoreCase("boulot")){ //on passe par 1 proxy
url = new URL(WotServiceImpl.proxy + urlServer + 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, 50));
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(DataPlayerInfos dataPlayerInfos : listPlayerInfos) {
//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() " + dataPlayerInfos.getAccount_id());
List<DataPlayerTankRatings> listPlayerTanksRatings = mapDataPlayerTankRatings.get(String.valueOf(dataPlayerInfos.getAccount_id()));
if(listPlayerTanksRatings == null)
continue ;
//calcul du tier moyen
Double nbBattles = 0.0;
Double levelByBattles = 0.0 ;
Double averageLevelTank =0.0;
//frag, dmg, spot def, xp, wr
double totalExpFrag = 0;
double totalExpDmg = 0;
double totalExpSpot = 0;
double totalExpDef = 0;
//int totalExpXp = 0;
double totalExpWr = 0;
//
AllStatistics allStatistics = dataPlayerInfos.getStatistics().getAllStatistics();
int ActualFrag = allStatistics.getFrags();
int ActualDmg = allStatistics.getDamage_dealt() ;
int ActualSpot = allStatistics.getSpotted() ;
int ActualDef = allStatistics.getDropped_capture_points() ;
double ActualWr = Double.valueOf(allStatistics.getWins())/ Double.valueOf(allStatistics.getBattles());
//
double rFrag = 0 ;
double rDmg = 0 ;
double rSpot = 0 ;
double rDef = 0 ;
double rWr = 0;
//
double rFragC = 0 ;
double rDmgC = 0 ;
double rSpotC = 0 ;
double rDefC = 0 ;
double rWrC = 0;
//
double wn8 = 0;
//---calculate WN8 -----
for (DataPlayerTankRatings dataPlayerTankRatings : listPlayerTanksRatings) {
try {
int tankId= dataPlayerTankRatings.getTank_id();
int tankBattles =0 ;
tankBattles = dataPlayerTankRatings.getStatistics().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 ");
continue;
}
else {
int levelTank = tankEncyclopedia.getData().get(String.valueOf(tankId)).getLevel();
//
nbBattles = nbBattles + tankBattles;
levelByBattles =levelByBattles + levelTank * tankBattles;
}
//for each tank do sum of frag, dmg, spot def, xp, wr
//In wnEfficientyTank we have the expected values for each tank
//
// for (DataWnEfficientyTank dataWnEfficientyTank : wnEfficientyTank.getData()) {
// //dataWnEfficientyTank.
//
// }
//for each tank do the sum of frag, dmg, spot def, xp, wr
DataWnEfficientyTank dataWnEfficientyTank = hMapWnEfficientyTankHashMap.get(String.valueOf(tankId));
if (dataWnEfficientyTank != null) {
// takes the counts of tanks played on account, and multiplies them by the expected stats to get the account total expected values.
totalExpFrag = totalExpFrag + Double.valueOf(dataWnEfficientyTank.getExpFrag()) * tankBattles;
totalExpDmg = totalExpDmg + Double.valueOf(dataWnEfficientyTank.getExpDamage()) * tankBattles;
totalExpSpot = totalExpSpot + Double.valueOf(dataWnEfficientyTank.getExpSpot()) * tankBattles;
totalExpDef = totalExpDef + Double.valueOf(dataWnEfficientyTank.getExpDef()) * tankBattles;
totalExpWr = totalExpWr + Double.valueOf(dataWnEfficientyTank.getExpWinRate()) * tankBattles;
}
//
/*
Very Bad below 500 below 300
Bad 500 - 699 300 - 599
Below Average 700 - 899 600 - 899
Average 900 - 1099 900 - 1249
Good 1100 - 1349 1250 - 1599
Very Good 1350 - 1499 1600 - 1899
Great 1500 - 1699 1900 - 2349
Unicum 1700 - 1999 2350 - 2899
Super Unicum 2000 and above 2900 and above
*/
}catch (Exception e) {
break ;
}
}//for
//Then the actual account totals (your total dmg, frags, spots, def, win-rate) are divided by the total expected values to give the ratios.
rFrag = ActualFrag /totalExpFrag ;
rDmg = ActualDmg /totalExpDmg ;
rSpot = ActualSpot / totalExpSpot;
rDef = ActualDef / totalExpDef;
rWr = ActualWr*100*allStatistics.getBattles() / totalExpWr;//0.50 * 100 = 50
//Step 2 ---
// sets the zero point for the ratios. See the assumptions section for more info on why this happen.
//min and max are functions to ensure the ratios stay within bounds. The constants are in the format of
//(rSTAT � constant) / (1 � constant)
/*
* rWINc = max(0, (rWIN - 0.71) / (1 - 0.71) )
rDAMAGEc = max(0, (rDAMAGE - 0.22) / (1 - 0.22) )
rFRAGc = max(0, min(rDAMAGEc + 0.2, (rFRAG - 0.12) / (1 - 0.12)))
rSPOTc = max(0, min(rDAMAGEc + 0.1, (rSPOT - 0.38) / (1 - 0.38)))
rDEFc = max(0, min(rDAMAGEc + 0.1, (rDEF - 0.10) / (1 - 0.10)))
*/
rWrC = Math.max(0, (rWr - 0.71) / (1 - 0.71) );
rDmgC = Math.max(0, (rDmg - 0.22) / (1 - 0.22) );
rFragC = Math.max(0, Math.min(rDmgC + 0.2, (rFrag - 0.12) / (1 - 0.12)));
rSpotC = Math.max(0, Math.min(rDmgC + 0.1, (rSpot - 0.38) / (1 - 0.38)));
rDefC = Math.max(0, Math.min(rDmgC + 0.1, (rDef - 0.10) / (1 - 0.10)));
//
//Step 3
//WN8 = 980*rDAMAGEc + 210*rDAMAGEc*rFRAGc + 155*rFRAGc*rSPOTc + 75*rDEFc*rFRAGc + 145*MIN(1.8,rWINc)
wn8 = 980*rDmgC + 210*rDmgC*rFragC + 155*rFragC*rSpotC + 75*rDefC*rFragC + 145*Math.min(1.8,rWrC);
//
//
averageLevelTank = levelByBattles/nbBattles;
//AllStatistics myDataCommunityAccountRatings = dataPlayerInfos.getStatistics().getAllStatistics();
// set wn8
allStatistics.setWn8(wn8);
//average level tank
allStatistics.setAverageLevelTankCalc(averageLevelTank);
//
//== Ratio capture points calculated ================
int ctfPoints = allStatistics.getCapture_points();
Double ctfPointsCal = (double) ((double)ctfPoints/(double)allStatistics.getBattles());// 1,28 :1 = 1.28
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intCtfPointsCal = (int) (ctfPointsCal * 100); //ex intCtfPointsCal : 128,27 ctfPointsCal = 1.28
ctfPointsCal = (double)intCtfPointsCal / 100 ; //ex ctfPointsCal : 1,28 intCtfPointsCal = 128
allStatistics.setRatioCtfPoints(ctfPointsCal);
//======================================================
//== Ratio DMG points calculated ================
int points = allStatistics.getDamage_dealt();
Double pointsCal = (double) ((double)points/(double)allStatistics.getBattles());// 1,28 :1 = 1.28
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intPointsCal = (int) (pointsCal * 100); //ex intCtfPointsCal : 128,27 ctfPointsCal = 1.28
pointsCal = (double)intPointsCal / 100 ; //ex ctfPointsCal : 1,28 intCtfPointsCal = 128
allStatistics.setRatioDamagePoints(pointsCal);
//======================================================
//== Ratio Destroyed points calculated ================
points = allStatistics.getFrags();
pointsCal = (double) ((double)points/(double)allStatistics.getBattles());// 1,28 :1 = 1.28
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
intPointsCal = (int) (pointsCal * 100); //ex intCtfPointsCal : 128,27 ctfPointsCal = 1.28
pointsCal = (double)intPointsCal / 100 ; //ex ctfPointsCal : 1,28 intCtfPointsCal = 128
allStatistics.setRatioDestroyedPoints(pointsCal);
//======================================================
//== Ratio Detected points calculated ================
points = allStatistics.getSpotted();
pointsCal = (double) ((double)points/(double)allStatistics.getBattles());// 1,28 :1 = 1.28
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
intPointsCal = (int) (pointsCal * 100); //ex intCtfPointsCal : 128,27 ctfPointsCal = 1.28
pointsCal = (double)intPointsCal / 100 ; //ex ctfPointsCal : 1,28 intCtfPointsCal = 128
allStatistics.setRatioDetectedPoints(pointsCal);
//======================================================
//== Ratio Dropped capture points calculated ================
points = allStatistics.getDropped_capture_points();
pointsCal = (double) ((double)points/(double)allStatistics.getBattles());// 1,28 :1 = 1.28
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
intPointsCal = (int) (pointsCal * 100); //ex intCtfPointsCal : 128,27 ctfPointsCal = 1.28
pointsCal = (double)intPointsCal / 100 ; //ex ctfPointsCal : 1,28 intCtfPointsCal = 128
allStatistics.setRatioDroppedCtfPoints(pointsCal);
//======================================================
//=== Battle_avg_performance win/batttles ====
//avg perf : ratio wins/battles
Double perf = new Double(allStatistics.getWins())/ new Double(allStatistics.getBattles());
allStatistics.setBattle_avg_performanceCalc(perf);
//=============================================
if (true){
//pm = PMF.get().getPersistenceManager();
try {
//must transform before persist the objet
pm.currentTransaction().begin();
DaoCommunityAccount2 daoCommunityAccount2 = TransformDtoObject.TransformCommunityAccountToDaoCommunityAccount(dataPlayerInfos);
//pour eviter trop de donn�es en base 60 write OP
//daoCommunityAccount.getData().setAchievements(null);
daoCommunityAccount2.setDateCommunityAccount(date);
//
pm.makePersistent(daoCommunityAccount2);
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(String.valueOf(dataPlayerInfos.getAccount_id()));
}
catch(Exception e){
e.printStackTrace();
log.log(Level.SEVERE, "Exception while saving daoCommunityAccount", e);
pm.currentTransaction().rollback();
}
}
}
} catch (MalformedURLException e) {
// ...
log.throwing("Persist stats", "", e);
log.severe("MalformedURLException " + e.getLocalizedMessage());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// ...
log.throwing("Persist stats", "", e);
log.severe("IOException " + e.getLocalizedMessage());
e.printStackTrace();
} catch (Exception e) {
// ...
e.printStackTrace();
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;
}