Answers
class RMS {
static String missionRMS = "MISSION_RMS ";
public RMS() {
}
void storeTask(Task task) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
// write task info
dos.writeInt(task.id);
dos.writeUTF(task.label);
dos.writeUTF(task.description);
dos.writeUTF(task.rewardInfo);
dos.writeInt(task.price);
// write components info
TaskComponent[] taskComponents = task.taskComponents;
int numTaskComponents = taskComponents.length;
dos.writeInt(numTaskComponents);
for (int i = 0; i < numTaskComponents; i++) {
TaskComponent component = taskComponents[i];
NPC npc = component.npc;
// write npc info in this component
dos.writeInt(npc.id);
dos.writeInt(npc.countryID);
dos.writeInt(npc.sex);
dos.writeInt(npc.ai);
dos.writeInt(npc.level);
dos.writeUTF(npc.name);
// component info
dos.writeInt(component.numWinTimes);
dos.writeInt(component.shouldWinSequentially);
}
byte data[] = baos.toByteArray();
RecordStore rs = openRecordStore();
if (rs != null) {
if (rs.getNumRecords() <= 0) {
rs.addRecord(data, 0, data.length);
System.out.println( "Adding new record ");
} else {
rs.setRecord(1, data, 0, data.length);
System.out.println( "updating record ");
}
rs.closeRecordStore();
System.out.println( "Task saved successfully. ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
Task getTask() {
try {
RecordStore rs = openRecordStore();
if (rs == null || rs.getNumRecords() <= 0) {
if (rs != null) {
rs.closeRecordStore();
}
return null;
} else {
byte[] data = rs.getRecord(1);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
int id, price, numComponents;
String label, description, rewardInfo;
TaskComponent[] taskComponents;
id = dis.readInt();
label = dis.readUTF();
description = dis.readUTF();
rewardInfo = dis.readUTF();
price = dis.readInt();
System.out.println( "id : " + id);
System.out.println( "label : " + label);
System.out.println( "description : " + description);
System.out.println( "rewardInfo " + rewardInfo);
// components
numComponents = dis.readInt();
taskComponents = new TaskComponent[numComponents];
for (int i = 0; i < numComponents; i++) {
int npcID, npcCountryID, npcSex, npcAI, npcLevel, numWinTimes, shouldWinSequentially;
String npcName;
npcID = dis.readInt();
npcCountryID = dis.readInt();
npcSex = dis.readInt();
npcAI = dis.readInt();
npcLevel = dis.readInt();
npcName = dis.readUTF();
numWinTimes = dis.readInt();