Skip to content

Commit

Permalink
Merge branch 'main' into CS2STT-57-Integrate-dynamic-generation-endpo…
Browse files Browse the repository at this point in the history
…int-for-user
  • Loading branch information
ereizas authored May 1, 2024
2 parents 747f9e5 + b57451b commit e6a621a
Show file tree
Hide file tree
Showing 81 changed files with 1,024 additions and 706 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
697 changes: 421 additions & 276 deletions app.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions documentation/src/pages/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
align-items: center;
justify-content: center;
}

Binary file added instance/ThrillTyper.db
Binary file not shown.
Binary file modified requirements.txt
Binary file not shown.
89 changes: 41 additions & 48 deletions sentence_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@


def main():
sentences = generate_sentences()
sentences = generate_sentences("hard")

#File names for each difficulty level
file_names = ['easy_sentences.txt', 'medium_sentences.txt', 'hard_sentences.txt']

#Write sentences to their respective files
for i, difficulty_level in enumerate(sentences):
with open(file_names[i], 'w') as file:
for sentence in difficulty_level:
file.write(sentence + '\n')
print(sentences)

def is_valid_characters(sentence):
allowed_characters = set(string.ascii_letters + string.punctuation + " ")
Expand All @@ -40,47 +33,47 @@ def is_similar(new_sentence, existing_sentences, first_three_words_set):

return False

def generate_sentences():
sentences_per_difficulty = 10
sentences = [[], [], []]
unique_checks = set() #Using a set to track unique sentences
first_three_words_set = set() #Set to track first three words of each sentence
def generate_sentences(difficulty):
difficulty_mapping = {"easy": 1, "medium": 2, "hard": 3}
if difficulty not in difficulty_mapping:
return "Invalid difficulty level"

difficulty_index = difficulty_mapping[difficulty] - 1
num_sentences = 10
sentences = []
unique_checks = set()
first_three_words_set = set()

prompt = "Create a sentence that has "
if difficulty_index == 0:
prompt += "short and simple words that have letters that are easy to type in sequence."
elif difficulty_index == 1:
prompt += "of medium length, with moderate vocabulary and one comma that have letters that are moderately difficult to type in sequence."
elif difficulty_index == 2:
prompt += "long and complex, using advanced vocabulary, including commas, semicolons, and at least one capitalized proper noun with words that have letters that are difficult to type in sequence."

for i in range(sentences_per_difficulty):
for difficulty in range(1, 4):
prompt = "Create a sentence that has "
if difficulty == 1:
prompt += "short and simple words that have letters that are easy to type in sequence."
elif difficulty == 2:
prompt += "of medium length, with moderate vocabulary and one comma that have letters that are moderately difficult to type in sequence."
elif difficulty == 3:
prompt += "long and complex, using advanced vocabulary, including commas, semicolons, and at least one capitalized proper noun with words that have letters that are difficult to type in sequence."

attempts = 0
while attempts < 5:
try:
response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt=prompt,
temperature=1.0,
max_tokens=100,
top_p=1,
frequency_penalty=2.0,
presence_penalty=2.0
)
generated_sentence = response.choices[0].text.strip()

#Check if the sentence is valid based on characters and if it is similar
if is_valid_characters(generated_sentence) and not is_similar(generated_sentence, unique_checks, first_three_words_set):
unique_checks.add(generated_sentence)
sentences[difficulty - 1].append(generated_sentence)
first_three_words_set.add(' '.join(generated_sentence.lower().translate(str.maketrans('', '', string.punctuation)).split()[:3]))
break
except Exception as e:
print(f"An error occurred: {e}")
attempts += 1
attempts = 0
while len(sentences) < num_sentences and attempts < 50:
try:
response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt=prompt,
temperature=1.0,
max_tokens=100,
top_p=1,
frequency_penalty=2.0,
presence_penalty=2.0
)
generated_sentence = response.choices[0].text.strip()
if is_valid_characters(generated_sentence) and not is_similar(generated_sentence, unique_checks, first_three_words_set):
unique_checks.add(generated_sentence)
sentences.append(generated_sentence)
first_three_words_set.add(' '.join(generated_sentence.lower().translate(str.maketrans('', '', string.punctuation)).split()[:3]))
except Exception as e:
print(f"An error occurred: {e}")
attempts += 1

return sentences
return ' '.join(sentences)

if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion static/js/content/About.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function About() {
<p><strong>Overall</strong> leaderboard is now available for both guests and authenticated users, showing the top 50 ranked fastest typers recorded on the website.</p>
<p><strong>Multiplayer</strong> chat rooms are now available for guest and authenticated users to chat.</p>
<p><strong>Import text</strong> mode under single player is now available for users to enter self-determined text content for practice mode.</p>
<p><strong>Cosmetics Background</strong> Pre-configured cosmetics are now added to the single player game mode.</p>
</div>
</div>
</section>
Expand All @@ -72,7 +73,7 @@ function About() {
</div>
<div className="contributor">
<img src="../static/pics/contributors/unicorn.gif" alt="Profile Photo" />
<p>Bochi</p>
<p>TU-Wenjie-Gao</p>
</div>
<div className="contributor">
<img src="../static/pics/contributors/disappointed.gif" alt="Profile Photo" />
Expand Down
5 changes: 4 additions & 1 deletion static/js/content/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ function Dashboard({ userSession }) {
Total Playing Time: {Math.floor(userData.totalTime)} minutes
</p>
<p className="center flexEven">
Accuracy: {userData.accuracy}%
Highest WPM: {userData.highestWPM} WPM
</p>
<p className="center flexEven">
Accuracy: {Number(userData.accuracy).toFixed(2)}%
</p>
</section>
{/* Specific Data, i.e. WPM and Accuracy */}
Expand Down
2 changes: 1 addition & 1 deletion static/js/content/Leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Leaderboard() {
{leaderboardData.map((player, index) => (
<tr key={player.username} className={index === 0 ? 'rank-one' : index === 1 ? 'rank-two' : index === 2 ? 'rank-three' : ''}>
<td>
<img src={player.profile_photo} alt="Profile" />
<img src={player.profile_photo} alt="Profile" referrerPolicy="no-referrer"/>
</td>
<td className="username">
<strong>{player.username}</strong>
Expand Down
7 changes: 4 additions & 3 deletions static/js/content/Menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

function Menu({}) {
const buttonStyle = {
width: '500px',
height: '300px',
width: '100%', // Change button width to be responsive
height: 'auto', // Allow button height to adjust based on content
fontSize: '2rem',
padding: '20px 15px',
border: '2px solid #294E95',
Expand Down Expand Up @@ -33,7 +34,7 @@ function Menu({}) {
<h1 style={{ color: '#294E95', fontFamily: 'Arial, sans-serif', fontWeight: 'bold', width: 'calc(100% - 20px)', textAlign: 'center', padding: '1rem', borderBottom: '1px solid #294E95', maxWidth: '900px' }}>
Choose Your Game Mode
</h1>
<div id="menu" style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gridTemplateRows: 'auto auto', rowGap: '2rem', columnGap: '4rem', justifyContent: 'center', maxWidth: '900px' }}>
<div id="menu" style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(300px, 1fr))', gridTemplateRows: 'auto auto', rowGap: '2rem', columnGap: '4rem', justifyContent: 'center', maxWidth: '900px' }}>
<Link id="singlePlayerMode" to="/singlePlayer" className="menu-item" style={linkStyle}>
<button style={buttonStyle}>
<img src="/static/pics/SinglePlayerMode.png" alt="Single Player Image" style={{ width: '150px', height: 'auto', marginBottom: '20px' }} />
Expand Down
12 changes: 11 additions & 1 deletion static/js/reusable/ImportTextPlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,30 @@ function ImportText() {
//console.log("userInputCorrectText: " + userInputCorrectText);
}


function changeBackground(season) {
const body = document.body;
switch(season) {
case 'spring':
body.style.backgroundImage = "url('/static/pics/spring.jpg')";
break;

case 'winter':
body.style.backgroundImage = "url('/static/pics/winter.jpg')";
break;

case 'summer':
body.style.backgroundImage = "url('/static/pics/summer.jpg')";
break;

case 'autumn':
body.style.backgroundImage = "url('/static/pics/autumn.jpg')";
break;

case 'original':
body.style.backgroundImage = "none";
break;

default:
body.style.backgroundImage = "none";
}
Expand All @@ -238,6 +246,8 @@ function ImportText() {
<button onClick={() => changeBackground('summer')}>Summer</button>
<button onClick={() => changeBackground('autumn')}>Autumn</button>
<button onClick={() => changeBackground('winter')}>Winter</button>
<button onClick={() => changeBackground('original')}>Original</button>

</div>
</div>
</div>
Expand Down
14 changes: 11 additions & 3 deletions static/js/reusable/RobotOpponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,29 +324,35 @@ function RobotOpponent() {
}



function changeBackground(season) {
const body = document.body;
switch(season) {
case 'spring':
body.style.backgroundImage = "url('/static/pics/spring.jpg')";
break;

case 'winter':
body.style.backgroundImage = "url('/static/pics/winter.jpg')";
break;

case 'summer':
body.style.backgroundImage = "url('/static/pics/summer.jpg')";
break;

case 'autumn':
body.style.backgroundImage = "url('/static/pics/autumn.jpg')";
break;

case 'original':
body.style.backgroundImage = "none";
break;

default:
body.style.backgroundImage = "none";
}
}


React.useEffect(() => {
const inputBox = document.getElementById("input-box");
const startBtn = document.getElementById("startBtn");
Expand Down Expand Up @@ -392,6 +398,8 @@ function RobotOpponent() {
<button onClick={() => changeBackground('summer')}>Summer</button>
<button onClick={() => changeBackground('autumn')}>Autumn</button>
<button onClick={() => changeBackground('winter')}>Winter</button>
<button onClick={() => changeBackground('original')}>Original</button>

</div>
</div>
</div>
Expand Down
Loading

0 comments on commit e6a621a

Please sign in to comment.