-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
80 lines (73 loc) · 2.58 KB
/
bot.js
File metadata and controls
80 lines (73 loc) · 2.58 KB
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
import { BotActions } from "./communication/bot-actions.js";
import { CommunicationManager } from "./communication/communication-manager.js";
import { BotConfiguration } from "./config/bot-configuration.js";
export class Bot{
/**
* Identifiant du bot
* @type {string}
* @memberof Bot
*/
id;
/**
* configuration du bot
* @type {BotConfiguration}
* @memberof Bot
*/
config;
/**
* communication manager
* @type {CommunicationManager}
* @memberof Bot
*/
manager;
/**
* liste des actions pour le bot
* @type {BotActions}
* @memberof Bot
*/
action;
/**
* Initialisation du bot
* @param {CommunicationManager} manager gestionnaire de communication avec le serveur
*/
init(manager){
if(!this.id){
throw "Bot id must be defined";
}
this.manager = manager;
this.action = new BotActions(manager, this.id);
}
/**
*
* @param {Function} statusListener ecoute du status, la fonction est du type (messageType : string, messageValue : any) : void
* @param {Function} scannerListener ecoute du status, la fonction est du type (items : array)
* @param {Function} onClose méthode appelée à la fin du jeux
*/
startWatchers(statusListener, scannerListener, onClose){
new Promise((resolve,reject) => {
let stompClient = this.manager.stompConnectAndListen(this.id, (message) => {
console.debug("status message : " + JSON.stringify(message));
console.info(message.msg_type);
if(message.msg_type === "game_status" && !message.data.value){
onClose();
console.info("######### Fin de partie #############");
//on arrete l'écoute
this.manager.closeStompConnexion(stompClient);
this.manager.closeMqttConnexion(mqttClient);
resolve();
}else{
//on déclenche l'appel
statusListener(message.msg_type, message.data.value);
}
});
let mqttClient = this.manager.mqttConnectAndListen(this.id, message => {
if(message.msg_type === "object_detection"){
console.debug("scanner message : " + JSON.stringify(message));
scannerListener(message.data);
}else{
console.warn("unknown type scanner message : " + JSON.stringify(message));
}
});
});
}
}