Skip to content

Commit

Permalink
implement logic input validation b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 28, 2024
1 parent 194e7f3 commit 1dd3ef1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion staff/marti-herms/project/dodge/logic/checkOutOfBounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import data from '../data'
import { SCREEN_WIDTH, SCREEN_HEIGHT } from '../util/constants'

export default (id) => {
//TODO validate id
if (typeof id !== 'number') throw new Error('invalid id')

const obstacle = data.obstacles.items.find(obstacle => obstacle.id === id)

return (obstacle.top >= SCREEN_HEIGHT - obstacle.height - 10 || obstacle.top < 0 || obstacle.left >= SCREEN_WIDTH - obstacle.width - 10 || obstacle.left < 0)
Expand Down
3 changes: 2 additions & 1 deletion staff/marti-herms/project/dodge/logic/createObstacle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import data from '../data'

export default (id) => {
//TODO validate id
if (typeof id !== 'number') throw new Error('invalid id')

data.obstacles.addObstacle(id)
}
3 changes: 2 additions & 1 deletion staff/marti-herms/project/dodge/logic/removeObstacle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import data from '../data'

export default (id) => {
//TODO validate id
if (typeof id !== 'number') throw new Error('invalid id')

data.obstacles.removeObstacle(id)
}
3 changes: 2 additions & 1 deletion staff/marti-herms/project/dodge/logic/setPoints.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import data from '../data'

export default (seconds) => {
// TODO validate seconds
if (typeof id !== 'number') throw new Error('invalid id')

if (seconds === 0)
data.player.item.points = 0
else if (seconds < 20)
Expand Down
8 changes: 6 additions & 2 deletions staff/marti-herms/project/dodge/src/game/Obstacle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import logic from '../../logic/index.js'

export default function Obstacle({ player, obstacle, onOutOfBounds, setEnd }) {
useEffect(() => {
if (logic.checkOutOfBounds(obstacle.id)) {
onOutOfBounds(obstacle.id)
try {
if (logic.checkOutOfBounds(obstacle.id)) {
onOutOfBounds(obstacle.id)
}
} catch (error) {
console.error(error)
}
}, [obstacle.top, obstacle.left])

Expand Down
20 changes: 14 additions & 6 deletions staff/marti-herms/project/dodge/src/game/Obstacles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export default function Obstacles({ player, pause, end, setEnd }) {

const handleSpawn = () => {
setSpawnIntervalId(setInterval(() => {
logic.createObstacle(obstacleCount++)
try {
logic.createObstacle(obstacleCount++)

const items = logic.getObstacles()
const items = logic.getObstacles()

setObstacles(() => ([...items]))
setObstacles(() => ([...items]))
} catch (error) {
console.error(error)
}
}, SPAWN_RATE))
}

Expand All @@ -52,11 +56,15 @@ export default function Obstacles({ player, pause, end, setEnd }) {
}

const handleOutOfBounds = (id) => {
logic.removeObstacle(id)
try {
logic.removeObstacle(id)

const items = logic.getObstacles()
const items = logic.getObstacles()

setObstacles(() => [...items])
setObstacles(() => [...items])
} catch (error) {
console.error(error)
}
}

const clearField = () => {
Expand Down

0 comments on commit 1dd3ef1

Please sign in to comment.