import LRU from 'lru-cache' ; import superagent from 'superagent' ; import { UserBuyMenu } from 'user/userbuymenu' ; import { UserCosmetics } from 'user/usercosmetics' ; import { UserInventoryItem } from 'user/userinventoryitem' ; import { UserLoadout } from 'user/userloadout' ; import { userSvcAuthority , UserSvcPing } from 'authorities' ; export class UserInventory { param userId returns public static async create ( userId ) { if ( UserSvcPing . isAlive ( ) === false ) { return false ; } const invPromises = [ UserInventory . createInventory ( userId ) , UserInventory . createCosmetics ( userId ) , UserInventory . createBuyMenu ( userId ) , UserInventory . createLoadouts ( userId ) ] ; const results = await Promise . all ( invPromises ) ; return results . includes ( false ) !== false ; } param ownerId returns public static async createInventory ( ownerId ) { try { const res = await superagent . post ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }` ) . accept ( 'json' ) ; return res . status === 201 ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return false ; } } param ownerId returns public static async createCosmetics ( ownerId ) { try { const res = await superagent . post ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/cosmetics` ) . accept ( 'json' ) ; return res . status === 201 ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return false ; } } param ownerId returns public static async createLoadouts ( ownerId ) { try { const res = await superagent . post ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/loadout` ) . accept ( 'json' ) ; return res . status === 201 ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return false ; } } param ownerId returns public static async createBuyMenu ( ownerId ) { try { const res = await superagent . post ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/buymenu` ) . accept ( 'json' ) ; return res . status === 201 ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param ownerId public static async getInventory ( ownerId ) { try { const inventory = inventoryCache . get ( ownerId ) ; if ( inventory != null ) { return inventory ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }` ) . accept ( 'json' ) ; if ( res . status === 200 ) { inventoryCache . set ( ownerId , res . body ) ; return res . body as UserInventory ; } return null ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param ownerId public static async getCosmetics ( ownerId ) { try { const cosmetics = cosmeticsCache . get ( ownerId ) ; if ( cosmetics != null ) { return cosmetics ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/cosmetics` ) . accept ( 'json' ) ; if ( res . status === 200 ) { cosmeticsCache . set ( ownerId , res . body ) ; return res . body as UserCosmetics ; } return null ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param ownerId param loadoutNum public static async getLoadout ( ownerId , loadoutNum ) { try { const res = await superagent . get ( userSvcAuthority ( ) + `/inventory/${ ownerId }/loadout/${ loadoutNum }` ) . send ( ) . accept ( 'json' ) ; if ( res . ok !== true ) { return null ; } return res . body as UserLoadout ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param ownerId public static async getAllLoadouts ( ownerId ) { let loadouts = loadoutsCache . get ( ownerId ) ; if ( loadouts != null ) { return loadouts ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const loadoutPromises = [ ] ; for ( let i = 0 ; i < 3 ; i ++ ) { loadoutPromises . push ( UserInventory . getLoadout ( ownerId , i ) ) ; } loadouts = await Promise . all ( loadoutPromises ) ; loadoutsCache . set ( ownerId , loadouts ) ; return loadouts ; } param ownerId public static async getBuyMenu ( ownerId ) { try { const buymenu = buymenuCache . get ( ownerId ) ; if ( buymenu != null ) { return buymenu ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/buymenu` ) . accept ( 'json' ) ; if ( res . status === 200 ) { buymenuCache . set ( ownerId , res . body ) ; return res . body as UserBuyMenu ; } return null ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param ownerId param slot param itemId public static async setCosmeticSlot ( ownerId , slot , itemId ) { const params = UserInventory . buildSetCosmeticParams ( slot , itemId ) ; try { const res = await superagent . put ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/cosmetics` ) . send ( params ) . accept ( 'json' ) ; if ( res . status === 200 ) { cosmeticsCache . del ( ownerId ) ; console . log ( 'Set cosmetic item successfully' ) ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } } param ownerId param loadout param slot param itemId public static async setLoadoutWeapon ( ownerId , loadout , slot , itemId ) { const params = UserInventory . buildSetLoadoutParams ( slot , itemId ) ; try { const res = await superagent . put ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/loadout/${ loadout }` ) . send ( params ) . accept ( 'json' ) ; if ( res . status === 200 ) { loadoutsCache . del ( ownerId ) ; console . log ( 'Set loadout weapon successfully' ) ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } } param ownerId param column param items public static async setBuyMenu ( ownerId , newBuyMenu ) { try { const res = await superagent . put ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/buymenu` ) . send ( newBuyMenu ) . accept ( 'json' ) ; if ( res . status === 200 ) { buymenuCache . del ( ownerId ) ; console . log ( 'Set buy menu successfully' ) ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } } param ownerId param column param items public static async setBuyMenuColumn ( ownerId , column , items ) { const params = UserInventory . buildSetBuyMenuParams ( column , items ) ; try { const res = await superagent . put ( `${ userSvcAuthority ( ) }/inventory/${ ownerId }/buymenu` ) . send ( params ) . accept ( 'json' ) ; if ( res . status === 200 ) { buymenuCache . del ( ownerId ) ; console . log ( 'Set buy menu column successfully' ) ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } } private static buildSetCosmeticParams ( slot , itemId ) { switch ( slot ) { case 0 : return { ct_item : itemId } ; case 1 : return { ter_item : itemId } ; case 2 : return { head_item : itemId } ; case 3 : return { glove_item : itemId } ; case 4 : return { back_item : itemId } ; case 5 : return { steps_item : itemId } ; case 6 : return { card_item : itemId } ; case 7 : return { spray_item : itemId } ; } console . error ( 'Bad item slot for cosmetics' ) ; return null ; } private static buildSetLoadoutParams ( slot , itemId ) { switch ( slot ) { case 0 : return { primary_weapon : itemId } ; case 1 : return { secondary_weapon : itemId } ; case 2 : return { melee : itemId } ; case 3 : return { hegrenade : itemId } ; case 4 : return { smoke : itemId } ; case 5 : return { flash : itemId } ; } console . error ( 'Bad item slot for loadout' ) ; return null ; } private static buildSetBuyMenuParams ( slot , items ) { switch ( slot ) { case 0 : return { pistols : items } ; case 1 : return { shotguns : items } ; case 2 : return { smgs : items } ; case 3 : return { rifles : items } ; case 4 : return { snipers : items } ; case 5 : return { machineguns : items } ; case 6 : return { melees : items } ; case 7 : return { equipment : items } ; } console . error ( 'Bad column for buy menu' ) ; return null ; } public owner_id : number ; public items : UserInventoryItem [ ] ; } const inventoryCache = new LRU < number , UserInventory > ( { max : 15 , maxAge : 1000 * 15 } ) ; const cosmeticsCache = new LRU < number , UserCosmetics > ( { max : 30 , maxAge : 1000 * 15 } ) ; const loadoutsCache = new LRU < number , UserLoadout [ ] > ( { max : 30 , maxAge : 1000 * 15 } ) ; const buymenuCache = new LRU < number , UserBuyMenu > ( { max : 30 , maxAge : 1000 * 15 } ) ;