Skip to content

Commit 547ebf3

Browse files
CopilotLeftofZen
andauthored
Report missing objects to server when not found in online index (#222)
* Initial plan * Add functionality to report missing objects to server Co-authored-by: LeftofZen <7483209+LeftofZen@users.noreply.github.com> * polish --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: LeftofZen <7483209+LeftofZen@users.noreply.github.com> Co-authored-by: Benjamin Sutas <benjamin.sutas@gmail.com>
1 parent c9c4437 commit 547ebf3

5 files changed

Lines changed: 71 additions & 8 deletions

File tree

Definitions/Web/Client.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,15 @@ public static async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync(HttpCli
4343
RoutesV2.Objects,
4444
request);
4545
}
46+
47+
public static async Task<UniqueObjectId> AddMissingObjectAsync(HttpClient client, DtoMissingObjectEntry entry, ILogger? logger = null)
48+
{
49+
logger?.Debug($"Posting missing object {entry.DatName} with checksum {entry.DatChecksum} to {client.BaseAddress?.OriginalString}{RoutesV2.Objects}{RoutesV2.Missing}");
50+
return await ClientHelpers.PostAsync<DtoMissingObjectEntry, UniqueObjectId>(
51+
client,
52+
ApiVersion,
53+
RoutesV2.Objects + RoutesV2.Missing,
54+
entry,
55+
logger);
56+
}
4657
}

Gui/ObjectServiceClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ public async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync()
6969

7070
public async Task<DtoObjectDescriptor?> UploadDatFileAsync(string filename, byte[] datFileBytes, DateOnly creationDate, DateOnly modifiedDate)
7171
=> await Client.UploadDatFileAsync(WebClient, filename, datFileBytes, creationDate, modifiedDate, Logger);
72+
73+
public async Task<UniqueObjectId> AddMissingObjectAsync(DtoMissingObjectEntry entry)
74+
=> await Client.AddMissingObjectAsync(WebClient, entry, Logger);
7275
}

Gui/ViewModels/LocoTypes/SCV5ViewModel.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,24 @@ async Task DownloadMissingObjects(GameObjDataFolder targetFolder)
160160

161161
if (onlineObj == null)
162162
{
163-
logger.Error("Couldn't find a matching object in the online index");
163+
logger.Error($"Couldn't find a matching object in the online index for {obj.ObjectType} \"{obj.Name}\" with checksum {obj.Checksum}");
164+
165+
// Add this missing object to the server's missing objects list
166+
var missingEntry = new Definitions.DTO.DtoMissingObjectEntry(
167+
obj.Name,
168+
obj.Checksum,
169+
obj.ObjectType.Convert());
170+
171+
var result = await Model.ObjectServiceClient.AddMissingObjectAsync(missingEntry);
172+
if (result != 0)
173+
{
174+
logger.Info($"Successfully added missing object to server: {obj.Name} ({obj.Checksum})");
175+
}
176+
else
177+
{
178+
logger.Error($"Failed to add missing object to server: {obj.Name} ({obj.Checksum})");
179+
}
180+
164181
continue;
165182
}
166183

Tests/ObjectServiceIntegrationTests/BaseReferenceDataTableTestFixture.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ public abstract class BaseRouteHandlerTestFixture
2222
[Test] public abstract Task PutAsync();
2323
[Test] public abstract Task DeleteAsync();
2424

25+
protected LocoDbContext GetDbContext()
26+
=> testWebAppFactory
27+
.Services
28+
.CreateScope()
29+
.ServiceProvider
30+
.GetRequiredService<LocoDbContext>();
31+
2532
async Task SeedDataAsync()
2633
{
27-
using (var scope = testWebAppFactory.Services.CreateScope())
28-
{
29-
var dbContext = scope.ServiceProvider.GetRequiredService<LocoDbContext>();
34+
using var dbContext = GetDbContext();
35+
await SeedDataCoreAsync(dbContext);
36+
_ = await dbContext.SaveChangesAsync();
3037

31-
// Seed data
32-
await SeedDataCoreAsync(dbContext);
33-
_ = await dbContext.SaveChangesAsync();
34-
}
3538
}
3639

3740
protected abstract Task SeedDataCoreAsync(LocoDbContext db);

Tests/ObjectServiceIntegrationTests/ObjectRoutesTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,33 @@ [new DtoDatObjectEntry(1, "AZVOG15C", 3072098364, 7051740550869341430, 3)], // d
224224

225225
AssertDtoObjectDescriptorsAreEqual(results, expected);
226226
}
227+
228+
[Test]
229+
public async Task AddMissingObjectAsync()
230+
{
231+
// arrange
232+
var missingEntry = new DtoMissingObjectEntry("TESTOBJ1", 123456789, ObjectType.Vehicle);
233+
234+
// act
235+
var result = await Client.AddMissingObjectAsync(HttpClient!, missingEntry, new Logger());
236+
237+
// assert
238+
Assert.That(result, Is.Not.Zero, "Adding missing object should return the new unique id for that object");
239+
240+
// verify the object was added to the database
241+
using var dbContext = GetDbContext();
242+
var addedObject = await dbContext.Objects
243+
.Include(x => x.DatObjects)
244+
.FirstOrDefaultAsync(x => x.Name == $"{missingEntry.DatName}_{missingEntry.DatChecksum}");
245+
246+
using (Assert.EnterMultipleScope())
247+
{
248+
Assert.That(addedObject, Is.Not.Null, "Object should exist in database");
249+
Assert.That(addedObject!.Availability, Is.EqualTo(ObjectAvailability.Missing));
250+
Assert.That(addedObject.ObjectType, Is.EqualTo(ObjectType.Vehicle));
251+
Assert.That(addedObject.DatObjects.Count, Is.EqualTo(1));
252+
Assert.That(addedObject.DatObjects.First().DatName, Is.EqualTo(missingEntry.DatName));
253+
Assert.That(addedObject.DatObjects.First().DatChecksum, Is.EqualTo(missingEntry.DatChecksum));
254+
}
255+
}
227256
}

0 commit comments

Comments
 (0)