/
socketHelperFunctions.ts
134 lines (123 loc) · 3.57 KB
/
socketHelperFunctions.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Paragraph from './schemas/paragraph';
import IWpmCalculation from './interfaces/calcutaltion.interface';
import gameState from './interfaces/gameState.interface';
import Iuser from './interfaces/user.interface';
import sequelize from './models';
import { Op } from 'sequelize';
async function getRandomParagraph(): Promise<string | undefined> {
const paragraph = await Paragraph.findOne({
order: sequelize.random(),
where: {
characterLengthNumeric: {
[Op.lt]: 400,
},
},
});
return paragraph?.text;
}
async function joinUser(
roomId: string,
socketId: string,
gameState: any,
): Promise<void> {
let isHost = false;
if (!gameState[roomId]) {
const paragraph: string | undefined = await getRandomParagraph();
gameState[roomId] = { users: {}, paragraph: paragraph };
isHost = true;
}
gameState[roomId].users[socketId] = {
userId: socketId,
userName: 'Guest',
color: '',
isHost: isHost,
isReady: false,
gameData: {},
WPMHistory: [],
userParagraph: gameState[roomId].paragraph,
availablePUs: [],
appliedPUs: [],
};
}
function calculateResults(
endTime: number,
startTime: string,
allKeyPresses: number,
charLength: number,
): IWpmCalculation {
const { minutes, remainder, time } = calculateTime(endTime, startTime);
const seconds = remainder < 10 ? `0${remainder}` : remainder;
const wpm = calculateWpm(charLength, time);
const accuracy = calculateAccuracy(allKeyPresses, charLength);
return { finishTime: `${minutes}:${seconds}`, WPM: wpm, accuracy };
}
function calculateTime(endTime: number, startTime: string) {
const seconds = Math.round((endTime - parseInt(startTime)) / 1000);
const minutes = Math.floor(seconds / 60);
const remainder = Math.round(seconds % 60);
const percentageOfMinute = remainder / 60;
return { minutes, remainder, time: minutes + percentageOfMinute };
}
function calculateWpm(charLength: number, time: number): number {
const wpm = Math.round(charLength / 5 / time);
return wpm;
}
function calculateAccuracy(allKeyPresses: number, charLength: number): number {
const accuracy = Math.round((charLength / allKeyPresses) * 100);
return accuracy;
}
function calculateAverageWPM(user: Iuser, WPM: number): any {
const newWPMHistory = user.WPMHistory;
newWPMHistory.push(WPM);
const newAVG =
Math.round(newWPMHistory.reduce((total, WPM) => total + WPM) / newWPMHistory.length);
return { newWPMHistory, newAVG };
}
function getPlayers(
gameState: gameState,
roomId: string | string[] | undefined,
): Iuser[] {
const usersArray = [];
for (const user in gameState[`${roomId}`].users) {
usersArray.push(gameState[`${roomId}`].users[user]);
}
return usersArray;
}
function checkIfReady(player: Iuser): boolean {
let isReady;
if (
player.availablePUs.length === 0 &&
player.userName !== 'Guest' &&
player.color !== ''
) {
isReady = true;
} else {
isReady = false;
}
return isReady;
}
function givePowers(players: number): any {
const powers = [
{ id: 'scramble', powerUp: 'ScrambleCard' },
{ id: 'longWord', powerUp: 'LongWordCard' },
{ id: 'symbols', powerUp: 'SymbolsCard' },
];
const res = [];
for (let i = 1; i < players; i++) {
if (powers.length > 0) {
const randomPower = Math.floor(Math.random() * powers.length);
const power = powers.splice(randomPower, 1);
res.push({ rank: players - i + 1, power: power[0] });
}
}
return res;
}
export default {
getRandomParagraph,
joinUser,
calculateResults,
calculateAverageWPM,
getPlayers,
checkIfReady,
givePowers,
};