<commit_before>/**
 * Created by kimown on 16-9-14.
 */


'use strict';

let {request,rmFileSync}= require('./../util');
let logger = require('./../logger');
let loggerfile = require('./../loggerfile');
let {writeUserData,readUserData}=require('./common');
let configPath=require('./common/config-path');


process.on('uncaughtException', (err) => {
    console.log(err);
});

process.on('exit',(code) => {
    //如果程序异常退出且 totalWordCount 没有达到 一共要猜测的单词数量 ,保存用户数据
    // saveUserDataSync();
    console.log(`程序退出了About to exit with code: ${code}`);
});

let CONFIG={};


async function main() {

    //里面保存了所有的用户数据．
    CONFIG= await readUserData();

    if(CONFIG.hasOwnProperty('sessionId')&&CONFIG.sessionId){
        //断线重玩游戏
        await makeAGuess();
    }else{
        //new 一个game
        await startGame();
    }
}


/**
 * 1. Start Game
 * https://github.com/strikingly/strikingly-interview-test-instructions#1-start-game
 */
async function startGame() {
    let postData = {
        playerId: CONFIG.playerId,
        action: CONFIG.action.startGame
    };
    let res = await request({
        method: 'post',
        body: postData,
        json: true,
        url: CONFIG.requestUrl
    });
    let {body, ok}=res;
    if (ok == true) {
        let {data, message, sessionId}=body;
        let {numberOfGuessAllowedForEachWord, numberOfWordsToGuess}=data;
        logger.info(`message: ${message} ,您的身份标示是 ${sessionId} ,您一共要猜测 ${numberOfWordsToGuess} 个单词,每个单词限制猜测 ${numberOfGuessAllowedForEachWord} 次`);

        CONFIG.sessionId = sessionId;
        CONFIG.numberOfGuessAllowedForEachWord = numberOfGuessAllowedForEachWord;
        CONFIG.numberOfWordsToGuess = numberOfWordsToGuess;

    } else {
        logger.error(`启动游戏失败!!!失败原因:`, res.body);
    }

    await GiveMeAWord();

}


/**
 *　2. Give Me A Word
 * https://github.com/strikingly/strikingly-interview-test-instructions#2-give-me-a-word
 */
async function GiveMeAWord() {

    await checkArriveLimitWords();

    let postData = {
        sessionId: CONFIG.sessionId,
        action: CONFIG.action.nextWord
    };
    let res = await request({
        method: 'post',
        body: postData,
        json: true,
        url: CONFIG.requestUrl
    });
    let {body, ok}=res;
    if (ok == true) {
        let {data}=body;
        let {totalWordCount, word, wrongGuessCountOfCurrentWord}=data;
        CONFIG.currentGuessWord.totalWordCount = totalWordCount;
        CONFIG.currentGuessWord.word = word;
        CONFIG.currentGuessWord.wrongGuessCountOfCurrentWord = wrongGuessCountOfCurrentWord;
        await makeAGuess(CONFIG);
    } else {
        logger.error(`获取猜谜单词失败!!!失败原因:`, res.body);
    }
}


/**
 * 3.Make A Guess
 * https://github.com/strikingly/strikingly-interview-test-instructions#3-make-a-guess
 * 猜测当前的单词
 */
async function makeAGuess() {

    let isNeedNextWord=  checkNeedNextWord();

    if(isNeedNextWord){
        initCurrentGuessWordStatus();
        await GiveMeAWord();
    }else {
        let {currentGuessWord, numberOfGuessAllowedForEachWord}=CONFIG;
        let {wrongGuessCountOfCurrentWord, totalWordCount, word,currentGuessCount}=currentGuessWord;
        logger.info(`第${totalWordCount}个单词: 第${currentGuessCount}次猜测,单词内容是 ${word},已经猜错了${wrongGuessCountOfCurrentWord}次(${numberOfGuessAllowedForEachWord})`);

        let mostPossibleChar = getBestMatchChar();

        let postData = {
            sessionId: CONFIG.sessionId,
            action: CONFIG.action.guessWord,
            guess: mostPossibleChar
        };
        let res = await request({
            method: 'post',
            body: postData,
            json: true,
            url: CONFIG.requestUrl
        });

        setCurrentGuessStatus(res);<commit_msg>//todo<commit_after>/**
 * Created by kimown on 16-9-14.
 */


'use strict';

let {request,rmFileSync}= require('./../util');
let logger = require('./../logger');
let loggerfile = require('./../loggerfile');
let {writeUserData,readUserData}=require('./common');
let configPath=require('./common/config-path');


process.on('uncaughtException', (err) => {
    console.log(err);
});

process.on('exit',(code) => {
    //如果程序异常退出且 totalWordCount 没有达到 一共要猜测的单词数量 ,保存用户数据
    // saveUserDataSync();
    console.log(`程序退出了About to exit with code: ${code}`);
});

let CONFIG={};


async function main() {

    //里面保存了所有的用户数据．
    CONFIG= await readUserData();

    if(CONFIG.hasOwnProperty('sessionId')&&CONFIG.sessionId){
        //断线重玩游戏
        await makeAGuess();
    }else{
        //new 一个game
        await startGame();
    }
}


/**
 * 1. Start Game
 * https://github.com/strikingly/strikingly-interview-test-instructions#1-start-game
 */
async function startGame() {
    let postData = {
        playerId: CONFIG.playerId,
        action: CONFIG.action.startGame
    };
    let res = await request({
        method: 'post',
        body: postData,
        json: true,
        url: CONFIG.requestUrl
    });
    let {body, ok}=res;
    if (ok == true) {
        let {data, message, sessionId}=body;
        let {numberOfGuessAllowedForEachWord, numberOfWordsToGuess}=data;
        logger.info(`message: ${message} ,您的身份标示是 ${sessionId} ,您一共要猜测 ${numberOfWordsToGuess} 个单词,每个单词限制猜测 ${numberOfGuessAllowedForEachWord} 次`);

        CONFIG.sessionId = sessionId;
        CONFIG.numberOfGuessAllowedForEachWord = numberOfGuessAllowedForEachWord;
        CONFIG.numberOfWordsToGuess = numberOfWordsToGuess;

    } else {
        logger.error(`启动游戏失败!!!失败原因:`, res.body);
    }

    await GiveMeAWord();

}


/**
 *　2. Give Me A Word
 * https://github.com/strikingly/strikingly-interview-test-instructions#2-give-me-a-word
 */
async function GiveMeAWord() {

    await checkArriveLimitWords();

    let postData = {
        sessionId: CONFIG.sessionId,
        action: CONFIG.action.nextWord
    };
    let res = await request({
        method: 'post',
        body: postData,
        json: true,
        url: CONFIG.requestUrl
    });
    let {body, ok}=res;
    if (ok == true) {
        let {data}=body;
        let {totalWordCount, word, wrongGuessCountOfCurrentWord}=data;
        CONFIG.currentGuessWord.totalWordCount = totalWordCount;
        CONFIG.currentGuessWord.word = word;
        CONFIG.currentGuessWord.wrongGuessCountOfCurrentWord = wrongGuessCountOfCurrentWord;
        await makeAGuess(CONFIG);
    } else {
        logger.error(`获取猜谜单词失败!!!失败原因:`, res.body);
    }
}


/**
 * 3.Make A Guess
 * https://github.com/strikingly/strikingly-interview-test-instructions#3-make-a-guess
 * 猜测当前的单词
 */
async function makeAGuess() {

    let isNeedNextWord=  checkNeedNextWord();

    if(isNeedNextWord){
        initCurrentGuessWordStatus();
        await GiveMeAWord();
    }else {
        let {currentGuessWord, numberOfGuessAllowedForEachWord}=CONFIG;
        let {wrongGuessCountOfCurrentWord, totalWordCount, word,currentGuessCount}=currentGuessWord;
        logger.info(`第${totalWordCount}个单词: 第${currentGuessCount}次猜测,单词内容是 ${word},已经猜错了${wrongGuessCountOfCurrentWord}次(${numberOfGuessAllowedForEachWord})`);

        let mostPossibleChar = getBestMatchChar();

        let postData = {
            sessionId: CONFIG.sessionId,
            action: CONFIG.action.guessWord,
            guess: mostPossibleChar
        };
        let res = await request({
            method: 'post',
            body: postData,
            json: true,
            url: CONFIG.requestUrl
        });


        //TODO 如果mostPossibleChar不在返回单词里面，去除含有mostPossibleChar的单词
        setCurrentGuessStatus(res);
{'fraction_non_alphanumeric': 0.13256369426751594, 'fraction_numerical': 0.00291932059447983, 'mean_word_length': 2.881050463439753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1971559', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2630, 'n_words': 431}