const readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let State = Object.freeze({
offHook: '拿起座机手柄',
connecting: '连接中',
connected: '已连接',
onHook: '放下座机手柄'
});
let Trigger = Object.freeze({
callDialed: '拔号',
hungUp: '挂断电话',
callConnected: '打通电话',
leftMessage: '留言'
});
let rules = {};
rules[State.offHook] = [
{
trigger: Trigger.callDialed,
state: State.connecting
}
];
rules[State.connecting] = [
{
trigger: Trigger.hungUp,
state: State.onHook
},
{
trigger: Trigger.callConnected,
state: State.connected
}
];
rules[State.connected] = [
{
trigger: Trigger.leftMessage,
state: State.onHook
},
{
trigger: Trigger.hungUp,
state: State.onHook
}
];
let state = State.offHook;
let exitState = State.onHook;
let getInput = function () {
let prompt = [
`当前电话正处于${state}`,
`下一步:`
]
for (let i = 0; i < rules[state].length; i++) {
let trigger = rules[state][i].trigger;
prompt.push(`${i}, ${trigger}`);
}
prompt.push('');
rl.question(prompt.join('\n'), function (answer) {
let input = parseInt(answer);
state = rules[state][input].state;
if (state !== exitState) {
getInput();
} else {
console.log(`我们使用完了座机电话`);
rl.close();
}
})
}
getInput();