Skip to content

Commit

Permalink
Separate routing logic and business logic (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
RDIL authored Jan 13, 2024
1 parent abd9ada commit 0e66d69
Show file tree
Hide file tree
Showing 30 changed files with 2,040 additions and 1,728 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/reece.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 0 additions & 43 deletions components/2016/legacyEventRouter.ts

This file was deleted.

217 changes: 1 addition & 216 deletions components/2016/legacyMenuData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,227 +19,13 @@
import { Router } from "express"
import { RequestWithJwt } from "../types/types"
import { getConfig } from "../configSwizzleManager"
import { getDefaultSuitFor, uuidRegex } from "../utils"
import { json as jsonMiddleware } from "body-parser"
import { controller } from "../controller"
import {
generateUserCentric,
getParentLocationByName,
getSubLocationByName,
} from "../contracts/dataGen"
import { getUserData } from "../databaseHandler"
import { log, LogLevel } from "../loggingInterop"
import { createInventory, getUnlockableById } from "../inventory"
import { getFlag } from "../flags"
import { loadouts } from "../loadouts"
import { StashpointQueryH2016, StashpointSlotName } from "../types/gameSchemas"
import { getParentLocationByName } from "../contracts/dataGen"

const legacyMenuDataRouter = Router()

legacyMenuDataRouter.get(
"/stashpoint",
(req: RequestWithJwt<StashpointQueryH2016>, res) => {
if (!uuidRegex.test(req.query.contractid)) {
res.status(400).send("contract id was not a uuid")
return
}

if (typeof req.query.slotname !== "string") {
res.status(400).send("invalid slot data")
return
}

const contractData = controller.resolveContract(req.query.contractid)

if (!contractData) {
res.status(404).send("contract not found")
return
}

const loadoutSlots: StashpointSlotName[] = [
"carriedweapon",
"carrieditem",
"concealedweapon",
"disguise",
"gear",
"gear",
"stashpoint",
]

if (loadoutSlots.includes(req.query.slotname.slice(0, -1))) {
req.query.slotid = req.query.slotname.slice(0, -1)
} else {
log(
LogLevel.ERROR,
`Unknown slotname in legacy stashpoint: ${req.query.slotname}`,
)
return
}

const userProfile = getUserData(req.jwt.unique_name, req.gameVersion)

const sublocation = getSubLocationByName(
contractData.Metadata.Location,
req.gameVersion,
)

const inventory = createInventory(
req.jwt.unique_name,
req.gameVersion,
sublocation,
)

const userCentricContract = generateUserCentric(
contractData,
req.jwt.unique_name,
"h1",
)

const defaultLoadout = {
2: "FIREARMS_HERO_PISTOL_TACTICAL_001_SU_SKIN01",
3: getDefaultSuitFor(sublocation),
4: "TOKEN_FIBERWIRE",
5: "PROP_TOOL_COIN",
}

const getLoadoutItem = (id: number) => {
if (getFlag("loadoutSaving") === "LEGACY") {
const dl = userProfile.Extensions.defaultloadout

if (!dl) {
return defaultLoadout[id]
}

const forLocation = (userProfile.Extensions.defaultloadout ||
{})[sublocation?.Properties?.ParentLocation]

if (!forLocation) {
return defaultLoadout[id]
}

return forLocation[id]
} else {
let dl = loadouts.getLoadoutFor("h1")

if (!dl) {
dl = loadouts.createDefault("h1")
}

const forLocation =
dl.data[sublocation?.Properties?.ParentLocation]

if (!forLocation) {
return defaultLoadout[id]
}

return forLocation[id]
}
}

res.json({
template: getConfig("LegacyStashpointTemplate", false),
data: {
ContractId: req.query.contractid,
// the game actually only needs the loadoutdata from the requested slotid, but this is what IOI servers do
LoadoutData: [...loadoutSlots.entries()].map(
([slotid, slotname]) => ({
SlotName: slotname,
SlotId: slotid.toString(),
Items: inventory
.filter((item) => {
return (
item.Unlockable.Properties.LoadoutSlot && // only display items
(item.Unlockable.Properties.LoadoutSlot ===
slotname || // display items for requested slot
(slotname === "stashpoint" && // else: if stashpoint
item.Unlockable.Properties
.LoadoutSlot !== "disguise")) && // => display all non-disguise items
(req.query.allowlargeitems === "true" ||
item.Unlockable.Properties.ItemSize === // regular gear slot or hidden stash => small item
"ITEMSIZE_SMALL" ||
(!item.Unlockable.Properties.ItemSize &&
item.Unlockable.Properties
.LoadoutSlot !== // use old logic if itemsize is not set
"carriedweapon")) &&
item.Unlockable.Type !==
"challengemultipler" &&
!item.Unlockable.Properties.InclusionData
) // not sure about this one
})
.map((item) => ({
Item: item,
ItemDetails: {
Capabilities: [],
StatList: item.Unlockable.Properties
.Gameplay
? Object.entries(
item.Unlockable.Properties
.Gameplay,
).map(([key, value]) => ({
Name: key,
Ratio: value,
}))
: [],
PropertyTexts: [],
},
SlotId: slotid.toString(),
SlotName: slotname,
})),
Page: 0,
Recommended: getLoadoutItem(slotid)
? {
item: getUnlockableById(
getLoadoutItem(slotid),
req.gameVersion,
),
type: loadoutSlots[slotid],
owned: true,
}
: null,
HasMore: false,
HasMoreLeft: false,
HasMoreRight: false,
OptionalData:
slotid === 6
? {
stashpoint: req.query.stashpoint,
AllowLargeItems:
req.query.allowlargeitems ||
!req.query.stashpoint,
}
: {},
}),
),
Contract: userCentricContract.Contract,
ShowSlotName: req.query.slotname,
UserCentric: userCentricContract,
},
})
},
)

legacyMenuDataRouter.get("/Safehouse", (req: RequestWithJwt, res, next) => {
const template = getConfig("LegacySafehouseTemplate", false)

// call /SafehouseCategory but rewrite the result a bit
req.url = `/SafehouseCategory?page=0&type=${req.query.type}&subtype=`
const originalJsonFunc = res.json

res.json = function json(originalData) {
return originalJsonFunc.call(this, {
template,
data: {
SafehouseData: originalData.data,
},
})
}

next()
})

legacyMenuDataRouter.get(
"/debriefingchallenges",
jsonMiddleware(),
(
req: RequestWithJwt<{ contractSessionId: string; contractId: string }>,
res,
Expand Down Expand Up @@ -269,7 +55,6 @@ legacyMenuDataRouter.get(

legacyMenuDataRouter.get(
"/MasteryLocation",
jsonMiddleware(),
(req: RequestWithJwt<{ locationId: string; difficulty: string }>, res) => {
const masteryData =
controller.masteryService.getMasteryDataForDestination(
Expand Down
11 changes: 0 additions & 11 deletions components/2016/legacyMenuSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import serveStatic from "serve-static"
import { Router } from "express"
import { join } from "path"
import md5File from "md5-file"
import { readFile } from "atomically"
import { imageFetchingMiddleware } from "../menus/imageHandler"
import { MenuSystemDatabase } from "../menus/menuSystem"

const legacyMenuSystemRouter = Router()

Expand All @@ -46,12 +43,4 @@ legacyMenuSystemRouter.get(
},
)

legacyMenuSystemRouter.use(MenuSystemDatabase.configMiddleware)

legacyMenuSystemRouter.use(
"/images/",
serveStatic("images", { fallthrough: true }),
imageFetchingMiddleware,
)

export { legacyMenuSystemRouter }
19 changes: 0 additions & 19 deletions components/2016/legacyProfileRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,15 @@ import { getConfig } from "../configSwizzleManager"

import { Router } from "express"
import { controller } from "../controller"
import { getPlatformEntitlements } from "../platformEntitlements"
import { json as jsonMiddleware } from "body-parser"
import { uuidRegex } from "../utils"
import { menuSystemDatabase } from "../menus/menuSystem"
import { compileRuntimeChallenge } from "../candle/challengeHelpers"
import { LegacyGetProgressionBody } from "../types/gameSchemas"

const legacyProfileRouter = Router()

// /authentication/api/userchannel/

legacyProfileRouter.post(
"/ProfileService/GetPlatformEntitlements",
jsonMiddleware(),
getPlatformEntitlements,
)

legacyProfileRouter.post(
"/AuthenticationService/GetBlobOfflineCacheDatabaseDiff",
(req: RequestWithJwt, res) => {
const configs: string[] = []

menuSystemDatabase.hooks.getDatabaseDiff.call(configs, req.gameVersion)

res.json(configs)
},
)

legacyProfileRouter.post(
"/ChallengesService/GetActiveChallenges",
jsonMiddleware(),
Expand Down
3 changes: 2 additions & 1 deletion components/candle/challengeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { SavedChallengeGroup } from "../types/challenges"
import { controller } from "../controller"
import { gameDifficulty, isSniperLocation } from "../utils"

// TODO: unused?
export function compileScoringChallenge(
challenge: RegistryChallenge,
): CompiledChallengeRewardData {
Expand Down Expand Up @@ -68,7 +69,7 @@ export function compileRuntimeChallenge(
}

export enum ChallengeFilterType {
// Note that this option will include global elusives and escalations challenges.
/** Note that this option will include global elusive and escalations challenges. */
None = "None",
Contract = "Contract",
/** Only used for the CAREER -> CHALLENGES page */
Expand Down
2 changes: 2 additions & 0 deletions components/candle/masteryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export class MasteryService {
)[0]
}

// TODO: what do we want to do with this? We should prob remove the template part
// to make this like the other routes, and more testable.
getMasteryDataForLocation(
locationId: string,
gameVersion: GameVersion,
Expand Down
7 changes: 2 additions & 5 deletions components/configSwizzleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export function getVersionedConfig<T = unknown>(
if (
// is this scpc, do we have a scpc config?
gameVersion === "scpc" &&
Object.prototype.hasOwnProperty.call(configs, `Scpc${config}`)
Object.hasOwn(configs, `Scpc${config}`)
) {
h1Prefix = "Scpc"
} else {
Expand All @@ -291,10 +291,7 @@ export function getVersionedConfig<T = unknown>(
}

// if this is H2, but we don't have a h2 specific config, fall back to h3
if (
gameVersion === "h2" &&
!Object.prototype.hasOwnProperty.call(configs, `H2${config}`)
) {
if (gameVersion === "h2" && !Object.hasOwn(configs, `H2${config}`)) {
return getConfig(config, clone)
}

Expand Down
Loading

0 comments on commit 0e66d69

Please sign in to comment.