-
Notifications
You must be signed in to change notification settings - Fork 39
/
engine.js
87 lines (83 loc) · 3.44 KB
/
engine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//------------------------
// NPM Modules
//------------------------
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')
//------------------------
// Functions
//------------------------
async function initiateGame(team1, team2, pitchDetails) {
validate.validateArguments(team1, team2, pitchDetails)
validate.validateTeam(team1)
validate.validateTeam(team2)
validate.validatePitch(pitchDetails)
let matchDetails = setVariables.populateMatchDetails(team1, team2, pitchDetails)
let kickOffTeam = setVariables.setGameVariables(matchDetails.kickOffTeam)
let secondTeam = setVariables.setGameVariables(matchDetails.secondTeam)
kickOffTeam = setVariables.koDecider(kickOffTeam, matchDetails)
matchDetails.iterationLog.push(`Team to kick off - ${kickOffTeam.name}`)
matchDetails.iterationLog.push(`Second team - ${secondTeam.name}`)
setPositions.switchSide(matchDetails, secondTeam)
matchDetails.kickOffTeam = kickOffTeam
matchDetails.secondTeam = secondTeam
return matchDetails
}
async function playIteration(matchDetails) {
let closestPlayerA = {
'name': '',
'position': 100000
}
let closestPlayerB = {
'name': '',
'position': 100000
}
validate.validateMatchDetails(matchDetails)
validate.validateTeamSecondHalf(matchDetails.kickOffTeam)
validate.validateTeamSecondHalf(matchDetails.secondTeam)
validate.validatePlayerPositions(matchDetails)
matchDetails.iterationLog = []
let { kickOffTeam, secondTeam } = matchDetails
common.matchInjury(matchDetails, kickOffTeam)
common.matchInjury(matchDetails, secondTeam)
matchDetails = ballMovement.moveBall(matchDetails)
if (matchDetails.endIteration == true) {
delete matchDetails.endIteration
return matchDetails
}
playerMovement.closestPlayerToBall(closestPlayerA, kickOffTeam, matchDetails)
playerMovement.closestPlayerToBall(closestPlayerB, secondTeam, matchDetails)
kickOffTeam = playerMovement.decideMovement(closestPlayerA, kickOffTeam, secondTeam, matchDetails)
secondTeam = playerMovement.decideMovement(closestPlayerB, secondTeam, kickOffTeam, matchDetails)
matchDetails.kickOffTeam = kickOffTeam
matchDetails.secondTeam = secondTeam
if (matchDetails.ball.ballOverIterations.length == 0 || matchDetails.ball.withTeam != '') {
playerMovement.checkOffside(kickOffTeam, secondTeam, matchDetails)
}
return matchDetails
}
async function startSecondHalf(matchDetails) {
validate.validateMatchDetails(matchDetails)
validate.validateTeamSecondHalf(matchDetails.kickOffTeam)
validate.validateTeamSecondHalf(matchDetails.secondTeam)
validate.validatePlayerPositions(matchDetails)
let { kickOffTeam, secondTeam } = matchDetails
setPositions.switchSide(matchDetails, kickOffTeam)
setPositions.switchSide(matchDetails, secondTeam)
common.removeBallFromAllPlayers(matchDetails)
setVariables.resetPlayerPositions(matchDetails)
setPositions.setBallSpecificGoalScoreValue(matchDetails, matchDetails.secondTeam)
matchDetails.iterationLog = [`Second Half Started: ${matchDetails.secondTeam.name} to kick offs`]
matchDetails.kickOffTeam.intent = `defend`
matchDetails.secondTeam.intent = `attack`
matchDetails.half++
return matchDetails
}
module.exports = {
initiateGame,
playIteration,
startSecondHalf
}