t00-multiuser/server/controller/HomeController.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-09-19 00:41:40 +01:00
import Controller from "./Controller";
2024-10-08 11:04:46 +01:00
import FunkyArray from "funky-array";
2024-09-19 00:41:40 +01:00
import UserService from "../services/UserService";
import HomeViewModel from "../models/home/HomeViewModel";
2024-09-23 23:55:00 +01:00
import PartyService from "../services/PartyService";
2024-10-08 11:04:46 +01:00
import BadgeService from "../services/BadgeService";
import Badge from "../entities/Badge";
import UserBadge from "../entities/UserBadge";
2024-09-19 00:41:40 +01:00
export default class HomeController extends Controller {
public async Index_Get_AllowAnonymous() {
if (this.session) {
const user = await UserService.GetUser(this.session.userId);
if (!user) {
return this.unauthorised();
}
2024-09-23 23:55:00 +01:00
const parties = await PartyService.GetUserParties(this.session.userId);
2024-09-19 00:41:40 +01:00
const activeUserParty = await UserService.GetActiveParty(this.session.userId);
2024-10-08 09:41:47 +01:00
const unlockedBadges = await UserService.LoadUserBadges(this.session.userId);
2024-10-08 11:04:46 +01:00
const unlockedBadgesById = new FunkyArray<number, UserBadge>();
for (const unlockedBadge of unlockedBadges) {
unlockedBadgesById.set(unlockedBadge.BadgeId, unlockedBadge);
}
const badges = await BadgeService.LoadAll();
const badgeById = new FunkyArray<number, Badge>();
for (const badge of badges) {
badgeById.set(badge.Id, badge);
}
2024-09-19 00:41:40 +01:00
const homeViewModel: HomeViewModel = {
user,
parties,
2024-10-08 09:41:47 +01:00
activeUserParty,
2024-10-08 11:04:46 +01:00
unlockedBadges,
unlockedBadgesById,
badgeById
2024-09-19 00:41:40 +01:00
};
return this.view("home", homeViewModel);
}
return this.view();
}
}