Skip to content

Commit

Permalink
Update FileUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-bansal authored Jul 27, 2018
1 parent e4a9673 commit 1b20dcf
Showing 1 changed file with 122 additions and 12 deletions.
134 changes: 122 additions & 12 deletions src/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.*;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;

Expand All @@ -14,12 +12,23 @@ public class FileUtils {

static void createAccount(String username, int PIN) throws java.lang.Exception {
JSONObject ob1 = new JSONObject();
JSONArray ob2 = new JSONArray();
JSONObject ob3 = new JSONObject();
ob3.put("username", username);
ob3.put("PIN", PIN);
ob2.add(ob3);
ob1.put(username, ob3);
JSONObject ob2 = new JSONObject();

JSONArray overall = new JSONArray();
JSONArray inventory = new JSONArray();
JSONArray battleD = new JSONArray();

ob2.put("username", username);
ob2.put("PIN", PIN);
ob2.put("OverallCards", overall);
ob2.put("Inventory", inventory);
ob2.put("BattleDeck", battleD);
ob2.put("PlayerLevel", 1);
ob2.put("PlayerDust", 25);
ob2.put("PlayerEXP", 0);
ob2.put("GameLevel", 1);
ob1.put(username, ob2);


Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
Expand Down Expand Up @@ -56,13 +65,114 @@ static boolean checkinDataBase(String username, int... pincheck) {
return true;
}


} catch (Exception e) {

}
return false;
}

static void prettyify() throws java.lang.Exception {
static void saveGame(GameUtils gm) throws java.lang.Exception {

ArrayList<String> saveOverall = new ArrayList<>();
ArrayList<String> saveInventory = new ArrayList<>();
ArrayList<String> saveBattleDeck = new ArrayList<>();

for(Card item : gm.overall) {
saveOverall.add(item.getName() + "~" + item.getLevel() + "~" + item.getDamage() + "~" + item.getBlock() + "~" + item.getCardType() + "~" + item.getLore());
}
for(Card item : gm.currentdeck) {
saveBattleDeck.add(item.getName() + "~" + item.getLevel() + "~" + item.getDamage() + "~" + item.getBlock() + "~" + item.getCardType() + "~" + item.getLore());
}
for(Card item : gm.inventory) {
saveInventory.add(item.getName() + "~" + item.getLevel() + "~" + item.getDamage() + "~" + item.getBlock() + "~" + item.getCardType() + "~" + item.getLore());
}

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(Main.datafile));
JSONObject Jobj = (JSONObject) obj;

JSONObject userData = (JSONObject) Jobj.get(Main.username);
userData.put("PlayerEXP", Main.playerEXP);
userData.put("PlayerLevel", Main.playerlevel);
userData.put("PlayerDust", Main.dust);
userData.put("GameLevel", Main.playGamelevel);

JSONArray setoverall = (JSONArray) userData.get("OverallCards");
setoverall.clear();
setoverall.addAll(saveOverall);
JSONArray setinventory = (JSONArray) userData.get("Inventory");
setinventory.clear();
setinventory.addAll(saveInventory);
JSONArray setbattledeck = (JSONArray) userData.get("BattleDeck");
setbattledeck.clear();
setbattledeck.addAll(saveBattleDeck);

userData.put("OverallCards", setoverall);
userData.put("Inventory", setinventory);
userData.put("BattleDeck", setbattledeck);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(Jobj.toJSONString());
String prettyJsonString = gson.toJson(je);
Main.jsondata = prettyJsonString;

try(FileWriter writer = new FileWriter(Main.datafile)) {
writer.write(prettyJsonString);
writer.flush();
System.out.println("Game Saved.");
}
}

static void loadData(GameUtils gm) throws Exception {

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(Main.datafile));
JSONObject Jobj = (JSONObject) obj;

JSONObject userData = (JSONObject) Jobj.get(Main.username);

Main.playerlevel = ((Long) userData.get("PlayerLevel")).intValue();
Main.dust = ((Long) userData.get("PlayerDust")).intValue();
Main.playGamelevel = ((Long) userData.get("GameLevel")).intValue();
Main.playerEXP = ((Long) userData.get("PlayerEXP")).intValue();
JSONArray Overall = (JSONArray) userData.get("OverallCards");
JSONArray Inventory = (JSONArray) userData.get("Inventory");
JSONArray BattleDeck = (JSONArray) userData.get("BattleDeck");
String[] getOverall = new String[Overall.size()];
String[] getInventory = new String[Overall.size()];
String[] getBattleDeck = new String[Overall.size()];

gm.overall.clear();
gm.inventory.clear();
gm.currentdeck.clear();

for (int i = 0; i < Overall.size(); i++) {
getOverall[i] = (String) Overall.get(i);
}
for (int i = 0; i < Inventory.size(); i++) {
getInventory[i] = (String) Inventory.get(i);
}
for (int i = 0; i < BattleDeck.size(); i++) {
getBattleDeck[i] = (String) BattleDeck.get(i);
}

for(String item : getOverall) {
String[] split = item.split("~");
gm.overall.add(new Card(split[0], Integer.parseInt(split[2]), Integer.parseInt(split[3]), split[4], split[5], Integer.parseInt(split[1])));
}
for(String item : getInventory) {
String[] split = item.split("~");
gm.inventory.add(new Card(split[0], Integer.parseInt(split[2]), Integer.parseInt(split[3]), split[4], split[5], Integer.parseInt(split[1])));
}
for(String item : getBattleDeck) {
String[] split = item.split("~");
gm.currentdeck.add(new Card(split[0], Integer.parseInt(split[2]), Integer.parseInt(split[3]), split[4], split[5], Integer.parseInt(split[1])));
}

}

private static void prettyify() throws java.lang.Exception {
BufferedReader in = new BufferedReader(new FileReader(Main.datafile));
String str;

Expand Down

0 comments on commit 1b20dcf

Please sign in to comment.