Skip to content

Commit

Permalink
feat: added 20 point and 30 point tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Дмитрий Никифоров committed Apr 24, 2022
1 parent 73df96e commit 38dbef8
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 9 deletions.
60 changes: 52 additions & 8 deletions cottage-cheese-response.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
const { pick } = require("ramda");
const { Quiz } = require("./quiz");
const { getState, removeState } = require("./state");

const prepareText = (originalText) => {
return originalText.toLowerCase().replaceAll(",", "").replaceAll(" ", "");
};

const getResponseText = (text) => {
if (
(text.indexOf("вездекод") >= 0 || text.indexOf("вездеход") >= 0) &&
text.indexOf("нетворогатворог") >= 0
) {
return "Привет вездекодерам!";
const getResponseText = (text, sessionId, originalText) => {
const state = getState(sessionId);
if (state[sessionId]?.quiz_start) {
if (text.indexOf("стоп") >= 0) {
if (state[sessionId].quiz) {
state[sessionId].quiz.destroy();
removeState(sessionId);
}
} else {
const rightAnswer =
state[sessionId].quiz.questions[
state[sessionId].quiz.currentQuestionId
].rightAnswer.toLowerCase();
if (rightAnswer === originalText.toLowerCase()) {
state[sessionId].quiz.rightAnswers++;
}
if (state[sessionId].quiz.currentQuestionId === 8) {
state[sessionId].quiz_start = false;
return `Опрос пройден! Правильных ответов: ${
state[sessionId].quiz.rightAnswers
}\n${state[sessionId].quiz.getRecomendations()}`;
} else {
state[sessionId].quiz.currentQuestionId++;
return `${state[sessionId].quiz.currentQuestionId}. ${state[
sessionId
].quiz.nextQuestion()}`;
}
}
} else {
if (
(text.indexOf("вездекод") >= 0 || text.indexOf("вездеход") >= 0) &&
text.indexOf("нетворогатворог") >= 0
) {
return "Привет вездекодерам!";
}
if (text.indexOf("пройтиопрос") >= 0) {
state[sessionId] = {
quiz: new Quiz(),
quiz_start: true,
};
return `Вам будет задано 8 вопросов!\nСкажите 'стоп' для выхода из опроса.\n1. ${state[
sessionId
].quiz.nextQuestion()}`;
}
}

return "Скажите 'Вездекод, команда не творог, а творог'";
return "Скажите 'Вездекод, команда не творог, а творог' или 'Пройти опрос'";
};

module.exports = ({ request, session, version }) => {
let text = prepareText(request.original_utterance);
let responseText = getResponseText(text);
let responseText = getResponseText(
text,
session.session_id,
request.original_utterance
);

return {
response: {
Expand Down
71 changes: 71 additions & 0 deletions quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Quiz {
currentQuestionId = 1;
rightAnswers = 0;
questions = {
1: {
question: "HTML - это язык программирования?",
rightAnswer: "Нет",
},
2: {
question: "Что быстрее python или java?",
rightAnswer: "python",
},
3: {
question: "Google создал Яндекс?",
rightAnswer: "Нет",
},
4: {
question: "Имя и фамилия человека, кто первым придумал концепцию интернета?",
rightAnswer: "Джозеф Ликлайдер",
},
5: {
question: "Нужны ли тестировщики?",
rightAnswer: "Да",
},
6: {
question: "Node это фреймворк python?",
rightAnswer: "Нет",
},
7: {
question: "JS это java?",
rightAnswer: "Нет",
},
8: {
question: "В каком году был представлен typescript?",
rightAnswer: "2012",
},
};
topics = [
"Gamedev",
"Java",
"Mobile",
"PHP",
"Back end",
"Маруся",
"Чат-боты",
"VK Mini Apps",
];

constructor() {}

nextQuestion() {
return this.questions[this.currentQuestionId].question;
}

getRecomendations() {
if (this.rightAnswers === 0) {
return "Рекомедуем Вам стоит подтянуть свои навыки, прежде чем учавствовать в вездекоде";
}
return `Рекомедуем Вам тему ${
this.topics[Math.round(Math.random() * (this.topics.length - 1))]
}`;
}

destroy() {
this.sessionId = "";
this.currentQuestionId = 1;
this.rightAnswers = 0;
}
}

module.exports = { Quiz };
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const createResponse = require("./cottage-cheese-response.js");
server
.use(bodyParser.json())
.post("/cottage-cheese", (req, res) => res.json(createResponse(req.body)))
.listen(process.env.PORT || 5000);
.listen(process.env.PORT || 5000);
21 changes: 21 additions & 0 deletions state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let skillState = new Map();

const getState = (sessionId) => {
let state = skillState.get(sessionId);
if (!state) {
state = {
[sessionId]: {},
};
skillState.set(sessionId, state);
}
return state;
};

const removeState = (sessionId) => {
skillState.delete(sessionId);
};

module.exports = {
getState,
removeState,
};

0 comments on commit 38dbef8

Please sign in to comment.