diff options
Diffstat (limited to 'core/src/kz/ilotterytea')
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/MaxonConstants.java | 10 | ||||
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/MaxonGame.java | 13 | ||||
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/screens/MenuScreen.java | 24 | ||||
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/utils/GameUpdater.java | 156 |
4 files changed, 98 insertions, 105 deletions
diff --git a/core/src/kz/ilotterytea/maxon/MaxonConstants.java b/core/src/kz/ilotterytea/maxon/MaxonConstants.java index 2f84a79..82e8143 100644 --- a/core/src/kz/ilotterytea/maxon/MaxonConstants.java +++ b/core/src/kz/ilotterytea/maxon/MaxonConstants.java @@ -14,8 +14,8 @@ public class MaxonConstants { public static final String GAME_NAME = "Maxon Petting Simulator"; public static final String GAME_APP_ID = "maxon"; public static final String GAME_APP_PACKAGE = "kz.ilotterytea." + GAME_APP_ID; + public static final String GAME_APP_URL; public static final String GAME_VERSION = "Alpha 1.2"; - public static final String GAME_GHTAG = "alpha-1.2"; public static final String GAME_MAIN_DEVELOPER = "ilotterytea"; public static final String[][] GAME_DEVELOPERS = { @@ -40,6 +40,8 @@ public class MaxonConstants { public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/M/yyyy hh:mm:ss"); public static final long startTime = System.currentTimeMillis(); + public static final String GAME_VERSIONS_FILE_URL = "https://assets.ilotterytea.kz/maxon/versions.json"; + public static final Texture MISSING_TEXTURE; static { @@ -62,5 +64,11 @@ public class MaxonConstants { MISSING_TEXTURE = new Texture(pixmap); pixmap.dispose(); + + if (OsUtils.isAndroid) { + GAME_APP_URL = "https://play.google.com/store/apps/details?id=kz.ilotterytea.maxon"; + } else { + GAME_APP_URL = "https://ilotterytea.itch.io/maxon"; + } } } diff --git a/core/src/kz/ilotterytea/maxon/MaxonGame.java b/core/src/kz/ilotterytea/maxon/MaxonGame.java index 0e3f9bc..67b4a0e 100644 --- a/core/src/kz/ilotterytea/maxon/MaxonGame.java +++ b/core/src/kz/ilotterytea/maxon/MaxonGame.java @@ -6,8 +6,11 @@ import com.badlogic.gdx.Preferences; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import de.tomgrill.gdxdialogs.core.GDXDialogs; +import de.tomgrill.gdxdialogs.core.GDXDialogsSystem; import kz.ilotterytea.maxon.pets.PetManager; import kz.ilotterytea.maxon.screens.SplashScreen; +import kz.ilotterytea.maxon.utils.GameUpdater; import kz.ilotterytea.maxon.utils.I18N; public class MaxonGame extends Game { @@ -19,6 +22,8 @@ public class MaxonGame extends Game { private PetManager petManager; + private GDXDialogs dialogWindows; + private static MaxonGame instance; public static MaxonGame getInstance() { @@ -32,8 +37,15 @@ public class MaxonGame extends Game { return petManager; } + public GDXDialogs getDialogWindows() { + return dialogWindows; + } + @Override public void create () { + // Check the latest version + new GameUpdater().checkLatestUpdate(); + batch = new SpriteBatch(); shapeRenderer = new ShapeRenderer(); prefs = Gdx.app.getPreferences(MaxonConstants.GAME_APP_PACKAGE); @@ -48,6 +60,7 @@ public class MaxonGame extends Game { assetManager = new AssetManager(); petManager = new PetManager(assetManager); + dialogWindows = GDXDialogsSystem.install(); this.setScreen(new SplashScreen()); } diff --git a/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java b/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java index 3158d67..1e15032 100644 --- a/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java +++ b/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java @@ -20,10 +20,12 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Timer; import com.badlogic.gdx.utils.viewport.ScreenViewport; +import de.tomgrill.gdxdialogs.core.dialogs.GDXButtonDialog; import kz.ilotterytea.maxon.MaxonConstants; import kz.ilotterytea.maxon.MaxonGame; import kz.ilotterytea.maxon.player.Savegame; import kz.ilotterytea.maxon.ui.*; +import kz.ilotterytea.maxon.utils.GameUpdater; import kz.ilotterytea.maxon.utils.I18N; import kz.ilotterytea.maxon.utils.OsUtils; import net.mgsx.gltf.scene3d.attributes.PBRCubemapAttribute; @@ -51,9 +53,31 @@ public class MenuScreen implements Screen { private final ArrayList<Timer.Task> tasks = new ArrayList<>(); private Sound clickSound; + private static boolean suggestedUpdate = false; + public MenuScreen() { this.game = MaxonGame.getInstance(); + // Suggest an update + if (!GameUpdater.CLIENT_IS_ON_LATEST_VERSION && !suggestedUpdate) { + GDXButtonDialog bDialog = game.getDialogWindows().newDialog(GDXButtonDialog.class); + + bDialog.setTitle(game.locale.TranslatableText("updater.title")); + bDialog.setMessage(game.locale.TranslatableText("updater.message")); + + bDialog.setClickListener(button -> { + if (button == 1) { + Gdx.net.openURI(MaxonConstants.GAME_APP_URL); + } + }); + + bDialog.addButton(game.locale.TranslatableText("updater.no")); + bDialog.addButton(game.locale.TranslatableText("updater.yes")); + + bDialog.build().show(); + suggestedUpdate = true; + } + // Stage and skin: this.stage = new Stage(new ScreenViewport()); this.stage.addAction(Actions.sequence(Actions.alpha(0.0f), Actions.alpha(1.0f, 1f))); diff --git a/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java b/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java index 0133d21..1c4ef94 100644 --- a/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java +++ b/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java @@ -1,127 +1,75 @@ package kz.ilotterytea.maxon.utils; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Net; +import com.badlogic.gdx.net.HttpRequestBuilder; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import kz.ilotterytea.maxon.MaxonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.Objects; +import java.util.ArrayList; +public class GameUpdater implements Net.HttpResponseListener { + private final Logger logger = LoggerFactory.getLogger(GameUpdater.class.getName()); + public static boolean CLIENT_IS_ON_LATEST_VERSION; -public class GameUpdater { - static class Release { - String url; - String html_url; - String assets_url; - String upload_url; - String tarball_url; - String zipball_url; - String discussion_url; - int id; - String node_id; - String tag_name; - String target_commitish; - String name; - String body; - boolean draft; - boolean prerelease; - String created_at; - String published_at; - Author author; - Assets[] assets; - } + public void checkLatestUpdate() { + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.GET) + .url(MaxonConstants.GAME_VERSIONS_FILE_URL) + .timeout(5000) + .build(); - static class Author { - String login; - int id; - String node_id; - String avatar_url; - String gravatar_id; - String url; - String html_url; - String followers_url; - String following_url; - String gists_url; - String starred_url; - String subscriptions_url; - String organizations_url; - String repos_url; - String events_url; - String received_events_url; - String type; - boolean site_admin; + Gdx.net.sendHttpRequest(request, this); } - static class Assets { - String url; - String browser_download_url; - int id; - String node_id; - String name; - String label; - String state; - String content_type; - int size; - int download_count; - String created_at; - String updated_at; - Uploader uploader; - } + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + String response = httpResponse.getResultAsString(); - static class Uploader { - String login; - int id; - String node_id; - String avatar_url; - String gravatar_id; - String url; - String html_url; - String followers_url; - String following_url; - String gists_url; - String starred_url; - String subscriptions_url; - String organizations_url; - String repos_url; - String events_url; - String received_events_url; - String type; - boolean site_admin; - } + if (response == null) { + logger.error("Got null in response"); + CLIENT_IS_ON_LATEST_VERSION = true; + return; + } - private static String readUrl(String urlString) throws Exception { - BufferedReader reader = null; - try { - URL url = new URL(urlString); - reader = new BufferedReader(new InputStreamReader(url.openStream())); - StringBuffer buffer = new StringBuffer(); - int read; - char[] chars = new char[1024]; - while ((read = reader.read(chars)) != -1) - buffer.append(chars, 0, read); + Gson gson = new Gson(); + ArrayList<GameVersion> versions = gson.fromJson(response, new TypeToken<ArrayList<GameVersion>>(){}.getType()); - return buffer.toString(); - } finally { - if (reader != null) - reader.close(); + try { + GameVersion latestVersion = versions.get(0); + CLIENT_IS_ON_LATEST_VERSION = latestVersion.getVersion().equals(MaxonConstants.GAME_VERSION); + } catch (Exception e) { + logger.error("Failed to find the latest version"); + CLIENT_IS_ON_LATEST_VERSION = true; } } - public static boolean isLatestRelease(String githubTag) throws Exception { - return Objects.equals(githubTag, getLatestRelease().tag_name); + @Override + public void failed(Throwable t) { + logger.error(t.getMessage()); + CLIENT_IS_ON_LATEST_VERSION = true; } - public static String getLatestVersion() throws Exception { - return getLatestRelease().tag_name; + @Override + public void cancelled() { + logger.info("Cancelled"); + CLIENT_IS_ON_LATEST_VERSION = true; } - public static Release getLatestRelease() throws Exception { - String url_link = "https://api.github.com/repos/NotDankEnough/MaxonPettingSim/releases/latest"; - - String json = readUrl(url_link); + private static class GameVersion { + private String version; - Gson gson = new Gson(); + public String getVersion() { + return version; + } - return gson.fromJson(json, Release.class); + public void setVersion(String version) { + this.version = version; + } } } |
