Skip to content

Commit

Permalink
improving seed function to update existing maps if they were changed
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 17, 2024
1 parent 14e0af7 commit fd5b30f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
33 changes: 22 additions & 11 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,34 @@ const LEVELS = [
["Z", " "],
],
},
{
grid: [
[" ", " "],
["R", " "],
["Z", "Z"],
["Z", "Z"],
],
},
];

export const seedMaps = internalMutation({
handler: async (ctx) => {
const firstMap = await ctx.db.query("maps").first();

if (firstMap) {
return;
}
const maps = await ctx.db.query("maps").collect();

await Promise.all(
LEVELS.map((map, idx) =>
ctx.db.insert("maps", {
level: idx + 1,
grid: map.grid,
}),
),
LEVELS.map((map, idx) => {
const existingMap = maps.find((it) => it.level === idx + 1);
if (existingMap) {
ctx.db.patch(existingMap._id, {
grid: map.grid,
});
} else {
ctx.db.insert("maps", {
level: idx + 1,
grid: map.grid,
});
}
}),
);
},
});
Expand Down
1 change: 1 addition & 0 deletions models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The 2d Grid is made up of characters, where each character has a meaning.
- often it's good to wall off between the zombies and players if possible, this will slow the zombies down.
- You should never put a player directly next to a zombie.
- You should try to put blocks directly next to players
- If the player is behind a choke point, blocking the path to the player is the best option.
# Output Format
Expand Down

0 comments on commit fd5b30f

Please sign in to comment.