Skip to content

Commit

Permalink
Merge pull request #102 from lyve-app/feat/reward
Browse files Browse the repository at this point in the history
feat: updated reward costs and checked if sender can afford rewards
  • Loading branch information
Louis3797 authored Jun 7, 2024
2 parents 4ce2d63 + 01a84ce commit f091850
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
55 changes: 51 additions & 4 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,50 @@ io.on("connection", (socket) => {
}

// check if reward type is valid and get points
const rewardPoints = rewards[reward.type];
const redwardInfo = rewards[reward.type];

// check if user can afford this reward
const sender = await prismaClient.user.findUnique({
where: { id: user.id },
select: {
id: true,
coins: true
}
});

if (!sender) {
return cb({
success: false,
data: null,
error: [
{
name: "User not Found",
code: -1,
msg: `Cannot find user with id: ${user.id}`
}
]
});
}

if (sender.coins < redwardInfo.cost) {
return cb({
success: false,
data: null,
error: [
{
name: "Not enough coins",
code: -1,
msg: "You cannot afford this reward"
}
]
});
}

// save in database
const newDBReward = await prismaClient.rewards.create({
data: {
type: reward.type,
points: rewardPoints.points,
points: redwardInfo.points,
receiverId: stream.streamer.id,
senderId: user.id
}
Expand All @@ -688,7 +725,17 @@ io.on("connection", (socket) => {
where: { id: stream.streamer.id },
data: {
promotionPoints: {
increment: rewardPoints.points
increment: redwardInfo.points
}
}
});

// decrease sender coins
await prismaClient.user.update({
where: { id: user.id },
data: {
coins: {
decrement: redwardInfo.cost
}
}
});
Expand All @@ -699,7 +746,7 @@ io.on("connection", (socket) => {
reward: {
id: newDBReward.id,
type: reward.type,
points: rewardPoints.points
points: redwardInfo.points
},
receiver: stream.streamer,
sender: user
Expand Down
24 changes: 13 additions & 11 deletions apps/api/src/utils/rewards.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { RewardType } from "@prisma/client";

export const rewards: { [Key in RewardType]: { points: number } } = {
popsicle: { points: 1 },
pizza: { points: 2 },
gift: { points: 5 },
rocket: { points: 10 },
star: { points: 20 },
cake: { points: 35 },
crown: { points: 50 },
heart: { points: 75 },
bouquet: { points: 100 },
lucky_cat: { points: 200 }
export const rewards: {
[Key in RewardType]: { points: number; cost: number };
} = {
popsicle: { points: 1, cost: 1 },
pizza: { points: 2, cost: 4 },
gift: { points: 5, cost: 10 },
rocket: { points: 10, cost: 20 },
star: { points: 25, cost: 50 },
cake: { points: 50, cost: 99 },
crown: { points: 75, cost: 150 },
heart: { points: 100, cost: 180 },
bouquet: { points: 200, cost: 380 },
lucky_cat: { points: 1000, cost: 1500 }
};

0 comments on commit f091850

Please sign in to comment.