-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|