Skip to content

Commit

Permalink
Modify Inspect API to show detailed data (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcz717 authored May 24, 2019
1 parent dd4c93c commit f6d005e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 72 deletions.
2 changes: 1 addition & 1 deletion SimCivil.Contract/EntityInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public class InspectionResult
public Guid EntityId { get; set; }
public DateTime TimeStamp { get; set; }
public Guid ObserverId { get; set; }
public Dictionary<string, string> Values { get; set; }
public Dictionary<string, object> Values { get; set; }
}
}
2 changes: 1 addition & 1 deletion SimCivil.Gate/Gate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static void Configure(HostBuilderContext context, IServiceCollection ser
o.BasePath = "logs";
o.EnsureBasePath = true;
o.FallbackFileName =
$"{Assembly.GetExecutingAssembly().GetName().Version}-{DateTime.Now:yyyy-dd-M-HH-mm-ss}.log";
$"gate-{Assembly.GetExecutingAssembly().GetName().Version}-{DateTime.Now:yyyy-dd-M-HH-mm-ss}.log";
}))
.Configure<ClusterOptions>(configuration.GetSection("Cluster"))
.Configure<DynamoDBGatewayOptions>(configuration.GetSection("DynamoDBClustering"));
Expand Down
2 changes: 2 additions & 0 deletions SimCivil.Gate/before-deploy.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
$name = "SimCivil.Gate.exe"
if ((Get-Process).Where({ $_.Name -Match $name }, 'First').Count -gt 0 -and $env:APPLICATION_PATH -Match "net461") {
Write-Output (Stop-Process -Name $name)
} else {
Write-Output $name + " not found"
}
4 changes: 2 additions & 2 deletions SimCivil.Orleans.Grains/Component/BaseGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public virtual Task<IReadOnlyDictionary<string, string>> Dump()
.ToDictionary(prop => prop.Name, prop => prop.GetValue(State, null)?.ToString()));
}

public virtual Task<IReadOnlyDictionary<string, string>> Inspect(IEntity observer)
public virtual Task<IReadOnlyDictionary<string, object>> Inspect(IEntity observer)
{
return Task.FromResult<IReadOnlyDictionary<string, string>>(null);
return Task.FromResult<IReadOnlyDictionary<string, object>>(null);
}

public Task Delete()
Expand Down
93 changes: 39 additions & 54 deletions SimCivil.Orleans.Grains/Component/ControllerGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// SOFTWARE.
//
// SimCivil - SimCivil.Orleans.Grains - ControllerGrain.cs
// Create Date: 2018/12/13
// Update Date: 2019/04/28
// Create Date: 2019/05/18
// Update Date: 2019/05/19

using System;
using System.Collections.Generic;
Expand All @@ -45,35 +45,32 @@ namespace SimCivil.Orleans.Grains.Component
public class ControllerGrain : Grain, IUnitController
{
private readonly ITerrainRepository _terrainRepository;
private double _lagPredict;
private DateTime _lastUpdateTime;
private IMovementSystem _moveSystem;
private (float X, float Y) _speed;
private double _lagPredict;
private DateTime _lastUpdateTime;
private IMovementSystem _moveSystem;
private (float X, float Y) _speed;

public TimeSpan UpdatePeriod { get; set; }

public ILogger<ControllerGrain> Logger { get; }
public IOptions<GameOptions> GameOptions { get; }
public IOptions<SyncOptions> SyncOptions { get; }
public ILogger<ControllerGrain> Logger { get; }
public IOptions<GameOptions> GameOptions { get; }
public IOptions<SyncOptions> SyncOptions { get; }

public ControllerGrain(
ILogger<ControllerGrain> logger,
ITerrainRepository terrainRepository,
IOptions<GameOptions> gameOptions,
IOptions<SyncOptions> syncOptions)
ITerrainRepository terrainRepository,
IOptions<GameOptions> gameOptions,
IOptions<SyncOptions> syncOptions)
{
Logger = logger;
GameOptions = gameOptions;
SyncOptions = syncOptions;
Logger = logger;
GameOptions = gameOptions;
SyncOptions = syncOptions;
_terrainRepository = terrainRepository;
UpdatePeriod = TimeSpan.FromMilliseconds(syncOptions.Value.UpdatePeriod);
_lastUpdateTime = DateTime.Now - UpdatePeriod - UpdatePeriod;
UpdatePeriod = TimeSpan.FromMilliseconds(syncOptions.Value.UpdatePeriod);
_lastUpdateTime = DateTime.Now - UpdatePeriod - UpdatePeriod;
}

public Task<(float X, float Y)> GetSpeed()
{
return Task.FromResult(_speed);
}
public Task<(float X, float Y)> GetSpeed() => Task.FromResult(_speed);

/// <summary>
/// Moves the specified direction.
Expand Down Expand Up @@ -106,7 +103,7 @@ public async Task Move((float X, float Y) direction, float speed)
public async Task MoveTo(PositionState position, DateTime timeStamp)
{
double delta = (timeStamp - DateTime.UtcNow).TotalMilliseconds;
_lagPredict = SyncOptions.Value.LagLearningRate * delta +
_lagPredict = SyncOptions.Value.LagLearningRate * delta +
(1 - SyncOptions.Value.LagLearningRate) * _lagPredict;
DateTime predictTimeStamp = timeStamp.AddMilliseconds(-_lagPredict);
if (Math.Abs(delta - _lagPredict) > SyncOptions.Value.MaxLag)
Expand Down Expand Up @@ -150,7 +147,7 @@ public async Task MoveTo(PositionState position, DateTime timeStamp)

// TODO check if construction exists
if (_terrainRepository.GetTerrain((await GrainFactory.GetTile(position.Tile, GameOptions)).Terrain)
.Flags.HasFlag(TerrainFlags.NotMovable))
.Flags.HasFlag(TerrainFlags.NotMovable))
return;

await GrainFactory.Get<IPosition>(this).SetData(position);
Expand All @@ -161,61 +158,49 @@ public async Task Stop()
await _moveSystem.Stop(this.GetPrimaryKey());
}

public Task Drop(IEntity target)
{
throw new NotImplementedException();
}
public Task Drop(IEntity target) => throw new NotImplementedException();

public Task Attack(IEntity target)
{
throw new NotImplementedException();
}
public Task Attack(IEntity target) => throw new NotImplementedException();

public Task Use(IEntity target)
{
throw new NotImplementedException();
}
public Task Use(IEntity target) => throw new NotImplementedException();

public async Task<InspectionResult> InspectEntity(IEntity target)
{
var result = new InspectionResult
{
EntityId = target.GetPrimaryKey(),
EntityId = target.GetPrimaryKey(),
ObserverId = this.GetPrimaryKey(),
TimeStamp = DateTime.UtcNow,
Values = new Dictionary<string, string>()
TimeStamp = DateTime.UtcNow,
Values = new Dictionary<string, object>()
};

var components = await GrainFactory.GetEntity(this).GetComponents();
var inspections = await Task.WhenAll(
components.Where(c => !(c is IUnitController)).Select(c => c.Inspect(target)));
components.Where(c => !(c is IUnitController)).Select(c => c.Inspect(target)));
result.Values = inspections.Where(i => i != null).SelectMany(i => i).ToDictionary(i => i.Key, i => i.Value);

return result;
}

public Task<IComponent> CopyTo(IEntity target)
{
return Task.FromResult((IComponent) GrainFactory.Get<IUnitController>(target));
}
=> Task.FromResult((IComponent) GrainFactory.Get<IUnitController>(target));

public Task<IReadOnlyDictionary<string, string>> Dump()
{
return Task.FromResult(
(IReadOnlyDictionary<string, string>) new Dictionary<string, string>
{
["Speed"] = _speed.ToString()
});
}
public Task<IReadOnlyDictionary<string, string>> Dump() => Task.FromResult(
(IReadOnlyDictionary<string, string>) new Dictionary<string, string>
{
["Speed"] = _speed.ToString()
});

public Task<IReadOnlyDictionary<string, string>> Inspect(IEntity target)
{
return Dump();
}
public Task<IReadOnlyDictionary<string, object>> Inspect(IEntity target) => Task.FromResult(
(IReadOnlyDictionary<string, object>) new Dictionary<string, object>
{
["Speed"] = _speed.ToString()
});

public Task Delete()
{
DeactivateOnIdle();

return Task.CompletedTask;
}

Expand Down
27 changes: 15 additions & 12 deletions SimCivil.Orleans.Grains/Component/UnitGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 TPDT
// Copyright (c) 2017 TPDT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,8 +19,8 @@
// SOFTWARE.
//
// SimCivil - SimCivil.Orleans.Grains - UnitGrain.cs
// Create Date: 2019/05/13
// Update Date: 2019/05/14
// Create Date: 2019/05/14
// Update Date: 2019/05/19

using System;
using System.Collections.Generic;
Expand All @@ -43,12 +43,15 @@ public class UnitGrain : BaseGrain<UnitState>, IUnit
{
public UnitGrain(ILoggerFactory factory) : base(factory) { }

public override Task<IReadOnlyDictionary<string, string>> Inspect(IEntity observer)
{
return Task.FromResult<IReadOnlyDictionary<string, string>>(
typeof(UnitState).GetProperties().ToDictionary(p => p.Name, p => p.GetValue(State)?.ToString())
public override Task<IReadOnlyDictionary<string, object>> Inspect(IEntity observer)
=> Task.FromResult<IReadOnlyDictionary<string, object>>(
new Dictionary<string, object>
{
[nameof(State.BodyParts)] = State.BodyParts,
[nameof(State.Abilities)] = State.Abilities,
[nameof(State.Effects)] = State.Effects,
}
);
}

/// <summary>Gets the heath point.</summary>
/// <returns></returns>
Expand Down Expand Up @@ -84,7 +87,7 @@ public Task UpdateAbilities()
UpdateLowerPower();
UpdateVision();

return WriteStateAsync();
return UpdateEffects();
}

public Task UpdateEffects()
Expand Down Expand Up @@ -144,9 +147,9 @@ private void UpdateReconstructionAbility() => State.ReconstructionAbility.Update
private void UpdateMentalAbility() => State.MentalAbility.Update(State.Soul.Efficiency);

private void UpdateVision() => State.Vision = State.Vision.Update(
Max(
State.LeftEye.Efficiency,
State.RightEye.Efficiency));
Max(
State.LeftEye.Efficiency,
State.RightEye.Efficiency));

private void UpdateCreativity() => State.Creativity.Update(State.Brain.Efficiency);

Expand Down
2 changes: 1 addition & 1 deletion SimCivil.Orleans.Interfaces/IComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IComponent : IGrainWithGuidKey
{
Task<IComponent> CopyTo(IEntity target);
Task<IReadOnlyDictionary<string, string>> Dump();
Task<IReadOnlyDictionary<string, string>> Inspect(IEntity observer);
Task<IReadOnlyDictionary<string, object>> Inspect(IEntity observer);
Task Delete();
}

Expand Down
2 changes: 1 addition & 1 deletion SimCivil.Orleans.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static void ConfigureLogging(HostBuilderContext context, ILoggingBuilder
o.BasePath = "logs";
o.EnsureBasePath = true;
o.FallbackFileName =
$"{Assembly.GetExecutingAssembly().GetName().Version}-{DateTime.Now:yyyy-dd-M-HH-mm-ss}.log";
$"server-{Assembly.GetExecutingAssembly().GetName().Version}-{DateTime.Now:yyyy-dd-M-HH-mm-ss}.log";
});
}
}
Expand Down

0 comments on commit f6d005e

Please sign in to comment.