16
16
using Ghosts . Api . Infrastructure ;
17
17
using ghosts . api . Infrastructure . Animations . AnimationDefinitions . Chat . Mattermost ;
18
18
using ghosts . api . Infrastructure . ContentServices ;
19
- using Ghosts . Api . Infrastructure . Data ;
20
19
using ghosts . api . Infrastructure . Extensions ;
21
20
using Ghosts . Api . Infrastructure . Extensions ;
22
21
using ghosts . api . Infrastructure . Models ;
@@ -35,24 +34,22 @@ public class ChatClient
35
34
private readonly HttpClient _client ;
36
35
private string _token ;
37
36
private string UserId { get ; set ; }
38
- private IFormatterService _formatterService ;
37
+ private readonly IFormatterService _formatterService ;
39
38
40
- private readonly ApplicationDbContext _context ;
41
- private IHubContext < ActivityHub > _activityHubContext ;
42
- private CancellationToken _cancellationToken ;
39
+ private readonly IHubContext < ActivityHub > _activityHubContext ;
40
+ private readonly CancellationToken _cancellationToken ;
43
41
44
42
public ChatClient ( ApplicationSettings . AnimatorSettingsDetail . AnimationsSettings . ChatSettings chatSettings ,
45
43
ChatJobConfiguration config , IFormatterService formatterService , IHubContext < ActivityHub > activityHubContext ,
46
- ApplicationDbContext context , CancellationToken ct )
44
+ CancellationToken ct )
47
45
{
48
46
_configuration = config ;
49
47
_chatSettings = chatSettings ;
50
48
this . _baseUrl = _configuration . Chat . BaseUrl ;
51
49
this . _client = new HttpClient ( ) ;
52
50
this . _formatterService = formatterService ;
53
- this . _context = context ;
54
51
this . _activityHubContext = activityHubContext ;
55
- this . _cancellationToken = _cancellationToken ;
52
+ this . _cancellationToken = ct ;
56
53
}
57
54
58
55
private async Task < User > AdminLogin ( )
@@ -80,7 +77,7 @@ private async Task<User> Login(string username, string password)
80
77
var content = new StringContent ( $ "{{\" login_id\" :\" { username } \" ,\" password\" :\" { password } \" }}", null ,
81
78
"application/json" ) ;
82
79
request . Content = content ;
83
- var response = await this . _client . SendAsync ( request ) ;
80
+ var response = await this . _client . SendAsync ( request , this . _cancellationToken ) ;
84
81
response . EnsureSuccessStatusCode ( ) ;
85
82
86
83
// Reading the 'Token' value from response headers
@@ -94,7 +91,7 @@ private async Task<User> Login(string username, string password)
94
91
throw new Exception ( "Token not found in the response headers." ) ;
95
92
}
96
93
97
- var contentString = await response . Content . ReadAsStringAsync ( ) ;
94
+ var contentString = await response . Content . ReadAsStringAsync ( this . _cancellationToken ) ;
98
95
var user = JsonSerializer . Deserialize < User > ( contentString ,
99
96
new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
100
97
if ( user == null )
@@ -447,15 +444,15 @@ private async Task<string> ExecuteRequest(HttpRequestMessage request)
447
444
{
448
445
try
449
446
{
450
- var response = await this . _client . SendAsync ( request ) ;
447
+ var response = await this . _client . SendAsync ( request , this . _cancellationToken ) ;
451
448
if ( response . StatusCode is HttpStatusCode . Forbidden or HttpStatusCode . BadRequest )
452
449
{
453
- _log . Info ( await response . Content . ReadAsStringAsync ( ) ) ;
450
+ _log . Info ( await response . Content . ReadAsStringAsync ( this . _cancellationToken ) ) ;
454
451
return string . Empty ;
455
452
}
456
453
457
454
response . EnsureSuccessStatusCode ( ) ;
458
- var contentString = await response . Content . ReadAsStringAsync ( ) ;
455
+ var contentString = await response . Content . ReadAsStringAsync ( this . _cancellationToken ) ;
459
456
460
457
return contentString ;
461
458
}
@@ -472,6 +469,9 @@ public async Task Step(Random random, IEnumerable<NpcRecord> agents)
472
469
{
473
470
while ( ! this . _cancellationToken . IsCancellationRequested )
474
471
{
472
+ if ( agents == null || ! agents . Any ( ) )
473
+ return ;
474
+
475
475
var u = await this . AdminLogin ( ) ;
476
476
if ( u == null )
477
477
return ;
@@ -669,15 +669,15 @@ await this._activityHubContext.Clients.All.SendAsync("show",
669
669
npcId ,
670
670
"chat" ,
671
671
message ,
672
- DateTime . Now . ToString ( CultureInfo . InvariantCulture )
672
+ DateTime . Now . ToString ( CultureInfo . InvariantCulture ) , this . _cancellationToken
673
673
) ;
674
674
}
675
675
else
676
676
{
677
677
_log . Info ( $ "{ prompt } |NOT SENT|{ message } ") ;
678
678
}
679
679
680
- await Task . Delay ( random . Next ( 5000 , 250000 ) ) ;
680
+ await Task . Delay ( random . Next ( 5000 , 250000 ) , this . _cancellationToken ) ;
681
681
}
682
682
}
683
683
catch ( Exception e )
0 commit comments