Created
April 14, 2015 09:34
-
-
Save nazargulov/a8b0184f7bd1321a2040 to your computer and use it in GitHub Desktop.
online game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package greenbox.mafia.model.game.server; | |
| import android.support.annotation.NonNull; | |
| import android.util.Log; | |
| import android.util.Pair; | |
| import com.google.common.util.concurrent.FutureCallback; | |
| import com.google.common.util.concurrent.Futures; | |
| import com.google.common.util.concurrent.ListenableFuture; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonElement; | |
| import com.microsoft.windowsazure.mobileservices.MobileServiceClient; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import magicgoose.common.Either; | |
| import magicgoose.common.android.SharedHandler; | |
| public class OnlineGameServer extends GameServerBase { | |
| private final MobileServiceClient azureServiceClient; | |
| public OnlineGameServer(final MobileServiceClient azureServiceClient) { | |
| this.azureServiceClient = azureServiceClient; | |
| } | |
| private void gotFooResult(Either<Throwable, JsonElement> fooResult) { | |
| if (!fooResult.isRight) { | |
| throw new Error(fooResult.left); | |
| } | |
| final JsonElement resultJSON = fooResult.right; | |
| Log.i("YOBA", String.valueOf(resultJSON)); | |
| final int[][] result = parseResult(resultJSON); | |
| this.positionSubject.onNext(result); | |
| } | |
| private int[][] parseResult(JsonElement resultJSON) { | |
| final JsonArray results = resultJSON.getAsJsonObject().getAsJsonArray("results"); | |
| final int[][] result = new int[results.size()][]; | |
| for (int i = 0; i < result.length; i++) { | |
| final JsonArray item = results.get(i).getAsJsonArray(); | |
| final int itemLength = item.size(); | |
| final int[] resultColumn = result[i] = new int[itemLength]; | |
| for (int j = 0; j < itemLength; j++) { | |
| resultColumn[j] = item.get(j).getAsInt(); | |
| } | |
| } | |
| return result; | |
| } | |
| @Override | |
| public void foo(final int parameter) { | |
| final List<Pair<String, String>> parameters = Arrays.asList(Pair.create( | |
| "parameter", Integer.toString(parameter) | |
| )); | |
| final ListenableFuture<JsonElement> fooResult = | |
| azureServiceClient.invokeApi("foo", "POST", parameters);// "foo" should be exists in API method in azure | |
| Futures.addCallback(fooResult, new FutureCallback<JsonElement>() { | |
| @Override | |
| public void onSuccess(JsonElement result) { | |
| SharedHandler.post(() -> | |
| gotFooResult(Either.createRight(result))); | |
| } | |
| @Override | |
| public void onFailure(@NonNull Throwable t) { | |
| SharedHandler.post(() -> | |
| gotFooResult(Either.createLeft(t))); | |
| Log.e("apifoo", t.toString()); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment