ca.live.hk12.crescent.server.database.WorldDatabase.java Source code

Java tutorial

Introduction

Here is the source code for ca.live.hk12.crescent.server.database.WorldDatabase.java

Source

/**
 * You should have received a copy of the GNU General Public License version 3
 * along with this work; Please find the Copyright information and Terms and
 * Conditions in the ClientLauncher.java or ServerLauncher.java file.
 */

package ca.live.hk12.crescent.server.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;

import ca.live.hk12.crescent.network.Packets.World;
import ca.live.hk12.crescent.network.Packets.WorldInfo;
import ca.live.hk12.crescent.server.service.FileService;
import ca.live.hk12.crescent.server.service.ServerException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class WorldDatabase {

    //   public static void main(String[] args) {
    //      WorldDatabase wdb = new WorldDatabase();
    //      WorldInfo info = new WorldInfo();
    //      info.blockViewRadius = 1;
    //      info.depth = 1;
    //      info.numSectors = 2;
    //      info.numTiles = 10;
    //      info.numZones = 3;
    //      wdb.addNewWorld(info, (short) 1, true);
    //   }

    private ArrayList<OnlineWorld> worlds;

    public WorldDatabase() {
        FileService.makeDirectory(FileService.getWorldDirectory());

        worlds = new ArrayList<OnlineWorld>();
    }

    public void gracefulExit() {
        ArrayList<Byte> worldsNotSaved = new ArrayList<Byte>();
        for (OnlineWorld world : worlds) {
            if (!saveWorld(world)) {
                worldsNotSaved.add(world.info.worldID);
            }
        }
        if (worldsNotSaved.size() > 0) {
            throw new ServerException(
                    "The following player(s) could not be saved:<br/>" + worldsNotSaved.toString());
        }
    }

    private World loadWorld(byte worldID) {
        if (!doesWorldExists(worldID)) {
            throw new ServerException("World id-" + worldID + " does not exist. Stopping load order.");
        }
        String dataLocation = FileService.getWorldDataLocation(worldID);
        String infoLocation = FileService.getWorldInfoLocation(worldID);
        World world = new World();
        world.map = FileService.readData(dataLocation, short[][].class);
        world.info = FileService.readData(infoLocation, WorldInfo.class);
        return world;
    }

    public boolean unloadWorld(byte worldID) {
        World world = getWorld(worldID);
        if (saveWorld(world)) {
            worlds.remove(world);
        }
        return false;
    }

    public boolean saveWorld(World world) {
        String dataLocation = FileService.getWorldDataLocation(world.info.worldID);
        String infoLocation = FileService.getWorldInfoLocation(world.info.worldID);

        Gson gson = new GsonBuilder().create();

        try (Writer writer = new OutputStreamWriter(new FileOutputStream(dataLocation), "UTF-8")) {
            gson.toJson(world.map, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        try (Writer writer = new OutputStreamWriter(new FileOutputStream(infoLocation), "UTF-8")) {
            gson.toJson(world.info, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        worlds.remove(world);

        return true;
    }

    private boolean doesWorldExists(byte worldID) {
        if (new File(FileService.getWorldInfoLocation(worldID)).exists()) {
            return true;
        }
        return false;
    }

    public OnlineWorld getWorld(byte worldID) {
        for (OnlineWorld world : worlds) {
            if (world.info.worldID == worldID) {
                return world;
            }
        }
        return null;
    }

    public ArrayList<OnlineWorld> getWorlds() {
        return worlds;
    }

    public boolean addWorld(byte worldID) {
        World world = loadWorld(worldID);
        if (world == null)
            return false;
        worlds.add(new OnlineWorld(world));
        return true;
    }

    public boolean saveWorld(byte worldID) {
        World world = getWorld(worldID);
        if (world == null)
            return false;
        return saveWorld(world);
    }

    /*========================================================================
                                    Database Testing Below  */
    /**
    * For Test World Creation
    */
    private void addNewWorld(WorldInfo info, short initialTileID, boolean initialSave) {
        World world = WorldFactory.createBlankWorld(info, initialTileID);
        worlds.add(new OnlineWorld(world));

        if (initialSave) {
            saveWorld(world);
        }
    }

    public static void main(String[] args) {
        WorldDatabase wdb = new WorldDatabase();

        WorldInfo info = new WorldInfo();
        info.worldID = (byte) 0;
        info.blockViewRadius = 6;
        info.depth = 3;
        info.sectorsPerWorld = 2;
        info.zonesPerSector = 3;
        info.tilesPerZone = 4;

        // WorldInfo info = new WorldInfo();
        // info.depth = 1;
        // info.worldID = 1;
        //   
        // info.numSectors = 1;
        // info.numZones = 3;
        // info.numTiles = 10;

        // FileService.makeDirectory(FileService.getWorldDataLocation(info.worldID));

        wdb.addNewWorld(info, (byte) 1, true);

        // wdb.saveWorld(world.info.worldID);
        // wdb.addWorld((byte) 1);
    }

}