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

addSecretsToJson(JsonObject)   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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