summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/SaveControl.java
blob: 14121997f87e8ce05e90186b1e65d7b7de7fa8b9 (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
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.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>();

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

		view.getLoadEvent().add(new IListener1<File>() {
			@Override
			public void handle(File file) {
				load(file);
			}
		});
	}

	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;
	}

	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) {
				// TODO Fehlermeldung
				System.err.println("laden ging nicht");
				return;
			}

			loadEvent.emit(gameSettings, gameState, roundState);

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

	private void save(File file) {
		if (gameState == null || gameSettings == null) {
			// TODO Menüpunkt ausgrauen
			System.err.println("kein aktives Spiel");
			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();
		}
	}
}