Skip to content

Commit abe1b80

Browse files
committed
Store player save & version data on Game Sync data creation (#36)
1 parent 8e614e0 commit abe1b80

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/main/java/entralinked/model/player/PlayerManager.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.fasterxml.jackson.databind.ObjectMapper;
1818
import com.fasterxml.jackson.databind.SerializationFeature;
1919

20+
import entralinked.GameVersion;
21+
2022
/**
2123
* Manager class for managing {@link Player} information (Global Link users)
2224
*/
@@ -175,7 +177,7 @@ private boolean savePlayer(Player player, File outputFile) {
175177
* That is, the specified Game Sync ID wasn't already registered and the player data
176178
* was saved without any errors.
177179
*/
178-
public Player registerPlayer(String gameSyncId) {
180+
public Player registerPlayer(String gameSyncId, GameVersion version) {
179181
// Check for duplicate Game Sync ID
180182
if(playerMap.containsKey(gameSyncId)) {
181183
logger.warn("Attempted to register duplicate player {}", gameSyncId);
@@ -193,6 +195,7 @@ public Player registerPlayer(String gameSyncId) {
193195
// Construct player object
194196
Player player = new Player(gameSyncId);
195197
player.setStatus(PlayerStatus.AWAKE);
198+
player.setGameVersion(version);
196199
player.setDataDirectory(playerDataDirectory);
197200

198201
// Try to save player data

src/main/java/entralinked/network/http/pgl/PglHandler.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package entralinked.network.http.pgl;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.File;
45
import java.io.FileInputStream;
56
import java.io.IOException;
@@ -355,6 +356,12 @@ private void handleMemoryLink(PglRequest request, Context ctx) throws IOExceptio
355356
return;
356357
}
357358

359+
// Version null check because this can happen in specific cases
360+
if(player.getGameVersion() == null) {
361+
writeStatusCode(outputStream, 5); // No game save data exists for this Game Sync ID
362+
return;
363+
}
364+
358365
// Check if the save file belongs to Black or White
359366
if(player.getGameVersion().isVersion2()) {
360367
writeStatusCode(outputStream, 10); // Not a Black or White save
@@ -485,13 +492,9 @@ private void handleUploadSaveData(PglRequest request, Context ctx) throws IOExce
485492
/**
486493
* POST handler for {@code /dsio/gw?p=account.create.upload}
487494
*/
488-
private void handleCreateAccount(PglRequest request, Context ctx) throws IOException {
489-
// It sends the entire save file, but we just skip through it because we don't need anything from it here
490-
ServletInputStream inputStream = ctx.req().getInputStream();
491-
492-
while(!inputStream.isFinished()) {
493-
inputStream.read();
494-
}
495+
private void handleCreateAccount(PglRequest request, Context ctx) throws IOException {
496+
// Have to read all the bytes first for some reason
497+
byte[] bytes = ctx.bodyAsBytes();
495498

496499
// Prepare response
497500
LEOutputStream outputStream = new LEOutputStream(ctx.outputStream());
@@ -509,11 +512,19 @@ private void handleCreateAccount(PglRequest request, Context ctx) throws IOExcep
509512
}
510513

511514
// Try to register player
512-
if(playerManager.registerPlayer(request.gameSyncId()) == null) {
515+
Player player = playerManager.registerPlayer(request.gameSyncId(), request.gameVersion());
516+
517+
if(player == null) {
513518
writeStatusCode(outputStream, 3); // Registration error
514519
return;
515520
}
516521

522+
// Try to store save data
523+
if(!playerManager.storePlayerGameSaveFile(player, new ByteArrayInputStream(bytes))) {
524+
writeStatusCode(outputStream, 4); // Game save data IO error
525+
return;
526+
}
527+
517528
// Write status code
518529
writeStatusCode(outputStream, 0);
519530
}
@@ -534,7 +545,8 @@ private void handleCreateData(PglRequest request, Context ctx) throws IOExceptio
534545
}
535546

536547
// Try to register player
537-
if(playerManager.registerPlayer(gameSyncId) == null) {
548+
// Regrettably, this request does not contain game save & version data.
549+
if(playerManager.registerPlayer(gameSyncId, null) == null) {
538550
writeStatusCode(outputStream, 3); // Registration error
539551
return;
540552
}

0 commit comments

Comments
 (0)