-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'adding-tests-and-extensions'
- Loading branch information
Showing
64 changed files
with
894 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tions/Curves/PassThroughlCurveFunction.cs → ...ctions/Curves/PassThroughCurveFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/OpenRpg.CurveFunctions/Extensions/IRandomizerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenRpg.Data.Database | ||
{ | ||
public interface IDatabaseDataSource : IDataSource | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/OpenRpg.Data.InMemory/Builder/InMemoryDataSourceBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenRpg.Data.InMemory | ||
{ | ||
public interface IInMemoryDataSource : IDataSource | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
src/OpenRpg.Data/Conventions/Extensions/RepositoryDataExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/OpenRpg.Data/Conventions/Extensions/RepositoryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
} |
Oops, something went wrong.