@@ -111,61 +111,14 @@ private function getStats(Ladder|string $ladder, int $tierId = 1)
111111 ];
112112 }
113113
114- /**
115- * Fetch details about games thare are currently in match
116- */
117- public function getActiveMatches (Request $ request , $ ladderAbbrev = null )
118- {
119- $ games = [];
120- if ($ ladderAbbrev == "all " )
121- {
122- $ ladders = \App \Models \Ladder::query ()
123- ->where ('private ' , '= ' , false )
124- ->with ([
125- 'current_history ' => function ($ q )
126- {
127- },
128- ])
129- ->with ([
130- 'sides ' ,
131- 'recent_spawned_matches ' ,
132- 'recent_spawned_matches.players ' ,
133- 'recent_spawned_matches.players.clan:id,short ' ,
134- 'recent_spawned_matches.players.player:id,username ' ,
135- 'recent_spawned_matches.map ' ,
136- ])
137- ->get ();
138-
139- foreach ($ ladders as $ ladder )
140- {
141- $ results = $ this ->getActiveMatchesByLadder ($ ladder );
142-
143- foreach ($ results as $ key => $ val )
144- {
145- $ games [$ ladder ->abbreviation ][$ key ] = $ val ;
146- }
147- }
148- }
149- else
150- {
151- $ results = $ this ->getActiveMatchesByLadder ($ ladderAbbrev );
152-
153- foreach ($ results as $ key => $ val )
154- {
155- $ games [$ ladderAbbrev ][$ key ] = $ val ;
156- }
157- }
158-
159- return $ games ;
160- }
161-
162114 /**
163115 * This V2 function will return the data as json array, where each object in the array is an object.
164116 * V1 returns array of strings.
165117 */
166- public function getActiveMatchesV2 (Request $ request , string $ ladderAbbrev )
118+ public function getActiveMatches (Request $ request , string $ ladderAbbrev )
167119 {
168120 $ games = [];
121+
169122 if ($ ladderAbbrev == "all " )
170123 {
171124 $ ladders = Ladder::query ()
@@ -178,14 +131,15 @@ public function getActiveMatchesV2(Request $request, string $ladderAbbrev)
178131 'recent_spawned_matches ' ,
179132 'recent_spawned_matches.players ' ,
180133 'recent_spawned_matches.players.clan:id,short ' ,
181- 'recent_spawned_matches.players.player:id,username ' ,
134+ 'recent_spawned_matches.players.player:id,user_id,username ' ,
135+ 'recent_spawned_matches.players.player.user:id,twitch_profile ' ,
182136 'recent_spawned_matches.map ' ,
183137 ])
184138 ->get ();
185139
186140 $ ladders ->each (function (Ladder $ ladder ) use (&$ games )
187141 {
188- $ games [$ ladder ->abbreviation ] = $ this ->getActiveMatchesByLadderV2 ($ ladder );
142+ $ games [$ ladder ->abbreviation ] = $ this ->getActiveMatchesByLadder ($ ladder );
189143 });
190144 }
191145 else
@@ -200,61 +154,19 @@ public function getActiveMatchesV2(Request $request, string $ladderAbbrev)
200154 'recent_spawned_matches ' ,
201155 'recent_spawned_matches.players ' ,
202156 'recent_spawned_matches.players.clan:id,short ' ,
203- 'recent_spawned_matches.players.player:id,username ' ,
157+ 'recent_spawned_matches.players.player:id,user_id,username ' ,
158+ 'recent_spawned_matches.players.player.user:id,twitch_profile ' ,
204159 'recent_spawned_matches.map ' ,
205160 ])
206161 ->first ();
207162
208- $ games [$ ladder ->abbreviation ] = $ this ->getActiveMatchesByLadderV2 ($ ladder );
163+ $ games [$ ladder ->abbreviation ] = $ this ->getActiveMatchesByLadder ($ ladder );
209164 }
210165
211166 return $ games ;
212167 }
213168
214- private function getActiveMatchesByLadder (Ladder |string $ ladder )
215- {
216- $ ladder = is_string ($ ladder ) ? $ this ->ladderService ->getLadderByGame ($ ladder ) : $ ladder ;
217- $ sides = $ ladder ->sides ->pluck ('name ' , 'local_id ' )->toArray ();
218-
219- if ($ ladder == null )
220- abort (400 , "Invalid ladder provided " );
221-
222- //get all recent QMs that whose games have spawned. (state_type_id == 5)
223- $ qms = $ ladder ->recent_spawned_matches ;
224-
225- $ games = [];
226-
227- foreach ($ qms as $ qm ) //iterate over every active quick match
228- {
229- $ map = trim ($ qm ->map ->description );
230- $ dt = $ qm ->created_at ;
231-
232- //get the player data pertaining to this quick match
233- $ players = $ qm ->players ;
234-
235- $ playersString = "" ;
236- if ($ ladder ->clans_allowed )
237- {
238- $ playersString = $ this ->getActiveClanMatchesData ($ sides , $ players );
239- }
240- else if ($ ladder ->ladder_type == \App \Models \Ladder::TWO_VS_TWO ) // 2v2
241- {
242- $ playersString = $ this ->getTeamActivePlayerMatchesData ($ sides , $ players , $ qm ->created_at );
243- }
244- else
245- {
246- $ playersString = $ this ->getActivePlayerMatchesData ($ sides , $ players , $ qm ->created_at );
247- }
248-
249- $ duration = Carbon::now ()->diff ($ dt );
250- $ duration_formatted = $ duration ->format ('%i mins %s sec ' );
251- $ games [] = $ playersString . " on " . $ map . " ( " . $ duration_formatted . ") " ;
252- }
253-
254- return $ games ;
255- }
256-
257- private function getActiveMatchesByLadderV2 (Ladder $ ladder )
169+ private function getActiveMatchesByLadder (Ladder $ ladder )
258170 {
259171 $ sides = $ ladder ->sides ->pluck ('name ' , 'local_id ' )->toArray ();
260172
@@ -282,11 +194,11 @@ private function getActiveMatchesByLadderV2(Ladder $ladder)
282194 }
283195 else if ($ ladder ->ladder_type == Ladder::TWO_VS_TWO ) // 2v2
284196 {
285- $ playersData = $ this ->getTeamActivePlayerMatchesDataV2 ($ sides , $ qmPlayers , $ qm ->created_at );
197+ $ playersData = $ this ->getTeamActivePlayerMatchesData ($ sides , $ qmPlayers , $ qm ->created_at );
286198 }
287199 else
288200 {
289- $ playersData = $ this ->getActivePlayerMatchesDataV2 ($ sides , $ qmPlayers , $ qm ->created_at );
201+ $ playersData = $ this ->getActivePlayerMatchesData ($ sides , $ qmPlayers , $ qm ->created_at );
290202 }
291203
292204 $ duration = Carbon::now ()->diff ($ dt );
@@ -356,29 +268,10 @@ private function getActiveClanMatchesData($sides, $players)
356268 return $ playersString ;
357269 }
358270
359- private function getActivePlayerMatchesData ($ sides , $ players , $ created_at )
360- {
361- $ playersString = "" ;
362- $ dt = new DateTime ($ created_at );
363- for ($ i = 0 ; $ i < count ($ players ); $ i ++)
364- {
365- $ player = $ players [$ i ];
366- $ playerName = "Player " . ($ i + 1 );
367- if (abs (Carbon::now ()->diffInSeconds ($ dt )) > 120 ) //only show real player name if 2mins has passed
368- $ playerName = $ player ->player ->username ;
369-
370- $ playersString .= $ playerName . " ( " . ($ sides [$ player ->actual_side ] ?? '' ) . ") " ;
371-
372- if ($ i < count ($ players ) - 1 )
373- $ playersString .= " vs " ;
374- }
375- return $ playersString ;
376- }
377-
378271 /**
379272 * @return an array containing every player's name and their faction
380273 */
381- private function getActivePlayerMatchesDataV2 (array $ sides , $ qmPlayers , $ created_at )
274+ private function getActivePlayerMatchesData (array $ sides , $ qmPlayers , $ created_at )
382275 {
383276 $ dt = new DateTime ($ created_at );
384277 $ showRealNames = abs (Carbon::now ()->diffInSeconds ($ dt )) > 120 ;
@@ -390,74 +283,32 @@ private function getActivePlayerMatchesDataV2(array $sides, $qmPlayers, $created
390283 return [
391284 "playerName " => $ showRealNames ? $ qmPlayer ->player ->username : "Player " . ($ index + 1 ),
392285 "playerFaction " => $ sides [$ qmPlayer ->actual_side ] ?? '' ,
393- "playerColor " => $ qmPlayer ->color
286+ "playerColor " => $ qmPlayer ->color ,
287+ "twitchProfile " => $ qmPlayer ->player ?->user?->twitch_profile
394288 ];
395289 })
396290 ->all ();
397291 }
398292
399- /**
400- * returns a 'pretty' message describing the players on each team
401- *
402- * should probably return a json array with the data but we are where we are
403- */
404- private function getTeamActivePlayerMatchesData ($ sides , $ players , $ created_at )
405- {
406- $ playersString = "" ;
407- $ dt = new DateTime ($ created_at );
408- $ teams = [];
409-
410- foreach ($ players as $ player )
411- {
412- $ teams [$ player ->team ][] = $ player ;
413- }
414-
415- $ teamCount = 0 ;
416- $ playerNum = 0 ;
417- foreach ($ teams as $ teamId => $ teammates )
418- {
419- $ teammateNum = 0 ;
420- foreach ($ teammates as $ player )
421- {
422- $ playerName = "Player " . ($ playerNum + 1 );
423- if (abs (Carbon::now ()->diffInSeconds ($ dt )) > 60 ) //only show real player name if 1 min has passed
424- {
425- $ playerName = $ player ->player ->username ;
426- }
427- $ playersString .= $ playerName . " ( " . ($ sides [$ player ->actual_side ] ?? '' ) . ") " ;
428-
429- if ($ teammateNum < count ($ teammates ) - 1 ) // if not last player on the team append ' and '
430- $ playersString .= " and " ;
431-
432- $ teammateNum ++;
433- $ playerNum ++;
434- }
435-
436- if ($ teamCount < count ($ teams ) - 1 ) // if not last team append ' vs '
437- $ playersString .= " vs " ;
438-
439- $ teamCount ++;
440- }
441-
442- return $ playersString ;
443- }
444-
445- private function getTeamActivePlayerMatchesDataV2 (array $ sides , $ qmPlayers , $ created_at )
293+ private function getTeamActivePlayerMatchesData (array $ sides , $ qmPlayers , $ created_at )
446294 {
447295 $ dt = new DateTime ($ created_at );
448- $ showRealNames = abs (Carbon::now ()->diffInSeconds ($ dt )) > 120 ;
296+ $ showRealNames = abs (Carbon::now ()->diffInSeconds ($ dt )) > 90 ;
449297
450298 return collect ($ qmPlayers )
451299 ->groupBy ('team ' )
452300 ->flatten ()
453301 ->values ()
454302 ->map (function ($ qmPlayer , $ index ) use ($ sides , $ showRealNames )
455303 {
304+ $ useRealName = $ showRealNames || $ qmPlayer ->team === "observer " ;
305+ $ faction = $ qmPlayer ->team === "observer " ? "Observer " : $ sides [$ qmPlayer ->actual_side ] ?? '' ;
456306 return [
457307 "teamId " => $ qmPlayer ->team ,
458- "playerName " => $ showRealNames ? $ qmPlayer ->player ->username : "Player " . ($ index + 1 ),
459- "playerFaction " => $ sides [$ qmPlayer ->actual_side ] ?? '' ,
460- "playerColor " => $ qmPlayer ->color
308+ "playerName " => $ useRealName ? $ qmPlayer ->player ->username : "Player " . ($ index + 1 ),
309+ "playerFaction " => $ faction ,
310+ "playerColor " => $ qmPlayer ->color ,
311+ "twitchProfile " => $ qmPlayer ->player ->user ->twitch_profile
461312 ];
462313 })
463314 ->all ();
0 commit comments