156 lines
5.0 KiB
C#
156 lines
5.0 KiB
C#
using ServerCommon;
|
|
using ServerCore; using ServerBase;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static ClientToGameReq.Types;
|
|
|
|
namespace GameServer
|
|
{
|
|
internal class RentalAgentAction : EntityActionBase
|
|
{
|
|
ConcurrentDictionary<(int, int, int), Rental> m_rentals = new();
|
|
|
|
public RentalAgentAction(Player owner)
|
|
: base(owner)
|
|
{ }
|
|
|
|
public override async Task<Result> onInit()
|
|
{
|
|
var result = new Result();
|
|
return await Task.FromResult(result);
|
|
}
|
|
|
|
public override void onClear()
|
|
{
|
|
return;
|
|
}
|
|
|
|
public List<Rental> getRentals()
|
|
{
|
|
return m_rentals.Values.ToList();
|
|
}
|
|
|
|
public async Task<Result> tryAddRentalFromDoc(RentalDoc rentalDoc)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var player = getOwner() as Player;
|
|
NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!");
|
|
|
|
var server_logic = GameServerApp.getServerLogic();
|
|
var rental = new Rental(player);
|
|
await rental.onInit();
|
|
|
|
var rental_attribute = rental.getEntityAttribute<RentalAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(rental_attribute, () => $"rental_attribute is null !!! - {player.toBasicString()}");
|
|
|
|
if (!rental_attribute.copyEntityAttributeFromDoc(rentalDoc))
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeFromDoc() !!! to:{rental_attribute.getTypeName()}, from:{rentalDoc.getTypeName()} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
if (!m_rentals.TryAdd((rental_attribute.LandMetaId, rental_attribute.BuildingMetaId, rental_attribute.Floor), rental))
|
|
{
|
|
err_msg = $"Failed to TryAdd() !!! : {rental.toBasicString()} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.RentalDocLoadDuplicatedRental, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void addRental(int landMetaId, int buildingMetaId, int floor, Rental rental)
|
|
{
|
|
m_rentals[(landMetaId, buildingMetaId, floor)] = rental;
|
|
}
|
|
|
|
public bool isRentalMyhome(string myhomeGuid)
|
|
{
|
|
foreach (var rental in m_rentals.Values)
|
|
{
|
|
var rental_attribute = rental.getEntityAttribute<RentalAttribute>();
|
|
if (rental_attribute == null)
|
|
continue;
|
|
|
|
if (rental_attribute.MyhomeGuid == myhomeGuid)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public DateTime? getRentalMyhomeFinishTime(string myhomeGuid)
|
|
{
|
|
foreach (var rental in m_rentals.Values)
|
|
{
|
|
var rental_attribute = rental.getEntityAttribute<RentalAttribute>();
|
|
if (rental_attribute == null)
|
|
continue;
|
|
|
|
if (rental_attribute.MyhomeGuid == myhomeGuid)
|
|
return rental_attribute.RentalFinishTime;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public (int, int, int) getAddrressFromMyhome(string myhomeGuid)
|
|
{
|
|
var land_meta_id = 0;
|
|
var building_meta_id = 0;
|
|
var floor = 0;
|
|
|
|
foreach (var rental in m_rentals.Values)
|
|
{
|
|
var rental_attribute = rental.getEntityAttribute<RentalAttribute>();
|
|
if (rental_attribute == null)
|
|
continue;
|
|
|
|
if (rental_attribute.MyhomeGuid == myhomeGuid)
|
|
{
|
|
land_meta_id = rental_attribute.LandMetaId;
|
|
building_meta_id = rental_attribute.BuildingMetaId;
|
|
floor = rental_attribute.Floor;
|
|
}
|
|
}
|
|
|
|
return (land_meta_id, building_meta_id, floor);
|
|
}
|
|
|
|
public override async Task onTick()
|
|
{
|
|
await Task.CompletedTask;
|
|
|
|
var remove_address = new List<(int, int, int)>();
|
|
|
|
foreach (var (address, rental) in m_rentals)
|
|
{
|
|
var rental_action = rental.getEntityAction<RentalAction>();
|
|
NullReferenceCheckHelper.throwIfNull(rental_action, () => $"rental_action is null !!!");
|
|
|
|
if (rental_action.isRentalFinsh())
|
|
{
|
|
remove_address.Add(address);
|
|
}
|
|
}
|
|
|
|
foreach (var address in remove_address)
|
|
{
|
|
m_rentals.TryRemove(address, out _);
|
|
}
|
|
}
|
|
}
|
|
}
|