|
1
|
|
|
package de.pewpewproject.lasertag.lasertaggame.state.synced.implementation; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.gson.GsonBuilder; |
|
4
|
|
|
import com.google.gson.reflect.TypeToken; |
|
5
|
|
|
import de.pewpewproject.lasertag.LasertagMod; |
|
6
|
|
|
import de.pewpewproject.lasertag.lasertaggame.state.synced.ITeamsConfigState; |
|
7
|
|
|
import de.pewpewproject.lasertag.lasertaggame.team.TeamDto; |
|
8
|
|
|
import de.pewpewproject.lasertag.lasertaggame.team.serialize.TeamConfigManagerDeserializer; |
|
9
|
|
|
import de.pewpewproject.lasertag.lasertaggame.team.serialize.TeamDtoSerializer; |
|
10
|
|
|
import net.minecraft.block.Blocks; |
|
11
|
|
|
|
|
12
|
|
|
import java.io.IOException; |
|
13
|
|
|
import java.nio.file.Files; |
|
14
|
|
|
import java.nio.file.Path; |
|
15
|
|
|
import java.nio.file.StandardOpenOption; |
|
16
|
|
|
import java.util.HashMap; |
|
17
|
|
|
import java.util.List; |
|
18
|
|
|
import java.util.Optional; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Implementation of ITeamsConfigState for the lasertag game. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Étienne Muser |
|
24
|
|
|
*/ |
|
25
|
|
|
public class TeamsConfigState implements ITeamsConfigState { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The path to the team config file |
|
29
|
|
|
*/ |
|
30
|
|
|
private static final Path teamConfigFilePath = LasertagMod.configFolderPath.resolve("teamConfig.json"); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The static instance of the dummy team "Spectators" |
|
34
|
|
|
*/ |
|
35
|
|
|
public static final TeamDto SPECTATORS = new TeamDto(0, "Spectators", 128, 128, 128, null); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The team config |
|
39
|
|
|
* key: The name of the team |
|
40
|
|
|
* value: The TeamDto |
|
41
|
|
|
*/ |
|
42
|
|
|
public HashMap<String, TeamDto> teamConfig; |
|
43
|
|
|
|
|
44
|
|
|
public TeamsConfigState() { |
|
45
|
|
|
reload(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//region Public methods |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Resets the team config manager to its default state |
|
52
|
|
|
*/ |
|
53
|
|
|
public synchronized void reset() { |
|
54
|
|
|
|
|
55
|
|
|
// Clear the old config |
|
56
|
|
|
teamConfig.clear(); |
|
57
|
|
|
|
|
58
|
|
|
// Create the new config |
|
59
|
|
|
var newConfig = createDefaultConfig(); |
|
60
|
|
|
|
|
61
|
|
|
// Set the new config |
|
62
|
|
|
teamConfig.putAll(newConfig); |
|
63
|
|
|
|
|
64
|
|
|
this.persist(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
@Override |
|
68
|
|
|
public synchronized void reload() { |
|
69
|
|
|
|
|
70
|
|
|
// If the config file exists |
|
71
|
|
|
if (Files.exists(teamConfigFilePath)) { |
|
72
|
|
|
try { |
|
73
|
|
|
// Read config file |
|
74
|
|
|
var configFileContents = Files.readString(teamConfigFilePath); |
|
75
|
|
|
|
|
76
|
|
|
teamConfig = deserializeTeamConfig(configFileContents); |
|
77
|
|
|
} catch (IOException ex) { |
|
78
|
|
|
LasertagMod.LOGGER.warn("Reading of team config file failed: " + ex.getMessage()); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
|
|
82
|
|
|
// Log |
|
83
|
|
|
LasertagMod.LOGGER.info("Using default team config..."); |
|
84
|
|
|
|
|
85
|
|
|
// Load default config |
|
86
|
|
|
teamConfig = createDefaultConfig(); |
|
87
|
|
|
|
|
88
|
|
|
// Persist |
|
89
|
|
|
this.persist(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Gets the team identified by its id |
|
95
|
|
|
* |
|
96
|
|
|
* @param id The id of the team |
|
97
|
|
|
* @return |
|
98
|
|
|
*/ |
|
99
|
|
|
public synchronized Optional<TeamDto> getTeamOfId(int id) { |
|
100
|
|
|
|
|
101
|
|
|
return teamConfig.values().stream() |
|
102
|
|
|
.filter(team -> team.id() == id) |
|
103
|
|
|
.findFirst(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gets the team identified by its name |
|
108
|
|
|
* |
|
109
|
|
|
* @param name The name of the team to find |
|
110
|
|
|
* @return |
|
111
|
|
|
*/ |
|
112
|
|
|
public synchronized Optional<TeamDto> getTeamOfName(String name) { |
|
113
|
|
|
|
|
114
|
|
|
return teamConfig.values().stream() |
|
115
|
|
|
.filter(team -> team.name().equals(name)) |
|
116
|
|
|
.findFirst(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
@Override |
|
120
|
|
|
public synchronized List<TeamDto> getTeams() { |
|
121
|
|
|
return teamConfig.values().stream().toList(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
@Override |
|
125
|
|
|
public synchronized void setTeamConfig(String jsonString) { |
|
126
|
|
|
|
|
127
|
|
|
// Clear the old config |
|
128
|
|
|
teamConfig.clear();; |
|
129
|
|
|
|
|
130
|
|
|
// Deserialize the new config |
|
131
|
|
|
var newConfig = deserializeTeamConfig(jsonString); |
|
132
|
|
|
|
|
133
|
|
|
// Set the new config |
|
134
|
|
|
teamConfig.putAll(newConfig); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public synchronized String toJson() { |
|
138
|
|
|
|
|
139
|
|
|
// Copy team config |
|
140
|
|
|
var teamConfigCopy = new HashMap<>(teamConfig); |
|
141
|
|
|
|
|
142
|
|
|
// Remove spectators (Do not persist spectators) |
|
143
|
|
|
teamConfigCopy.remove(SPECTATORS.name()); |
|
144
|
|
|
|
|
145
|
|
|
// Get gson builder |
|
146
|
|
|
var gsonBuilder = new GsonBuilder(); |
|
147
|
|
|
|
|
148
|
|
|
// Register type adapter |
|
149
|
|
|
gsonBuilder.registerTypeAdapter(TeamDto.class, TeamDtoSerializer.getSerializer()); |
|
150
|
|
|
|
|
151
|
|
|
// Serialize |
|
152
|
|
|
return gsonBuilder.setPrettyPrinting().create().toJson(teamConfigCopy); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
//endregion |
|
156
|
|
|
|
|
157
|
|
|
//region Private methods |
|
158
|
|
|
|
|
159
|
|
|
private static HashMap<String, TeamDto> deserializeTeamConfig(String json) { |
|
160
|
|
|
|
|
161
|
|
|
// get gson builder |
|
162
|
|
|
var gsonBuilder = new GsonBuilder(); |
|
163
|
|
|
|
|
164
|
|
|
// Get deserializer |
|
165
|
|
|
var deserializer = TeamConfigManagerDeserializer.getDeserializer(); |
|
166
|
|
|
|
|
167
|
|
|
// Register type |
|
168
|
|
|
gsonBuilder.registerTypeAdapter(HashMap.class, deserializer); |
|
169
|
|
|
|
|
170
|
|
|
// Parse |
|
171
|
|
|
HashMap<String, TeamDto> teamConfig = gsonBuilder.create().fromJson(json, new TypeToken<HashMap<String, TeamDto>>() {}.getType()); |
|
172
|
|
|
|
|
173
|
|
|
// Add dummy team spectators |
|
174
|
|
|
teamConfig.put(SPECTATORS.name(), SPECTATORS); |
|
175
|
|
|
|
|
176
|
|
|
return teamConfig; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private static HashMap<String, TeamDto> createDefaultConfig() { |
|
180
|
|
|
|
|
181
|
|
|
// Create map |
|
182
|
|
|
var teamConfig = new HashMap<String, TeamDto>(); |
|
183
|
|
|
|
|
184
|
|
|
// Fill map with default values |
|
185
|
|
|
teamConfig.put("Red", new TeamDto(1, "Red", 255, 0, 0, Blocks.RED_CONCRETE)); |
|
186
|
|
|
teamConfig.put("Green", new TeamDto(2, "Green", 0, 255, 0, Blocks.LIME_CONCRETE)); |
|
187
|
|
|
teamConfig.put("Blue", new TeamDto(3, "Blue", 0, 0, 255, Blocks.BLUE_CONCRETE)); |
|
188
|
|
|
teamConfig.put("Orange", new TeamDto(4,"Orange", 255, 128, 0, Blocks.ORANGE_CONCRETE)); |
|
189
|
|
|
teamConfig.put("Teal", new TeamDto(5, "Teal", 0, 128, 255, Blocks.LIGHT_BLUE_CONCRETE)); |
|
190
|
|
|
teamConfig.put("Pink", new TeamDto(6, "Pink", 255, 0, 255, Blocks.PINK_CONCRETE)); |
|
191
|
|
|
|
|
192
|
|
|
// Add dummy team spectators |
|
193
|
|
|
teamConfig.put(SPECTATORS.name(), SPECTATORS); |
|
194
|
|
|
|
|
195
|
|
|
return teamConfig; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
private void persist() { |
|
199
|
|
|
|
|
200
|
|
|
// Serialize |
|
201
|
|
|
var configJson = this.toJson(); |
|
202
|
|
|
|
|
203
|
|
|
try { |
|
204
|
|
|
// Write to file |
|
205
|
|
|
Files.createDirectories(teamConfigFilePath.getParent()); |
|
206
|
|
|
Files.writeString(teamConfigFilePath, configJson, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); |
|
207
|
|
|
} catch (IOException e) { |
|
208
|
|
|
LasertagMod.LOGGER.error("Writing to team config file failed: " + e.getMessage()); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
//endregion |
|
213
|
|
|
} |
|
214
|
|
|
|