arc-agents
Version:
A library for creating and deploying gaming agents at scale
92 lines (81 loc) • 2.33 kB
JavaScript
const axios = require('axios')
const { BACKEND } = require('../constants')
class Registry {
static apiKey = ""
static backend = BACKEND
constructor(gameId) {
this.gameId = gameId
}
static getApiKey() {
return Registry.apiKey
}
static setApiKey(apiKey) {
Registry.apiKey = apiKey
}
static overrideBackendUrl(newBackend) {
Registry.backend = newBackend
}
static getHeaders() {
return {
// origin: "http://localhost:3000",
"x-api-key": Registry.apiKey
}
}
async fetchRegistrySlots() {
try {
const query = `gameId=${this.gameId}`
const registeredSlotsResult = await axios.get(`${Registry.backend}admin/get-registered-slots?${query}`)
this.registrySlots = registeredSlotsResult.data.registeredSlots
}
catch (err) {
console.log(err.response?.data)
this.registrySlots = []
}
this.maxSlots = 5 // Will be pulled from server when fetching slots
}
async unregister(architectureId) {
try {
const registerResult = await axios.post(
`${Registry.backend}admin/unregister-model`,
{
gameId: this.gameId,
architectureId
},
{ headers: Registry.getHeaders() }
)
this.registrySlots = registerResult.data?.registeredSlots
}
catch (err) {
console.log(err.response?.data)
}
}
async register(registrationInputs) {
try {
const registerResult = await axios.post(
`${Registry.backend}admin/register-model`,
{ gameId: this.gameId, ...registrationInputs },
{ headers: Registry.getHeaders() }
)
this.registrySlots = registerResult.data?.registeredSlots
}
catch (err) {
console.log(err.response?.data)
}
}
async printSlots() {
if (this.registrySlots === undefined) {
await this.fetchRegistrySlots()
}
let slotIdx = 0
console.log("")
this.registrySlots.forEach((slot) => {
slotIdx += 1
console.log(`Slot ${slotIdx}: ${slot.architectureId}`)
})
const emptySlots = [...Array(this.maxSlots - slotIdx).keys()]
emptySlots.forEach((i) => {
console.log(`Slot ${slotIdx + i + 1}: empty`)
})
}
}
module.exports = Registry