forked from OpenWF/SpaceNinjaServer
		
	feat: Clan creation & persistence (#146)
This commit is contained in:
		
							parent
							
								
									3acd8e9f74
								
							
						
					
					
						commit
						d07e99f0c9
					
				
							
								
								
									
										35
									
								
								src/controllers/api/createGuildController.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/controllers/api/createGuildController.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
 | 
					import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
				
			||||||
 | 
					import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
				
			||||||
 | 
					import { Guild } from "@/src/models/guildModel";
 | 
				
			||||||
 | 
					import { IGuild, ICreateGuildRequest } from "@/src/types/guildTypes";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const createGuildController: RequestHandler = async (req, res) => {
 | 
				
			||||||
 | 
					    let payload: ICreateGuildRequest = getJSONfromString(req.body.toString());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Create guild on database
 | 
				
			||||||
 | 
					    let guild = new Guild({
 | 
				
			||||||
 | 
					        Name: payload.guildName
 | 
				
			||||||
 | 
					    } satisfies IGuild);
 | 
				
			||||||
 | 
					    await guild.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Update inventory
 | 
				
			||||||
 | 
					    let inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
 | 
				
			||||||
 | 
					    if (inventory) {
 | 
				
			||||||
 | 
					        // Set GuildId
 | 
				
			||||||
 | 
					        inventory.GuildId = guild._id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Give clan key
 | 
				
			||||||
 | 
					        inventory.LevelKeys ??= [];
 | 
				
			||||||
 | 
					        inventory.LevelKeys.push({
 | 
				
			||||||
 | 
					            ItemType: "/Lotus/Types/Keys/DojoKey",
 | 
				
			||||||
 | 
					            ItemCount: 1
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await inventory.save();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    res.json(guild);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export { createGuildController };
 | 
				
			||||||
@ -1,6 +1,24 @@
 | 
				
			|||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
 | 
					import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
				
			||||||
 | 
					import { Guild } from "@/src/models/guildModel";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const getGuildController: RequestHandler = (_req, res) => {
 | 
					const getGuildController: RequestHandler = async (req, res) => {
 | 
				
			||||||
 | 
					    if (!req.query.accountId) {
 | 
				
			||||||
 | 
					        res.status(400).json({ error: "accountId was not provided" });
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    const inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
 | 
				
			||||||
 | 
					    if (!inventory) {
 | 
				
			||||||
 | 
					        res.status(400).json({ error: "inventory was undefined" });
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (inventory.GuildId) {
 | 
				
			||||||
 | 
					        const guild = await Guild.findOne({ _id: inventory.GuildId });
 | 
				
			||||||
 | 
					        if (guild) {
 | 
				
			||||||
 | 
					            res.json(guild);
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    res.json({});
 | 
					    res.json({});
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										16
									
								
								src/models/guildModel.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/models/guildModel.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					import { IGuild, IDatabaseGuild } from "@/src/types/guildTypes";
 | 
				
			||||||
 | 
					import { model, Schema } from "mongoose";
 | 
				
			||||||
 | 
					import { toOid } from "@/src/helpers/inventoryHelpers";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const guildSchema = new Schema<IGuild>({
 | 
				
			||||||
 | 
					    Name: { type: String, required: true }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					guildSchema.set("toJSON", {
 | 
				
			||||||
 | 
					    virtuals: true,
 | 
				
			||||||
 | 
					    transform(_document, guild) {
 | 
				
			||||||
 | 
					        guild._id = toOid(guild._id);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const Guild = model<IGuild>("Guild", guildSchema);
 | 
				
			||||||
@ -874,6 +874,9 @@ const inventorySchema = new Schema<IInventoryDatabase, InventoryDocumentProps>(
 | 
				
			|||||||
        //You first Dialog with NPC or use new Item
 | 
					        //You first Dialog with NPC or use new Item
 | 
				
			||||||
        NodeIntrosCompleted: [String],
 | 
					        NodeIntrosCompleted: [String],
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Current guild id, if applicable.
 | 
				
			||||||
 | 
					        GuildId: { type: Schema.Types.ObjectId, ref: "Guild" },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //https://warframe.fandom.com/wiki/Heist
 | 
					        //https://warframe.fandom.com/wiki/Heist
 | 
				
			||||||
        //ProfitTaker(1-4) Example:"LocationTag": "EudicoHeists", "Jobs":Mission name
 | 
					        //ProfitTaker(1-4) Example:"LocationTag": "EudicoHeists", "Jobs":Mission name
 | 
				
			||||||
        CompletedJobChains: [completedJobChainsSchema],
 | 
					        CompletedJobChains: [completedJobChainsSchema],
 | 
				
			||||||
@ -971,6 +974,9 @@ inventorySchema.set("toJSON", {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        inventoryResponse.TrainingDate = toMongoDate(inventoryDatabase.TrainingDate);
 | 
					        inventoryResponse.TrainingDate = toMongoDate(inventoryDatabase.TrainingDate);
 | 
				
			||||||
        inventoryResponse.Created = toMongoDate(inventoryDatabase.Created);
 | 
					        inventoryResponse.Created = toMongoDate(inventoryDatabase.Created);
 | 
				
			||||||
 | 
					        if (inventoryDatabase.GuildId) {
 | 
				
			||||||
 | 
					            inventoryResponse.GuildId = toOid(inventoryDatabase.GuildId);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        if (inventoryResponse.BlessingCooldown) {
 | 
					        if (inventoryResponse.BlessingCooldown) {
 | 
				
			||||||
            inventoryResponse.BlessingCooldown = toMongoDate(inventoryDatabase.BlessingCooldown);
 | 
					            inventoryResponse.BlessingCooldown = toMongoDate(inventoryDatabase.BlessingCooldown);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -43,6 +43,7 @@ import { setActiveShipController } from "@/src/controllers/api/setActiveShipCont
 | 
				
			|||||||
import { updateThemeController } from "../controllers/api/updateThemeController";
 | 
					import { updateThemeController } from "../controllers/api/updateThemeController";
 | 
				
			||||||
import { getGuildController } from "@/src/controllers/api/getGuildController";
 | 
					import { getGuildController } from "@/src/controllers/api/getGuildController";
 | 
				
			||||||
import { addFriendImageController } from "@/src/controllers/api/addFriendImageController";
 | 
					import { addFriendImageController } from "@/src/controllers/api/addFriendImageController";
 | 
				
			||||||
 | 
					import { createGuildController } from "@/src/controllers/api/createGuildController";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const apiRouter = express.Router();
 | 
					const apiRouter = express.Router();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -96,5 +97,6 @@ apiRouter.post("/saveLoadout.php", saveLoadoutController);
 | 
				
			|||||||
apiRouter.post("/trainingResult.php", trainingResultController);
 | 
					apiRouter.post("/trainingResult.php", trainingResultController);
 | 
				
			||||||
apiRouter.post("/updateTheme.php", updateThemeController);
 | 
					apiRouter.post("/updateTheme.php", updateThemeController);
 | 
				
			||||||
apiRouter.post("/addFriendImage.php", addFriendImageController);
 | 
					apiRouter.post("/addFriendImage.php", addFriendImageController);
 | 
				
			||||||
 | 
					apiRouter.post("/createGuild.php", createGuildController);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export { apiRouter };
 | 
					export { apiRouter };
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										18
									
								
								src/types/guildTypes.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/types/guildTypes.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
 | 
					import { IOid } from "@/src/types/commonTypes";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface IGuild {
 | 
				
			||||||
 | 
					    Name: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface IDatabaseGuild extends IGuild {
 | 
				
			||||||
 | 
					    _id: Types.ObjectId;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface IGuildResponse extends IGuild {
 | 
				
			||||||
 | 
					    _id: IOid;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface ICreateGuildRequest {
 | 
				
			||||||
 | 
					    guildName: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -21,6 +21,7 @@ export interface IInventoryDatabase
 | 
				
			|||||||
        | "TrainingDate"
 | 
					        | "TrainingDate"
 | 
				
			||||||
        | "LoadOutPresets"
 | 
					        | "LoadOutPresets"
 | 
				
			||||||
        | "Mailbox"
 | 
					        | "Mailbox"
 | 
				
			||||||
 | 
					        | "GuildId"
 | 
				
			||||||
        | "PendingRecipes"
 | 
					        | "PendingRecipes"
 | 
				
			||||||
        | "Created"
 | 
					        | "Created"
 | 
				
			||||||
        | "QuestKeys"
 | 
					        | "QuestKeys"
 | 
				
			||||||
@ -32,6 +33,7 @@ export interface IInventoryDatabase
 | 
				
			|||||||
    TrainingDate: Date; // TrainingDate changed from IMongoDate to Date
 | 
					    TrainingDate: Date; // TrainingDate changed from IMongoDate to Date
 | 
				
			||||||
    LoadOutPresets: Types.ObjectId; // LoadOutPresets changed from ILoadOutPresets to Types.ObjectId for population
 | 
					    LoadOutPresets: Types.ObjectId; // LoadOutPresets changed from ILoadOutPresets to Types.ObjectId for population
 | 
				
			||||||
    Mailbox: Types.ObjectId; // Mailbox changed from IMailbox to Types.ObjectId
 | 
					    Mailbox: Types.ObjectId; // Mailbox changed from IMailbox to Types.ObjectId
 | 
				
			||||||
 | 
					    GuildId?: Types.ObjectId; // GuildId changed from ?IOid to ?Types.ObjectId
 | 
				
			||||||
    PendingRecipes: IPendingRecipe[];
 | 
					    PendingRecipes: IPendingRecipe[];
 | 
				
			||||||
    QuestKeys: IQuestKeyDatabase[];
 | 
					    QuestKeys: IQuestKeyDatabase[];
 | 
				
			||||||
    BlessingCooldown: Date;
 | 
					    BlessingCooldown: Date;
 | 
				
			||||||
@ -232,6 +234,7 @@ export interface IInventoryResponse {
 | 
				
			|||||||
    RecentVendorPurchases: Array<number | string>;
 | 
					    RecentVendorPurchases: Array<number | string>;
 | 
				
			||||||
    Hoverboards: IHoverboard[];
 | 
					    Hoverboards: IHoverboard[];
 | 
				
			||||||
    NodeIntrosCompleted: string[];
 | 
					    NodeIntrosCompleted: string[];
 | 
				
			||||||
 | 
					    GuildId?: IOid;
 | 
				
			||||||
    CompletedJobChains: ICompletedJobChain[];
 | 
					    CompletedJobChains: ICompletedJobChain[];
 | 
				
			||||||
    SeasonChallengeHistory: ISeasonChallengeHistory[];
 | 
					    SeasonChallengeHistory: ISeasonChallengeHistory[];
 | 
				
			||||||
    MoaPets: IMoaPet[];
 | 
					    MoaPets: IMoaPet[];
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user