这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions server/controllers/startGameController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Express from 'express'
import jwt from 'jsonwebtoken'
import Express from 'express';
import jwt from 'jsonwebtoken';
import fs from 'fs';;

import config from '../config'
import Player from '../models/Player'
import config from '../config';
import Player from '../models/Player';
/*
Request:
{
Expand All @@ -19,29 +20,49 @@ Response:
}
}
*/
const numberOfWordsToGuess = 80;
const numberOfGuessAllowedForEachWord = 10;

export default (req, res) => {
const body = req.body
const playerId = body.playerId
const body = req.body;
const playerId = body.playerId;
if (validateEmail(playerId)) {
const sessionId = jwt.sign({ email: playerId }, config.secret, {
expiresIn: 60 * 60 * 24 * 30 // expires in 30 days
})
const playerDocument = new Player({ email: playerId, sessionId: sessionId })
playerDocument.save((err) => {
if (err) throw err
res.json({
message: "THE GAME IS ON",
sessionId: sessionId,
data: {
numberOfWordsToGuess: 40,
numberOfGuessAllowedForEachWord: 10
}
})
})
});
fs.readFile('./server/resource/train.txt', 'utf8', (err, data) => {
const wordList = buildWordList(data.split('\n'), numberOfWordsToGuess);
console.log(wordList);
const playerDocument = new Player({ email: playerId, sessionId: sessionId, gameStatus: wordList });
playerDocument.save((err) => {
if (err) throw err;
res.json({
message: "THE GAME IS ON",
sessionId: sessionId,
data: { numberOfWordsToGuess, numberOfGuessAllowedForEachWord }
});
});
});
}
}

const validateEmail = (email) => {
var re = /\S+@\S+\.\S+/
return re.test(email)
}

const buildWordList = (wordList, size) => {
const length = wordList.length;
return Array(size)
.fill(true)
.map((item, index, arr) => {
let word = wordList[Math.floor(Math.random() * length)]
while (arr.indexOf(word) >= 0) {
word = wordList[Math.floor(Math.random() * length)]
}
return {
word: word,
result: word.replace(/\w/g, '*')
};
});
}
19 changes: 4 additions & 15 deletions server/models/player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import mongoose, { Schema } from 'mongoose'

import wordList from '../resource/wordList'
import mongoose, { Schema } from 'mongoose';

// Sub document for Player
const WordSchema = new Schema({
Expand All @@ -10,7 +8,7 @@ const WordSchema = new Schema({
default: 0
},
result: String
})
});

export default mongoose.model('Player', new Schema({
email: String,
Expand All @@ -19,14 +17,5 @@ export default mongoose.model('Player', new Schema({
type: Number,
default: 0
},
gameStatus: {
type: [WordSchema],
default: () => {
const res = []
wordList.forEach((word) => {
res.push({ word: word, result: word.replace(/\w/g, '*') })
})
return res
}
}
}))
gameStatus: [WordSchema]
}));
1 change: 0 additions & 1 deletion src/actions/autoPlayAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { guessFetch } from '../utils/request';
export const autoPlay = async function() {
return async(dispatch, getState) => {
dispatch(clearAll());
console.log('start');
const email = getState().global.email;
await dispatch(startGame(email));
const { sessionId, numberOfWordsToGuess, numberOfGuessAllowedForEachWord } = getState().global;
Expand Down