Skip to content

Commit

Permalink
first set of freekick data
Browse files Browse the repository at this point in the history
  • Loading branch information
AidenGallagher committed May 25, 2019
1 parent 9d9b90f commit 752440e
Show file tree
Hide file tree
Showing 6 changed files with 1,240 additions and 5 deletions.
159 changes: 156 additions & 3 deletions lib/setFreekicks.js
@@ -1,8 +1,150 @@
const common = require(`../lib/common`)
const setPos = require(`../lib/setPositions`)

function setTopFreekick(matchDetails) {
removeBallFromAllPlayers(matchDetails)
let { kickOffTeam, secondTeam } = matchDetails
let [pitchWidth, pitchHeight] = matchDetails.pitchSize
let [ballX, ballY] = matchDetails.ball.position
let attack = (kickOffTeam.players[0].originPOS[1] < pitchHeight / 2) ? kickOffTeam : secondTeam
let defence = (kickOffTeam.players[0].originPOS[1] < pitchHeight / 2) ? secondTeam : kickOffTeam
let hundredToHalfway = common.isBetween(ballY, 100, (pitchHeight / 2) + 1)
let halfwayToLastQuarter = common.isBetween(ballY, pitchHeight / 2, pitchHeight - (pitchHeight / 4))
let penaltyXspan = common.isBetween(ballX, (pitchWidth / 4) + 5, (pitchWidth - (pitchWidth / 4) - 5))
let leftOfPenaltyXspan = common.isBetween(ballX, 0, (pitchWidth / 4) + 5)
let rightOfPenaltyXspan = common.isBetween(ballX, (pitchWidth - (pitchWidth / 4) - 5), pitchWidth)
let upperFinalQuarter = common.isBetween(ballY, pitchHeight - (pitchHeight / 5), pitchHeight - (pitchHeight / 6) - 5)
let lowerFinalQuarter = common.isBetween(ballY, pitchHeight - (pitchHeight / 6) - 5, pitchHeight)

if (ballY < 101) return setTopOneHundredYPos(matchDetails, attack, defence)
if (hundredToHalfway) return setTopOneHundredToHalfwayYPos(matchDetails, attack, defence)
if (halfwayToLastQuarter) return setTopHalfwayToBottomQuarterYPos(matchDetails, attack, defence)
if (penaltyXspan && upperFinalQuarter) return setTopBottomQuarterCentreYPos(matchDetails, attack, defence)
if (leftOfPenaltyXspan && upperFinalQuarter) return setTopUpperFinalQuarterLeftPos(matchDetails, attack, defence)
if (leftOfPenaltyXspan && lowerFinalQuarter) return setTopLowerFinalQuarterBylineLeftPos(matchDetails, attack, defence)
if (rightOfPenaltyXspan && upperFinalQuarter) return setTopUpperFinalQuarterRightPos(matchDetails, attack, defence)
if (rightOfPenaltyXspan && lowerFinalQuarter) return setTopLowerFinalQuarterBylineRightPos(matchDetails, attack, defence)
}

function setBottomFreekick(matchDetails) {
return matchDetails
}

function setTopOneHundredYPos(matchDetails, attack, defence) {
attack.players[0].hasBall = true
let { ball } = matchDetails
ball.Player = attack.players[0].name
ball.withTeam = attack.name
ball.direction = 'south'
for (let player of attack.players) {
if (player.position == 'GK') player.startPOS = matchDetails.ball.position.map(x => x)
if (player.position != 'GK') player.startPOS = player.originPOS.map(x => x)
}
for (let player of defence.players) {
if (player.position == 'GK') player.startPOS = player.originPOS.map(x => x)
if (player.position != 'GK') player.startPOS = [player.originPOS[0], player.originPOS[1] - 100]
}
return matchDetails
}

function setTopOneHundredToHalfwayYPos(matchDetails, attack, defence) {
let { ball } = matchDetails
let [, pitchHeight] = matchDetails.pitchSize
let goalieToKick = common.isBetween(ball.position[1], 0, (pitchHeight * 0.25) + 1)
let kickPlayer = (goalieToKick) ? attack.players[0] : attack.players[3]
kickPlayer.hasBall = true
ball.Player = kickPlayer.name
ball.withTeam = attack.name
ball.direction = 'south'
for (let player of attack.players) {
if (kickPlayer.position == 'GK') {
if (player.position == 'GK') player.startPOS = ball.position.map(x => x)
if (player.name != kickPlayer.name) player.startPOS = [player.originPOS[0], player.originPOS[1] + 300]
} else {
let newYPOS = player.originPOS[1] + (ball.position[1] - player.originPOS[1]) + 300
if (player.name == kickPlayer.name) player.startPOS = ball.position.map(x => x)
else if (player.position == 'GK') {
let maxYPOSCheck = parseInt(common.upToMax(newYPOS, pitchHeight * 0.25), 10)
player.startPOS = [player.originPOS[0], maxYPOSCheck]
} else if (['CB', 'LB', 'RB'].includes(player.position)) {
let maxYPOSCheck = parseInt(common.upToMax(newYPOS, pitchHeight * 0.5), 10)
player.startPOS = [player.originPOS[0], maxYPOSCheck]
} else if (['CM', 'LM', 'RM'].includes(player.position)) {
let maxYPOSCheck = parseInt(common.upToMax(newYPOS, pitchHeight * 0.75), 10)
player.startPOS = [player.originPOS[0], maxYPOSCheck]
} else {
let maxYPOSCheck = parseInt(common.upToMax(newYPOS, pitchHeight * 0.9), 10)
player.startPOS = [player.originPOS[0], maxYPOSCheck]
}
}
}
for (let player of defence.players) {
if (kickPlayer.position == 'GK') {
if (player.position == 'GK') player.startPOS = player.originPOS.map(x => x)
if (player.position != 'GK') player.startPOS = [player.originPOS[0], player.originPOS[1] - 100]
} else {
if (['GK', 'CB', 'LB', 'RB'].includes(player.position)) player.startPOS = player.originPOS.map(x => x)
else if (['CM', 'LM', 'RM'].includes(player.position)) {
player.startPOS = [player.originPOS[0], parseInt((pitchHeight * 0.75) + 5, 10)]
} else {
player.startPOS = [player.originPOS[0], parseInt(pitchHeight * 0.5, 10)]
}
}
}
return matchDetails
}

function setTopHalfwayToBottomQuarterYPos(matchDetails, attack, defence) {
let { ball } = matchDetails
let [pitchWidth, pitchHeight] = matchDetails.pitchSize
let kickPlayer = attack.players[5]
kickPlayer.hasBall = true
ball.Player = kickPlayer.name
ball.withTeam = attack.name
let ballInCentre = common.isBetween(ball.position[0], (pitchWidth / 4) + 5, (pitchWidth - (pitchWidth / 4) - 5))
let ballLeft = common.isBetween(ball.position[0], 0, (pitchWidth / 4) + 4)
ball.direction = (ballInCentre) ? 'south' : (ballLeft) ? 'southeast' : 'southwest'
kickPlayer.startPOS = ball.position.map(x => x)
for (let player of attack.players) {
if (player.position == 'GK') player.startPOS = [player.originPOS[0], parseInt(pitchHeight * 0.25, 10)]
else if (['CB', 'LB', 'RB'].includes(player.position)) {
let maxYPOSCheck = parseInt(common.upToMax(ball.position[1] - 100, pitchHeight * 0.5), 10)
player.startPOS = [player.originPOS[0], maxYPOSCheck]
} else if (['CM', 'LM', 'RM'].includes(player.position)) {
let maxYPOSCheck = common.upToMax(ball.position[1] + common.getRandomNumber(150, 300), pitchHeight * 0.75)
if (player.name != kickPlayer.name) player.startPOS = [player.originPOS[0], parseInt(maxYPOSCheck, 10)]
} else {
let maxYPOSCheck = common.upToMax(ball.position[1] + common.getRandomNumber(300, 400), pitchHeight * 0.9)
player.startPOS = [player.originPOS[0], parseInt(maxYPOSCheck, 10)]
}
}
for (let player of defence.players) {
if (['GK', 'CB', 'LB', 'RB'].includes(player.position)) {
player.startPOS = player.originPOS.map(x => x)
} else if (['CM', 'LM', 'RM'].includes(player.position)) {
player.startPOS = [player.originPOS[0], parseInt(pitchHeight * 0.75, 10)]
} else {
player.startPOS = [player.originPOS[0], parseInt(pitchHeight * 0.5, 10)]
}
}
return matchDetails
}


function setTopBottomQuarterCentreYPos(matchDetails, attack, defence) { return matchDetails }
function setTopUpperFinalQuarterLeftPos(matchDetails, attack, defence) { return matchDetails }
function setTopLowerFinalQuarterBylineLeftPos(matchDetails, attack, defence) { return matchDetails }
function setTopUpperFinalQuarterRightPos(matchDetails, attack, defence) { return matchDetails }
function setTopLowerFinalQuarterBylineRightPos(matchDetails, attack, defence) { return matchDetails }



//OLD STUFF
//
//
//

function setFreekick(ballPosition, team, opposition, side, matchDetails) {
setPos.removeBallFromAllPlayers(matchDetails)
removeBallFromAllPlayers(matchDetails)
const [matchWidth, matchHeight] = matchDetails.pitchSize
let tempArray = ballPosition
team.players[5].startPOS = tempArray.map(x => x)
Expand Down Expand Up @@ -354,6 +496,17 @@ function setFreekick(ballPosition, team, opposition, side, matchDetails) {
}
}

function removeBallFromAllPlayers(matchDetails) {
for (let player of matchDetails.kickOffTeam.players) {
player.hasBall = false
}
for (let player of matchDetails.secondTeam.players) {
player.hasBall = false
}
}

module.exports = {
setFreekick
setFreekick,
setTopFreekick,
setBottomFreekick
}
4 changes: 2 additions & 2 deletions lib/setPositions.js
Expand Up @@ -277,7 +277,7 @@ function closestPlayerToPosition(player, team, position) {
function setSetpieceKickOffTeam(matchDetails) {
const [matchWidth, matchHeight] = matchDetails.pitchSize
let ballPosition = matchDetails.ball.position.map(x => x)
let ballInPenalyBoxX = common.isBetween(ballPosition[0], (matchWidth / 4) - 5, matchWidth - (matchWidth / 4) + 5)
let ballInPenalyBoxX = common.isBetween(ballPosition[0], (matchWidth / 4) + 5, matchWidth - (matchWidth / 4) - 5)
let ballInTopPenalyBoxY = common.isBetween(ballPosition[1], 0, (matchHeight / 6) - 5)
let ballInBottomPenalyBoxY = common.isBetween(ballPosition[1], matchHeight - (matchHeight / 6) + 5, matchHeight)
let attackingTowardsTop = (matchDetails.kickOffTeam.players[0].startPOS[1] > matchHeight / 2)
Expand All @@ -298,7 +298,7 @@ function setSetpieceKickOffTeam(matchDetails) {
function setSetpieceSecondTeam(matchDetails) {
const [matchWidth, matchHeight] = matchDetails.pitchSize
let ballPosition = matchDetails.ball.position.map(x => x)
let ballInPenalyBoxX = common.isBetween(ballPosition[0], (matchWidth / 4) - 5, matchWidth - (matchWidth / 4) + 5)
let ballInPenalyBoxX = common.isBetween(ballPosition[0], (matchWidth / 4) + 5, matchWidth - (matchWidth / 4) - 5)
let ballInBottomPenalyBoxY = common.isBetween(ballPosition[1], matchHeight - (matchHeight / 6) + 5, matchHeight)
let ballInTopPenalyBoxY = common.isBetween(ballPosition[1], 0, (matchHeight / 6) - 5)
let attackingTowardsTop = (matchDetails.secondTeam.players[0].startPOS[1] > matchHeight / 2)
Expand Down
24 changes: 24 additions & 0 deletions test/lib/set_freekicks.js
@@ -0,0 +1,24 @@
const common = require('../../lib/common')
const setFreekick = require('../../lib/setFreekicks')

async function setTopFreekick(iterationFile, ballPosition) {
let matchDetails = await common.readFile(iterationFile)
.catch(function(err) {
throw err.stack
})
matchDetails.ball.position = ballPosition.map(x => x)
return setFreekick.setTopFreekick(matchDetails)
}
async function setBottomFreekick(iterationFile, ballPosition) {
let matchDetails = await common.readFile(iterationFile)
.catch(function(err) {
throw err.stack
})
matchDetails.ball.position = ballPosition.map(x => x)
return setFreekick.setBottomFreekick(matchDetails)
}

module.exports = {
setTopFreekick,
setBottomFreekick
}

0 comments on commit 752440e

Please sign in to comment.