Skip to content

Commit

Permalink
Added a Stats field containing world statistics (#30)
Browse files Browse the repository at this point in the history
## How does this PR impact the user?

<!-- Add "before" and "after" screenshots or screen recordings; we like
loom for screen recordings https://www.loom.com/ -->

## Description

<!-- Concisely describe the changes in this PR -->
Added console logging showing world stats (kills, deaths, count of eaten
food)

## Limitations

<!-- Anything related to this PR that wasn't "done" in this PR -->

## Checklist

- [ ] my PR is focused and contains one wholistic change
- [ ] I have added screenshots or screen recordings to show the changes
  • Loading branch information
PlutoniumSoup authored Sep 7, 2024
1 parent f67c6bd commit a547c19
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion utils/prepareBotCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { WorldState } from "~/utils/world";

type PrepareBotCodeArgs = {
bot: BotCode;
state: WorldState;
state: Pick<WorldState, "bots" | "food" | "width" | "height">;
botApi: string;
};

Expand Down
36 changes: 36 additions & 0 deletions utils/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface WorldState {
food: Sprite[];
width: number;
height: number;
stats: Map<string, Stats>;
}

type BotSprites = Map<string, BotSprite>;
Expand All @@ -43,9 +44,16 @@ const BOT_COLORS = [
"#FFA500",
];

type Stats = {
kills: number;
deaths: number;
foodEaten: number;
};

export default class World {
private botSpawns: BotSprites = new Map();
private botIdToSpawnId: Map<string, string> = new Map();
private stats: Map<string, Stats> = new Map();
private food: Sprite[] = [];
private width: number;
private height: number;
Expand Down Expand Up @@ -138,6 +146,13 @@ export default class World {
};
this.botSpawns.set(spawnId, newBot);
this.botIdToSpawnId.set(botId, spawnId);
if (!this.stats.has(botId)) {
this.stats.set(botId, {
kills: 0,
deaths: 0,
foodEaten: 0,
});
}
return newBot;
}

Expand All @@ -147,6 +162,7 @@ export default class World {
food: this.food,
width: this.width,
height: this.height,
stats: this.stats,
};
}

Expand Down Expand Up @@ -213,6 +229,19 @@ export default class World {
const distance = World.distance(bot, otherBot);
if (distance < bot.radius && bot.radius > otherBot.radius) {
botIdsToRemove.push(otherBotSpawnId);
// updating statistic
const botStats = this.stats.get(bot.botId);
if (!botStats) {
console.error(new Error("can't find bot stats; check if they were created in `.addBot`"));
continue;
}
const otherBotStats = this.stats.get(otherBot.botId);
if (!otherBotStats) {
console.error(new Error("can't find other bot stats; check if they were created in `.addBot`"));
continue;
}
botStats.kills += 1;
otherBotStats.deaths + 1;
}
}

Expand All @@ -224,6 +253,13 @@ export default class World {
const distance = World.distance(bot, food[i] as Sprite);
if (distance < bot.radius) {
foodIdxToRemove.push(i);
// updating statistic
const botStats = this.stats.get(bot.botId);
if (!botStats) {
console.error(new Error("can't find bot stats; check if they were created in `.addBot`"));
continue;
}
botStats.foodEaten += 1;
}
}

Expand Down

0 comments on commit a547c19

Please sign in to comment.