Skip to content

Commit

Permalink
final updates to hints in backend + gameplay page
Browse files Browse the repository at this point in the history
  • Loading branch information
cathli66 committed Apr 18, 2024
1 parent f06e288 commit 7267e0c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
26 changes: 15 additions & 11 deletions game/lib/gameplay/challenge_completed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {

@override
Widget build(BuildContext context) {
final int hintsDeduction = 25;

return Consumer5<ChallengeModel, EventModel, TrackerModel, ApiClient,
GroupModel>(
builder: (context, challengeModel, eventModel, trackerModel, apiClient,
Expand Down Expand Up @@ -123,10 +125,13 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
var total_pts = 0;
List<Widget> completedChallenges = [];
for (PrevChallengeDto prevChal in (tracker.prevChallenges ?? [])) {
print(prevChal.dateCompleted.toString());
print(prevChal.hintsUsed);
var completedChal =
challengeModel.getChallengeById(prevChal.challengeId);
if (completedChal == null) continue;
var pts = (completedChal.points ?? 0) - (prevChal.hintsUsed * 25);
var pts =
(completedChal.points ?? 0) - (prevChal.hintsUsed * hintsDeduction);
total_pts += pts;

completedChallenges.add(Container(
Expand Down Expand Up @@ -235,16 +240,14 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
Spacer(),
Text(
"+ " +
((challenge.points ?? 0) -
(tracker.hintsUsed ?? 0) * 25)
.toString() +
(challenge.points ?? 0).toString() +
" points",
style:
TextStyle(color: Colors.white, fontSize: 16.0),
),
],
)),
if ((tracker.hintsUsed ?? 0) > 0)
if (tracker.prevChallenges.last.hintsUsed > 0)
Container(
margin:
EdgeInsets.only(left: 30, bottom: 10, right: 30),
Expand All @@ -264,15 +267,15 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
),
Spacer(),
Text(
"- 25 points",
"- " + hintsDeduction.toString() + " points",
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
],
)),
if ((tracker.hintsUsed ?? 0) > 1)
if (tracker.prevChallenges.last.hintsUsed > 1)
Container(
margin:
EdgeInsets.only(left: 30, bottom: 10, right: 30),
Expand All @@ -292,13 +295,13 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
),
Spacer(),
Text(
"- 25 points",
"- " + hintsDeduction.toString() + " points",
style: TextStyle(
color: Colors.white, fontSize: 16.0),
),
],
)),
if ((tracker.hintsUsed ?? 0) > 2)
if ((tracker.prevChallenges.last.hintsUsed ?? 0) > 2)
Container(
margin:
EdgeInsets.only(left: 30, bottom: 10, right: 30),
Expand All @@ -318,7 +321,7 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
),
Spacer(),
Text(
"- 25 points",
"- " + hintsDeduction.toString() + " points",
style: TextStyle(
color: Colors.white, fontSize: 16.0),
),
Expand All @@ -332,7 +335,8 @@ class _ChallengeCompletedState extends State<ChallengeCompletedPage> {
? "Total Points: " + total_pts.toString()
: "Points Earned: " +
((challenge.points ?? 0) -
(tracker.hintsUsed ?? 0) * 25)
(tracker.prevChallenges.last.hintsUsed ?? 0) *
hintsDeduction)
.toString(),
style: TextStyle(
color: Colors.white,
Expand Down
15 changes: 14 additions & 1 deletion game/lib/gameplay/gameplay_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ class _GameplayPageState extends State<GameplayPage> {
vertical: 4.0, horizontal: 8.0),
child: Text(
(event.challenges!.length > 1
? "Journey"
? "Journey " +
(tracker.prevChallenges.length +
1)
.toString() +
"/" +
event.challenges!.length
.toString()
: "Challenge"),
style: TextStyle(
fontSize: 14,
Expand Down Expand Up @@ -221,6 +227,13 @@ class _GameplayPageState extends State<GameplayPage> {
"assets/icons/bearcoins.svg"),
Text(
' ' +
((tracker.hintsUsed > 0)
? ((challenge.points ?? 0) -
tracker.hintsUsed *
25)
.toString() +
'/'
: '') +
(challenge.points ?? 0).toString() +
" PTS",
style: TextStyle(
Expand Down
7 changes: 4 additions & 3 deletions game/lib/preview/preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ class _PreviewState extends State<Preview> {
child: Row(children: [
Icon(Icons.tour,
size: 24, color: Preview.purpleColor),
Text(
location, // should call new parameter; replace later
Text(location,
style: TextStyle(
fontSize: 20, color: Preview.purpleColor)),
SizedBox(width: 10),
Expand Down Expand Up @@ -371,7 +370,9 @@ class _PreviewState extends State<Preview> {
builder: (context) => GameplayPage()));
},
child: Text(
"Continue exploring",
(numberCompleted == 0)
? "Let's Go!"
: "Continue exploring",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
Expand Down
1 change: 1 addition & 0 deletions server/src/event/event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface EventDto {
latitudeF?: number;
}

/** DTO for PrevChallenge as used in EventTrackerDto */
export interface PrevChallengeDto {
challengeId: string;
hintsUsed: number;
Expand Down
2 changes: 0 additions & 2 deletions server/src/event/event.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ export class EventGateway {
@CallingUser() user: User,
@MessageBody() data: UseEventTrackerHintDto,
) {
console.log("use event tracker hint called");
const tracker = await this.eventService.useEventTrackerHint(user, data);
if (tracker) {
await this.eventService.emitUpdateEventTracker(tracker, user);
console.log("emitted update tracker event, " + tracker.curChallengeId);
return;
}
await this.clientService.emitErrorData(user, 'Failed to track used hint!');
Expand Down
43 changes: 17 additions & 26 deletions server/src/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class EventService {
private orgService: OrganizationService,
private readonly prisma: PrismaService,
private abilityFactory: CaslAbilityFactory,
) { }
) {}

/** Get event by id */
async getEventById(id: string) {
Expand Down Expand Up @@ -222,10 +222,12 @@ export class EventService {
) {
const evs: EventBase[] = await this.prisma.$queryRaw`
select * from "EventBase" ev
where ev."id" in (select e."A" from "_eventOrgs" e inner join "_player" p on e."B" = p."A" and ${user.id
where ev."id" in (select e."A" from "_eventOrgs" e inner join "_player" p on e."B" = p."A" and ${
user.id
} = p."B")
order by ((ev."latitude" - ${data.latitudeF})^2 + (ev."longitude" - ${data.longitudeF
})^2)
order by ((ev."latitude" - ${data.latitudeF})^2 + (ev."longitude" - ${
data.longitudeF
})^2)
fetch first ${data.count ?? 4} rows only
`;
return evs;
Expand Down Expand Up @@ -270,8 +272,8 @@ export class EventService {
ev.difficulty === DifficultyMode.EASY
? 'Easy'
: ev.difficulty === DifficultyMode.NORMAL
? 'Normal'
: 'Hard',
? 'Normal'
: 'Hard',
latitudeF: ev.latitude,
longitudeF: ev.longitude,
};
Expand All @@ -283,32 +285,22 @@ export class EventService {
* @returns an EventTrackerDTO for the event tracker
*/
async dtoForEventTracker(tracker: EventTracker): Promise<EventTrackerDto> {
const completedChallenges = await this.prisma.challenge.findMany({
where: {
completions: { some: { userId: tracker.userId } },
linkedEventId: tracker.eventId,
},
include: { completions: { where: { userId: tracker.userId } } },
});

const prevChallenges = await this.prisma.prevChallenge.findMany({
where: {
trackerId: tracker.id
}
trackerId: tracker.id,
},
});

return {
eventId: tracker.eventId,
isRanked: tracker.isRankedForEvent,
hintsUsed: tracker.hintsUsed,
curChallengeId: tracker.curChallengeId,
prevChallenges: prevChallenges.map(pc => (
{
challengeId: pc.challengeId,
hintsUsed: pc.hintsUsed,
dateCompleted: pc.timestamp.toUTCString(),
}
)),
prevChallenges: prevChallenges.map(pc => ({
challengeId: pc.challengeId,
hintsUsed: pc.hintsUsed,
dateCompleted: pc.timestamp.toUTCString(),
})),
};
}

Expand All @@ -333,7 +325,6 @@ export class EventService {
where: { id: evTracker.id },
data: { hintsUsed: evTracker.hintsUsed + 1 },
});
console.log('increased hints used for event tracker');
return evTracker;
}

Expand Down Expand Up @@ -445,8 +436,8 @@ export class EventService {
(event.difficulty === 'Easy'
? DifficultyMode.EASY
: event.difficulty === 'Normal'
? DifficultyMode.NORMAL
: DifficultyMode.HARD),
? DifficultyMode.NORMAL
: DifficultyMode.HARD),
latitude: event.latitudeF,
longitude: event.longitudeF,
};
Expand Down

0 comments on commit 7267e0c

Please sign in to comment.