Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Feb 21, 2024
1 parent d8c492b commit c70072f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
42 changes: 42 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PrismaClient } from '@prisma/client'

import calculateNewPosition from "./src/helpers/calculateNewPosition";
import ShipRepository from "./src/repositories/ShipRepository";

const prisma = new PrismaClient()

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});

async function main() {
setInterval(tick, 1000);
}

const shipRepository = new ShipRepository(prisma);
async function tick() {
const ships = await shipRepository.getAll();
for (const ship of ships) {
if (ship.isStationary) {
console.log(`Ship ${ship.name} is at ${ship.currentPosition}`);
continue;
}

const newPosition = calculateNewPosition(ship.currentPosition, ship.destination.position, 1);
if (newPosition.x === ship.currentPosition.x && newPosition.y === ship.currentPosition.y) {
console.log(`Ship ${ship.name} has arrived at ${ship.destination.name}`);
ship.resetDestination();
await shipRepository.update(ship);
continue;
}
ship.currentPosition = newPosition;
await shipRepository.update(ship);
console.log(`Ship ${ship.name} moved to ${newPosition} (going to ${ship.destination.name})`);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A space game",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"start": "ts-node app.ts"
},
"author": "Clément Latzarus <[email protected]>",
"license": "ISC",
Expand Down

0 comments on commit c70072f

Please sign in to comment.