//
// CMapResponder.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Extensions.Logging;
using NosSmooth.Core.Extensions;
using NosSmooth.Core.Packets;
using NosSmooth.Data.Abstractions;
using NosSmooth.Game.Data.Maps;
using NosSmooth.Packets.Server.Maps;
using Remora.Results;
namespace NosSmooth.Game.PacketHandlers.Map;
///
/// Responds to by creating a new map.
///
public class CMapResponder : IPacketResponder
{
private readonly Game _game;
private readonly IInfoService _infoService;
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The nostale game.
/// The info service.
/// The logger.
public CMapResponder(Game game, IInfoService infoService, ILogger logger)
{
_game = game;
_infoService = infoService;
_logger = logger;
}
///
public async Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default)
{
var packet = packetArgs.Packet;
var mapInfoResult = await _infoService.GetMapInfoAsync(packet.Id, ct);
if (!mapInfoResult.IsSuccess)
{
_logger.LogWarning
(
"Could not obtain a map info for id {id}: {error}",
packet.Id,
mapInfoResult.ToFullString()
);
}
await _game.CreateMapAsync
(
() =>
{
var map = packet.Id == 20001
? new Miniland
(
packet.Id,
packet.Type,
mapInfoResult.IsSuccess ? mapInfoResult.Entity : null,
new MapEntities(),
Array.Empty(),
Array.Empty()
)
: new Data.Maps.Map
(
packet.Id,
packet.Type,
mapInfoResult.IsSuccess ? mapInfoResult.Entity : null,
new MapEntities(),
Array.Empty()
);
var character = _game.Character;
if (character is not null)
{
map.Entities.AddEntity(character);
}
return map;
},
ct: ct
);
return Result.FromSuccess();
}
}