t00-multiuser/server/controller/PartyController.ts

95 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-09-19 00:41:40 +01:00
import CreateEditPartyViewModel from "../models/party/CreateEditPartyViewModel";
2024-09-23 23:55:00 +01:00
import DeletePartyModel from "../models/party/DeletePartyModel";
2024-09-19 00:41:40 +01:00
import JoinPartyViewModel from "../models/party/JoinPartyViewModel";
import LeavePartyModel from "../models/party/LeavePartyModel";
import SetActivePartyModel from "../models/party/SetActivePartyModel";
2024-09-23 23:55:00 +01:00
import PartyService from "../services/PartyService";
2024-09-19 00:41:40 +01:00
import UserService from "../services/UserService";
import Controller from "./Controller";
export default class PartyController extends Controller {
public async Create_Get() {
return this.view("createEdit");
}
public async Create_Post(createEditPartyViewModel: CreateEditPartyViewModel) {
if (typeof(createEditPartyViewModel.name) !== "string" || typeof(createEditPartyViewModel.partyRef) !== "string") {
return this.badRequest();
}
2024-09-23 23:55:00 +01:00
const party = await PartyService.GetPartyByPartyRef(createEditPartyViewModel.partyRef);
2024-09-19 00:41:40 +01:00
if (party) {
createEditPartyViewModel.message = "That Party ID is already taken!";
return this.view("createEdit", createEditPartyViewModel);
}
2024-09-23 23:55:00 +01:00
await PartyService.CreateParty(this.session.userId, createEditPartyViewModel.name, createEditPartyViewModel.partyRef);
2024-09-19 00:41:40 +01:00
return this.redirectToAction("index", "home");
}
public async Join_Get() {
return this.view();
}
public async Join_Post(joinPartyViewModel: JoinPartyViewModel) {
if (typeof(joinPartyViewModel.partyRef) !== "string") {
return this.badRequest();
}
2024-09-23 23:55:00 +01:00
const party = await PartyService.GetPartyByPartyRef(joinPartyViewModel.partyRef);
2024-09-19 00:41:40 +01:00
if (!party) {
joinPartyViewModel.message = "That Join Code / Party ID is invalid.";
return this.view(joinPartyViewModel);
}
const userPartyExisting = await UserService.GetUserPartyForUser(this.session.userId, party.Id);
if (userPartyExisting) {
joinPartyViewModel.message = "You are already in this party.";
return this.view(joinPartyViewModel);
}
await UserService.AddUserToParty(this.session.userId, party.Id);
return this.redirectToAction("index", "home");
}
public async Leave_Get(leavePartyModel: LeavePartyModel) {
const partyId = parseInt(leavePartyModel.id ?? "-1");
if (typeof(leavePartyModel.id) !== "string" || isNaN(partyId)) {
return this.badRequest();
}
await UserService.LeaveParty(this.session.userId, partyId);
return this.redirectToAction("index", "home");
}
public async SetActive_Get(setActivePartyModel: SetActivePartyModel) {
const partyId = parseInt(setActivePartyModel.id ?? "-1");
if (typeof(setActivePartyModel.id) !== "string" || isNaN(partyId)) {
return this.badRequest();
}
await UserService.SetActiveParty(this.session.userId, partyId);
return this.redirectToAction("index", "home");
}
public async Deactivate_Get() {
await UserService.DeactivateCurrentParty(this.session.userId);
return this.redirectToAction("index", "home");
}
2024-09-23 23:55:00 +01:00
public async Delete_Get(deletePartyModel:DeletePartyModel) {
const partyId = parseInt(deletePartyModel.id ?? "-1");
if (typeof(deletePartyModel.id) !== "string" || isNaN(partyId)) {
return this.badRequest();
}
await PartyService.DeleteParty(this.session.userId, partyId);
return this.redirectToAction("index", "home");
}
2024-09-19 00:41:40 +01:00
}