下面我们演示如何获取当前用户。只有当TrustedPlatformClient使用玩家访问令牌进行认证时,这才会有效。因此,这个查询在客户端更有意义。如果你需要在服务器上获取玩家,你可以明确定义玩家的ID或名称。
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.user.*;
public class ExampleServer {
private TrustedPlatformClient client;
public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}
public void getCurrentPlayer() {
GetUser query = new GetUser().me();
HttpResponse<>> httpResponse = client.getUserService().getUserSync(query);
if (!httpResponse.isEmpty()) {
GraphQLResponsegraphQLResponse = httpResponse.body();
if (!graphQLResponse.hasErrors()) {
User user = graphQLResponse.getData();
}
}
}
}
下面的例子演示了如何在用户查询中包含玩家身份。
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.identity.*;
import com.enjin.sdk.models.user.*;
import java.util.List;
public class ExampleServer {
private TrustedPlatformClient client;
public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}
public void getIdentities(String name) {
GetUser query = new GetUser().name(name)
.withUserIdentities(); // Includes the user identities
HttpResponse<>> httpResponse = client.getUserService().getUserSync(query);
if (!httpResponse.isEmpty()) {
GraphQLResponsegraphQLResponse = httpResponse.body();
if (!graphQLResponse.hasErrors()) {
User user = graphQLResponse.getData();
Listidentities = user.getIdentities();
}
}
}
}
虽然可以在SDK中明确地将Ethereum地址链接到一个身份,但我们建议向玩家的显示链接代码或链接代码QR,以便他们可以从NFTEX Wallet android或iOS应用程序中进行链接。下面我们演示一下如何获取链接码和QR URL。
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.identity.*;
import com.enjin.sdk.models.user.*;
import java.util.*;
public class ExampleServer {
private TrustedPlatformClient client;
public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}
public void getIdentityCode(String name) {
GetUser query = new GetUser().name(name)
.withUserIdentities()
.withLinkingCode() // Includes the linking code and
.withLinkingCodeQr(); // the qr url for the identities
HttpResponse<>> httpResponse = client.getUserService().getUserSync(query);
if (!httpResponse.isEmpty()) {
GraphQLResponsegraphQLResponse = httpResponse.body();
if (!graphQLResponse.hasErrors()) {
User user = graphQLResponse.getData();
Listidentities = user.getIdentities();
if (identities.size() > 0) {
Identity identity = identities.get(0);
String linkingCode = identity.getLinkingCode();
String qrCodeUrl = identity.getLinkingCodeQr();
}
}
}
}
}
获取玩家余额的方法有很多种。我们强烈建议使用钱包服务来明确获取Ethereum地址的余额。与其他方法相比,通过这样做,您可以限制响应中返回的数据范围。为了演示的目的,我们将展示如何在获取玩家时包含余额,以及如何获取特定Ethereum地址的钱包。
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.balance.*;
import com.enjin.sdk.models.identity.*;
import com.enjin.sdk.models.user.*;
import com.enjin.sdk.models.wallet.*;
import java.util.*;
public class ExampleServer {
private TrustedPlatformClient client;
public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}
// Recommended approach
public void getWallet(String ethAddr) {
GetWallet query = new GetWallet().ethAddress(ethAddr)
.withBalances(); // Includes the balances
HttpResponse<>> httpResponse = client.getWalletService().getWalletSync(query);
if (!httpResponse.isEmpty()) {
GraphQLResponsegraphQLResponse = httpResponse.body();
if (!graphQLResponse.hasErrors()) {
Wallet wallet = graphQLResponse.getData();
Listbalances = wallet.getBalances();
}
}
}
public void getBalances(String name) {
GetUser query = new GetUser().name(name)
.withUserIdentities()
.withWallet() // Includes the wallet and
.withBalances(); // the balances for it
HttpResponse<>> httpResponse = client.getUserService().getUserSync(query);
if (!httpResponse.isEmpty()) {
GraphQLResponsegraphQLResponse = httpResponse.body();
if (!graphQLResponse.hasErrors()) {
User user = graphQLResponse.getData();
Listidentities = user.getIdentities();
if (identities.size() > 0) {
Identity identity = identities.get(0);
Wallet wallet = identity.getWallet();
Listbalances = wallet.getBalances();
}
}
}
}
}