summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/SaveControl.java
blob: 7b2b86d5aa79143ad65edc065b282fb69e8388fd (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jrummikub.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.util.Event;
import jrummikub.util.Event3;
import jrummikub.util.IEvent3;
import jrummikub.util.IListener1;
import jrummikub.view.IView;

/**
 * The save control is responsible for loading and saving game and round states
 */
public class SaveControl {
	private GameSettings gameSettings;
	private GameState gameState;
	private IRoundState roundState;
	private Event3<GameSettings, GameState, IRoundState> loadEvent = new Event3<GameSettings, GameState, IRoundState>();
	private Event loadErrorEvent = new Event();

	/**
	 * Creates a new SaveControl
	 * 
	 * @param view
	 *            the view to use
	 */
	public SaveControl(final IView view) {
		view.getSaveEvent().add(new IListener1<File>() {
			@Override
			public void handle(File file) {
				save(file);
			}
		});
		view.getLoadFileEvent().add(new IListener1<File>() {
			@Override
			public void handle(final File file) {
				load(file);
			}
		});
	}

	/**
	 * Getter for loadEvent
	 * 
	 * @return loadEvent
	 */
	public IEvent3<GameSettings, GameState, IRoundState> getLoadEvent() {
		return loadEvent;
	}

	/**
	 * Sets the current game settings
	 * 
	 * @param gameSettings
	 *            the game settings
	 */
	public void setGameSettings(GameSettings gameSettings) {
		this.gameSettings = gameSettings;
	}

	/**
	 * Sets the current game state
	 * 
	 * @param gameState
	 *            the game state
	 */
	public void setGameState(GameState gameState) {
		this.gameState = gameState;
	}

	/**
	 * Sets the current round state
	 * 
	 * @param roundState
	 *            the round state
	 */
	public void setRoundState(IRoundState roundState) {
		this.roundState = roundState;
	}

	/**
	 * Loads the specified file and sets game state and round state. If the file
	 * selected cannot be handled, an error will occur and show
	 * 
	 * @param file
	 *            to be loaded
	 */
	private void load(File file) {
		try {
			ObjectInputStream stream = new ObjectInputStream(
					new FileInputStream(file));

			gameSettings = (GameSettings) stream.readObject();
			gameState = (GameState) stream.readObject();
			roundState = (IRoundState) stream.readObject();

			stream.close();

			if (gameState == null || gameSettings == null) {
				loadErrorEvent.emit();
				return;
			}

			loadEvent.emit(gameSettings, gameState, roundState);

		} catch (Exception e) {
			loadErrorEvent.emit();
		}
	}

	/**
	 * The load error event is emitted when the file selected for loading is not
	 * a rum file
	 * 
	 * @return the event
	 */
	public Event getLoadErrorEvent() {
		return loadErrorEvent;
	}

	/**
	 * Saves the current game state and round state to a file with the specified
	 * file name
	 * 
	 * @param file
	 *            file to save game in
	 */
	private void save(File file) {
		if (gameState == null || gameSettings == null) {
			return;
		}
		try {
			ObjectOutputStream stream = new ObjectOutputStream(
					new FileOutputStream(file));

			stream.writeObject(gameSettings);
			stream.writeObject(gameState);
			stream.writeObject(roundState);
			stream.flush();

			stream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}