import registry from 'registry'; const minutes = function(numberOfMinutes){ const oneSecond = 1000; const oneMinute = oneSecond*60; return oneMinute*numberOfMinutes; } export default class Json { #session = null; constructor(session){ this.#session = session; } async prompt(){ const avatar = this.#session.user.avatar; const username = this.#session.username; const response = { format: 'prompt', type: 'input', name: 'text', message: username, }; this.#session.socket.emit('screen', response) return response; } async data(data){ this.#session.socket.emit('screen', Object.assign(data,{format: 'data'})) return data; } async print(data){ const username = this.#session.username; const avatar = this.#session.user.avatar; const response = Object.assign({username, format:'print'},data); this.#session.socket.emit('screen', response) return response; } async input(data){ const {name, message, initial} = Object.assign({name:'value', message:'', initial:''}, data); const packet = { format: 'input', message, initial, name, // this is the name of the variable that will store the response, it should be value }; // return a promise return new Promise((resolve,reject)=>{ const timeoutId = setTimeout(()=>reject('Request timed out.'), minutes(15)) this.#session.socket.emit('screen', packet, function(response){ resolve(response); clearTimeout(timeoutId); }); }) } async select(data){ const {name, message, choices} = Object.assign({name:'value', message:'', choices:[]}, data); // NOTE: structure of choices:[ // {name:'I feel great!', value='true'}, // {name:'I don\'t feel so good.', value='false'}, // ] const packet = { format: 'select', message, choices, name, // this is the name of the variable that will store the response, it should be value }; // return a promise return new Promise((resolve,reject)=>{ const timeoutId = setTimeout(()=>reject('Request timed out.'), minutes(15)) this.#session.socket.emit('screen', packet, function(response){ resolve(response); clearTimeout(timeoutId); }); }) } }