aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/zajc/gogarchiver/api/Game.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/zajc/gogarchiver/api/Game.java')
-rw-r--r--src/main/java/zajc/gogarchiver/api/Game.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/main/java/zajc/gogarchiver/api/Game.java b/src/main/java/zajc/gogarchiver/api/Game.java
new file mode 100644
index 0000000..5180a20
--- /dev/null
+++ b/src/main/java/zajc/gogarchiver/api/Game.java
@@ -0,0 +1,103 @@
1//SPDX-License-Identifier: GPL-3.0
2/*
3 * gogarchiver-ng, an archival tool for GOG.com
4 * Copyright (C) 2024 Marko Zajc
5 *
6 * This program is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this
15 * program. If not, see <https://www.gnu.org/licenses/>.
16 */
17package zajc.gogarchiver.api;
18
19import static java.util.Objects.hash;
20import static zajc.gogarchiver.util.Utilities.stream;
21
22import java.util.*;
23
24import javax.annotation.Nonnull;
25
26import kong.unirest.json.*;
27import zajc.gogarchiver.api.GameDownload.Platform;
28
29public class Game {
30
31 @Nonnull private final User user;
32 @Nonnull private final String id;
33 @Nonnull private final String title;
34 @Nonnull private final List<GameDownload> downloads;
35 @Nonnull private final List<GameDlc> dlcs;
36
37 @SuppressWarnings("null")
38 protected Game(@Nonnull User user, @Nonnull String id, @Nonnull String title, @Nonnull JSONObject downloads,
39 @Nonnull JSONArray dlcs) {
40 this.user = user;
41 this.id = id;
42 this.title = title;
43
44 this.downloads = downloads.keySet().stream().flatMap(p -> {
45 var platform = Platform.valueOf(p.toUpperCase());
46 return stream(downloads.getJSONArray(p)).map(JSONObject.class::cast)
47 .map(d -> GameDownload.fromJson(this, d, platform)); // NOSONAR
48 }).toList();
49
50 this.dlcs = stream(dlcs).map(JSONObject.class::cast).map(j -> GameDlc.fromJson(this, j)).toList();
51 }
52
53 @Nonnull
54 @SuppressWarnings("null")
55 public static Game fromJson(@Nonnull User user, @Nonnull JSONObject json, @Nonnull String id) {
56 var title = json.getString("title");
57 var downloads = json.getJSONArray("downloads").getJSONArray(0).getJSONObject(1);
58 var dlcs = json.getJSONArray("dlcs");
59
60 return new Game(user, id, title, downloads, dlcs);
61 }
62
63 @Nonnull
64 public User getUser() {
65 return this.user;
66 }
67
68 @Nonnull
69 public String getId() {
70 return this.id;
71 }
72
73 @Nonnull
74 public String getTitle() {
75 return this.title;
76 }
77
78 @Nonnull
79 public List<GameDownload> getDownloads() {
80 return this.downloads;
81 }
82
83 @Nonnull
84 public List<GameDlc> getDlcs() {
85 return this.dlcs;
86 }
87
88 @Override
89 public int hashCode() {
90 return hash(this.id, this.title);
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj)
96 return true;
97 if (obj instanceof Game other)
98 return Objects.equals(this.id, other.id) && Objects.equals(this.title, other.title);
99 else
100 return false;
101 }
102
103}