mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-17 10:53:57 +01:00
feat: start of all tanks
This commit is contained in:
1 parent
53d05e73a6
commit
fcfc97b964
14 files changed
+174
-58
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import type { Tank } from "../../protos/tank";
|
||||
import type { TankList } from "../../protos/tank_list";
|
||||
import type { Tanks } from "../../protos/tanks";
|
||||
|
||||
export abstract class AbstractAPI {
|
||||
abstract tanks(): Promise<Tanks>;
|
||||
|
||||
abstract tankList(): Promise<TankList>;
|
||||
|
||||
abstract strings(
|
||||
locale: string,
|
||||
prefix?: string
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { fetchPB } from "@blitzkit/core";
|
||||
import { Tank } from "../../protos/tank";
|
||||
import { TankList } from "../../protos/tank_list";
|
||||
import { Tanks } from "../../protos/tanks";
|
||||
import { AbstractAPI } from "./abstract";
|
||||
|
||||
export class ClientAPI extends AbstractAPI {
|
||||
async tank(id: string) {
|
||||
const response = await fetch(`/api/tanks/${id}.pb`);
|
||||
const buffer = await response.arrayBuffer();
|
||||
const array = new Uint8Array(buffer);
|
||||
tanks() {
|
||||
return fetchPB("/api/tanks.pb", Tanks);
|
||||
}
|
||||
|
||||
return Tank.decode(array);
|
||||
tankList() {
|
||||
return fetchPB("/api/tanks/list.pb", TankList);
|
||||
}
|
||||
|
||||
tank(id: string) {
|
||||
return fetchPB(`/api/tanks/${id}.pb`, Tank);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { sluggify } from "@blitzkit/core";
|
||||
import type { RemoteStorageComponent } from "@protos/blitz_static_remote_storage_component";
|
||||
import { parse } from "yaml";
|
||||
import type { Tank } from "../../protos/tank";
|
||||
import type { TanksEntry } from "../../protos/tanks";
|
||||
import type { TankListEntry } from "../../protos/tank_list";
|
||||
import type { Tanks } from "../../protos/tanks";
|
||||
import { AbstractAPI } from "./abstract";
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
@@ -91,9 +92,9 @@ export class ServerAPI extends AbstractAPI {
|
||||
return strings;
|
||||
}
|
||||
|
||||
async tanks() {
|
||||
async tankList() {
|
||||
const group = this.metadata.group("TankEntity");
|
||||
const tanks: TanksEntry[] = [];
|
||||
const list: TankListEntry[] = [];
|
||||
const strings = await this.strings("en");
|
||||
|
||||
for (const item of group) {
|
||||
@@ -108,17 +109,37 @@ export class ServerAPI extends AbstractAPI {
|
||||
const id = item.name;
|
||||
const slug = sluggify(name);
|
||||
|
||||
tanks.push({ id, slug });
|
||||
list.push({ id, slug });
|
||||
}
|
||||
|
||||
return { tanks };
|
||||
return { list };
|
||||
}
|
||||
|
||||
async tanks() {
|
||||
const tankList = await this.tankList();
|
||||
const data: Tanks = { tanks: {} };
|
||||
|
||||
for (const { id } of tankList.list) {
|
||||
console.log(id);
|
||||
data.tanks[id] = await this.tank(id);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
async tank(id: string) {
|
||||
const tankList = await this.tankList();
|
||||
const tankListEntry = tankList.list.find((tank) => tank.id === id);
|
||||
|
||||
if (!tankListEntry) {
|
||||
throw new Error(`Tank with id ${id} not found`);
|
||||
}
|
||||
|
||||
const { slug } = tankListEntry;
|
||||
const item = this.metadata.item(`TankEntity.${id}`);
|
||||
const tank = item.TankCatalog();
|
||||
const compensation = item.Compensation();
|
||||
|
||||
return { tank, compensation } satisfies Tank;
|
||||
return { tank, compensation, slug } satisfies Tank;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@ import {
|
||||
} from "./characteristics";
|
||||
import type { TankState } from "./tankState";
|
||||
|
||||
export type ComputedCharacteristics = Record<
|
||||
CharacteristicName,
|
||||
CharacteristicOutput
|
||||
>;
|
||||
|
||||
export function computeCharacteristics(
|
||||
id: string,
|
||||
tank: TankCatalogComponent,
|
||||
@@ -23,8 +28,7 @@ export function computeCharacteristics(
|
||||
tank.upgrade_stages.slice(0, state.stage)
|
||||
);
|
||||
|
||||
const computed: Partial<Record<CharacteristicName, CharacteristicOutput>> =
|
||||
{};
|
||||
const computed: Partial<ComputedCharacteristics> = {};
|
||||
|
||||
function characteristic(name: CharacteristicName) {
|
||||
if (name in computed) return computed[name] as CharacteristicOutput;
|
||||
@@ -99,10 +103,7 @@ export function computeCharacteristics(
|
||||
}
|
||||
|
||||
return {
|
||||
characteristics: computed as Record<
|
||||
CharacteristicName,
|
||||
CharacteristicOutput
|
||||
>,
|
||||
characteristics: computed as ComputedCharacteristics,
|
||||
parameters,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { TankCatalogComponent } from "@protos/blitz_static_tank_component";
|
||||
import { TerrainHardness } from "./tankState";
|
||||
|
||||
export function createTankState(tank: TankCatalogComponent) {
|
||||
return {
|
||||
stage: tank.upgrade_stages.length,
|
||||
shell: 0,
|
||||
isGunDamaged: false,
|
||||
isHullTraversing: false,
|
||||
isShooting: false,
|
||||
isTurretTraversing: false,
|
||||
speed: 0,
|
||||
terrainHardness: TerrainHardness.Hard,
|
||||
isCaughtOnFire: false,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user