-
Notifications
You must be signed in to change notification settings - Fork 2
/
Commentator.ts
111 lines (91 loc) · 3.66 KB
/
Commentator.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { GameEvent } from './types/GameEvent';
import { Event } from './enums/Event';
import type Team from './Team';
import type Player from './Player';
const importantEvents = [
Event.GameStart,
Event.Kickoff,
Event.HalfTime,
Event.Advance,
Event.Save,
Event.Block,
Event.Goal,
Event.GameEnd,
Event.Injury,
];
export default class Commentator {
name: string;
constructor(name: string = 'Mr. Commentator') {
this.name = name;
}
routeComment(event: GameEvent): string | null {
switch (event.event) {
case Event.GameStart:
return this.gameStarted(event);
case Event.Kickoff:
return this.kickoff(event);
case Event.HalfTime:
return this.halfTime(event);
case Event.Advance:
return this.advance(event);
case Event.Defence:
return this.defence(event);
case Event.Save:
return this.save(event);
case Event.Block:
return this.block(event);
case Event.Goal:
return this.goal(event);
case Event.GameEnd:
return this.gameEnded(event);
default:
return null;
}
}
comment(event: GameEvent): string | null {
const importantEvent = event.event in importantEvents;
if (!importantEvent && Math.random() > 0.5) {
return null;
}
return this.routeComment(event);
}
gameStarted(event: GameEvent): string {
return `The game between ${event.homeTeam.name} and ${event.awayTeam.name} has started.`;
}
kickoff(event: GameEvent): string {
return `${(event.data as Team).name} with the kickoff.`;
}
halfTime(event: GameEvent): string {
return `It's half time! The score is ${event.gameInfo.homeGoals} - ${event.gameInfo.awayGoals}`;
}
advance(event: GameEvent): string {
return `${event.attackingTeam.name} advances with the ball.`;
}
defence(event: GameEvent): string {
return `${event.defendingTeam.name} tries to advance but good defence by ${event.attackingTeam.name} that steals the ball.`;
}
rebound(comment: string, event: GameEvent): string {
if (event.data === event.attackingTeam) {
return `${event.attackingTeam.name} gets the ball back.`;
}
return [comment, `${event.attackingTeam.name} can take control over the ball.`].join(' ');
}
save(event: GameEvent): string {
return this.rebound(`${(event.attackingPrimaryPlayer as Player).info.name} tries to score but the goalkeeper saves the ball.`, event);
}
block(event: GameEvent): string {
return this.rebound(`${(event.attackingPrimaryPlayer as Player).info.name} tries to score but the ball was blocked by the defence.`, event);
}
goal(event: GameEvent): string {
return `${(event.attackingPrimaryPlayer as Player).info.name} shoots and he scores! ${event.gameInfo.homeGoals}-${event.gameInfo.awayGoals}`;
}
gameEnded(event: GameEvent): string {
if (event.gameInfo.homeGoals > event.gameInfo.awayGoals) {
return `The game has ended! ${event.homeTeam.name} wins ${event.gameInfo.homeGoals}-${event.gameInfo.awayGoals}`;
}
if (event.gameInfo.homeGoals < event.gameInfo.awayGoals) {
return `The game has ended! ${event.awayTeam.name} takes 3 points on the road! ${event.gameInfo.homeGoals}-${event.gameInfo.awayGoals}`;
}
return `The game ends with a draw! Final score ${event.gameInfo.homeGoals}-${event.gameInfo.awayGoals}`;
}
}