Passed
Pull Request — master (#7)
by
unknown
02:52
created

setJoinSecrert(String)   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
package net.labymod.serverapi.discord;
2
3
import java.io.Closeable;
4
import java.io.IOException;
5
import java.util.Observable;
6
import java.util.Observer;
7
8
import com.google.gson.JsonObject;
9
10
import lombok.Builder;
11
import lombok.Getter;
12
13
@Builder
14
@Getter
15
public class RichPresence extends Observable implements Observer, Closeable {
16
	private String matchSecret;
17
	private String spectateSecret;
18
	private String joinSecrert;
19
	private Game game;
20
	private Party party;
21
	
22
	public void setMatchSecret(String matchSecret) {
23
		this.matchSecret = matchSecret;
24
		setChanged();
25
		notifyObservers();
26
	}
27
28
	public void setSpectateSecret(String spectateSecret) {
29
		this.spectateSecret = spectateSecret;
30
		setChanged();
31
		notifyObservers();
32
	}
33
34
	public void setJoinSecrert(String joinSecret) {
35
		this.joinSecrert = joinSecret;
36
		setChanged();
37
		notifyObservers();
38
	}
39
40
	public void setParty(Party party) {
41
		if(this.party != null) {
42
			this.party.deleteObserver(this);
43
		}
44
		
45
		this.party = party;
46
		
47
		if(party != null) {
48
			party.addObserver(this);
49
		}
50
		
51
		setChanged();
52
		notifyObservers();
53
	}
54
	
55
	public void setGame(Game game) {
56
		if(this.game != null) {
57
			this.game.deleteObserver(this);
58
		}
59
		
60
		this.game = game;
61
		
62
		if(game != null) {
63
			game.addObserver(this);
64
		}	
65
		
66
		setChanged();
67
		notifyObservers();
68
	}
69
	
70
	@Override
71
	public void update(Observable o, Object arg) {
72
		setChanged();
73
		notifyObservers();
74
	}
75
	
76
	public JsonObject toJson() {
77
		JsonObject object = new JsonObject();
78
		
79
		addSecretsToJson(object);
80
		addGameToJson(object);
81
		addPartyToJson(object);
82
		
83
		return object;
84
	}
85
	
86
	private void addSecretsToJson(JsonObject object) {
87
		object.addProperty("hasMatchSecret", matchSecret != null);
88
		if(matchSecret != null) {
89
			object.addProperty("matchSecret", matchSecret);
90
		}
91
		
92
		object.addProperty("hasSpectateSecret", spectateSecret != null);
93
		if(spectateSecret != null) {
94
			object.addProperty("spectateSecret", spectateSecret);
95
		}
96
		
97
		object.addProperty("hasJoinSecret", joinSecrert != null);
98
		if(spectateSecret != null) {
99
			object.addProperty("joinSecret", joinSecrert);
100
		}
101
	}
102
	
103
	private void addGameToJson(JsonObject object) {
104
		object.addProperty("hasGame", game != null);
105
		if(game != null) {
106
			object.addProperty("game_mode", game.getGameMode());
107
			object.addProperty("game_startTime", game.getStartTime() != null ? game.getStartTime().toEpochMilli() : 0);
108
			object.addProperty("game_endTime", game.getEndTime() != null ? game.getEndTime().toEpochMilli() : 0);
109
		}
110
	}
111
	
112
	private void addPartyToJson(JsonObject object) {
113
		object.addProperty("hasParty", party != null);
114
		if(party != null) {
115
			object.addProperty("partyId", party.getPartyId());
116
			object.addProperty("party_size", party.getPartySize());
117
			object.addProperty("party_max", party.getPartyMax());
118
		}
119
	}
120
	
121
	public void destroy() {
122
		deleteObservers();
123
		setGame(null);
124
		setParty(null);
125
	}
126
127
	@Override
128
	public void close() throws IOException {
129
		destroy();
130
	}
131
}
132