Skip to content

Commit 73d8dca

Browse files
aschlosserGrover-c13
authored andcommitted
added a request to accept level up rewards and unlocks (#259)
1 parent 7df5857 commit 73d8dca

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.pokegoapi.api.player;
2+
3+
import POGOProtos.Inventory.Item.ItemAwardOuterClass;
4+
import POGOProtos.Inventory.Item.ItemIdOuterClass;
5+
import POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse;
6+
import lombok.Data;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
/**
12+
* A data class containing the results of a trainer level up. This includes a list of items received for this level up,
13+
* a list of items which were unlocked by this level up (for example razz berries)
14+
* and the status of these level up results.
15+
* If the rewards for this level up have been
16+
* accepted in the past the status will be ALREADY_ACCEPTED, if this level up has not yet been achieved
17+
* by the player it will be NOT_UNLOCKED_YET otherwise it will be NEW.
18+
*
19+
* @author Alex Schlosser
20+
*/
21+
@Data
22+
public class PlayerLevelUpRewards {
23+
private final Status status;
24+
private final List<ItemAwardOuterClass.ItemAward> rewards;
25+
private final List<ItemIdOuterClass.ItemId> unlockedItems;
26+
27+
28+
/**
29+
* Create new empty result object with the specified status.
30+
*
31+
* @param status the status of this result
32+
*/
33+
public PlayerLevelUpRewards(final Status status) {
34+
this.status = status;
35+
this.rewards = Collections.emptyList();
36+
this.unlockedItems = Collections.emptyList();
37+
}
38+
39+
public enum Status {
40+
ALREADY_ACCEPTED, NEW, NOT_UNLOCKED_YET
41+
}
42+
43+
/**
44+
* Create a new result object based on a server response
45+
*
46+
* @param response the response which contains the request results
47+
*/
48+
public PlayerLevelUpRewards(final LevelUpRewardsResponse response) {
49+
this.rewards = response.getItemsAwardedList();
50+
this.unlockedItems = response.getItemsUnlockedList();
51+
this.status = (rewards.isEmpty() ? Status.ALREADY_ACCEPTED : Status.NEW);
52+
}
53+
}

src/main/java/com/pokegoapi/api/player/PlayerProfile.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
import POGOProtos.Data.Player.CurrencyOuterClass;
1919
import POGOProtos.Data.Player.EquippedBadgeOuterClass;
2020
import POGOProtos.Data.Player.PlayerStatsOuterClass;
21+
import POGOProtos.Inventory.Item.ItemAwardOuterClass;
2122
import POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage;
23+
import POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass;
24+
import POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage;
25+
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
2226
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
2327
import POGOProtos.Networking.Responses.GetPlayerResponseOuterClass;
28+
import POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass;
2429
import com.google.protobuf.InvalidProtocolBufferException;
2530
import com.pokegoapi.api.PokemonGo;
31+
import com.pokegoapi.api.inventory.Item;
32+
import com.pokegoapi.api.inventory.ItemBag;
2633
import com.pokegoapi.exceptions.InvalidCurrencyException;
2734
import com.pokegoapi.exceptions.LoginFailedException;
2835
import com.pokegoapi.exceptions.RemoteServerException;
@@ -128,6 +135,43 @@ public void updateProfile() throws RemoteServerException, LoginFailedException {
128135

129136
}
130137

138+
/**
139+
* Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the
140+
* server until a player actively accepts them.
141+
* The rewarded items are automatically inserted into the players item bag.
142+
*
143+
* @see PlayerLevelUpRewards
144+
* @param level the trainer level that you want to accept the rewards for
145+
* @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
146+
* @throws LoginFailedException if the login failed
147+
* @throws RemoteServerException if the server failed to respond
148+
*/
149+
public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RemoteServerException, LoginFailedException {
150+
// Check if we even have achieved this level yet
151+
if (level > stats.getLevel()) {
152+
return new PlayerLevelUpRewards(PlayerLevelUpRewards.Status.NOT_UNLOCKED_YET);
153+
}
154+
LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder()
155+
.setLevel(level)
156+
.build();
157+
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.LEVEL_UP_REWARDS, msg);
158+
api.getRequestHandler().sendServerRequests(serverRequest);
159+
LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse response;
160+
try {
161+
response = LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse.parseFrom(serverRequest.getData());
162+
} catch (InvalidProtocolBufferException e) {
163+
throw new RemoteServerException(e);
164+
}
165+
// Add the awarded items to our bag
166+
ItemBag bag = api.getInventories().getItemBag();
167+
for (ItemAwardOuterClass.ItemAward itemAward : response.getItemsAwardedList()) {
168+
Item item = bag.getItem(itemAward.getItemId());
169+
item.setCount(item.getCount() + itemAward.getItemCount());
170+
}
171+
// Build a new rewards object and return it
172+
return new PlayerLevelUpRewards(response);
173+
}
174+
131175
/**
132176
* Add currency.
133177
*

0 commit comments

Comments
 (0)