forked from OpenWF/SpaceNinjaServer
		
	feat: divvy alliance vault (#1455)
Reviewed-on: OpenWF/SpaceNinjaServer#1455 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									c18abab9c4
								
							
						
					
					
						commit
						b3374eb66e
					
				
							
								
								
									
										74
									
								
								src/controllers/api/divvyAllianceVaultController.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/controllers/api/divvyAllianceVaultController.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,74 @@
 | 
			
		||||
import { Alliance, AllianceMember, Guild, GuildMember } from "@/src/models/guildModel";
 | 
			
		||||
import { getAccountForRequest } from "@/src/services/loginService";
 | 
			
		||||
import { GuildPermission } from "@/src/types/guildTypes";
 | 
			
		||||
import { logger } from "@/src/utils/logger";
 | 
			
		||||
import { RequestHandler } from "express";
 | 
			
		||||
 | 
			
		||||
export const divvyAllianceVaultController: RequestHandler = async (req, res) => {
 | 
			
		||||
    // Afaict, there's no way to put anything other than credits in the alliance vault (anymore?), so just no-op if this is not a request to divvy credits.
 | 
			
		||||
    if (req.query.credits == "1") {
 | 
			
		||||
        // Check requester is a warlord in their guild
 | 
			
		||||
        const account = await getAccountForRequest(req);
 | 
			
		||||
        const guildMember = (await GuildMember.findOne({ accountId: account._id, status: 0 }))!;
 | 
			
		||||
        if (guildMember.rank > 1) {
 | 
			
		||||
            res.status(400).end();
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Check guild has treasurer permissions in the alliance
 | 
			
		||||
        const allianceMember = (await AllianceMember.findOne({
 | 
			
		||||
            allianceId: req.query.allianceId,
 | 
			
		||||
            guildId: guildMember.guildId
 | 
			
		||||
        }))!;
 | 
			
		||||
        if (!(allianceMember.Permissions & GuildPermission.Treasurer)) {
 | 
			
		||||
            res.status(400).end();
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const allianceMembers = await AllianceMember.find({ allianceId: req.query.allianceId });
 | 
			
		||||
        const memberCounts: Record<string, number> = {};
 | 
			
		||||
        let totalMembers = 0;
 | 
			
		||||
        await parallelForeach(allianceMembers, async allianceMember => {
 | 
			
		||||
            const memberCount = await GuildMember.countDocuments({
 | 
			
		||||
                guildId: allianceMember.guildId
 | 
			
		||||
            });
 | 
			
		||||
            memberCounts[allianceMember.guildId.toString()] = memberCount;
 | 
			
		||||
            totalMembers += memberCount;
 | 
			
		||||
        });
 | 
			
		||||
        logger.debug(`alliance has ${totalMembers} members between all its clans`);
 | 
			
		||||
 | 
			
		||||
        const alliance = (await Alliance.findById(allianceMember.allianceId, "VaultRegularCredits"))!;
 | 
			
		||||
        if (alliance.VaultRegularCredits) {
 | 
			
		||||
            let creditsHandedOutInTotal = 0;
 | 
			
		||||
            await parallelForeach(allianceMembers, async allianceMember => {
 | 
			
		||||
                const memberCount = memberCounts[allianceMember.guildId.toString()];
 | 
			
		||||
                const cutPercentage = memberCount / totalMembers;
 | 
			
		||||
                const creditsToHandOut = Math.trunc(alliance.VaultRegularCredits! * cutPercentage);
 | 
			
		||||
                logger.debug(
 | 
			
		||||
                    `${allianceMember.guildId.toString()} has ${memberCount} member(s) = ${Math.trunc(cutPercentage * 100)}% of alliance; giving ${creditsToHandOut} credit(s)`
 | 
			
		||||
                );
 | 
			
		||||
                if (creditsToHandOut != 0) {
 | 
			
		||||
                    await Guild.updateOne(
 | 
			
		||||
                        { _id: allianceMember.guildId },
 | 
			
		||||
                        { $inc: { VaultRegularCredits: creditsToHandOut } }
 | 
			
		||||
                    );
 | 
			
		||||
                    creditsHandedOutInTotal += creditsToHandOut;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            alliance.VaultRegularCredits -= creditsHandedOutInTotal;
 | 
			
		||||
            logger.debug(
 | 
			
		||||
                `handed out ${creditsHandedOutInTotal} credits; alliance vault now has ${alliance.VaultRegularCredits} credit(s)`
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        await alliance.save();
 | 
			
		||||
    }
 | 
			
		||||
    res.end();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const parallelForeach = async <T>(data: T[], op: (datum: T) => Promise<void>): Promise<void> => {
 | 
			
		||||
    const promises: Promise<void>[] = [];
 | 
			
		||||
    for (const datum of data) {
 | 
			
		||||
        promises.push(op(datum));
 | 
			
		||||
    }
 | 
			
		||||
    await Promise.all(promises);
 | 
			
		||||
};
 | 
			
		||||
@ -33,6 +33,7 @@ import { declineAllianceInviteController } from "@/src/controllers/api/declineAl
 | 
			
		||||
import { declineGuildInviteController } from "@/src/controllers/api/declineGuildInviteController";
 | 
			
		||||
import { deleteSessionController } from "@/src/controllers/api/deleteSessionController";
 | 
			
		||||
import { destroyDojoDecoController } from "@/src/controllers/api/destroyDojoDecoController";
 | 
			
		||||
import { divvyAllianceVaultController } from "@/src/controllers/api/divvyAllianceVaultController";
 | 
			
		||||
import { dojoComponentRushController } from "@/src/controllers/api/dojoComponentRushController";
 | 
			
		||||
import { dojoController } from "@/src/controllers/api/dojoController";
 | 
			
		||||
import { dronesController } from "@/src/controllers/api/dronesController";
 | 
			
		||||
@ -149,6 +150,7 @@ apiRouter.get("/credits.php", creditsController);
 | 
			
		||||
apiRouter.get("/declineAllianceInvite.php", declineAllianceInviteController);
 | 
			
		||||
apiRouter.get("/declineGuildInvite.php", declineGuildInviteController);
 | 
			
		||||
apiRouter.get("/deleteSession.php", deleteSessionController);
 | 
			
		||||
apiRouter.get("/divvyAllianceVault.php", divvyAllianceVaultController);
 | 
			
		||||
apiRouter.get("/dojo", dojoController);
 | 
			
		||||
apiRouter.get("/drones.php", dronesController);
 | 
			
		||||
apiRouter.get("/getDailyDealStockLevels.php", getDailyDealStockLevelsController);
 | 
			
		||||
 | 
			
		||||
@ -318,7 +318,5 @@ export interface IAllianceMemberDatabase {
 | 
			
		||||
    Permissions: number;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: GET /api/divvyAllianceVault.php?accountId=6633b81e9dba0b714f28ff02&nonce=5702391171614479&ct=MSI&guildId=663e9be9f741eeb5782f9df0&allianceId=000000000000000000000069&credits=1
 | 
			
		||||
// TODO: GET /api/divvyAllianceVault.php?accountId=6633b81e9dba0b714f28ff02&nonce=5702391171614479&ct=MSI&guildId=663e9be9f741eeb5782f9df0&allianceId=000000000000000000000069&credits=0
 | 
			
		||||
// TODO: GET /api/removeFromAlliance.php?accountId=6633b81e9dba0b714f28ff02&nonce=5702391171614479&ct=MSI&guildId=663e9be9f741eeb5782f9df0
 | 
			
		||||
// TODO: GET /api/setAllianceGuildPermissions.php?accountId=6633b81e9dba0b714f28ff02&nonce=5702391171614479&ct=MSI&guildId=000000000000000000000042&perms=2
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user