Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exception: http://img534.imageshack.us/img534/9008/32921273.png
- package model.db;
- import Levels.Level;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.Reader;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.ListIterator;
- import misc.Background;
- import org.jdom2.Document;
- import org.jdom2.Element;
- import org.jdom2.input.SAXBuilder;
- /**
- *
- * @author Nindustries
- */
- public class FIconnect {
- private static String DELI = ",";
- private static String FILENAME = "scores.txt";
- private static String FILE_LEVELS = "levels.xml";
- public ArrayList<Level> loadLevels() throws Exception {
- ArrayList<Level> result = new ArrayList();
- StarFactory factory = new StarFactory();
- SAXBuilder builder = new SAXBuilder(false);
- Document doc = builder.build(this.getClass().getResourceAsStream("/resources/" + FIconnect.FILE_LEVELS));
- Element project = doc.getRootElement();
- List levels = project.getChildren();
- ListIterator it2 = levels.listIterator();
- while (it2.hasNext()){
- //Loop through levels
- Element el = (Element)it2.next();
- Level l = new Level();
- List blocks = el.getChildren();
- ListIterator it22 = blocks.listIterator();
- l.setBackground(new Background(el.getAttribute("Background").getValue()));
- while (it22.hasNext()){
- //Loop through targets
- Element target = (Element)it22.next();
- l.addTarget(factory.produceTarget(target.getText()));
- }
- result.add(l);
- }
- return result;
- }
- public void saveScore( String username, String score ) throws Exception {
- try {
- PrintWriter output = new PrintWriter(new FileWriter(convertStreamToString(getClass().getResourceAsStream("/resources/" + FILENAME)),true));
- output.printf("%s\r\n", username + FIconnect.DELI + score);
- output.close();
- }
- catch (Exception e) {
- throw new Exception("Error while saving offline score: " + e.toString());
- }
- }
- private String convertStreamToString(InputStream is) throws Exception {
- if (is != null) {
- Writer writer = new StringWriter();
- char[] buffer = new char[1024];
- try {
- Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- int n;
- while ((n = reader.read(buffer)) != -1) {
- writer.write(buffer, 0, n);
- }
- } finally {
- is.close();
- }
- return writer.toString();
- } else {
- return "";
- }
- }
- public ArrayList<String> loadScores() throws Exception {
- ArrayList<String> result = new ArrayList();
- try {
- BufferedReader br = new BufferedReader(new FileReader(convertStreamToString(getClass().getResourceAsStream("/resources/" + FILENAME))));
- try {
- StringBuilder sb = new StringBuilder();
- String line = br.readLine();
- while (line != null) {
- sb.append(line);
- sb.append("\n");
- result.add(br.readLine());
- }
- } finally {
- br.close();
- }
- } catch (Exception e){
- throw new Exception("Error while loading offline scores: " + e.toString());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment