Skip to content

Commit

Permalink
allow players to be given specific actions
Browse files Browse the repository at this point in the history
Users can now supply player actions to overwrite simulated actions.
This must be provided in the players json. Starting a game will add the
‘action’ object which should be set to ‘none’ if no action is taken, or
a valid action if not.
Updated read me to reflect the changes
  • Loading branch information
AidenGallagher committed Sep 9, 2018
1 parent 6bc767a commit 8512f1a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
2 changes: 2 additions & 0 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const common = require('./lib/common')
const setPositions = require('./lib/setPositions')
const setVariables = require('./lib/setVariables')
const playerMovement = require('./lib/playerMovement')
const ballMovement = require('./lib/ballMovement')
const validate = require('./lib/validate')

//------------------------
Expand Down Expand Up @@ -79,6 +80,7 @@ function playIteration(matchDetails) {
playerMovement.decideMovement(closestPlayerB, secondTeam, kickOffTeam, matchDetails).then(function (secondTeam) {
matchDetails.kickOffTeam = kickOffTeam
matchDetails.secondTeam = secondTeam
// console.log(matchDetails)
resolve(matchDetails)
}).catch(function (error) {
console.error('Error: ', error)
Expand Down
55 changes: 40 additions & 15 deletions lib/playerMovement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,48 @@ function decideMovement(closestPlayer, team, opposition, matchDetails) {
var ballToPlayerY = thisPlayer.startPOS[1] - matchDetails.ball.position[1]
actions.findPossibleActions(thisPlayer, opposition, ballToPlayerX, ballToPlayerY, matchDetails).then(function (possibleActions) {
actions.selectAction(possibleActions).then(function (action) {
if (matchDetails.ball.withTeam !== team.name) {
if (closestPlayer.name === thisPlayer.name) {
if (action !== 'tackle' && action !== 'slide' && action !== 'intercept') {
action = 'sprint'
var providedAction = (thisPlayer.action) ? thisPlayer.action : 'unassigned'
const allActions = ['shoot', 'throughBall', 'pass', 'cross', 'tackle', 'intercept', 'slide', 'run', 'sprint', 'cleared', 'boot']
if (allActions.includes(providedAction)) {
if (thisPlayer.name !== matchDetails.ball.Player) {
if (providedAction === 'shoot' || providedAction === 'throughBall' || providedAction === 'pass' || providedAction === 'cross' || providedAction === 'cleared' || providedAction === 'boot') {
console.error('Player: ', thisPlayer.name, ' does not have the ball so cannot ', providedAction, ' player action now to run')
action = 'run'
} else {
action = providedAction
}
if (common.isBetween(ballToPlayerX, -30, 30) === false) {
if (ballToPlayerX > 29) {
ballToPlayerX = 29
} else {
ballToPlayerX = -29
}
} else {
if (providedAction === 'tackle' || providedAction === 'slide' || providedAction === 'intercept') {
console.error('Player: ', thisPlayer.name, ' has the ball so cannot ', providedAction, ' player action now to run')
const actions = ['shoot', 'throughBall', 'pass', 'cross', 'cleared', 'boot']
action = actions[common.getRandomNumber(0, 5)]
} else {
action = providedAction
}
if (common.isBetween(ballToPlayerY, -30, 30) === false) {
if (ballToPlayerY > 29) {
ballToPlayerY = 29
} else {
ballToPlayerY = -29
}
} else {
if (thisPlayer.action !== 'none') {
console.error('Could not read player action for ', thisPlayer.name, 'please provide a valid action')
} else {
if (matchDetails.ball.withTeam !== team.name) {
if (closestPlayer.name === thisPlayer.name) {
if (action !== 'tackle' && action !== 'slide' && action !== 'intercept') {
action = 'sprint'
}
if (common.isBetween(ballToPlayerX, -30, 30) === false) {
if (ballToPlayerX > 29) {
ballToPlayerX = 29
} else {
ballToPlayerX = -29
}
}
if (common.isBetween(ballToPlayerY, -30, 30) === false) {
if (ballToPlayerY > 29) {
ballToPlayerY = 29
} else {
ballToPlayerY = -29
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/setVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function setGameVariables(team) {
team.players.forEach(player => {
player.originPOS = player.startPOS.slice()
player.relativePOS = player.startPOS.slice()
player.action = "none"
player.hasBall = false
})

Expand Down
27 changes: 26 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ playIteration(matchDetails).then(function (matchDetails) {
}
```
#### Example Match Details
v2.1.0 - ball movement added so that a kicked ball makes movements over time. This can be seen in 'ball.ballOverIterations'. If empty, no new ball movements will occur. Deflections may occur as players move over iterations.
v2.1.0 - ball movement added so that a kicked ball makes movements over time. This can be seen in 'ball.ballOverIterations'. If empty, no new ball movements will occur. Deflections may occur as players move over iterations.
```
{ kickOffTeam:
{ name: 'Team1',
Expand Down Expand Up @@ -138,6 +138,31 @@ v2.1.0 - ball movement added so that a kicked ball makes movements over time. Th
[ 'Closest Player to ball: Aiden Gallagher',
'Closest Player to ball: Joe Bloggs' ] }
```
Example Player JSON:
Any and all player objects may be altered between iterations. Including the relative position, origin position and action.
Action should be - 'null' if the simulation is to be run normally. This can be overriden with any of the following actions:
'shoot', 'throughBall', 'pass', 'cross', 'tackle', 'intercept', 'slide', 'run', 'sprint', 'cleared', 'boot'. The player must have the ball in order to complete ball specific actions like 'shoot'. Any invalid actions will result in the simulation running as normal.
```
{ name: 'Louise Johnson',
position: 'ST',
rating: '88',
skill:
{ passing: '20',
shooting: '20',
tackling: '20',
saving: '20',
agility: '20',
strength: '20',
penalty_taking: '20',
jumping: '280' },
startPOS: [ 60, 300 ],
fitness: 100,
injured: false,
originPOS: [ 70, 270 ],
relativePOS: [ 70, 270 ],
action: 'none',
hasBall: true }
```
---
## Start Second Half (Switch Sides)
This function is a promise that switches the side of each team, as happens typically at the end of a half. This uses the output from either an initiate game or a play iteration.
Expand Down

0 comments on commit 8512f1a

Please sign in to comment.