Skip to content

Instantly share code, notes, and snippets.

@Jire
Created July 3, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jire/1062716 to your computer and use it in GitHub Desktop.
Save Jire/1062716 to your computer and use it in GitHub Desktop.
This implementation is used for a generic world loader in which player process results and save/load results are at standard.
/*
* Nital is an effort to provide a well documented, powerful, scalable, and robust
* RuneScape server framework delivered open-source to all users.
*
* Copyright (C) 2011 Nital Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package us.nital.world.impl;
import java.io.IOException;
import org.jboss.netty.buffer.ChannelBuffers;
import us.nital.model.Player;
import us.nital.net.io.InBuffer;
import us.nital.net.io.OutBuffer;
import us.nital.util.FileUtils;
import us.nital.world.Result;
import us.nital.world.ReturnCodes;
import us.nital.world.WorldLoader;
/**
* This implementation is used for a generic world loader in which player
* process results and save/load results are at standard.
*
* <p>When a player is processed the result created contains <tt>2</tt> attachments.
* The attachment <b>player</b> is equivalent to the player who the result was processed
* for. The attachment <b>returnCode</b> is a {@link ReturnCodes} constant value.</p>
*
* @author Thomas Nappo
* @see {@link WorldLoader} if you are unclear about how the implementation
* works.
*/
public class GenericWorldLoader implements WorldLoader {
/**
* The location of player file saves.
*/
private static final String FILE_LOCATION = "data/games/";
@Override
public Result process(Player player) {
/*
* The player's game save could not be loaded therefore
* the result cannot be processed using their save information.
*/
if (!load(player))
return null;
/*
* Create a new result which we use add our attachments to and
* finally return back.
*/
Result result = new Result();
/*
* Because the player has been loaded we can attach it to the result.
*/
result.putAttachment("player", player);
// TODO: Generate a real return code based on
// the player's attributes.
result.putAttachment("returnCode", ReturnCodes.SUCCESS);
/*
* Return back the product result.
*/
return result;
}
@Override
public boolean save(Player player) {
/*
* Construct a new output buffer with a provided dynamic buffer.
*/
OutBuffer buf = new OutBuffer(ChannelBuffers.dynamicBuffer());
/*
* Use the buffer to call saving of the player's game progress.
*/
player.save(buf);
/*
* Attempt to use our file utilities to write the buffer towards
* an appropriate game file.
*/
try {
FileUtils.writeBufferToFile(FILE_LOCATION
+ player.getSession().getUsername()
+ ".bin", buf.getInternalBuffer());
} catch (IOException e) {
/*
* An exception occuring notifies us that the player file was
* not loaded successfully, therefore we return back to notify
* a caller of this method that the save attempt failed.
*/
return false;
}
/*
* If everything went good we should arrive here. We then return
* back to notify the caller that the save was a success.
*/
return true;
}
@Override
public boolean load(Player player) {
/*
* Attempt to process the load request.
*/
try {
/*
* We attempt to create a new input buffer.
*/
InBuffer buf = new InBuffer(FileUtils.buildBufferFromFile(FILE_LOCATION
+ player.getSession().getUsername()
+ ".bin"));
/*
* We then call the load method from the player's save progress.
* This will throw an exception if {buf} was equivalent to null.
*/
player.load(buf);
} catch (Throwable t) {
/*
* Should it fail we catch it and return back false to signal
* the load request failed.
*/
return false;
}
/*
* If everything went good we should arrive here. We then return
* back to notify the caller that the save was a success.
*/
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment