Skip to content

Commit

Permalink
feat: Add houses query
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa authored and Atralupus committed Aug 20, 2023
1 parent cc55174 commit 957acc0
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 9 deletions.
63 changes: 59 additions & 4 deletions backend/app/Savor22b/GraphTypes/Query/VillagesQuery.cs
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;
}
}
21 changes: 21 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/House.cs
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;
}
};
28 changes: 28 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/HouseType.cs
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()
);
}
}
40 changes: 40 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/VillageDetail.cs
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);
}
}
13 changes: 9 additions & 4 deletions backend/app/Savor22b/GraphTypes/Types/VillageType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Savor22b.GraphTypes.Types;

using GraphQL.Types;
using Savor22b.Model;

public class VillageType : ObjectGraphType<Village>
public class VillageType : ObjectGraphType<VillageDetail>
{
public VillageType()
{
Expand Down Expand Up @@ -33,14 +32,20 @@ public VillageType()

Field<NonNullGraphType<IntGraphType>>(
"worldX",
description: "The world X position of the village.",
description: "The world X position of the village. These coordinates are relative to the center of the World (0,0 coordinates), which means that negative values are also included. (For example, if the width of the World is 5, the x coordinate can range from -2 to 2.)",
resolve: context => context.Source.WorldX
);

Field<NonNullGraphType<IntGraphType>>(
"worldY",
description: "The world Y position of the village.",
description: "The world Y position of the village. These coordinates are relative to the center of the World (0,0 coordinates), which means that negative values are also included. (For example, if the height of the World is 5, the x coordinate can range from -2 to 2.)",
resolve: context => context.Source.WorldY
);

Field<NonNullGraphType<ListGraphType<HouseType>>>(
"houses",
description: "This is a list of houses that exist within the village",
resolve: context => context.Source.Houses
);
}
}
15 changes: 14 additions & 1 deletion backend/app/Savor22b/States/GlobalUserHouseState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Savor22b.States;

using System.Globalization;
using Bencodex.Types;
using Libplanet;
using Libplanet.Headless.Extensions;
using Savor22b.GraphTypes.Types;

public class GlobalUserHouseState : State
{
Expand Down Expand Up @@ -55,11 +57,22 @@ public void SetUserHouse(string address, Address userAddress)
UserHouse[address] = userAddress;
}

public string CreateKey(int villageId, int targetX, int targetY)
public static string CreateKey(int villageId, int targetX, int targetY)
{
return $"{villageId},{targetX},{targetY}";
}

public static House ParsingKey(string key, Address address)
{
string[] keys = key.Split(',');
return new House(
int.Parse(keys[0], CultureInfo.InvariantCulture),
int.Parse(keys[1], CultureInfo.InvariantCulture),
int.Parse(keys[2], CultureInfo.InvariantCulture),
address
);
}

public bool CheckPlacedHouse(int villageID, int targetX, int targetY)
{
string key = CreateKey(villageID, targetX, targetY);
Expand Down

0 comments on commit 957acc0

Please sign in to comment.