Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
feat: Add dungeon list query
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa committed Mar 18, 2024
1 parent 92304e6 commit 33f3e8a
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 36 deletions.
1 change: 1 addition & 0 deletions backend/app/Savor22b/Constants/CsvDataFileNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public static class CsvDataFileNames
public static readonly string VillageCharacteristics = "village_characteristics.csv";
public static readonly string Stat = "stat.csv";
public static readonly string Level = "level.csv";
public static readonly string Dungeon = "dungeon.csv";
}
44 changes: 30 additions & 14 deletions backend/app/Savor22b/GraphTypes/Types/DungeonStateType.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
namespace Savor22b.GraphTypes.Types;

using GraphQL.Types;
using Libplanet.Blockchain;
using Libplanet.Net;
using Libplanet.Store;
using Savor22b.States;
using Savor22b.Model;

public class DungeonStateType : ObjectGraphType<DungeonState>
public class DungeonStateType : ObjectGraphType<Dungeon>
{
public DungeonStateType(
BlockChain blockChain,
BlockRenderer blockRenderer,
IStore store,
Swarm? swarm = null
)
public DungeonStateType()
{
Field<NonNullGraphType<StringGraphType>>(
"name",
description: "던전의 이름입니다.",
resolve: context => context.Source.Name
);

Field<NonNullGraphType<IntGraphType>>(
"x",
description: "던전이 위치하는 마을의 x 좌표입니다.",
resolve: context => context.Source.X
);

Field<NonNullGraphType<IntGraphType>>(
"y",
description: "던전이 위치하는 마을의 y 좌표입니다.",
resolve: context => context.Source.Y
);

Field<NonNullGraphType<IntGraphType>>(
"id",
description: "던전의 유니크 아이디입니다.",
resolve: context => context.Source.ID
);

Field<NonNullGraphType<IntGraphType>>(
name: "DungeonKeyCount",
description: "The number of dungeon keys the user has.",
resolve: context => context.Source.GetDungeonKeyCount(blockChain.Count)
"villageId",
description: "던전이 위치하는 마을의 id 입니다.",
resolve: context => context.Source.VillageId
);
}
}
24 changes: 24 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/UserDungeonStateType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Savor22b.GraphTypes.Types;

using GraphQL.Types;
using Libplanet.Blockchain;
using Libplanet.Net;
using Libplanet.Store;
using Savor22b.States;

public class UserDungeonStateType : ObjectGraphType<UserDungeonState>
{
public UserDungeonStateType(
BlockChain blockChain,
BlockRenderer blockRenderer,
IStore store,
Swarm? swarm = null
)
{
Field<NonNullGraphType<IntGraphType>>(
name: "DungeonKeyCount",
description: "The number of dungeon keys the user has.",
resolve: context => context.Source.GetDungeonKeyCount(blockChain.Count)
);
}
}
4 changes: 2 additions & 2 deletions backend/app/Savor22b/GraphTypes/Types/UserStateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public UserStateType()
description: "The village state of the user.",
resolve: context => context.Source.VillageState
);
Field<NonNullGraphType<DungeonStateType>>(
Field<NonNullGraphType<UserDungeonStateType>>(
name: "dungeonState",
description: "The dungeon state of the user.",
resolve: context => context.Source.DungeonState
resolve: context => context.Source.UserDungeonState
);
}
}
10 changes: 10 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/VillageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@ public VillageType()
description: "This is a list of houses that exist within the village",
resolve: context => context.Source.Houses
);

Field<NonNullGraphType<ListGraphType<DungeonStateType>>>(
"dungeons",
description: "마을 내에 위치하는 던전 목록입니다. ",
resolve: context =>
{
var dungeons = CsvDataHelper.GetDungeonCSVData().ToArray();
return dungeons.Where(d => d.VillageId == context.Source.Id).ToArray();
}
);
}
}
30 changes: 30 additions & 0 deletions backend/app/Savor22b/Helpers/CsvDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static class CsvDataHelper
private static ImmutableList<Level> levelList;
private static ImmutableList<VillageCharacteristic> villageCharacteristicList;

private static ImmutableList<Dungeon> dungeonList;

public static void Initialize(string csvDataPath)
{
csvDataResourcePath = csvDataPath;
Expand All @@ -35,6 +37,34 @@ public static string GetRootPath()
return csvDataResourcePath;
}

#region dungeon

private static void initDungeonCSVData()
{
if (dungeonList == null)
{
CsvParser<Dungeon> csvParser = new CsvParser<Dungeon>();
var csvPath = GetCSVDataPath(CsvDataFileNames.Dungeon);
dungeonList = csvParser.ParseCsv(csvPath).ToImmutableList();
}
}

public static ImmutableList<Dungeon> GetDungeonCSVData()
{
initDungeonCSVData();

return dungeonList;
}

public static Dungeon? GetDungeonById(int id)
{
initDungeonCSVData();

return dungeonList.Find(dungeon => dungeon.ID == id);
}

#endregion dungeon

#region seed

private static void initSeedCSVData()
Expand Down
25 changes: 15 additions & 10 deletions backend/app/Savor22b/States/RootState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Savor22b.States;
public class RootState : State
{
public InventoryState InventoryState { get; private set; }
public DungeonState DungeonState { get; private set; }
public UserDungeonState UserDungeonState { get; private set; }

public RelocationState? RelocationState { get; private set; }

Expand All @@ -14,15 +14,20 @@ public class RootState : State
public RootState()
{
InventoryState = new InventoryState();
DungeonState = new DungeonState();
UserDungeonState = new UserDungeonState();
VillageState = null;
RelocationState = null;
}

public RootState(InventoryState inventoryState, DungeonState dungeonState, VillageState? villageState = null, RelocationState? relocationState = null)
public RootState(
InventoryState inventoryState,
UserDungeonState dungeonState,
VillageState? villageState = null,
RelocationState? relocationState = null
)
{
InventoryState = inventoryState ?? new InventoryState();
DungeonState = dungeonState ?? new DungeonState();
UserDungeonState = dungeonState ?? new UserDungeonState();
VillageState = villageState;
RelocationState = relocationState;
}
Expand All @@ -40,15 +45,15 @@ public RootState(Bencodex.Types.Dictionary encoded)
InventoryState = new InventoryState();
}

if (encoded.ContainsKey((Text)nameof(DungeonState)))
if (encoded.ContainsKey((Text)nameof(UserDungeonState)))
{
DungeonState = new DungeonState(
(Bencodex.Types.Dictionary)encoded[nameof(DungeonState)]
UserDungeonState = new UserDungeonState(
(Bencodex.Types.Dictionary)encoded[nameof(UserDungeonState)]
);
}
else
{
DungeonState = new DungeonState();
UserDungeonState = new UserDungeonState();
}

if (encoded.ContainsKey((Text)nameof(VillageState)))
Expand Down Expand Up @@ -98,8 +103,8 @@ public IValue Serialize()
InventoryState.Serialize()
),
new KeyValuePair<IKey, IValue>(
(Text)nameof(DungeonState),
DungeonState.Serialize()
(Text)nameof(UserDungeonState),
UserDungeonState.Serialize()
),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ namespace Savor22b.States;
using System.Collections.Immutable;
using Bencodex.Types;

public class DungeonState : State
public class UserDungeonState : State
{
public static readonly int MaxDungeonKey = 5;
public static readonly int DungeonKeyChargeIntervalBlock = 12;

public ImmutableList<DungeonKeyHistory> DungeonKeyHistories { get; private set; }

public DungeonState()
public UserDungeonState()
{
DungeonKeyHistories = ImmutableList<DungeonKeyHistory>.Empty;
}

public DungeonState(ImmutableList<DungeonKeyHistory> dungeonKeyHistories)
public UserDungeonState(ImmutableList<DungeonKeyHistory> dungeonKeyHistories)
{
DungeonKeyHistories = dungeonKeyHistories;
}

public DungeonState(Dictionary encoded)
public UserDungeonState(Dictionary encoded)
{
if (encoded.TryGetValue((Text)nameof(DungeonKeyHistories), out var dungeonKeyHistories))
{
Expand All @@ -36,12 +36,15 @@ public DungeonState(Dictionary encoded)

public IValue Serialize()
{
return new Dictionary(new[]
{
new KeyValuePair<IKey, IValue>(
(Text)nameof(DungeonKeyHistories),
new List(DungeonKeyHistories.Select(element => element.Serialize()))),
});
return new Dictionary(
new[]
{
new KeyValuePair<IKey, IValue>(
(Text)nameof(DungeonKeyHistories),
new List(DungeonKeyHistories.Select(element => element.Serialize()))
),
}
);
}

public int GetDungeonKeyCount(long blockIndex)
Expand Down

0 comments on commit 33f3e8a

Please sign in to comment.