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

addPartyToJson(JsonObject)   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
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 setSpectateSecret(String spectateSecret)
29
	{
30
		this.spectateSecret = spectateSecret;
31
		setChanged();
32
		notifyObservers();
33
	}
34
35
	public void setJoinSecrert(String joinSecret)
36
	{
37
		this.joinSecrert = joinSecret;
38
		setChanged();
39
		notifyObservers();
40
	}
41
42
	public void setParty(Party party)
43
	{
44
		this.party = party;
45
		
46
		if(party != null)
47
			party.addObserver(this);
48
		
49
		setChanged();
50
		notifyObservers();
51
	}
52
	
53
	public void setGame(Game game)
54
	{
55
		this.game = game;
56
		
57
		if(game != null)
58
			game.addObserver(this);
59
		
60
		setChanged();
61
		notifyObservers();
62
	}
63
	
64
	@Override
65
	public void update(Observable o, Object arg)
66
	{
67
		setChanged();
68
		notifyObservers();
69
	}
70
	
71
	public JsonObject toJson()
72
	{
73
		JsonObject object = new JsonObject();
74
		
75
		addSecretsToJson(object);
76
		addGameToJson(object);
77
		addPartyToJson(object);
78
		
79
		return object;
80
	}
81
	
82
	private void addSecretsToJson(JsonObject object)
83
	{
84
		object.addProperty("hasMatchSecret", matchSecret != null);
85
		if(matchSecret != null)
86
		{
87
			object.addProperty("matchSecret", matchSecret);
88
		}
89
		
90
		object.addProperty("hasSpectateSecret", spectateSecret != null);
91
		if(spectateSecret != null)
92
		{
93
			object.addProperty("spectateSecret", spectateSecret);
94
		}
95
		
96
		object.addProperty("hasJoinSecret", joinSecrert != null);
97
		if(spectateSecret != null)
98
		{
99
			object.addProperty("joinSecret", joinSecrert);
100
		}
101
	}
102
	
103
	private void addGameToJson(JsonObject object)
104
	{
105
		object.addProperty("hasGame", game != null);
106
		if(game != null)
107
		{
108
			object.addProperty("game_mode", game.getGameMode());
109
			object.addProperty("game_startTime", game.getStartTime() != null ? game.getStartTime().toEpochMilli() : 0);
110
			object.addProperty("game_endTime", game.getEndTime() != null ? game.getEndTime().toEpochMilli() : 0);
111
		}
112
	}
113
	
114
	private void addPartyToJson(JsonObject object)
115
	{
116
		object.addProperty("hasParty", party != null);
117
		if(party != null)
118
		{
119
			object.addProperty("partyId", party.getPartyId());
120
			object.addProperty("party_size", party.getPartySize());
121
			object.addProperty("party_max", party.getPartyMax());
122
		}
123
	}
124
}
125