userAgent = escapeHtml(userAgent);
List<CommunityAccount> listCommunityAccount = new ArrayList<CommunityAccount>();
AllCommunityAccount myAllCommunityAccount = new AllCommunityAccount ();
myAllCommunityAccount.setListCommunityAccount(listCommunityAccount);
PersistenceManager pm =null;
try {
pm = PMF.get().getPersistenceManager();
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;
}
//System.out.println(AllLinesUser);
readerUser.close();
Gson gsonUser = new Gson();
//log.info(AllLinesUser.substring(0, 200));
PlayerRatings playerRatings = gsonUser.fromJson(AllLinesUser, PlayerRatings.class);
//playerRatings.setIdUser(idUser);
//Transform playerRatings en communityAccount (pour utiliser des types compatibles avec la sérialisation (pas de MAP !!))
List<CommunityAccount> listCommunityAccount1 = TransformDtoObject.TransformPlayerRatingsToListCommunityAccount(playerRatings);
//////////////////////////////////
// ==API encyclopédie des tanks - Pour obtenir le level des char (on doit calculier le tier moyen joué)
// Dans les stats des joueurs nous avons le tank-id mais pas son level
//http://api.worldoftanks.eu/2.0/encyclopedia/tanks/?application_id=d0a293dc77667c9328783d489c8cef73
/*
* {"status":"ok",
* "count":336,
* "data":{
"3073":{
"nation_i18n":"U.S.S.R.","name":"#ussr_vehicles:T-46","level":3,"nation":"ussr","is_premium":false,"name_i18n":"T-46","type":"lightTank","tank_id":3073}
,"6417":{
"nation_i18n":"Germany","name":"#germany_vehicles:PzIII_IV","level":5,"nation":"germany","is_premium":false,"name_i18n":"Pz.Kpfw. III\/IV","type":"mediumTank","tank_id":6417}
*/
//=======================
// if (tankEncyclopedia == null) {
// urlServer = urlServerEU +"/2.0/encyclopedia/tanks/?application_id=" + applicationIdEU ;
// url = new URL(urlServer);
//
// 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;
// }
// readerUser.close();
//
// gsonUser = new Gson();
// tankEncyclopedia = gsonUser.fromJson(AllLinesUser, TankEncyclopedia.class);
// }
// 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();
//
// // for(Entry<String, DataTankEncyclopedia> entry : set) {
// // log.warning(entry.getValue().getName());
// // log.warning(entry.getKey());
// // }
// if (tankEncyclopedia.getData().get("6417") == null ){
// log.severe("tankEncyclopedia data get tank id 1 is null" );
// }
// }
//
// }
//=========================
// //API stats des joueurs ========================
// //PB access denied sometimes
// //http://api.worldoftanks.eu/2.0/stats/accountbytime/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=500423304&hours_ago=24
// //http://api.worldoftanks.ru/2.0/account/ratings/?application_id=171745d21f7f98fd8878771da1000a31&account_id=461
//
// // ==API stats des véhicules ==
// //http://api.worldoftanks.eu/2.0/account/tanks/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=506486576,502674600
// 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;
// }
// readerUser.close();
// gsonUser = new Gson();
// PlayerTankRatings playerTankRatings = gsonUser.fromJson(AllLinesUser, PlayerTankRatings.class);
//=========================
//on requete notre base les averageLevel y ont été stockés par le cron dans la table community Account
Map <String, CommunityAccount> mapCommAcc = getMapHistorizedStatsUsers(listIdUser, 2);
/////////////////////////////////
//make some calculation of stats
for(CommunityAccount communityAccount : listCommunityAccount1) {
String user_id = communityAccount.getIdUser();
//
// if(playerVehicles != null) {
// if (playerVehicles.getData() != null ) {
// for (DataPlayerVehicles myDataPlayerVehicles : playerVehicles.getData()) {
// if ( user_id == myDataPlayerVehicles.getStat().getAccount_id() ) {
// for (DataStatVehicle myDataStatVehicle : myDataPlayerVehicles.getStat().getVehicles() ) {
// log.info(""+ myDataStatVehicle.getTank_id());
// }
// }
// }
// }else {
// log.severe("playerVehicles.getData() is null ");
// }
// } else {
// log.severe("playerVehicles is null ");
// }
//Rechercher dans playerTankRatings, les batailles sur tous les char du joueur ( pour caluler son tier moyen)
//
//Map<String,List<DataPlayerTankRatings>> mapDataPlayerTankRatings = playerTankRatings.getData();
//Set<Entry<String, List<DataPlayerTankRatings>>> setData = mapDataPlayerTankRatings.entrySet();
//setData.
log.warning("mapCommAcc " + mapCommAcc);
CommunityAccount commAcc= mapCommAcc.get(user_id);
//calcul du tier moyen
//Double nbBattles = 0.0;
//Double levelByBattles = 0.0 ;
log.warning("commAcc " + commAcc);
log.warning("commAcc.getData() " + commAcc.getData());
///log.warning("commAcc.getData().getAverageLevel " + commAcc.getData().getAverageLevel);
Double averageLevelTank =commAcc.getData().getAverageLevel();
// 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 ");
//
// int levelTank = tankEncyclopedia.getData().get(String.valueOf(tankId)).getLevel();
// //
// nbBattles = nbBattles + battles;
// levelByBattles =levelByBattles + levelTank * battles;
// }
// averageLevelTank = levelByBattles/nbBattles;
DataCommunityAccountRatings myDataCommunityAccountRatings = communityAccount.getData();
//average level tank
myDataCommunityAccountRatings.setAverageLevel(averageLevelTank);
//log.info("averageLevelTank" + averageLevelTank);
//== WR calculated
int battles = myDataCommunityAccountRatings.getBattles();
int battlesWin = myDataCommunityAccountRatings.getBattle_wins();
Double wrCal = (double) ((double)battlesWin/(double)battles);
//on ne conserve que 2 digits après la virgule
wrCal = wrCal * 100; //ex : 51,844444
int intWrCal = (int) (wrCal * 100); //ex : 5184
wrCal = (double)intWrCal / 100 ; //ex : 51,84
myDataCommunityAccountRatings.setBattle_avg_performanceCalc(wrCal);
//== Ratio capture points calculated
int ctfPoints = myDataCommunityAccountRatings.getCtf_points();
Double ctfPointsCal = (double) ((double)ctfPoints/(double)battles);// 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
myDataCommunityAccountRatings.setRatioCtfPoints(ctfPointsCal);
//==Damage Ration calculated
int damagePoints = myDataCommunityAccountRatings.getDamage_dealt();
Double ratioDamagePoints = (double) ((double)damagePoints/(double)battles);
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intRatioDamagePoints = (int) (ratioDamagePoints * 100); //ex : 128,27
ratioDamagePoints = (double)intRatioDamagePoints / 100 ; //ex : 1,28
myDataCommunityAccountRatings.setRatioDamagePoints(ratioDamagePoints);
//==Ratio Defense calculated
int droppedCtfPoints = myDataCommunityAccountRatings.getDropped_ctf_points();
Double ratioDroppedCtfPoints = (double) ((double)droppedCtfPoints/(double)battles);
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intRatioDroppedCtfPoints = (int) (ratioDroppedCtfPoints * 100); //ex : 128,27
ratioDroppedCtfPoints = (double)intRatioDroppedCtfPoints / 100 ; //ex : 1,28
myDataCommunityAccountRatings.setRatioDroppedCtfPoints(ratioDroppedCtfPoints);
//==Ratio Destroyed calculated
int destroyedPoints = myDataCommunityAccountRatings.getFrags();
Double ratiodestroyedPoints = (double) ((double)destroyedPoints/(double)battles);
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intRatiodestroyedPoints = (int) (ratiodestroyedPoints * 100); //ex : 128,27
ratiodestroyedPoints = (double)intRatiodestroyedPoints / 100 ; //ex : 1,28
myDataCommunityAccountRatings.setRatioDestroyedPoints(ratiodestroyedPoints);
//==Ratio Detected calculated
int detectedPoints = myDataCommunityAccountRatings.getSpotted();
Double ratioDetectedPoints = (double) ((double)detectedPoints/(double)battles);
//on ne conserve que 2 digits après la virgule
//ctfPointsCal = ctfPointsCal * 100; //ex : 1,2827
int intRatioDetectedPoints = (int) (ratioDetectedPoints * 100); //ex : 128,27
ratioDetectedPoints = (double)intRatioDetectedPoints / 100 ; //ex : 1,28
myDataCommunityAccountRatings.setRatioDetectedPoints(ratioDetectedPoints);
}
//add account
listCommunityAccount.addAll(listCommunityAccount1);
} catch (MalformedURLException e) {
// ...
e.printStackTrace();
} catch (IOException e) {
// ...
e.printStackTrace();
}
finally {
pm.close();
}
return myAllCommunityAccount;
}