step.close();
}
}
private void uploadDisks(BlobKey zipBlobKey) throws GameEngineException {
Step step = MiniProfiler.step("API.uploadDisks");
try {
if (zipBlobKey == null) {
return;
}
final ZipInputStream zis;
try {
zis = getZipFile(zipBlobKey);
ZipEntry ze;
CSVParser p = new CSVParser();
while ((ze = zis.getNextEntry()) != null) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] b = new byte[2048];
int size = 0;
while ((size = zis.read(b, 0, b.length)) != -1) {
baos.write(b, 0, size);
}
String[] parts = ze.getName().split("/");
String fileName = parts[parts.length - 1];
if (fileName.contains("disks.csv")) {
final String csv = new String(baos.toByteArray());
List<?> headers = null;
boolean h = true;
for (String line : csv.split("\n")) {
line = line.trim();
// first row
if (h) {
headers = Arrays.asList(p.parseLine(line));
h = false;
}
// data
else {
final String[] diskData = p.parseLine(line);
final int length = diskData.length;
if (length <= 1) {
break;
}
final String diskName = diskData[headers
.indexOf("name")];
final String type = diskData[headers
.indexOf("type")];
int attack = Integer.parseInt(diskData[headers
.indexOf("attack")]);
int defense = Integer.parseInt(diskData[headers
.indexOf("defense")]);
int toughness = Integer
.parseInt(diskData[headers
.indexOf("toughness")]);
int movement = Integer
.parseInt(diskData[headers
.indexOf("movement")]);
int wounds = Integer.parseInt(diskData[headers
.indexOf("wounds")]);
int indexOfFlying = headers.indexOf("flying");
String s = diskData[indexOfFlying];
boolean flying = Boolean.parseBoolean(s);
boolean swashbuckler = headers
.indexOf("swashbuckler") > -1
&& !diskData[headers
.indexOf("swashbuckler")]
.equals("null") ? Boolean
.parseBoolean(diskData[headers
.indexOf("swashbuckler")])
: false;
boolean archer = headers.indexOf("archer") > -1
&& !diskData[headers.indexOf("archer")]
.equals("null") ? Boolean
.parseBoolean(diskData[headers
.indexOf("archer")]) : false;
int arrows = headers.indexOf("arrows") > -1
&& !diskData[headers.indexOf("arrows")]
.equals("null") ? Integer
.parseInt(diskData[headers
.indexOf("arrows")]) : 0;
int bolts = headers.indexOf("bolts") > -1
&& !diskData[headers.indexOf("bolts")]
.equals("null") ? Integer
.parseInt(diskData[headers
.indexOf("bolts")]) : 0;
int fireballs = headers.indexOf("fireballs") > -1
&& !diskData[headers
.indexOf("fireballs")]
.equals("null") ? Integer
.parseInt(diskData[headers
.indexOf("fireballs")]) : 0;
int boulders = headers.indexOf("boulders") > -1
&& !diskData[headers
.indexOf("boulders")]
.equals("null") ? Integer
.parseInt(diskData[headers
.indexOf("boulders")]) : 0;
boolean missileImmunity = headers
.indexOf("missileImmunity") > -1
&& !diskData[headers
.indexOf("missileImmunity")]
.equals("null") ? Boolean
.parseBoolean(diskData[headers
.indexOf("missileImmunity")])
: false;
boolean firstblow = Boolean
.parseBoolean(headers
.indexOf("firstblow") > -1
&& !diskData[headers
.indexOf("firstblow")]
.equals("null") ? diskData[headers
.indexOf("firstblow")]
: "false");
int spellcaster = Integer
.parseInt(headers
.indexOf("spellcaster") > -1
&& !diskData[headers
.indexOf("spellcaster")]
.equals("null") ? diskData[headers
.indexOf("spellcaster")] : "0");
int limit = headers.indexOf("limit") > -1
&& !diskData[headers.indexOf("limit")]
.equals("null") ? Integer
.parseInt(diskData[headers
.indexOf("limit")]) : 0;
int cost = Integer.parseInt(diskData[headers
.indexOf("cost")]);
ScriptableObject disk = createDisk(
diskName,
type,
attack,
defense,
toughness,
movement,
wounds,
flying,
swashbuckler,
archer,
arrows,
bolts,
fireballs,
boulders,
missileImmunity,
firstblow,
spellcaster,
limit,
cost,
diskData[headers.indexOf("faction")],
diskData[headers.indexOf("alignment")],
diskData[headers.indexOf("diameter")],
diskData[headers.indexOf("description")],
diskData[headers.indexOf("price")]);
saveDisk(disk);
}
}
}
}
zis.close();
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// delete the zip
if (zipBlobKey != null) {
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
blobstoreService.delete(zipBlobKey);
}
}
} finally {
step.close();
}
}