public void run() {
try {
Random rand=new Random();
System.out.println("Initing client");
i = index++;
SimpleClient client = new SimpleClient("client.properties");
Thread.sleep(Math.abs(rand.nextInt() % 20) * 1000);
client.connect("localhost", PORT);
AccountResult resAcc = client.createAccount("testUsername" + i, "password","email");
assertTrue("Account creation must not fail", !resAcc.failed());
assertEquals("testUsername" + i, resAcc.getUsername());
assertEquals(Result.OK_CREATED, resAcc.getResult());
Thread.sleep(Math.abs(rand.nextInt() % 100) * 1000 + 5000);
client.login("testUsername" + i, "password");
RPObject template = new RPObject();
template.put("client", "junit" + i);
CharacterResult resChar = client.createCharacter("testCharacter", template);
assertEquals("testCharacter", resChar.getCharacter());
RPObject result = resChar.getTemplate();
assertTrue(result.has("client"));
assertEquals("junit" + i, result.get("client"));
client.logout();
for (int logins = 0; logins < TIMES_TO_LOGIN; logins++) {
Thread.sleep(Math.abs(rand.nextInt() % 30) * 1000 + 5000);
client.login("testUsername" + i, "password");
String[] characters = client.getCharacters();
assertEquals(1, characters.length);
assertEquals("testCharacter", characters[0]);
boolean choosen = client.chooseCharacter("testCharacter");
assertTrue(choosen);
int amount = Math.abs(rand.nextInt() % 90) + 10;
while (client.getPerceptions() < amount) {
client.loop(0);
if (rand.nextInt() % 10 == 0) {
/*
* Send an action to server.
*/
RPAction action = new RPAction();
action.put("type", "chat");
action.put("text", "Hello world");
client.send(action);
}
if (rand.nextInt() % 1000 == 0) {
/*
* Randomly close the connection
*/
System.out
.println("FORCED CLOSE CONNECTION: Testint random disconnects on server");
client.close();
return;
}
Thread.sleep(1000);
}
client.logout();
}
client.close();
Thread.sleep(Math.abs(rand.nextInt() % 60) * 1000);
} catch (Exception e) {
System.out.println("Problem for player: testUsername" + i);
e.printStackTrace();