Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added api/conf/version getConfigVersion

Added api/conf          getConfig
Added api/conf/my       getUserGameData
Added api/achievements  saveAchievemnt
Added tests
  • Loading branch information...
commit 6e7b5d606c6a0d99ad14deb9bf4e41389137a929 1 parent 3fe156a
Brian authored
View
7 src/com/mogade/java/Mogade.java
@@ -2,9 +2,7 @@
import com.mogade.java.data.Leaderboard;
import com.mogade.java.data.Score;
-import com.mogade.java.protocol.GetConfigVersionResponse;
-import com.mogade.java.protocol.GetLeaderboardResponse;
-import com.mogade.java.protocol.SaveScoreResponse;
+import com.mogade.java.protocol.*;
public interface Mogade
{
@@ -13,4 +11,7 @@
public SaveScoreResponse saveScore(String leaderboardId, Score score);
public GetLeaderboardResponse getLeaderboard(Leaderboard leaderboard);
public GetConfigVersionResponse getConfigVersion();
+ public GetConfigResponse getConfig();
+ public GetUserGameDataResponse getUserGameData(String username, String unique);
+ public SaveAchievementResponse saveAchievement(String achievementId, String username, String unique);
}
View
51 src/com/mogade/java/MogadeImpl.java
@@ -88,6 +88,57 @@ public GetConfigVersionResponse getConfigVersion()
return new GetConfigVersionResponse(null, null, ex.getMessage());
}
}
+ public GetConfigResponse getConfig()
+ {
+ try
+ {
+ return gson.fromJson(sendRequest(new GetConfigRequest(gameKey, getApiVersion())), GetConfigResponse.class);
+ }
+ catch(JsonParseException ex)
+ {
+ return new GetConfigResponse(null, null, "json parse exception:" + ex.getMessage());
+ }
+ catch(IOException ex)
+ {
+ return new GetConfigResponse(null, null, ex.getMessage());
+ }
+ }
+ public GetUserGameDataResponse getUserGameData(String username, String unique)
+ {
+ Validator.assertNotNullOrEmpty(username, "Invalid username");
+ Validator.assertNotNullOrEmpty(unique, "Invalid unique");
+ try
+ {
+ return gson.fromJson(sendRequest(new GetUserGameDataRequest(gameKey, getApiVersion(), username, unique)), GetUserGameDataResponse.class);
+ }
+ catch(JsonParseException ex)
+ {
+ return new GetUserGameDataResponse(null, null, "json parse exception:" + ex.getMessage());
+ }
+ catch(IOException ex)
+ {
+ return new GetUserGameDataResponse(null, null, ex.getMessage());
+ }
+ }
+ public SaveAchievementResponse saveAchievement(String achievementId, String username, String unique)
+ {
+ Validator.assertNotNullOrEmpty(username, "Invalid achievementId");
+ Validator.assertNotNullOrEmpty(unique, "Invalid username");
+ Validator.assertNotNullOrEmpty(unique, "Invalid unique");
+ try
+ {
+ return gson.fromJson(sendRequest(new SaveAchievementRequest(gameKey, getApiVersion(), achievementId, username, unique)), SaveAchievementResponse.class);
+ }
+ catch(JsonParseException ex)
+ {
+ return new SaveAchievementResponse(null, null, "json parse exception:" + ex.getMessage());
+ }
+ catch(IOException ex)
+ {
+ return new SaveAchievementResponse(null, null, ex.getMessage());
+ }
+ }
+
private String sendRequest(Request request) throws IOException
{
request.setSig(request.calculateSignature(secret));
View
37 src/com/mogade/java/data/Achievement.java
@@ -0,0 +1,37 @@
+package com.mogade.java.data;
+
+public class Achievement
+{
+ private String id;
+ private String name;
+ private String desc;
+ private long points;
+
+ private Achievement()
+ {
+ }
+ public Achievement(String id, String name, String desc, long points)
+ {
+ this.id = id;
+ this.name = name;
+ this.desc = desc;
+ this.points = points;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+ public String getName()
+ {
+ return name;
+ }
+ public String getDescription()
+ {
+ return desc;
+ }
+ public long getPoints()
+ {
+ return points;
+ }
+}
View
19 src/com/mogade/java/protocol/GetConfigRequest.java
@@ -0,0 +1,19 @@
+package com.mogade.java.protocol;
+
+public class GetConfigRequest extends BaseRequestImpl
+{
+ public GetConfigRequest(String key, int v)
+ {
+ super(key, v);
+ }
+
+ public RequestMethod getRequestMethod()
+ {
+ return RequestMethod.POST;
+ }
+
+ public String getUrl()
+ {
+ return "api/conf";
+ }
+}
View
27 src/com/mogade/java/protocol/GetConfigResponse.java
@@ -0,0 +1,27 @@
+package com.mogade.java.protocol;
+
+import com.mogade.java.data.Achievement;
+
+import java.util.List;
+
+public class GetConfigResponse extends GetConfigVersionResponse
+{
+ private List<Achievement> achievements;
+
+ private GetConfigResponse()
+ {
+ }
+ public GetConfigResponse(String info, String maintenance, String error)
+ {
+ super(info, maintenance, error);
+ }
+
+ public boolean hasAchievements()
+ {
+ return (achievements != null && achievements.size() > 0);
+ }
+ public List<Achievement> getAchievements()
+ {
+ return achievements;
+ }
+}
View
2  src/com/mogade/java/protocol/GetConfigVersionRequest.java
@@ -12,6 +12,6 @@ public String getUrl()
}
public RequestMethod getRequestMethod()
{
- return RequestMethod.GET;
+ return RequestMethod.POST;
}
}
View
3  src/com/mogade/java/protocol/GetConfigVersionResponse.java
@@ -3,7 +3,8 @@
public class GetConfigVersionResponse extends BaseResponseImpl
{
private int version;
- private GetConfigVersionResponse()
+
+ protected GetConfigVersionResponse()
{
}
public GetConfigVersionResponse(String info, String maintenance, String error)
View
23 src/com/mogade/java/protocol/GetUserGameDataRequest.java
@@ -0,0 +1,23 @@
+package com.mogade.java.protocol;
+
+public class GetUserGameDataRequest extends BaseRequestImpl
+{
+ private String username;
+ private String unique;
+
+ public GetUserGameDataRequest(String key, int v, String username, String unique)
+ {
+ super(key, v);
+ this.username = username;
+ this.unique = unique;
+ }
+
+ public RequestMethod getRequestMethod()
+ {
+ return RequestMethod.POST;
+ }
+ public String getUrl()
+ {
+ return "api/conf/my";
+ }
+}
View
25 src/com/mogade/java/protocol/GetUserGameDataResponse.java
@@ -0,0 +1,25 @@
+package com.mogade.java.protocol;
+
+import java.util.List;
+
+public class GetUserGameDataResponse extends BaseResponseImpl
+{
+ private List<String> achievements;
+
+ private GetUserGameDataResponse()
+ {
+ }
+ public GetUserGameDataResponse(String info, String maintenance, String error)
+ {
+ super(info, maintenance, error);
+ }
+
+ public boolean hasAchievements()
+ {
+ return (achievements != null && achievements.size() > 0);
+ }
+ public List<String> getAchievements()
+ {
+ return achievements;
+ }
+}
View
28 src/com/mogade/java/protocol/SaveAchievementRequest.java
@@ -0,0 +1,28 @@
+package com.mogade.java.protocol;
+
+import com.google.gson.annotations.SerializedName;
+
+public class SaveAchievementRequest extends BaseRequestImpl
+{
+ @SerializedName("achievement_id")
+ private String achievementId;
+ private String username;
+ private String unique;
+
+ public SaveAchievementRequest(String key, int v, String achievementId, String username, String unique)
+ {
+ super(key, v);
+ this.achievementId = achievementId;
+ this.username = username;
+ this.unique = unique;
+ }
+
+ public String getUrl()
+ {
+ return "api/achievements";
+ }
+ public RequestMethod getRequestMethod()
+ {
+ return RequestMethod.PUT;
+ }
+}
View
19 src/com/mogade/java/protocol/SaveAchievementResponse.java
@@ -0,0 +1,19 @@
+package com.mogade.java.protocol;
+
+public class SaveAchievementResponse extends BaseResponseImpl
+{
+ private long points;
+
+ private SaveAchievementResponse()
+ {
+ }
+ public SaveAchievementResponse(String info, String maintenance, String error)
+ {
+ super(info, maintenance, error);
+ }
+
+ public long getPoints()
+ {
+ return points;
+ }
+}
View
44 tests/src/com/mogade/java/tests/functional/TestLive.java
@@ -4,9 +4,7 @@
import com.mogade.java.MogadeImpl;
import com.mogade.java.data.Leaderboard;
import com.mogade.java.data.Score;
-import com.mogade.java.protocol.GetConfigVersionResponse;
-import com.mogade.java.protocol.GetLeaderboardResponse;
-import com.mogade.java.protocol.SaveScoreResponse;
+import com.mogade.java.protocol.*;
import org.junit.Before;
import org.junit.Test;
@@ -17,6 +15,7 @@
private final static String gameKey = "4cce25151d9517161400000e";
private final static String secret = "qRA]:A;28q]V?UU";
private final static String leaderboardId = "4cceda2a563d8a335a000008";
+ private final static int achievementCount = 0; //!!!needs to reflect achievement count for this test account
@Before
public void setupProxy()
@@ -112,4 +111,43 @@ public void testConfigVersionSuccess()
assertTrue(response.getVersion() > 0);
}
+ @Test
+ public void testConfigSuccess()
+ {
+ Mogade mogade = MogadeImpl.create(gameKey, secret);
+ GetConfigResponse response = mogade.getConfig();
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertTrue(response.getVersion() > 0);
+ assertNotNull(response.getAchievements());
+ assertTrue(response.getAchievements().size() > 0);
+ }
+ @Test
+ public void testGetUserGameDataResponse()
+ {
+ Mogade mogade = MogadeImpl.create(gameKey, secret);
+ GetUserGameDataResponse response = mogade.getUserGameData("brian", "1");
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertNotNull(response.getAchievements());
+ assertTrue(response.getAchievements().size() > 0);
+ }
+ @Test
+ public void testSaveAchievementInvalidAchievementId()
+ {
+ Mogade mogade = MogadeImpl.create(gameKey, secret);
+ SaveAchievementResponse response = mogade.saveAchievement("invalid", "brian", "1");
+
+ assertFalse(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertTrue(response.isError());
+
+ assertEquals(0, response.getPoints());
+ }
}
View
113 tests/src/com/mogade/java/tests/functional/TestMogade.java
@@ -6,12 +6,11 @@
import com.mogade.java.Mogade;
import com.mogade.java.MogadeConfigurationImpl;
import com.mogade.java.MogadeImpl;
+import com.mogade.java.data.Achievement;
import com.mogade.java.data.Leaderboard;
import com.mogade.java.data.Score;
import com.mogade.java.helpers.Utility;
-import com.mogade.java.protocol.GetConfigVersionResponse;
-import com.mogade.java.protocol.GetLeaderboardResponse;
-import com.mogade.java.protocol.SaveScoreResponse;
+import com.mogade.java.protocol.*;
import com.mogade.java.tests.FakeServer;
import org.junit.After;
import org.junit.Before;
@@ -183,7 +182,7 @@ public void testGetLeaderboardSuccess()
}
@Test
- public void testConfigVersionSuccess()
+ public void testGetConfigVersionSuccess()
{
fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"version\":10}"));
Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
@@ -195,4 +194,110 @@ public void testConfigVersionSuccess()
assertEquals(10, response.getVersion());
}
+ @Test
+ public void testGetConfigNoAchievements()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"version\":10}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetConfigResponse response = mogade.getConfig();
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertEquals(10, response.getVersion());
+ assertFalse(response.hasAchievements());
+ assertNull(response.getAchievements());
+ }
+ @Test
+ public void testGetConfigEmptyAchievements()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"version\":10,\"achievements\":[]}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetConfigResponse response = mogade.getConfig();
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertEquals(10, response.getVersion());
+ assertNotNull(response.getAchievements());
+ assertFalse(response.hasAchievements());
+ assertEquals(0, response.getAchievements().size());
+ }
+ @Test
+ public void testGetConfigSuccess()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"version\":10,\"achievements\":[{\"id\":\"9sdf923sdfsfd9\",\"name\":\"Level 1 expert\",\"desc\":\"finish first level in 20 secs\",\"points\":90210},{\"id\":\"2l3k23kj2l3jkl3k\",\"name\":\"Level 5 expert\", \"desc\":\"finish fifth level in 120 secs\",\"points\":450578}]}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetConfigResponse response = mogade.getConfig();
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertEquals(10, response.getVersion());
+
+ assertTrue(response.hasAchievements());
+ assertTrue(response.getAchievements().size() == 2);
+
+ Achievement achievement = response.getAchievements().get(0);
+ assertEquals("9sdf923sdfsfd9", achievement.getId());
+ assertEquals("Level 1 expert", achievement.getName());
+ assertEquals("finish first level in 20 secs", achievement.getDescription());
+ assertEquals(90210, achievement.getPoints());
+
+ achievement = response.getAchievements().get(1);
+ assertEquals("2l3k23kj2l3jkl3k", achievement.getId());
+ assertEquals("Level 5 expert", achievement.getName());
+ assertEquals("finish fifth level in 120 secs", achievement.getDescription());
+ assertEquals(450578, achievement.getPoints());
+ }
+ @Test
+ public void testGetUserGameDataNoEarnedAchievements()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetUserGameDataResponse response = mogade.getUserGameData("brian", "1");
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertFalse(response.hasAchievements());
+ assertNull(response.getAchievements());
+ }
+ @Test
+ public void testGetUserGameDataEmptyEarnedAchievements()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"achievements\":[]}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetUserGameDataResponse response = mogade.getUserGameData("brian", "1");
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertFalse(response.hasAchievements());
+ assertNotNull(response.getAchievements());
+ assertEquals(0, response.getAchievements().size());
+ }
+ @Test
+ public void testGetUserGameDataSuccess()
+ {
+ fakeServer.addResponse(new FakeServer.FakeResponse(HttpURLConnection.HTTP_OK, "{\"achievements\":[\"achieveid1\",\"achieveid2\",\"achieveid3\"]}"));
+ Mogade mogade = MogadeImpl.create("GAMEKEY", "SECRET");
+ GetUserGameDataResponse response = mogade.getUserGameData("brian", "1");
+
+ assertTrue(response.isOk());
+ assertFalse(response.isUnavailable());
+ assertFalse(response.isError());
+
+ assertTrue(response.hasAchievements());
+ assertNotNull(response.getAchievements());
+
+ assertEquals("achieveid1", response.getAchievements().get(0));
+ assertEquals("achieveid2", response.getAchievements().get(1));
+ assertEquals("achieveid3", response.getAchievements().get(2));
+ }
}
View
13 tests/src/com/mogade/java/tests/unit/TestMogade.java
@@ -63,4 +63,17 @@ public void testGetLeaderboardInvalidLeaderboard()
Mogade mogade = MogadeImpl.create("GAMEKEY","SECRET");
mogade.getLeaderboard(null);
}
+
+ @Test(expected=MogadeException.class)
+ public void testGetUserGameDataInvalidUsername()
+ {
+ Mogade mogade = MogadeImpl.create("GAMEKEY","SECRET");
+ mogade.getUserGameData(null, "1");
+ }
+ @Test(expected=MogadeException.class)
+ public void testGetUserGameDataInvalidUnique()
+ {
+ Mogade mogade = MogadeImpl.create("GAMEKEY","SECRET");
+ mogade.getUserGameData("brian", "");
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.