Skip to content

Commit

Permalink
Merge branch 'adding-tests-and-extensions'
Browse files Browse the repository at this point in the history
  • Loading branch information
grofit committed Mar 25, 2022
2 parents 8c6b75b + 1f6c94a commit d212075
Show file tree
Hide file tree
Showing 64 changed files with 894 additions and 299 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.18.{build}
version: 0.19.{build}
branches:
only:
- master
Expand Down Expand Up @@ -27,7 +27,7 @@ deploy:
APPVEYOR_REPO_TAG: true
server:
api_key:
secure: 0yJPCmw0EEE6ea73EEvaB8+TjIhUs0A8wG0dR60FEAu+B/i6x33b0GweDBVyz0PE
secure: ZBJLq3pN+Q0n1I99/r9rVbdWuCfAiv7bobV9WYDiMR3ebjIx/3d/AVcYK27zC3vm
skip_symbols: true
symbol_server:
artifact: /.*\.nupkg/
8 changes: 4 additions & 4 deletions src/OpenRpg.Core/Utils/DefaultRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ namespace OpenRpg.Core.Utils
{
public class DefaultRandomizer : IRandomizer
{
private System.Random _random;
public System.Random NativeRandomizer { get; }

public DefaultRandomizer(System.Random random)
{ _random = random; }
{ NativeRandomizer = random; }

public int Random(int min, int max)
{ return _random.Next(min, max + 1); }
{ return NativeRandomizer.Next(min, max + 1); }

public float Random(float min, float max)
{ return (float)_random.NextDouble() * (max - min) + min; }
{ return (float)NativeRandomizer.NextDouble() * (max - min) + min; }
}
}
2 changes: 1 addition & 1 deletion src/OpenRpg.Core/Utils/IRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace OpenRpg.Core.Utils
public interface IRandomizer
{
int Random(int min, int max);
float Random(float min, float max);
float Random(float min = 0, float max = 1.0f);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenRpg.CurveFunctions.Curves
{
public class PassThroughlCurveFunction : ICurveFunction
public class PassThroughCurveFunction : ICurveFunction
{
public float Plot(float value)
{ return value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static float SanitizeValue(this ICurveFunction curve, float value)
/// <returns>The denormalized value from the curve</returns>
public static float ScaledPlot(this ICurveFunction curve, float value, float maxValue)
{
if (value == 0) { return 0; }
var normalizedValue = value / maxValue;
var normalizedValue = (value + float.Epsilon) / maxValue;
var normalizedOutput = curve.Plot(normalizedValue);
return normalizedOutput * maxValue;
}
Expand Down
31 changes: 31 additions & 0 deletions src/OpenRpg.CurveFunctions/Extensions/IRandomizerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using OpenRpg.Core.Utils;

namespace OpenRpg.CurveFunctions.Extensions
{
public static class IRandomizerExtensions
{
public static float Random(this IRandomizer randomizer, ICurveFunction curve)
{
var randomNumber = randomizer.Random();
return curve.Plot(randomNumber);
}

public static float Random(this IRandomizer randomizer, ICurveFunction curve, float minValue, float maxValue)
{
var randomNumber = randomizer.Random(minValue, maxValue);
var isNegative = minValue < 0;
if (isNegative)
{
randomNumber = Math.Abs(randomNumber);
maxValue = Math.Abs(minValue);
}

var result = curve.ScaledPlot(randomNumber, maxValue);
return isNegative ? -result : result;
}

public static int Random(this IRandomizer randomizer, ICurveFunction curve, int minValue, int maxValue)
{ return (int)Random(randomizer, curve, minValue, (float)maxValue); }
}
}
4 changes: 4 additions & 0 deletions src/OpenRpg.CurveFunctions/OpenRpg.CurveFunctions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<PackageTags>rpg game-development xna monogame unity godot</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OpenRpg.Core\OpenRpg.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/OpenRpg.CurveFunctions/PresetCurves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class PresetCurves
public static SineCurveFunction InverseSineWave = new SineCurveFunction(-1.0f, 0, 0);
public static StepCurveFunction GreaterThanHalf = new StepCurveFunction(0.5f);
public static StepCurveFunction LessThanHalf = new StepCurveFunction(0.5f, 1.0f, 0.0f);
public static PassThroughlCurveFunction PassThrough = new PassThroughlCurveFunction();
public static PassThroughCurveFunction PassThrough = new PassThroughCurveFunction();
}
}
23 changes: 23 additions & 0 deletions src/OpenRpg.Data.Database/DatabaseDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Data;
using Dapper;

namespace OpenRpg.Data.Database
{
public class DatabaseDataSource : IDatabaseDataSource
{
public IDbConnection Connection { get; }

public object NativeSource => Connection;

public DatabaseDataSource(IDbConnection connection)
{ Connection = connection; }

public T Get<T>(object id) => Connection.Get<T>(id);
public IEnumerable<T> GetAll<T>() => Connection.GetList<T>();
public void Create<T>(T data, object id = null) => Connection.Insert(data);
public void Update<T>(T data, object id) => Connection.Update(data);
public bool Delete<T>(object id) => Connection.Delete<T>(id) > 0;
public bool Exists<T>(object id) => Get<T>(id) != null;
}
}
7 changes: 7 additions & 0 deletions src/OpenRpg.Data.Database/IDatabaseDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenRpg.Data.Database
{
public interface IDatabaseDataSource : IDataSource
{

}
}
24 changes: 24 additions & 0 deletions src/OpenRpg.Data.Database/OpenRpg.Data.Database.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.0.0</Version>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<Title>OpenRpg.Data.Database</Title>
<Authors>Grofit (LP)</Authors>
<PackageLicenseUrl>https://github.com/openrpg/OpenRpg/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/openrpg/OpenRpg</PackageProjectUrl>
<Description>An SQL database implementation of the OpenRpg data layer using Dapper</Description>
<PackageTags>rpg game-development repository data xna monogame unity godot</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OpenRpg.Core\OpenRpg.Core.csproj" />
<ProjectReference Include="..\OpenRpg.Data\OpenRpg.Data.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Dapper.SimpleCRUD" Version="2.3.0" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions src/OpenRpg.Data.InMemory/Builder/InMemoryDataSourceBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenRpg.Data.InMemory.Builder
{
public class InMemoryDataSourceBuilder
{
private readonly Dictionary<Type, Dictionary<object, object>> Database;

protected InMemoryDataSourceBuilder(Dictionary<Type, Dictionary<object, object>> database)
{
Database = database;
}

public static InMemoryDataSourceBuilder Create()
{ return new InMemoryDataSourceBuilder(new Dictionary<Type, Dictionary<object, object>>()); }

public InMemoryDataSourceBuilder WithData<T>(IEnumerable<T> data, Func<T, object> keySelector)
{
var typeOfT = typeof(T);
if (!Database.ContainsKey(typeOfT))
{
var contents = data.ToDictionary(keySelector, x => (object)x);
Database.Add(typeOfT, contents);
return this;
}

var typeContainer = Database[typeOfT];

foreach(var element in data)
{ typeContainer.Add(keySelector(element), element); }

return this;
}

public IDataSource Build()
{ return new InMemoryDataSource(Database); }
}
}
7 changes: 7 additions & 0 deletions src/OpenRpg.Data.InMemory/IInMemoryDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenRpg.Data.InMemory
{
public interface IInMemoryDataSource : IDataSource
{

}
}
28 changes: 28 additions & 0 deletions src/OpenRpg.Data.InMemory/InMemoryDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenRpg.Data.InMemory
{
public class InMemoryDataSource : IInMemoryDataSource
{
public Dictionary<Type, Dictionary<object, object>> Database { get; }

public object NativeSource => Database;

public InMemoryDataSource(Dictionary<Type, Dictionary<object, object>> database = null)
{ Database = database ?? new Dictionary<Type, Dictionary<object, object>>(); }

public T Get<T>(object id) => (T)Database[typeof(T)][id];
public IEnumerable<T> GetAll<T>() => Database[typeof(T)].Values.Cast<T>();
public void Update<T>(T data, object id) => Database[typeof(T)][id] = data;
public bool Delete<T>(object id) => Database[typeof(T)].Remove(id);
public bool Exists<T>(object id) => Database[typeof(T)].ContainsKey(id);

public void Create<T>(T data, object id = null)
{
if(id == null) { throw new ArgumentNullException(nameof(id), "In Memory DB Requires explicit keys on creation"); }
Database[typeof(T)].Add(id, data);
}
}
}
19 changes: 19 additions & 0 deletions src/OpenRpg.Data.InMemory/OpenRpg.Data.InMemory.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.0.0</Version>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<Title>OpenRpg.Data.InMemory</Title>
<Authors>Grofit (LP)</Authors>
<PackageLicenseUrl>https://github.com/openrpg/OpenRpg/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/openrpg/OpenRpg</PackageProjectUrl>
<Description>An in memory implementation of the OpenRpg data layer</Description>
<PackageTags>rpg game-development repository data xna monogame unity godot</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OpenRpg.Core\OpenRpg.Core.csproj" />
<ProjectReference Include="..\OpenRpg.Data\OpenRpg.Data.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using OpenRpg.Core.Common;
using OpenRpg.Data.Conventions.Queries;

namespace OpenRpg.Data.Conventions.Extensions
{
public static class RepositoryDataExtensions
{
public static T Create<T>(this IRepository repository, T entity) where T : class, IHasDataId
{ return repository.Query(new CreateEntityQuery<T>(entity, entity.Id)); }

public static T Update<T>(this IRepository repository, T entity) where T : class, IHasDataId
{ return repository.Query(new UpdateEntityQuery<T>(entity, entity.Id)); }

public static bool Delete<T>(this IRepository repository, T entity) where T : class, IHasDataId
{ return repository.Query(new DeleteEntityQuery<T>(entity.Id)); }

public static bool Exists<T>(this IRepository repository, T entity) where T : class, IHasDataId
{ return repository.Query(new EntityExistsQuery<T>(entity.Id)); }
}
}
22 changes: 22 additions & 0 deletions src/OpenRpg.Data/Conventions/Extensions/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using OpenRpg.Data.Conventions.Queries;

namespace OpenRpg.Data.Conventions.Extensions
{
public static class RepositoryExtensions
{
public static T Create<T>(this IRepository repository, T entity, object id = null) where T : class
{ return repository.Query(new CreateEntityQuery<T>(entity, id)); }

public static T Get<T>(this IRepository repository, object id) where T : class
{ return repository.Query(new GetEntityQuery<T>(id)); }

public static T Update<T>(this IRepository repository, T entity, object id) where T : class
{ return repository.Query(new UpdateEntityQuery<T>(entity, id)); }

public static bool Delete<T>(this IRepository repository, object id) where T : class
{ return repository.Query(new DeleteEntityQuery<T>(id)); }

public static bool Exists<T>(this IRepository repository, object id) where T : class
{ return repository.Query(new EntityExistsQuery<T>(id)); }
}
}
20 changes: 20 additions & 0 deletions src/OpenRpg.Data/Conventions/Queries/CreateEntityQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace OpenRpg.Data.Conventions.Queries
{
public class CreateEntityQuery<T> : IQuery<T>
{
public T Entity { get; }
public object Id { get; }

public CreateEntityQuery(T entity, object id = null)
{
Entity = entity;
Id = id;
}

public T Execute(IDataSource dataSource)
{
dataSource.Create(Entity, Id);
return Entity;
}
}
}
13 changes: 13 additions & 0 deletions src/OpenRpg.Data/Conventions/Queries/DeleteEntityQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OpenRpg.Data.Conventions.Queries
{
public class DeleteEntityQuery<T> : IQuery<bool>
{
public object Id { get; }

public DeleteEntityQuery(object id)
{ Id = id; }

public bool Execute(IDataSource dataSource)
{ return dataSource.Delete<T>(Id); }
}
}
13 changes: 13 additions & 0 deletions src/OpenRpg.Data/Conventions/Queries/EntityExistsQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OpenRpg.Data.Conventions.Queries
{
public class EntityExistsQuery<T> : IQuery<bool>
{
public object Id { get; }

public EntityExistsQuery(object id)
{ Id = id; }

public bool Execute(IDataSource dataSource)
{ return dataSource.Exists<T>(Id); }
}
}
13 changes: 13 additions & 0 deletions src/OpenRpg.Data/Conventions/Queries/GetEntityQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OpenRpg.Data.Conventions.Queries
{
public class GetEntityQuery<T> : IQuery<T>
{
public object Id { get; }

public GetEntityQuery(object id)
{ Id = id; }

public T Execute(IDataSource dataSource)
{ return dataSource.Get<T>(Id); }
}
}
Loading

0 comments on commit d212075

Please sign in to comment.