Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Display and Download Typing Game Data Functionalities #41

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<link rel="stylesheet" type="text/css" href="logic/main.css">
</head>
<body>

<div id='selectors'>
<div>
<select id='layout' autocomplete="off">
Expand Down Expand Up @@ -135,6 +134,11 @@ <h6>Keyboard Mapping: <span>off</span></h6>
</label>
</div>

<div id="buttonContainer">
<button onclick="displayGameData()">Show Data</button>
<button onclick="downloadGameData()">Download Data</button>
</div>
<div id="csvDisplay"></div>

<script type="text/javascript" src="logic/layoutInfo.js"></script>
<script type="text/javascript" src="logic/wordList.js"></script>
Expand Down
71 changes: 71 additions & 0 deletions logic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,32 @@ function checkAnswer() {
return inputVal == correctAnswer;
}

function storeGameData() {
// Collect game data
var mode = currentLayout; // Assuming currentLayout represents the mode
var level = currentLevel;
var wpm = wpmText.textContent.split(': ')[1]; // Extract WPM from the textContent
var words = score; // Total words typed
var accuracy = accuracyText.textContent.split(': ')[1]; // Extract accuracy from textContent

// Create an object with the data
var gameData = {
mode: mode,
level: level,
wpm: wpm,
words: words,
accuracy: accuracy
};

// Get existing data from local storage
var existingData = JSON.parse(localStorage.getItem('typingGameData')) || [];

// Add the new game data to the existing data
existingData.push(gameData);

// Save back to local storage
localStorage.setItem('typingGameData', JSON.stringify(existingData));
}


function endGame() {
Expand Down Expand Up @@ -1503,8 +1529,53 @@ function endGame() {
// set letter index (where in the word the user currently is)
// to the beginning of the word
letterIndex = 0;
storeGameData();
}


function displayGameData() {
var displayContainer = document.getElementById('csvDisplay');

// Check if the display container is already showing data
if (displayContainer.innerHTML !== '') {
// Clear the container to hide data
displayContainer.innerHTML = '';
} else {
// Retrieve stored data
var storedData = JSON.parse(localStorage.getItem('typingGameData')) || [];

// Convert array of objects to a CSV-like string
var displayContent = '<pre>Mode,Level,WPM,Words,Accuracy\n';
storedData.forEach(function(row) {
displayContent += row.mode + ',' + row.level + ',' + row.wpm + ',' + row.words + ',' + row.accuracy + '\n';
});
displayContent += '</pre>';

// Display in the designated container
displayContainer.innerHTML = displayContent;
}
}


function downloadGameData() {
var storedData = JSON.parse(localStorage.getItem('typingGameData')) || [];
var csvContent = 'Mode,Level,WPM,Words,Accuracy\n';
storedData.forEach(function(row) {
csvContent += row.mode + ',' + row.level + ',' + row.wpm + ',' + row.words + ',' + row.accuracy + '\n';
});

var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'typing-game-data.csv';
link.style.visibility = 'hidden';

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}


// generates a single line to be appended to the answer array
// if a line with a maximum number of words is desired, pass it in as a parameter
function generateLine(maxWords) {
Expand Down
16 changes: 14 additions & 2 deletions logic/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ input {
font-size: 5vmin;
}


#buttonContainer {
position: fixed; /* Fixed positioning */
top: 10px; /* Adjust as needed */
left: 10px; /* Adjust as needed */
display: flex;
flex-direction: column;
align-items: flex-start;
}

#buttonContainer button {
margin-bottom: 5px; /* Spacing between buttons */
}

#scoreText, #timeText {
font-family: 'Verdana', sans-serif;
}
Expand Down Expand Up @@ -836,5 +850,3 @@ input:checked + .slider:before {