Skip to content

Instantly share code, notes, and snippets.

@AgiosAndreas
Created October 4, 2012 10:25
Show Gist options
  • Save AgiosAndreas/3832763 to your computer and use it in GitHub Desktop.
Save AgiosAndreas/3832763 to your computer and use it in GitHub Desktop.
package ru.inn.autotests.pixelwars.scenaries;
import ru.inn.autotests.pixelwars.common.BaseTestScenarioController;
import ru.inn.autotests.pixelwars.common.Debugger;
import ru.inn.autotests.pixelwars.common.PrConstants;
import ru.inn.autotests.pixelwars.common.entities.PixelUser;
import ru.inn.autotests.pixelwars.common.user.RequestFactory;
import ru.inn.autotests.pixelwars.common.user.UserFactory;
import ru.inn.autotests.pixelwars.protos.GameObject;
import ru.inn.autotests.pixelwars.protos.Result;
import ru.inn.autotests.pixelwars.protos.Users;
/**
* @author Aleksey Niss
*/
public class BasicUserScenario extends BaseTestScenarioController implements Runnable{
private static Debugger debug = Debugger.getInstance();
private PixelUser pUser;
@Override
public void run(){
init();
createUserScenario();
debug.notice("Bot started!");
userScenarioRepeated();
}
//Preconditions etc
private void init (){
}
//Scenario one time run
private void createUserScenario () {
this.pUser = UserFactory.createBasicUser();
Users.LoginResult loginResult = loginUser(RequestFactory.getRegisterObject(pUser), "/User/Register");
Result.OperationResult loginOperationResult = loginResult.getOperationResult();
this.pUser.setSessionKey(loginResult.getSessionKey());
this.pUser.setUserId(loginResult.getUserId());
this.pUser.setUserPixels(loginOperationResult.getUserPixels());
this.pUser.setUserMaxPixels(loginOperationResult.getUserMaxPixels());
this.pUser.setTeleportCount(loginOperationResult.getTeleportCount());
sleep(1000); // 1 sec
loginResult = loginUser(RequestFactory.getLoginObject(pUser), "/User/Login");
this.pUser.setSessionKey(loginResult.getSessionKey());
sleep(1000); // 1 sec
}
//Scenario repeated run
//TODO Make this method in right way
private void userScenarioRepeated () {
boolean out = true; //TODO control from main thread? Nah, not now
while (out) {
validateSession ();
GameObject.WhatsUpResult whatsUpResult = whatsUp(RequestFactory.getWhatsUpObject(pUser), "/Objects/WhatsUp");
sleep(PrConstants.DELAY);
for (int i = 0; i < 9; i++) {
GameObject.GetGameObjectsRequest getGameObjectsReq = RequestFactory.getGameObjects(PrConstants.getGameObjects(), pUser);
GameObject.GetGameObjectsResult getGameObjectsResult = getGameObjects(getGameObjectsReq, "/Objects/GetGameObjects");
int max = getGameObjectsResult.getGameObjectsCount();
if (max == 0) {
debug.error("Bad request: " + getGameObjectsReq.toString());
debug.error("Received empty game object list, initating scenario restart. GetGameObjectsRequest: " + RequestFactory.getGameObjects(PrConstants.getGameObjects(), pUser)
+ ", GetGameObjectsResult: " + getGameObjectsResult.getOperationResult().getResult().toString() + " SessionKey: " + pUser.getSessionKey() );
out = true;
break;
}
int object2atack = (int)(Math.random() * (max)) ;
GameObject.GameObjectProto gameObject = getGameObjectsResult.getGameObjects(object2atack);
sleep(PrConstants.DELAY);
if (pUser.getUserPixels() > (pUser.getUserMaxPixels() / 10)) {
if (gameObject.getOwnerId().equals(pUser.getUserId())) {
GameObject.ActionRequest req = RequestFactory.getActionRequest(gameObject, pUser, PrConstants.ActionTypes.DEFENSE);
GameObject.DefenseResult res = defense(req, "/Objects/Defense");
pUser.setUserPixels(res.getOperationResult().getUserPixels());
pUser.setUserMaxPixels(res.getOperationResult().getUserMaxPixels());
}
else {
boolean isFree = (gameObject.getGameObjectId() == null || gameObject.getGameObjectId().isEmpty());
GameObject.ActionRequest req = RequestFactory.getActionRequest(gameObject, pUser,
isFree ? PrConstants.ActionTypes.DIGITALIZE : PrConstants.ActionTypes.ATTACK);
GameObject.AttackResult res = attack(req, isFree ? "/Objects/Digitalize" : "/Objects/Attack");
pUser.setUserPixels(res.getOperationResult().getUserPixels());
pUser.setUserMaxPixels(res.getOperationResult().getUserMaxPixels());
}
} else {
createUserScenario();
break;
}
sleep(PrConstants.DELAY);
}
}
}
private boolean validateSession () {
if (this.pUser.getSessionKey() == "") {
debug.error("Session key not avaiable...");
debug.error("Recreating user due to session error...");
createUserScenario ();
return false;
} else {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment