Skip to content

Commit 553d98e

Browse files
committed
Adds Add/RemoveAllPlayers()
1 parent f60f64f commit 553d98e

File tree

3 files changed

+119
-40
lines changed

3 files changed

+119
-40
lines changed

Assets/_Project/Scenes/tally-viz.unity

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_Project/Scripts/Data/Timeline.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void Start ()
173173
*/
174174
public void SetTimelineStatus (TimelineStatus _status, bool fromUI = false)
175175
{
176-
Debug.Log ("Timeline.SetTimelineStatus() status = " + status + ", _status = " + _status);
176+
//Debug.Log ("Timeline.SetTimelineStatus() status = " + status + ", _status = " + _status);
177177

178178
// update status var
179179
status = _status;
@@ -186,18 +186,23 @@ public void SetTimelineStatus (TimelineStatus _status, bool fromUI = false)
186186
/**
187187
* Called from the UI button to start / stop
188188
*/
189-
public void StartBtn ()
189+
public void OnStartBtnClick ()
190190
{
191191
// if currently active
192192
if (status == TimelineStatus.active) {
193-
SetStartBtn (" --- ", false);
193+
SetStartBtnText (" --- ", false);
194194
// stop everything
195195
StopBufferLoop ();
196196
StopHistoryLoop ();
197197
// set status
198198
SetTimelineStatus (TimelineStatus.inactive);
199199
// update btn text and make interactable
200-
SetStartBtn ("Start", true);
200+
SetStartBtnText ("Start", true);
201+
// if there are players active
202+
if (PlayerManager.Instance.playerCount > 0) {
203+
// then add them
204+
EventManager.TriggerEvent ("RemoveAllPlayers");
205+
}
201206
}
202207
// if not active
203208
else {
@@ -210,7 +215,7 @@ public void StartBtn ()
210215
UpdateTimelineLogs ();
211216
}
212217

213-
void SetStartBtn (string txt, bool interact)
218+
void SetStartBtnText (string txt, bool interact)
214219
{
215220
startButtonText.text = txt;
216221
startButton.interactable = interact;
@@ -320,7 +325,7 @@ IEnumerator BufferLoop ()
320325
waitingForDataProgress = 0;
321326

322327
// disable button until data arrives
323-
SetStartBtn (" ... ", false);
328+
SetStartBtnText (" ... ", false);
324329

325330
// set to waiting
326331
SetTimelineStatus (TimelineStatus.waitingForData);
@@ -366,20 +371,22 @@ IEnumerator BufferLoop ()
366371
// after new data, sort ascending
367372
buffer.Sort ((x, y) => x.createdAt.CompareTo (y.createdAt));
368373

369-
// remove duplicate users here?
370-
371374

372-
// if we have data in the buffer or history but no players then add them
375+
// if we have data in the buffer or history but no players
373376
if (PlayerManager.Instance.playerCount < 1) {
374-
EventManager.TriggerEvent ("ResetPlayers");
377+
// then add them
378+
EventManager.TriggerEvent ("AddAllPlayers");
379+
} else {
380+
// otherwise update them
381+
EventManager.TriggerEvent ("CheckUpdatePlayers");
375382
}
376383

377384

378385
// start history loop again
379386
StartHistoryLoop ();
380387

381388
// update btn text and make interactable
382-
SetStartBtn ("Stop", true);
389+
SetStartBtnText ("Stop", true);
383390

384391
// set status
385392
SetTimelineStatus (TimelineStatus.active);

Assets/_Project/Scripts/Players/PlayerManager.cs

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ protected PlayerManager () { }
1111
//listeners
1212
void OnEnable ()
1313
{
14-
EventManager.StartListening ("ResetPlayers", ResetPlayers);
15-
EventManager.StartListening ("ClearNonActivePlayers", ClearNonActivePlayers);
14+
EventManager.StartListening ("AddAllPlayers", AddAllPlayers);
15+
EventManager.StartListening ("RemoveAllPlayers", RemoveAllPlayers);
16+
EventManager.StartListening ("CheckUpdatePlayers", CheckUpdatePlayers);
1617
}
1718
void OnDisable ()
1819
{
19-
EventManager.StopListening ("ResetPlayers", ResetPlayers);
20-
EventManager.StopListening ("ClearNonActivePlayers", ClearNonActivePlayers);
20+
EventManager.StopListening ("AddAllPlayers", AddAllPlayers);
21+
EventManager.StopListening ("RemoveAllPlayers", RemoveAllPlayers);
22+
EventManager.StopListening ("CheckUpdatePlayers", CheckUpdatePlayers);
2123
}
2224

2325

@@ -38,6 +40,7 @@ void OnDisable ()
3840
public BoxCollider worldContainerCollider;
3941
public GameObject playerPrefab;
4042
public Dictionary<string, GameObject> playerDict;
43+
public Dictionary<string, GameObject> playersToRemoveDict;
4144
public CameraManager cameraManager;
4245

4346
// temp sprites for assigning avatars
@@ -51,6 +54,7 @@ void OnDisable ()
5154
// player currently showing an event
5255
public GameObject currentPlayerObj;
5356
public Player currentPlayerScript;
57+
public string currentEventType;
5458

5559

5660

@@ -82,37 +86,94 @@ private void Awake ()
8286
}
8387

8488

89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
85100
/**
86-
* Remove all players from stage, reset dict
101+
* Add all players to screen and dict - called at start or after data reset
102+
* - called when Timeline.status == TimelineStatus.newDataReceived
87103
*/
88-
public void ResetPlayers ()
104+
void AddAllPlayers ()
89105
{
90-
// remove any players first?
106+
Debug.Log ("PlayerManager.AddAllPlayers()");
91107

92-
// clear the dictionary
93-
playerDict.Clear ();
94-
95-
// loop through the buffer and add players
108+
// loop through the buffer and add players to scene and dict
96109
foreach (var feed in Timeline.Instance.buffer) {
110+
// max hasn't been reached
111+
if (playerDict.Count > maxPlayersAllowed) break;
112+
// check if player exists and add if not
97113
CreateNewPlayer (feed.username, feed.avatarPath);
98114
}
99115
foreach (var feed in Timeline.Instance.history) {
116+
// max hasn't been reached
117+
if (playerDict.Count > maxPlayersAllowed) break;
118+
// check if player exists and add if not
100119
CreateNewPlayer (feed.username, feed.avatarPath);
101120
}
102121

103-
// update player count
104-
playerCount = playerDict.Count;
122+
UpdateCounts ();
105123

106-
// trigger data updated event
107-
EventManager.TriggerEvent ("PlayersUpdated");
108124
}
109125

126+
110127
/**
111-
* Remove players who haven't been active in a while
112-
*/
113-
public void ClearNonActivePlayers ()
128+
* Remove all players from screen and dict
129+
* - called from Timeline.OnStartBtnClick() to stop timeline / reset data
130+
*/
131+
void RemoveAllPlayers ()
114132
{
133+
Debug.Log ("PlayerManager.RemoveAllPlayers()");
134+
135+
// for each in playerDict, remove from scene
136+
foreach (KeyValuePair<string, GameObject> kvp in playerDict) {
137+
Destroy (kvp.Value);
138+
}
115139

140+
// clear the dictionary
141+
playerDict.Clear ();
142+
// update the count
143+
UpdateCounts ();
144+
}
145+
146+
147+
/**
148+
* - called when Timeline.status == TimelineStatus.newDataReceived
149+
*/
150+
void CheckUpdatePlayers ()
151+
{
152+
Debug.Log ("PlayerManager.CheckUpdatePlayers()");
153+
154+
// - make a copy of the current player dict -> playersToRemoveDict
155+
// - for each event/player in current buffer and history
156+
// - if player found in playerDict
157+
// - remove from playersToRemoveDict
158+
// - else
159+
// - add to playerDict / scene
160+
// - add ALL players until max per resolution
161+
// - if players still left in playersToRemoveDict
162+
// - remove those players from scene and clear dict
163+
// - update counts
164+
165+
166+
167+
UpdateCounts ();
168+
}
169+
170+
171+
172+
173+
void UpdateCounts ()
174+
{
175+
// update player count
176+
playerCount = playerDict.Count;
116177

117178
// trigger data updated event
118179
EventManager.TriggerEvent ("PlayersUpdated");
@@ -123,13 +184,16 @@ public void ClearNonActivePlayers ()
123184

124185

125186

187+
188+
189+
126190
/**
127191
* Create a new player
128192
*/
129-
public void CreateNewPlayer (string username, string avatarPath)
193+
public bool CreateNewPlayer (string username, string avatarPath)
130194
{
131195
// make sure the player doesn't already exist
132-
if (playerDict.ContainsKey (username)) return;
196+
if (playerDict.ContainsKey (username)) return false;
133197

134198
// get a position that doesn't contain any other colliders
135199
Vector3 spawnPosition = GetClearSpawnPosition ();
@@ -159,6 +223,7 @@ public void CreateNewPlayer (string username, string avatarPath)
159223
// Allow the player to be selected by the camera
160224
cameraManager.AddPlayer (username);
161225
}
226+
return true;
162227
}
163228

164229

@@ -181,6 +246,12 @@ public void PlayEvent (FeedData feed)
181246
// reference to script (contains all the other references we need)
182247
currentPlayerScript = currentPlayerObj.GetComponent<Player> ();
183248

249+
// show event in public var
250+
currentEventType = feed.eventType;
251+
252+
253+
254+
184255

185256
// EFFECTS
186257

0 commit comments

Comments
 (0)