-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwords.js
86 lines (78 loc) · 2.79 KB
/
words.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Filter from 'bad-words';
import { readFileSync } from "fs";
// import {sendWordMessage} from "./kafka.js";
const filter = new Filter();
const current_word = {}
const previous_words = {}
// Function to choose 3 words from the noun list
export const chooseWord = (round, room) => {
// Clear the current word
current_word[room] = ''
// Retrieve words from txt file
let text = readFileSync("./pictionary.txt", "utf-8").split('\n').map(w => { return w.trim() })
// Filter list, checking against previous words used
if (previous_words[room]) {
text = text.filter((w) => {
for (var i=0; i<previous_words[room].length; i++) {
if (w === previous_words[room][i]) {
return false
}
}
return true
})
}
const word1 = text.splice(Math.floor(Math.random() * text.length), 1)
const word2 = text.splice(Math.floor(Math.random() * text.length), 1)
const word3 = text.splice(Math.floor(Math.random() * text.length), 1)
return { "word1": word1[0], "word2": word2[0], "word3": word3[0] }
}
// Function to update current word for room
export const updateRoom = (room, word) => {
// sendWordMessage(word).then(console.log).catch((e) => console.log(e.message));
current_word[room] = word
// Keep track of previous words so all new words are provided for the duration of the room
if (previous_words[room]) {
previous_words[room].push(current_word[room])
} else {
previous_words[room] = [current_word[room]]
}
return true
}
// Function to get word for a room
export const getWord = (room) => {
return current_word[room]
}
// Function to remove room from object when no players are left
export const removeRoom = (room) => {
delete current_word[room]
delete previous_words[room]
return
}
// Function to check message against room word to check if it has been guessed right
export const checkWord = (message, room) => {
var msg = ''
const sanitizedMessage = message.replace(/[^A-Za-z]/g," ")
const myWord = sanitizedMessage.trim().toLowerCase()
const word = getWord(room)
const parts = word.split(" ")
if (word != myWord) {
msg = sanitizedMessage.trim() === "" ? "Not the word!\n" + message : "Not the word!\n" + filter.clean(sanitizedMessage)
if (parts.length > 1) {
const msg_parts = myWord.split(parts[0])
if (msg_parts.length < 2) {
return msg
}
if (msg_parts[1] == parts[1]) {
msg = "Correct!"
}
} else {
const msg_parts = myWord.split(" ")
if (msg_parts.join("") == word) {
msg = "Correct!"
}
}
} else {
msg = "Correct!"
}
return msg
}