1
+ const { Worker, isMainThread } = require ( 'worker_threads' ) ;
1
2
const readline = require ( 'readline-sync' ) ;
2
3
const gameController = require ( "./GameController/gameController.js" ) ;
3
4
const cliColor = require ( 'cli-color' ) ;
4
5
const beep = require ( 'beepbeep' ) ;
5
6
const position = require ( "./GameController/position.js" ) ;
6
7
const letters = require ( "./GameController/letters.js" ) ;
7
- const telemetryClient = require ( "./TelemetryClient/telemetryClient.js" ) ;
8
- const appInsights = require ( 'applicationinsights' ) ;
9
8
10
- let telemetry = new telemetryClient ( ) ;
9
+ let telemetryWorker = new Worker ( "./TelemetryClient/telemetryClient.js" ) ;
11
10
12
11
class Battleship {
13
- constructor ( ) {
14
- this . start = this . start . bind ( this ) ;
15
- this . printWelcomeScreen = this . printWelcomeScreen . bind ( this ) ;
16
- }
17
-
18
12
start ( ) {
19
13
console . log ( "Starting..." ) ;
20
- telemetry . trackevent ( "ApplicationStarted" , { Technolog : "Node.js" } , this . printWelcomeScreen ) ;
21
- } ;
14
+ telemetryWorker . postMessage ( { eventName : 'ApplicationStarted' , properties : { Technology : 'Node.js' } } ) ;
22
15
23
- printWelcomeScreen ( ) {
24
16
console . log ( cliColor . magenta ( " |__" ) ) ;
25
17
console . log ( cliColor . magenta ( " |\\/" ) ) ;
26
18
console . log ( cliColor . magenta ( " ---" ) ) ;
@@ -60,6 +52,8 @@ class Battleship {
60
52
var position = Battleship . ParsePosition ( readline . question ( ) ) ;
61
53
var isHit = gameController . CheckIsHit ( this . enemyFleet , position ) ;
62
54
55
+ telemetryWorker . postMessage ( { eventName : 'Player_ShootPosition' , properties : { Position : position . toString ( ) , IsHit : isHit } } ) ;
56
+
63
57
if ( isHit ) {
64
58
beep ( ) ;
65
59
@@ -77,6 +71,9 @@ class Battleship {
77
71
78
72
var computerPos = this . GetRandomPosition ( ) ;
79
73
var isHit = gameController . CheckIsHit ( this . myFleet , computerPos ) ;
74
+
75
+ telemetryWorker . postMessage ( { eventName : 'Computer_ShootPosition' , properties : { Position : computerPos . toString ( ) , IsHit : isHit } } ) ;
76
+
80
77
console . log ( ) ;
81
78
console . log ( `Computer shot in ${ computerPos . column } ${ computerPos . row } and ` + ( isHit ? `has hit your ship !` : `miss` ) ) ;
82
79
if ( isHit ) {
@@ -127,6 +124,7 @@ class Battleship {
127
124
for ( var i = 1 ; i < ship . size + 1 ; i ++ ) {
128
125
console . log ( `Enter position ${ i } of ${ ship . size } (i.e A3):` ) ;
129
126
const position = readline . question ( ) ;
127
+ telemetryWorker . postMessage ( { eventName : 'Player_PlaceShipPosition' , properties : { Position : position , Ship : ship . name , PositionInShip : i } } ) ;
130
128
ship . addPosition ( Battleship . ParsePosition ( position ) ) ;
131
129
}
132
130
} )
0 commit comments