A node js football simulation module
This module was designed to allow the simulation of football (soccer) matches between two teams. This module allows for an iterative football match to be played out given initial "state" of players.
The module consists of three functions that:
For examples of how this module can be used, see:
Make sure your version of Node.js is at least 7.6.0. (The 'async/await' function is used)
npm install --save footballsimulationengine
This function is a promise that expects two teams and pitch information in JSON format (JSON format is described below). The function will return match details including player start positions, match statistics, ball position and an iteration log.
initiateGame(team1, team2, pitch).then(function(matchDetails){
console.log(matchDetails);
}).catch(function(error){
console.error("Error: ", error);
})
This function is a promise that expects a single JSON input. This input will match the output of the initiate game function and will make the next move of both teams, resolving the final position of each player.
playIteration(matchDetails).then(function (matchDetails) {
console.log(matchDetails);
}).catch(function(error){
console.error("Error: ", error);
}
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.
startSecondHalf(matchDetails).then(function (matchDetails) {
console.log(matchDetails);
}).catch(function(error){
console.error("Error: ", error);
}
init_config
is the data used for testingExamples are baked into the file system (>v2.1.0) in the init_config
directory:
index.js
: example function usagesteam1.json
: example json for a primary teamteam2.json
: example json for a secondary teampitch.json
: example json for pitch detailsiteration.json
: shows what the overall output given for each iterationEach team must have the following information and contain 11 players.
{
"name": "Team1",
"players": [{
"name": "Player",
"position": "GK",
"rating": "99",
"skill": {
"passing": "99",
"shooting": "99",
"tackling": "99",
"saving": "99",
"agility": "99",
"strength": "99",
"penalty_taking": "99",
"jumping": "300"
},
"currentPOS": [60,0],
"fitness": 100,
"injured": false
}...],
"manager": "Aiden"
}
Pitch has been tested for width of 120 - 680 and height of 600 - 1050 and a goal width of 90. The below is the current provided pitch size.
{
"pitchWidth": 680,
"pitchHeight": 1050,
goalWidth: 90
}
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',
players:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
manager: 'Aiden'
intent: 'defend' },
secondTeam:
{ name: 'Team2',
players:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
manager: 'Joe',
intent: 'attack' },
pitchSize: [ 120, 600 ],
ball:
{ position: [ 76, 314, 0 ],
withPlayer: true,
Player: 'Joe Bloggs',
withTeam: 'Team2',
direction: 'south' },
lastTouch: {
playerName: 'Peter Johnson',
playerID: 78883930303030109,
teamID: 72464187147564590
},
ballOverIterations: []
half: 1,
kickOffTeamStatistics:
{ goals: 0,
shots: 0,
corners: 0,
freekicks: 0,
penalties: 0,
fouls: 0 },
secondTeamStatistics:
{ goals: 0,
shots: 0,
corners: 0,
freekicks: 0,
penalties: 0,
fouls: 0 },
iterationLog:
[ 'Closest Player to ball: Aiden Gallagher',
'Closest Player to ball: Joe Bloggs' ] }
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' },
currentPOS: [ 60, 300 ],
fitness: 100,
injured: false,
originPOS: [ 70, 270 ],
intentPOS: [ 70, 270 ],
action: 'none',
offside: false,
cards: {
yellow: 0,
red: 0
}
hasBall: true }