aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/controller/commands/LoadCommand.java
blob: 26e18e251333eb229a4a8397657c0575ecdf4526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package esieequest.controller.commands;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

import esieequest.model.Game;
import esieequest.model.Text;
import esieequest.view.Viewable;

/**
 * Loads a saved Game.
 * 
 * @author Pacien TRAN-GIRARD
 */
public class LoadCommand implements Executable {

	@Override
	public void execute(final String argument, final Game game, final Viewable view) {

		if (argument == null) {
			return;
		}

		final String jsonString;
		if (argument.startsWith("{")) {
			jsonString = argument;
		} else {
			jsonString = view.load(argument);
		}

		JSONObject datas = new JSONObject();
		try {
			datas = (JSONObject) JSONValue.parseWithException(jsonString);
		} catch (final ParseException e) {
			view.echo(Text.PARSING_ERROR_PREFIX.toString() + e.getMessage()
					+ Text.PARSING_ERROR_SUFFIX.toString());
			return;
		}

		try {
			game.newGame(false, false);
			game.deserialise(datas);
		} catch (final Exception e) {
			view.echo(Text.DESERIALISING_ERROR_PREFIX.toString() + e.getMessage()
					+ Text.DESERIALISING_ERROR_SUFFIX.toString());
			e.printStackTrace();
			return;
		}

		view.enableInput();
		view.updateQuest(game.getPlayer().getCurrentQuest());
		view.updateInventory(game.getPlayer().getInventory());
		view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer()
				.getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer()
				.canGoBack());

	}
}