1414using System . Threading ;
1515using System . Threading . Tasks ;
1616using System . Windows ;
17+ using PCL . Core . UI ;
1718
1819namespace PCL . Core . Link . Lobby ;
1920
@@ -32,7 +33,7 @@ public class LobbyService() : GeneralService("lobby", "LobbyService")
3233 private static readonly Timer _ServerGameWatcher =
3334 new ( _CheckGameState , null , TimeSpan . FromSeconds ( 5 ) , TimeSpan . FromSeconds ( 15 ) ) ;
3435
35- private static bool _isGameWatcherRunnable = false ;
36+ private static bool _isGameWatcherRunnable ;
3637
3738 /// <summary>
3839 /// Current lobby state.
@@ -72,11 +73,6 @@ public class LobbyService() : GeneralService("lobby", "LobbyService")
7273 /// </summary>
7374 public static event Action < LobbyState , LobbyState > ? StateChanged ;
7475
75- /// <summary>
76- /// Used for UI layer to send Hint.
77- /// </summary>
78- public static event Action < string , CoreHintType > ? OnHint ;
79-
8076 /// <summary>
8177 /// Invoked when need to download EasyTier core files.
8278 /// </summary>
@@ -159,7 +155,7 @@ public static async Task InitializeAsync()
159155 Convert . ToDateTime ( expTime ) . CompareTo ( DateTime . Now ) < 0 )
160156 {
161157 Config . Link . NaidRefreshToken = string . Empty ;
162- OnHint ? . Invoke ( "Natayark ID 令牌已过期,请重新登录" , CoreHintType . Critical ) ;
158+ HintWrapper . Show ( "Natayark ID 令牌已过期,请重新登录" , HintType . Critical ) ;
163159 }
164160 else
165161 {
@@ -175,7 +171,7 @@ public static async Task InitializeAsync()
175171 catch ( Exception ex )
176172 {
177173 LogWrapper . Error ( ex , "LobbyService" , "Lobby service initialization failed." ) ;
178- OnHint ? . Invoke ( "大厅服务初始化失败,请检查网络连接。" , CoreHintType . Critical ) ;
174+ HintWrapper . Show ( "大厅服务初始化失败,请检查网络连接。" , HintType . Critical ) ;
179175 _SetState ( LobbyState . Error ) ;
180176 }
181177 }
@@ -251,7 +247,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
251247 {
252248 if ( _NotHaveNaid ( ) )
253249 {
254- OnHint ? . Invoke ( "请先登录 Natayark ID 再使用大厅!" , CoreHintType . Critical ) ;
250+ HintWrapper . Show ( "请先登录 Natayark ID 再使用大厅!" , HintType . Critical ) ;
255251 return false ;
256252 }
257253
@@ -265,7 +261,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
265261 var serverEntity = await _LobbyController . LaunchServerAsync ( username , port ) . ConfigureAwait ( false ) ;
266262 if ( serverEntity is null )
267263 {
268- OnHint ? . Invoke ( "在创建房间的时候遇到了问题,请查看日志并将此问题反馈给开发者!" , CoreHintType . Critical ) ;
264+ HintWrapper . Show ( "在创建房间的时候遇到了问题,请查看日志并将此问题反馈给开发者!" , HintType . Critical ) ;
269265 return false ;
270266 }
271267
@@ -289,7 +285,7 @@ public static async Task<bool> CreateLobbyAsync(int port, string username)
289285 catch ( Exception ex )
290286 {
291287 LogWrapper . Error ( ex , "LobbyService" , "Failed to create lobby." ) ;
292- OnHint ? . Invoke ( "创建大厅失败,请检查日志或向开发者反馈。" , CoreHintType . Critical ) ;
288+ HintWrapper . Show ( "创建大厅失败,请检查日志或向开发者反馈。" , HintType . Critical ) ;
293289 await LeaveLobbyAsync ( ) . ConfigureAwait ( false ) ;
294290
295291 return false ;
@@ -333,7 +329,7 @@ private static void _ServerOnPlayerPing(IReadOnlyList<PlayerProfile> players)
333329 var sortedNewPlayers = PlayerListHandler . Sort ( players ) ;
334330
335331 var idsToRemove = currentMachineIds . Except ( newMachineIds ) . ToList ( ) ;
336- if ( idsToRemove . Any ( ) )
332+ if ( idsToRemove . Count != 0 )
337333 {
338334 var playersToRemove = Players . Where ( p => idsToRemove . Contains ( p . MachineId ) ) . ToList ( ) ;
339335 foreach ( var player in playersToRemove )
@@ -343,13 +339,12 @@ private static void _ServerOnPlayerPing(IReadOnlyList<PlayerProfile> players)
343339 }
344340
345341 var idsToAdd = newMachineIds . Except ( currentMachineIds ) . ToList ( ) ;
346- if ( idsToAdd . Any ( ) )
347- {
348- var playersToAdd = sortedNewPlayers . Where ( p => idsToAdd . Contains ( p . MachineId ) ) . ToList ( ) ;
349- foreach ( var player in playersToAdd )
350- {
351- Players . Add ( player ) ;
352- }
342+ if ( idsToAdd . Count == 0 ) return ;
343+
344+ var playersToAdd = sortedNewPlayers . Where ( p => idsToAdd . Contains ( p . MachineId ) ) . ToList ( ) ;
345+ foreach ( var player in playersToAdd )
346+ {
347+ Players . Add ( player ) ;
353348 }
354349 } ) ;
355350 }
@@ -376,27 +371,18 @@ public static async Task<bool> JoinLobbyAsync(string lobbyCode, string username)
376371
377372 if ( clientEntity is null )
378373 {
379- throw new InvalidOperationException (
380- "加入大厅失败,可能是大厅不存在或已被解散" ) ;
374+ return false ;
381375 }
382376
383377 clientEntity . Client . Heartbeat += _ClientOnHeartbeat ;
384378 clientEntity . Client . ServerShuttedDown += _ClientOnServerShutDown ;
385379
386380 _SetState ( LobbyState . Connected ) ;
387381 }
388- catch ( ArgumentException codeEx )
389- {
390- LogWrapper . Error ( codeEx , "LobbyService" , $ "Failed to join lobby { lobbyCode } .") ;
391- OnHint ? . Invoke ( "大厅编号不正确,请检查后再试!" , CoreHintType . Critical ) ;
392- await LeaveLobbyAsync ( ) . ConfigureAwait ( false ) ;
393-
394- return false ;
395- }
396382 catch ( Exception ex )
397383 {
398384 LogWrapper . Error ( ex , "LobbyService" , $ "Failed to join lobby { lobbyCode } .") ;
399- OnHint ? . Invoke ( ex . Message , CoreHintType . Critical ) ;
385+ HintWrapper . Show ( ex . Message , HintType . Critical ) ;
400386 await LeaveLobbyAsync ( ) . ConfigureAwait ( false ) ;
401387
402388 return false ;
@@ -548,28 +534,4 @@ private static async Task _RunInUiAsync(Action action)
548534/// </summary>
549535/// <param name="Name">World name.</param>
550536/// <param name="Port">World share port.</param>
551- public record FoundWorld ( string Name , int Port ) ;
552-
553- /// <summary>
554- /// Hint type in PCL.Core (for UI display).
555- /// </summary>
556- public enum CoreHintType
557- {
558- /// <summary>
559- /// 信息,通常是蓝色的“i”。
560- /// </summary>
561- /// <remarks></remarks>
562- Info ,
563-
564- /// <summary>
565- /// 已完成,通常是绿色的“√”。
566- /// </summary>
567- /// <remarks></remarks>
568- Finish ,
569-
570- /// <summary>
571- /// 错误,通常是红色的“×”。
572- /// </summary>
573- /// <remarks></remarks>
574- Critical
575- }
537+ public record FoundWorld ( string Name , int Port ) ;
0 commit comments