159 lines
5.8 KiB
C#
159 lines
5.8 KiB
C#
using LocationsApi.Models;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
namespace LocationsApi.Services;
|
|
|
|
public class LocationStore
|
|
{
|
|
private readonly IMongoCollection<Location> _col;
|
|
|
|
public LocationStore(IConfiguration cfg)
|
|
{
|
|
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
|
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
|
|
var client = new MongoClient(cs);
|
|
var db = client.GetDatabase(dbName);
|
|
var collectionName = "Locations";
|
|
EnsureLocationSchema(db, collectionName);
|
|
_col = db.GetCollection<Location>(collectionName);
|
|
|
|
var coordIndex = Builders<Location>.IndexKeys
|
|
.Ascending(l => l.Coord.X)
|
|
.Ascending(l => l.Coord.Y);
|
|
var coordIndexOptions = new CreateIndexOptions { Unique = true };
|
|
_col.Indexes.CreateOne(new CreateIndexModel<Location>(coordIndex, coordIndexOptions));
|
|
|
|
EnsureOriginLocation();
|
|
}
|
|
|
|
private static void EnsureLocationSchema(IMongoDatabase db, string collectionName)
|
|
{
|
|
var validator = new BsonDocument
|
|
{
|
|
{
|
|
"$jsonSchema", new BsonDocument
|
|
{
|
|
{ "bsonType", "object" },
|
|
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
|
|
{
|
|
"properties", new BsonDocument
|
|
{
|
|
{ "name", new BsonDocument { { "bsonType", "string" } } },
|
|
{
|
|
"coord", new BsonDocument
|
|
{
|
|
{ "bsonType", "object" },
|
|
{ "required", new BsonArray { "x", "y" } },
|
|
{
|
|
"properties", new BsonDocument
|
|
{
|
|
{ "x", new BsonDocument { { "bsonType", "int" } } },
|
|
{ "y", new BsonDocument { { "bsonType", "int" } } }
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"characterIds", new BsonDocument
|
|
{
|
|
{ "bsonType", "array" },
|
|
{ "items", new BsonDocument { { "bsonType", "string" } } }
|
|
}
|
|
},
|
|
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
var collections = db.ListCollectionNames().ToList();
|
|
if (!collections.Contains(collectionName))
|
|
{
|
|
var createCommand = new BsonDocument
|
|
{
|
|
{ "create", collectionName },
|
|
{ "validator", validator },
|
|
{ "validationAction", "error" }
|
|
};
|
|
db.RunCommand<BsonDocument>(createCommand);
|
|
return;
|
|
}
|
|
|
|
var command = new BsonDocument
|
|
{
|
|
{ "collMod", collectionName },
|
|
{ "validator", validator },
|
|
{ "validationAction", "error" }
|
|
};
|
|
db.RunCommand<BsonDocument>(command);
|
|
}
|
|
|
|
public Task CreateAsync(Location location) => _col.InsertOneAsync(location);
|
|
|
|
public Task<List<Location>> GetAllAsync() =>
|
|
_col.Find(Builders<Location>.Filter.Empty).ToListAsync();
|
|
|
|
public async Task<bool> DeleteAsync(string id)
|
|
{
|
|
var filter = Builders<Location>.Filter.Eq(l => l.Id, id);
|
|
var result = await _col.DeleteOneAsync(filter);
|
|
return result.DeletedCount > 0;
|
|
}
|
|
|
|
public async Task<bool> UpdateNameAsync(string id, string name)
|
|
{
|
|
var filter = Builders<Location>.Filter.Eq(l => l.Id, id);
|
|
var update = Builders<Location>.Update.Set(l => l.Name, name);
|
|
var result = await _col.UpdateOneAsync(filter, update);
|
|
return result.ModifiedCount > 0;
|
|
}
|
|
|
|
public async Task<bool> UpdatePresenceAsync(string characterId, Coord coord)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(characterId))
|
|
return false;
|
|
|
|
var pullFilter = Builders<Location>.Filter.AnyEq(l => l.CharacterIds, characterId);
|
|
var pullUpdate = Builders<Location>.Update.Pull(l => l.CharacterIds, characterId);
|
|
await _col.UpdateManyAsync(pullFilter, pullUpdate);
|
|
|
|
var targetFilter = Builders<Location>.Filter.And(
|
|
Builders<Location>.Filter.Eq(l => l.Coord.X, coord.X),
|
|
Builders<Location>.Filter.Eq(l => l.Coord.Y, coord.Y)
|
|
);
|
|
var addUpdate = Builders<Location>.Update.AddToSet(l => l.CharacterIds, characterId);
|
|
var result = await _col.UpdateOneAsync(targetFilter, addUpdate);
|
|
return result.MatchedCount > 0;
|
|
}
|
|
|
|
private void EnsureOriginLocation()
|
|
{
|
|
var filter = Builders<Location>.Filter.And(
|
|
Builders<Location>.Filter.Eq(l => l.Coord.X, 0),
|
|
Builders<Location>.Filter.Eq(l => l.Coord.Y, 0)
|
|
);
|
|
var existing = _col.Find(filter).FirstOrDefault();
|
|
if (existing is not null)
|
|
return;
|
|
|
|
var origin = new Location
|
|
{
|
|
Name = "Origin",
|
|
Coord = new Coord { X = 0, Y = 0 },
|
|
CharacterIds = new List<string>(),
|
|
CreatedUtc = DateTime.UtcNow
|
|
};
|
|
|
|
try
|
|
{
|
|
_col.InsertOne(origin);
|
|
}
|
|
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
|
|
{
|
|
// Another instance seeded it first.
|
|
}
|
|
}
|
|
}
|