-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,86 @@ | ||
namespace Savor22b.GraphTypes.Query; | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Bencodex.Types; | ||
using GraphQL; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using Libplanet.Blockchain; | ||
using Savor22b.Constants; | ||
using Savor22b.GraphTypes.Types; | ||
using Savor22b.Model; | ||
using Savor22b.States; | ||
|
||
public class VillagesQuery : FieldType | ||
{ | ||
public VillagesQuery() | ||
public VillagesQuery(BlockChain blockChain) | ||
: base() | ||
{ | ||
Name = "villages"; | ||
Type = typeof(NonNullGraphType<ListGraphType<VillageType>>); | ||
Description = "Get all villages"; | ||
Resolver = new FuncFieldResolver<ImmutableList<Village>>(context => | ||
Resolver = new FuncFieldResolver<ImmutableList<VillageDetail>>(context => | ||
{ | ||
try | ||
{ | ||
ImmutableList<Village> villages = CsvDataHelper.GetVillageCSVData(); | ||
return villages; | ||
var villages = CsvDataHelper.GetVillageCSVData().ToArray(); | ||
var villageDetails = GetVillageDetails( | ||
villages.ToImmutableList(), | ||
context, | ||
blockChain | ||
); | ||
return villageDetails; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new ExecutionError(e.Message); | ||
} | ||
}); | ||
} | ||
|
||
public static ImmutableList<VillageDetail> GetVillageDetails( | ||
ImmutableList<Village> villages, | ||
IResolveFieldContext context, | ||
BlockChain blockChain | ||
) | ||
{ | ||
GlobalUserHouseState globalUserHouseState = blockChain.GetState( | ||
Addresses.UserHouseDataAddress | ||
) | ||
is Dictionary stateEncoded | ||
? new GlobalUserHouseState(stateEncoded) | ||
: new GlobalUserHouseState(); | ||
|
||
var houseStates = globalUserHouseState.UserHouse.ToImmutableList(); | ||
var houses = new Dictionary<int, List<House>>(); | ||
|
||
foreach (var houseState in houseStates) | ||
{ | ||
House house = GlobalUserHouseState.ParsingKey(houseState.Key, houseState.Value); | ||
|
||
if (houses.ContainsKey(house.VillageId)) | ||
{ | ||
houses[house.VillageId].Add(house); | ||
} | ||
else | ||
{ | ||
houses[house.VillageId] = new List<House> { house }; | ||
} | ||
} | ||
|
||
var villageDetails = villages | ||
.Select(village => | ||
{ | ||
var housesInVillage = houses.ContainsKey(village.Id) | ||
? houses[village.Id].ToImmutableList() | ||
: ImmutableList<House>.Empty; | ||
return new VillageDetail(village, housesInVillage); | ||
}) | ||
.ToImmutableList(); | ||
|
||
return villageDetails; | ||
} | ||
} |
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,21 @@ | ||
using Libplanet; | ||
|
||
namespace Savor22b.GraphTypes.Types; | ||
|
||
public class House | ||
{ | ||
public int VillageId { get; private set; } | ||
public int X { get; private set; } | ||
|
||
public int Y { get; private set; } | ||
|
||
public Address Owner { get; private set; } | ||
|
||
public House(int villageId, int x, int y, Address owner) | ||
{ | ||
VillageId = villageId; | ||
X = x; | ||
Y = y; | ||
Owner = owner; | ||
} | ||
}; |
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 @@ | ||
namespace Savor22b.GraphTypes.Types; | ||
|
||
using GraphQL.Types; | ||
using Savor22b.Model; | ||
|
||
public class HouseType : ObjectGraphType<House> | ||
{ | ||
public HouseType() | ||
{ | ||
Field<NonNullGraphType<IntGraphType>>( | ||
"x", | ||
description: "The x coordinate of the house.", | ||
resolve: context => context.Source.X | ||
); | ||
|
||
Field<NonNullGraphType<IntGraphType>>( | ||
"y", | ||
description: "The y coordinate of the house.", | ||
resolve: context => context.Source.Y | ||
); | ||
|
||
Field<NonNullGraphType<StringGraphType>>( | ||
"owner", | ||
description: "The owner of the house.", | ||
resolve: context => context.Source.Owner.ToString() | ||
); | ||
} | ||
} |
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.Collections.Immutable; | ||
using Savor22b.Model; | ||
|
||
namespace Savor22b.GraphTypes.Types; | ||
|
||
public class VillageDetail : Village | ||
{ | ||
public ImmutableList<House> Houses { get; set; } | ||
|
||
public VillageDetail(Village village, ImmutableList<House> houses) | ||
: base() | ||
{ | ||
Id = village.Id; | ||
Name = village.Name; | ||
Width = village.Width; | ||
Height = village.Height; | ||
WorldX = village.WorldX; | ||
WorldY = village.WorldY; | ||
|
||
Houses = houses; | ||
} | ||
|
||
public VillageDetail(Village village) | ||
: base() | ||
{ | ||
Id = village.Id; | ||
Name = village.Name; | ||
Width = village.Width; | ||
Height = village.Height; | ||
WorldX = village.WorldX; | ||
WorldY = village.WorldY; | ||
|
||
Houses = ImmutableList<House>.Empty; | ||
} | ||
|
||
public VillageDetail UpdateHouses(ImmutableList<House> houses) | ||
{ | ||
return new VillageDetail(this, houses); | ||
} | ||
} |
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