Advertisement
stirante

Untitled

Jul 24th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.12 KB | None | 0 0
  1. /*
  2.  *     This program is free software: you can redistribute it and/or modify
  3.  *     it under the terms of the GNU General Public License as published by
  4.  *     the Free Software Foundation, either version 3 of the License, or
  5.  *     (at your option) any later version.
  6.  *
  7.  *     This program is distributed in the hope that it will be useful,
  8.  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *     GNU General Public License for more details.
  11.  *
  12.  *     You should have received a copy of the GNU General Public License
  13.  *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15.  
  16. package com.pokegoapi.api.map.pokemon;
  17.  
  18. import POGOProtos.Enums.PokemonIdOuterClass;
  19. import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
  20. import POGOProtos.Map.Fort.FortDataOuterClass.FortData;
  21. import POGOProtos.Networking.Responses.GetIncensePokemonResponseOuterClass.GetIncensePokemonResponse;
  22. import POGOProtos.Map.Pokemon.MapPokemonOuterClass.MapPokemon;
  23. import POGOProtos.Map.Pokemon.WildPokemonOuterClass.WildPokemon;
  24. import POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage;
  25. import POGOProtos.Networking.Requests.Messages.EncounterMessageOuterClass;
  26. import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass;
  27. import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass.UseItemCaptureMessage;
  28. import POGOProtos.Networking.Requests.RequestTypeOuterClass;
  29. import POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse;
  30. import POGOProtos.Networking.Responses.EncounterResponseOuterClass;
  31. import POGOProtos.Networking.Responses.EncounterResponseOuterClass.EncounterResponse;
  32. import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass;
  33. import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass.UseItemCaptureResponse;
  34. import POGOProtos.Networking.Requests.Messages.IncenseEncounterMessageOuterClass;
  35. import POGOProtos.Networking.Responses.IncenseEncounterResponseOuterClass;
  36. import POGOProtos.Networking.Responses.IncenseEncounterResponseOuterClass.IncenseEncounterResponse;
  37. import com.google.protobuf.InvalidProtocolBufferException;
  38. import com.pokegoapi.api.PokemonGo;
  39. import com.pokegoapi.api.inventory.ItemBag;
  40. import com.pokegoapi.api.inventory.Pokeball;
  41. import com.pokegoapi.exceptions.LoginFailedException;
  42. import com.pokegoapi.exceptions.RemoteServerException;
  43. import com.pokegoapi.main.ServerRequest;
  44. import com.pokegoapi.util.Log;
  45. import lombok.Getter;
  46. import lombok.ToString;
  47.  
  48. /**
  49.  * The type Catchable pokemon.
  50.  */
  51. @ToString
  52. public class CatchablePokemon {
  53.     private static final String TAG = CatchablePokemon.class.getSimpleName();
  54.     private final PokemonGo api;
  55.  
  56.     @Getter
  57.     private final String spawnPointId;
  58.     @Getter
  59.     private final long encounterId;
  60.     @Getter
  61.     private final PokemonIdOuterClass.PokemonId pokemonId;
  62.     @Getter
  63.     private final long expirationTimestampMs;
  64.     @Getter
  65.     private final double latitude;
  66.     @Getter
  67.     private final double longitude;
  68.     @Getter
  69.     private final boolean incense;
  70.  
  71.     @Getter
  72.     private boolean encountered = false;
  73.  
  74.     /**
  75.      * Instantiates a new Catchable pokemon.
  76.      *
  77.      * @param api
  78.      *            the api
  79.      * @param proto
  80.      *            the proto
  81.      */
  82.     public CatchablePokemon(PokemonGo api, MapPokemon proto) {
  83.         this.api = api;
  84.  
  85.         this.spawnPointId = proto.getSpawnPointId();
  86.         this.encounterId = proto.getEncounterId();
  87.         this.pokemonId = proto.getPokemonId();
  88.         this.expirationTimestampMs = proto.getExpirationTimestampMs();
  89.         this.latitude = proto.getLatitude();
  90.         this.longitude = proto.getLongitude();
  91.         this.incense = false;
  92.     }
  93.  
  94.     /**
  95.      * Instantiates a new Catchable pokemon.
  96.      *
  97.      * @param api
  98.      *            the api
  99.      * @param proto
  100.      *            the proto
  101.      */
  102.     public CatchablePokemon(PokemonGo api, WildPokemon proto) {
  103.         this.api = api;
  104.         this.spawnPointId = proto.getSpawnPointId();
  105.         this.encounterId = proto.getEncounterId();
  106.         this.pokemonId = proto.getPokemonData().getPokemonId();
  107.         this.expirationTimestampMs = proto.getTimeTillHiddenMs();
  108.         this.latitude = proto.getLatitude();
  109.         this.longitude = proto.getLongitude();
  110.         this.incense = false;
  111.     }
  112.  
  113.     /**
  114.      * Instantiates a new Catchable pokemon.
  115.      *
  116.      * @param api
  117.      *            the api
  118.      * @param proto
  119.      *            the proto
  120.      */
  121.     public CatchablePokemon(PokemonGo api, FortData proto) {
  122.         if (!proto.hasLureInfo()) {
  123.             throw new IllegalArgumentException("Fort does not have lure");
  124.         }
  125.         this.api = api;
  126.         // TODO: does this work?
  127.         this.spawnPointId = null;
  128.         this.encounterId = proto.getLureInfo().getEncounterId();
  129.         this.pokemonId = proto.getLureInfo().getActivePokemonId();
  130.         this.expirationTimestampMs = proto.getLureInfo()
  131.                 .getLureExpiresTimestampMs();
  132.         this.latitude = proto.getLatitude();
  133.         this.longitude = proto.getLongitude();
  134.         this.incense = false;
  135.     }
  136.  
  137.     /**
  138.      * Instantiates a new Catchable pokemon.
  139.      *
  140.      * @param api
  141.      *            the api
  142.      * @param proto
  143.      *            the proto
  144.      */
  145.     public CatchablePokemon(PokemonGo api, GetIncensePokemonResponse proto) {
  146.         if (proto.getResult() != GetIncensePokemonResponse.Result.INCENSE_ENCOUNTER_AVAILABLE) {
  147.             throw new IllegalArgumentException("Incense is not available");
  148.         }
  149.         this.api = api;
  150.         this.latitude = proto.getLatitude();
  151.         this.longitude = proto.getLongitude();
  152.         this.pokemonId = PokemonIdOuterClass.PokemonId.forNumber(proto.getPokemonTypeId());
  153.         this.expirationTimestampMs = proto.getDisappearTimestampMs();
  154.         this.encounterId = proto.getEncounterId();
  155.         this.spawnPointId = proto.getEncounterLocation();
  156.         this.incense = true;
  157.     }
  158.  
  159.     /**
  160.      * Encounter pokemon encounter result.
  161.      *
  162.      * @return the encounter result
  163.      * @throws LoginFailedException
  164.      *             the login failed exception
  165.      * @throws RemoteServerException
  166.      *             the remote server exception
  167.      */
  168.     public EncounterResult encounterPokemon() throws LoginFailedException,
  169.             RemoteServerException {
  170.         if (!incense) {
  171.             EncounterMessageOuterClass.EncounterMessage reqMsg = EncounterMessageOuterClass.EncounterMessage
  172.                     .newBuilder().setEncounterId(getEncounterId())
  173.                     .setPlayerLatitude(api.getLatitude())
  174.                     .setPlayerLongitude(api.getLongitude())
  175.                     .setSpawnPointId(getSpawnPointId()).build();
  176.             ServerRequest serverRequest = new ServerRequest(
  177.                     RequestTypeOuterClass.RequestType.ENCOUNTER, reqMsg);
  178.             api.getRequestHandler().sendServerRequests(serverRequest);
  179.             EncounterResponseOuterClass.EncounterResponse response = null;
  180.             try {
  181.                 response = EncounterResponseOuterClass.EncounterResponse
  182.                         .parseFrom(serverRequest.getData());
  183.             } catch (InvalidProtocolBufferException e) {
  184.                 throw new RemoteServerException(e);
  185.             }
  186.             encountered = response.getStatus() == EncounterResponse.Status.ENCOUNTER_SUCCESS;
  187.             return new EncounterResult(response);
  188.         }
  189.         else {
  190.             //TODO: Or maybe separate method?
  191.             IncenseEncounterMessageOuterClass.IncenseEncounterMessage msg = IncenseEncounterMessageOuterClass.IncenseEncounterMessage.newBuilder().setEncounterId(encounterId).setEncounterLocation(spawnPointId).build();
  192.             ServerRequest serverRequest = new ServerRequest(
  193.                     RequestTypeOuterClass.RequestType.INCENSE_ENCOUNTER, msg);
  194.             api.getRequestHandler().sendServerRequests(serverRequest);
  195.             IncenseEncounterResponse response = null;
  196.             try {
  197.                 response = IncenseEncounterResponse
  198.                         .parseFrom(serverRequest.getData());
  199.             } catch (InvalidProtocolBufferException e) {
  200.                 throw new RemoteServerException(e);
  201.             }
  202.             encountered = response.getResult() == IncenseEncounterResponse.Result.INCENSE_ENCOUNTER_SUCCESS;
  203.             //TODO: I have no idea what to do here :/
  204.             //Here we get from response result, PokemonData and CaptureProbability
  205.             Log.d(TAG, response.toString());
  206.             return null;
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * Tries to catch a pokemon (will attempt to use a pokeball, if you have
  212.      * none will use greatball etc) and uwill use a single razz berry if available.
  213.      *
  214.      * @return CatchResult
  215.      * @throws LoginFailedException
  216.      *             if failed to login
  217.      * @throws RemoteServerException
  218.      *             if the server failed to respond
  219.      */
  220.     public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
  221.             RemoteServerException {
  222.         Pokeball pokeball;
  223.  
  224.         ItemBag bag = api.getInventories().getItemBag();
  225.         if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
  226.             pokeball = Pokeball.POKEBALL;
  227.         } else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
  228.             pokeball = Pokeball.GREATBALL;
  229.         } else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
  230.             pokeball = Pokeball.ULTRABALL;
  231.         } else {
  232.             pokeball = Pokeball.MASTERBALL;
  233.         }
  234.  
  235.         useItem(ItemId.ITEM_RAZZ_BERRY);
  236.         return catchPokemon(pokeball, -1, -1);
  237.     }
  238.  
  239.  
  240.     /**
  241.      * Tries to catch a pokemon (will attempt to use a pokeball, if you have
  242.      * none will use greatball etc).
  243.      *
  244.      * @return CatchResult
  245.      * @throws LoginFailedException
  246.      *             if failed to login
  247.      * @throws RemoteServerException
  248.      *             if the server failed to respond
  249.      */
  250.     public CatchResult catchPokemon() throws LoginFailedException,
  251.             RemoteServerException {
  252.         Pokeball pokeball;
  253.  
  254.         ItemBag bag = api.getInventories().getItemBag();
  255.         if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
  256.             pokeball = Pokeball.POKEBALL;
  257.         } else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
  258.             pokeball = Pokeball.GREATBALL;
  259.         } else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
  260.             pokeball = Pokeball.ULTRABALL;
  261.         } else {
  262.             pokeball = Pokeball.MASTERBALL;
  263.         }
  264.  
  265.         return catchPokemon(pokeball);
  266.     }
  267.  
  268.  
  269.  
  270.     /**
  271.      * Tries to catch a pokeball with the given type.
  272.      *
  273.      * @param pokeball
  274.      *            Type of pokeball
  275.      * @return CatchResult
  276.      * @throws LoginFailedException
  277.      *             if failed to login
  278.      * @throws RemoteServerException
  279.      *             if the server failed to respond
  280.      */
  281.     public CatchResult catchPokemon(Pokeball pokeball)
  282.             throws LoginFailedException, RemoteServerException {
  283.         return catchPokemon(pokeball, -1);
  284.     }
  285.  
  286.     /**
  287.      * Tried to catch a pokemon with given pokeball and max number of pokeballs.
  288.      *
  289.      * @param pokeball
  290.      *            Type of pokeball
  291.      * @param amount
  292.      *            Max number of pokeballs to use
  293.      * @return CatchResult
  294.      * @throws LoginFailedException
  295.      *             if failed to login
  296.      * @throws RemoteServerException
  297.      *             if the server failed to respond
  298.      */
  299.     public CatchResult catchPokemon(Pokeball pokeball, int amount)
  300.             throws LoginFailedException, RemoteServerException {
  301.         return catchPokemon(1.0, 1.95 + Math.random() * 0.05,
  302.                 0.85 + Math.random() * 0.15, pokeball, amount);
  303.     }
  304.  
  305.     /**
  306.      * Tried to catch a pokemon with given pokeball and max number of pokeballs.
  307.      *
  308.      * @param pokeball
  309.      *            Type of pokeball
  310.      * @param amount
  311.      *            Max number of pokeballs to use
  312.      * @param razberryLimit
  313.      *            Max number of razberrys to use
  314.      * @return CatchResult
  315.      * @throws LoginFailedException
  316.      *             if failed to login
  317.      * @throws RemoteServerException
  318.      *             if the server failed to respond
  319.      */
  320.     public CatchResult catchPokemon(Pokeball pokeball, int amount, int razberryLimit)
  321.             throws LoginFailedException, RemoteServerException {
  322.         return catchPokemon(1.0, 1.95 + Math.random() * 0.05,
  323.                 0.85 + Math.random() * 0.15, pokeball, razberryLimit);
  324.     }
  325.  
  326.     /**
  327.      * Tries to catch a pokemon.
  328.      *
  329.      * @param normalizedHitPosition
  330.      *            the normalized hit position
  331.      * @param normalizedReticleSize
  332.      *            the normalized hit reticle
  333.      * @param spinModifier
  334.      *            the spin modifier
  335.      * @param type
  336.      *            Type of pokeball to throw
  337.      * @param amount
  338.      *            Max number of Pokeballs to throw, negative number for
  339.      *            unlimited
  340.      * @return CatchResult of resulted try to catch pokemon
  341.      * @throws LoginFailedException
  342.      *             if failed to login
  343.      * @throws RemoteServerException
  344.      *             if the server failed to respond
  345.      */
  346.     public CatchResult catchPokemon(double normalizedHitPosition,
  347.                                     double normalizedReticleSize, double spinModifier, Pokeball type,
  348.                                     int amount) throws LoginFailedException, RemoteServerException {
  349.  
  350.         return catchPokemon(normalizedHitPosition, normalizedReticleSize, spinModifier, type, amount, -1);
  351.     }
  352.  
  353.     /**
  354.      * Tries to catch a pokemon.
  355.      *
  356.      * @param normalizedHitPosition
  357.      *            the normalized hit position
  358.      * @param normalizedReticleSize
  359.      *            the normalized hit reticle
  360.      * @param spinModifier
  361.      *            the spin modifier
  362.      * @param type
  363.      *            Type of pokeball to throw
  364.      * @param amount
  365.      *            Max number of Pokeballs to throw, negative number for
  366.      *            unlimited
  367.      * @param razberriesLimit
  368.      *            The maximum amount of razberries to use, -1 for unlimited
  369.      * @return CatchResult of resulted try to catch pokemon
  370.      * @throws LoginFailedException
  371.      *             if failed to login
  372.      * @throws RemoteServerException
  373.      *             if the server failed to respond
  374.      */
  375.     public CatchResult catchPokemon(double normalizedHitPosition,
  376.             double normalizedReticleSize, double spinModifier, Pokeball type,
  377.             int amount, int razberriesLimit) throws LoginFailedException, RemoteServerException {
  378.         if (!isEncountered()) {
  379.             return new CatchResult();
  380.         }
  381.  
  382.         int razberries = 0;
  383.         int numThrows = 0;
  384.         CatchPokemonResponse response = null;
  385.         do {
  386.  
  387.             if (razberries < razberriesLimit || razberriesLimit == -1) {
  388.                 useItem(ItemId.ITEM_RAZZ_BERRY);
  389.                 razberries++;
  390.             }
  391.  
  392.             CatchPokemonMessage reqMsg = CatchPokemonMessage.newBuilder()
  393.                     .setEncounterId(getEncounterId()).setHitPokemon(true)
  394.                     .setNormalizedHitPosition(normalizedHitPosition)
  395.                     .setNormalizedReticleSize(normalizedReticleSize)
  396.                     .setSpawnPointId(getSpawnPointId())
  397.                     .setSpinModifier(spinModifier)
  398.                     .setPokeball(type.getBallType()).build();
  399.             ServerRequest serverRequest = new ServerRequest(
  400.                     RequestTypeOuterClass.RequestType.CATCH_POKEMON, reqMsg);
  401.             api.getRequestHandler().sendServerRequests(serverRequest);
  402.  
  403.             try {
  404.                 response = CatchPokemonResponse.parseFrom(serverRequest
  405.                         .getData());
  406.             } catch (InvalidProtocolBufferException e) {
  407.                 throw new RemoteServerException(e);
  408.             }
  409.  
  410.             if (response.getStatus() != CatchPokemonResponse.CatchStatus.CATCH_ESCAPE
  411.                     && response.getStatus() != CatchPokemonResponse.CatchStatus.CATCH_MISSED) {
  412.                 break;
  413.             }
  414.             numThrows++;
  415.         }
  416.         while (amount < 0 || numThrows < amount);
  417.  
  418.         api.getInventories().updateInventories();
  419.  
  420.         return new CatchResult(response);
  421.     }
  422.  
  423.     /**
  424.      * Tries to use an item on a catchable pokemon (ie razzberry).
  425.      *
  426.      * @param item
  427.      *            the item ID
  428.      * @return CatchItemResult info about the new modifiers about the pokemon (can move, item capture multi) eg
  429.      * @throws LoginFailedException
  430.      *             if failed to login
  431.      * @throws RemoteServerException
  432.      *             if the server failed to respond
  433.      */
  434.     public CatchItemResult useItem(ItemId item) throws LoginFailedException, RemoteServerException {
  435.  
  436.         UseItemCaptureMessage reqMsg = UseItemCaptureMessage
  437.                 .newBuilder()
  438.                 .setEncounterId(this.getEncounterId())
  439.                 .setSpawnPointGuid(this.getSpawnPointId())
  440.                 .setItemId(item)
  441.                 .build();
  442.  
  443.         ServerRequest serverRequest = new ServerRequest(
  444.                 RequestTypeOuterClass.RequestType.USE_ITEM_CAPTURE, reqMsg);
  445.         api.getRequestHandler().sendServerRequests(serverRequest);
  446.         UseItemCaptureResponse response = null;
  447.         try {
  448.             response = UseItemCaptureResponse.parseFrom(serverRequest.getData());
  449.         } catch (InvalidProtocolBufferException e) {
  450.             throw new RemoteServerException(e);
  451.         }
  452.         return new CatchItemResult(response);
  453.     }
  454.  
  455.     @Override
  456.     public boolean equals(Object obj) {
  457.         if (obj == this) {
  458.             return true;
  459.         } else if (obj instanceof CatchablePokemon) {
  460.             return this.getEncounterId() == ((CatchablePokemon) obj)
  461.                     .getEncounterId();
  462.         }
  463.         return false;
  464.     }
  465.  
  466.     @Override
  467.     public int hashCode() {
  468.         return (int) this.getEncounterId();
  469.     }
  470.  
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement