| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 
 | package surveillance;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
 
public class Ouroboros {
	public static String[] mat ;
	public static String login;
	public static String mdp;
	public static String configPath=System.getProperty("user.dir") ;
	public static String separator="   ";
	public static String nom="";
 
	final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
	HtmlPage page;
 
	public Ouroboros () {
		new Matricules();
		new Identifiants();
 
		try {
			FSProject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
	public static String now() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat day = new SimpleDateFormat("dd.MM.yy");
		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
		return day.format(cal.getTime())+" "+time.format(cal.getTime());
 
	}
 
	/**
         * Check si serveur pas en maintenance ou deconnexion pas perdue
         * @return true si connexion toujours ok, faux sinon
         * @throws Exception
         */
	public boolean estConnecte() throws Exception{
		String textPage=page.getBody().getTextContent();
		return (!textPage.contains("Serveur en cours de Maintenance") && !textPage.contains("Vous devez vous identifier pour accéder") && !textPage.contains("Internal Server Error"));
	}
 
	/**
         * Connexion 
         * @return true si connexion reussie, faux sinon
         * @throws Exception
         */
	public boolean connect() throws Exception {
 
		boolean connexionOk=false;
		HtmlForm form;
		HtmlSubmitInput button;
		HtmlTextInput textFieldLogin;
		HtmlPasswordInput textFieldCode;
 
		page=(HtmlPage)webClient.getPage("http://www.demange-le-jeu.com/joueur_accueil.php");
 
		// vérif qu'on est sur la page de connexion
 
		if (page.getBody().getTextContent().contains("Vous avez oublié votre mot de passe:")) {
			form = page.getFormByName("log");
 
			button = (HtmlSubmitInput) form.getInputByValue("Connexion");
			textFieldLogin = (HtmlTextInput) form.getInputByName("joueur_email");
			textFieldCode = (HtmlPasswordInput) form.getInputByName("code");
 
			textFieldLogin.setValueAttribute(login);
			textFieldCode.setValueAttribute(mdp);
 
			page = (HtmlPage) button.click();
 
		}
 
		if(page.getBody().getTextContent().contains("Jouer ce personnage")) {
			connexionOk = true;
		}
 
		return connexionOk;
	}
 
	/**
         * Si deconnexion, tentative de reco maxTourConnex de fois
         * @throws Exception
         */
	public boolean reconnect() throws Exception{
		while (!connect()) {
			// pause de 10min entre les tentatives de reconnexion
			Thread.sleep(1000*60*10);
			System.out.println("Tentative de reconnexion");
		}
		return estConnecte();
	}
 
	public boolean verifConnect() throws Exception{
		if(!estConnecte()) {
			if(reconnect()){
				System.out.println("Reconnexion reussie");
			}
		}
		return estConnecte();
	}
 
	/**
         * Creation des fichiers de suivi pour les matricules que contient la liste des matricules
         * @return true si tous les fichiers bien crees, faux sinon
         * @throws Exception
         */
	public boolean initLogFiles() throws Exception{
		int i=0;
 
		// premier tour de piste, init des fichiers de log
		while(verifConnect() && i<mat.length) {
			page=null;
			page=(HtmlPage) webClient.getPage("http://www.demange-le-jeu.com/perso_events_view.php?id_perso="+mat[i]);
 
			// ecriture du nom et du matricule en tête du fichier
			HtmlTable table = (HtmlTable)page.getElementsByTagName("table").item(1); 
 
			if (table.getRow(1).asText().contains("Matricule")){
				// personnage sans photo
				writer(mat[i],table.getRow(0).asText()+" - "+mat[i]);
			}else{
				// personnage avec photo
				writer(mat[i],table.getRow(1).asText()+" - "+mat[i]);
			}
 
			writer(mat[i]," ");
 
			// pause pour laisser le temps au programme de respirer un coup
			// et éviter au pc de ramer... pauvre bestiole...
			Thread.sleep(2000);
 
			i++;
		}
 
		verifConnect();
 
		return i<mat.length;
	}
 
	/**
         * Place les infos necessaires dans les fichiers de log de toute la liste des matricules
         * @throws Exception
         */
	public void recolteInfos() throws Exception {
		int i=0;
 
 
		// boucle de recolte des xp
		while(verifConnect()) {
			while(verifConnect() && i<mat.length) {
				page=null;
				page=(HtmlPage) webClient.getPage("http://www.demange-le-jeu.com/perso_events_view.php?id_perso="+mat[i]);
 
				String textPage=page.getBody().getTextContent();
				// verification connexion et qu'on est sur la page de jeu d'un perso
				if(verifConnect() && (textPage.contains("Membre des factions :")||textPage.contains("Membre de la faction :"))) {
					final HtmlTable table = (HtmlTable)page.getElementsByTagName("table").item(1); 
					int k=0;
					while(k<table.getRowCount() && !table.getRow(k).getCell(0).asText().contains("Expérience")){
						k++;
					}
					if(k<table.getRowCount()){
						writer(mat[i],now()+separator+table.getRow(k).getCell(1).asText());
					}else{
						System.out.println("Probleme ! Champ Experience absent pour le matricule :"+mat[i]);
						writer(mat[i],now()+"Probleme ! Champ Experience absent");
					}
 
				} 
				Thread.sleep(2000);
				i++;
			}
 
			i=0;
 
			// nettoyage des fichiers de log
			//sheWashSheWash();
 
			System.out.println("Nouveau tour de surveillance effectue "+now());
 
			// appel du garbage collector
			//System.gc();
 
			// pause entre les moments de check globaux : environ 1h, equivalent a Thread.sleep(1000*60*60)
			for (int j=0;j<60*59;j++)
			{
			//for (int j=0;j<8;j++){
				Thread.sleep(100);
			}
 
			verifConnect();
		}
 
	}
 
 
 
	/**
         * connexion au site demange
         * initialisation des fichiers de log
         * surveillance
         */
	public void FSProject() {
		try {
			System.out.println("Tentative de connexion au site...");
			System.out.println(".........");
			reconnect();
			System.out.println("Connexion ok");
			System.out.println("");
			System.out.println("Initialisation des fichiers de surveillance...");
			System.out.println(".........");
			initLogFiles();
			System.out.println("initlog ok");
			System.out.println("");
			System.out.println("Debut de la surveillance");
			recolteInfos();
			System.out.println("FIN SURVEILLANCE OK");
			Thread.sleep(60000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
 
			System.out.println("FIN SURVEILLANCE !!!PROBLEME D EXECUTION!!!");
			e.printStackTrace();
			try {
				Thread.sleep(60000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
 
	}
 
	/**
         * Permet d'ecrire dans le fichier correspondant au matricule "matricule", la string "toWrite"
         * @param matricule
         * @param toWrite
         */
	public void writer(String matricule, String toWrite) {
		PrintWriter writer = null;
		FileWriter fw=null;
		try {
			fw=new FileWriter(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+matricule+".txt",true);
			writer = new PrintWriter(fw,true);
 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		writer.println(toWrite);
 
		writer.flush();
 
		try {
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		writer.close();
 
	}
 
 
	/**
         * Nettoie les fichiers de log des donnees inutiles
         */
	public void sheWashSheWash() {
		String t1;String t1b;
		String t2;String t2b;
		String t3;String t3b;
		for(int i=0; i<mat.length;i++) {
			t1="";t1b="";
			t2="";t2b="";
			t3="";t3b="";
			try {
				BufferedReader is = new BufferedReader(new FileReader(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+mat[i]+".txt"));
				PrintWriter writer = new PrintWriter(new FileWriter(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+mat[i]+".tmp"));
 
				// lecture ecriture du pseudo - matricule
				t1=is.readLine();
				writer.println(t1);
				// lecture de la ligne vide
				t2=is.readLine();
 
				while((t3=is.readLine())!=null){
					t1b=t1.split(Ouroboros.separator).length==2?t1.split(Ouroboros.separator)[1]:t1.split(Ouroboros.separator)[0];
					t2b=t2.split(Ouroboros.separator).length==2?t2.split(Ouroboros.separator)[1]:t2.split(Ouroboros.separator)[0];
					t3b=t3.split(Ouroboros.separator).length==2?t3.split(Ouroboros.separator)[1]:t3.split(Ouroboros.separator)[0];
					if(!(t1b.compareTo(t2b)==0 && t2b.compareTo(t3b)==0)){
						writer.println(t2);
					}
 
					t1=t2;
					t2=t3;
				}
				writer.println(t2);
 
				writer.close();
				is.close();
 
				new File(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+mat[i]+".txt").delete();
				new File(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+mat[i]+".tmp").renameTo(new File(Ouroboros.configPath+"/oracle/"+Ouroboros.nom+"/"+mat[i]+".txt"));
 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 
 
 
 
 
 
	public static void main(String [] args) {
		new Ouroboros();
	}
 
} |