adding timer cfgs
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using LocationsApi.Models;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace LocationsApi.Services;
|
||||
|
||||
public class GameSettingsService
|
||||
{
|
||||
public const string ScoutTrainingSeconds = "ScoutTrainingSeconds";
|
||||
public const string LogCampBuildSeconds = "LogCampBuildSeconds";
|
||||
|
||||
private readonly IMongoCollection<GameSetting> _settings;
|
||||
|
||||
public GameSettingsService(IConfiguration cfg)
|
||||
{
|
||||
var connectionString = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
var databaseName = cfg["MongoDB:DatabaseName"] ?? "territory-autobattler";
|
||||
var client = new MongoClient(connectionString);
|
||||
var database = client.GetDatabase(databaseName);
|
||||
_settings = database.GetCollection<GameSetting>("GameSettings");
|
||||
|
||||
_settings.Indexes.CreateOne(new CreateIndexModel<GameSetting>(
|
||||
Builders<GameSetting>.IndexKeys.Ascending(x => x.Key),
|
||||
new CreateIndexOptions { Unique = true }));
|
||||
}
|
||||
|
||||
public async Task<int> GetIntAsync(string key, int defaultValue)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var setting = await _settings.FindOneAndUpdateAsync(
|
||||
x => x.Key == key,
|
||||
Builders<GameSetting>.Update
|
||||
.SetOnInsert(x => x.Key, key)
|
||||
.SetOnInsert(x => x.IntValue, defaultValue)
|
||||
.SetOnInsert(x => x.CreatedAtUtc, now)
|
||||
.Set(x => x.UpdatedAtUtc, now),
|
||||
new FindOneAndUpdateOptions<GameSetting>
|
||||
{
|
||||
IsUpsert = true,
|
||||
ReturnDocument = ReturnDocument.After
|
||||
});
|
||||
|
||||
return Math.Max(1, setting.IntValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user