File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,26 @@ namespace Data
55{
66 internal class DataLayer : DataAPI
77 {
8+ private static readonly SemaphoreSlim _fileSemaphore = new SemaphoreSlim ( 1 , 1 ) ;
9+
810 public DataLayer ( ) { }
11+
12+ public override async Task LogDiagnosticDataAsync ( string data )
13+ {
14+ string filePath = "diagnostic_log.json" ;
15+ await _fileSemaphore . WaitAsync ( ) ;
16+ try
17+ {
18+ using ( StreamWriter writer = new StreamWriter ( filePath , true ) )
19+ {
20+ await writer . WriteLineAsync ( data ) ;
21+ }
22+ }
23+ finally
24+ {
25+ _fileSemaphore . Release ( ) ;
26+ }
27+ }
928 }
1029
1130 public abstract class DataAPI
@@ -24,5 +43,7 @@ public virtual Table getTableData(int width, int height)
2443 {
2544 return new Table ( width , height ) ;
2645 }
46+
47+ public abstract Task LogDiagnosticDataAsync ( string data ) ;
2748 }
2849}
Original file line number Diff line number Diff line change 22using System ;
33using System . Collections . ObjectModel ;
44using System . Numerics ;
5+ using System . Text . Json ;
56using System . Threading ;
67
78namespace Logic
@@ -30,11 +31,11 @@ public override void RunSimulation()
3031
3132 foreach ( BallLogic ball in balls )
3233 {
33- Task task = Task . Run ( ( ) =>
34+ Task task = Task . Run ( async ( ) =>
3435 {
3536 barrier . SignalAndWait ( ) ;
3637 var timer = new System . Timers . Timer ( ) ;
37- timer . Elapsed += ( _ , elapsedArgs ) =>
38+ timer . Elapsed += async ( _ , elapsedArgs ) =>
3839 {
3940 if ( token . IsCancellationRequested )
4041 {
@@ -43,6 +44,8 @@ public override void RunSimulation()
4344 return ;
4445 }
4546
47+ List < string > logsToSave = new List < string > ( ) ;
48+
4649 lock ( balls )
4750 {
4851 ball . setPosition ( ) ;
@@ -55,9 +58,24 @@ public override void RunSimulation()
5558 else if ( ball . ballCollision ( otherBall ) )
5659 {
5760 ball . handleBallColission ( otherBall ) ;
61+ // Logowanie kolizji
62+ var logData = new
63+ {
64+ Event = "Collision" ,
65+ Ball1 = ball ,
66+ Ball2 = otherBall ,
67+ Timestamp = DateTime . UtcNow
68+ } ;
69+ string jsonData = JsonSerializer . Serialize ( logData ) ;
70+ logsToSave . Add ( jsonData ) ;
5871 }
5972 }
6073 }
74+
75+ foreach ( var log in logsToSave )
76+ {
77+ await _dataAPI . LogDiagnosticDataAsync ( log ) ;
78+ }
6179 } ;
6280 timer . Interval = 8.8888 ; // 1/113Hz
6381 timer . Enabled = true ;
You can’t perform that action at this time.
0 commit comments