summaryrefslogtreecommitdiff
path: root/lambda-calcul/javascript/index.js
blob: d8b5e68e3c8167f20fe56aab93a7816145cf96e5 (plain)
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
const { createInterface } = require('readline/promises');

const LC = require('./src/client');

const playerName = process.env.PLAYER_NAME || 'A shy user';

const client = LC.newClient({
  playerName,
  challengerURL: process.env.CHALLENGER_URL,
});

let token;

const solveNextChallenge = (token) => client
  .nextChallenge(token)
  .then(challenge => console.log(`Next challenge: ${challenge}`) || challenge)
  .then(client.solve)
  .then(solution => console.log(`Posting solution: ${solution}`) || solution)
  .then(solution => client.postSolution(token, solution));

const waitForEnterToBePressed = () => {
    const readlineInterface = createInterface({
        input: process.stdin,
        output: process.stdout,
    });

    return readlineInterface.question(`Done. Press Enter to unregister ${playerName}.`)
      .then(() => readlineInterface.close());
};

client.start()
  .then(t => {
    token = t;
    console.log(`${playerName} has registered with token ${t}`);
  })
  .then(() => solveNextChallenge(token))
  .then(() => waitForEnterToBePressed())
  .then(() => client.unregister(token))
  .then(() => console.log(`${playerName} unregistered!`));