Skip to content

Commit

Permalink
Merge pull request #57 from kthchew/streaks
Browse files Browse the repository at this point in the history
Added Streak Functionality
  • Loading branch information
MaximilianMeiler authored Apr 23, 2024
2 parents ccd09d4 + 3258999 commit 2d812d2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Backend/generic-food.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ var filteredWords = [
"Clam",
"Squid",
"Shrimp",
"Onigiri",
"Alan Wang",
"Keke",
"Software Engineering",
"Cat Corp the Cat",
"Microplastics",
"Crayfish",
"Flatfish",
"Piglet",
Expand Down
26 changes: 21 additions & 5 deletions Backend/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function checkUsernameAvailable(username) {
// This function does NOT do any checks for unique usernames, etc.
export async function registerAccount(username, password) {
const hashedPassword = await bcrypt.hash(password, 10);
const user = await getUserDB().insertOne({ "username": username, "password": hashedPassword, "lastLogin": Date.now(), "gems": 0, "cats": [] })
const user = await getUserDB().insertOne({ "username": username, "password": hashedPassword, "lastLogin": Date.now(), "gems": 0, "cats": [], "streak": 0 })
return user.insertedId
}

Expand Down Expand Up @@ -109,6 +109,11 @@ export async function getCanvasUserId(session) {
}

export async function cashSubmissions(session, courses) {
let streakMult = await getUserProperty(session, "streak");
if (isNaN(streakMult)) {
await setUserProperty(session, "streak", 0)
streakMult = 0;
}
var sum = 0
courses.forEach((course, i) => {
var temp = course[3];
Expand Down Expand Up @@ -137,15 +142,16 @@ export async function cashSubmissions(session, courses) {
const frac = (due - sub) / (due - unlock);
multiplier *= Math.min(Math.max((Math.cbrt(frac) + .5), .000000001), 1.5);
}

multiplier *= (1 + streakMult/20);
// submission.push(Math.ceil(multiplier * 100)) this works too???
sum += Math.ceil(multiplier * 100);
courses[i][3][j].push(Math.ceil(multiplier * 100))
})

})


const results = await updateClasses(session, courses)

const results = await updateClasses(session, courses);
await incrementUserProperty(session, "gems", sum);
await updateLastLogin(session);
return [courses, sum, results[0], results[1]];
Expand Down Expand Up @@ -241,11 +247,21 @@ async function updateClasses(session, courses) {
}
}

effects.push(effect)
if (effect.result !== undefined) {
effects.push(effect)
}
bosses.push([data.courseName, data.courseId, data.users]);
}
}))

if (effects.length > 0) {
const wonAll = effects.every(effect => effect.result === "win")
if (wonAll) {
await incrementUserProperty(session, "streak", 1)
} else if (effects.some(e => e.result === "lose")) {
await setUserProperty(session, "streak", 0)
}
}
return [effects, bosses]
}

Expand Down
2 changes: 1 addition & 1 deletion Frontend/src/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Home({ userData, setUserData, courses, setCourses, bossData, overlay, s
<div>
{
overlay == "rewards" ?
<Rewards courses={courses} setOverlay={setOverlay}/>
<Rewards courses={courses} setOverlay={setOverlay} streak={userData.streak}/>
: overlay == "checklist" ?
<Checklist courses={courses} setOverlay={setOverlay}/>
: overlay == "store" ?
Expand Down
6 changes: 4 additions & 2 deletions Frontend/src/Rewards.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import "./css/Rewards.css"
import gem from "../src/img/gem.png"

export default function Rewards({ courses, setOverlay }) {
export default function Rewards({ courses, setOverlay, streak }) {
const [index, setIndex] = useState(0)

const newSubmissions = courses
Expand All @@ -17,6 +17,7 @@ export default function Rewards({ courses, setOverlay }) {
return (
<div className="rewardsBackground">
<h1 className="rewardsHeader">Gems earned:</h1>
<h3 className="rewardsStreak">Boss Fight Win Streak: {streak}</h3>
{newSubmissions.length != 0 ?
<div style={{ height: "60%" }}>
<div style={{ display: "flex", justifyContent: "center" }}>
Expand Down Expand Up @@ -46,12 +47,13 @@ export default function Rewards({ courses, setOverlay }) {
</div>
: <h2 style={{ height: "60%" }}>No assignments submitted since last login!</h2>
}
<button onClick={() => setOverlay("home")} className="rewardsConfirm">OK</button>
<button onClick={() => setOverlay("home")} className="rewardsConfirm">OK</button>
</div>
)
}

Rewards.propTypes = {
courses: PropTypes.object,
setOverlay: PropTypes.func,
streak: PropTypes.number
}
5 changes: 5 additions & 0 deletions Frontend/src/css/Rewards.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
margin-top: 5%;
}

.rewardsStreak {
color: grey;
margin-top: -0%;
}

.rewardsList {
overflow-y: auto;
align-items: center;
Expand Down

0 comments on commit 2d812d2

Please sign in to comment.