Skip to content

Commit

Permalink
feat: find map seed from a player name (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha authored Sep 27, 2021
1 parent 8f6c750 commit 4763715
Show file tree
Hide file tree
Showing 100 changed files with 1,745 additions and 187 deletions.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"scripts": {
"build": "tsc -b",
Expand Down
3 changes: 2 additions & 1 deletion packages/bintools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand All @@ -19,7 +20,7 @@
"dependencies": {
"@diablo2/data": "^0.5.0",
"@diablo2/mpq": "^0.5.0",
"binparse": "^1.0.1",
"binparse": "^1.2.1",
"ulid": "^2.3.0"
},
"publishConfig": {
Expand Down
8 changes: 4 additions & 4 deletions packages/bintools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { LangReader, LangNode } from './lang/lang.reader';
export { MonsterReader, MonsterNode } from './monster/monster.stat.reader';
export { LangReader, LangNode } from './lang/lang.reader.js';
export { MonsterReader, MonsterNode } from './monster/monster.stat.reader.js';

export { Diablo2MpqLoader } from './mpq.loader';
export { Logger } from './log.type';
export { Diablo2MpqLoader } from './mpq.loader.js';
export { Logger } from './log.type.js';
4 changes: 2 additions & 2 deletions packages/bintools/src/item/item.reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bp } from 'binparse';
import { StrutTypeStringFixed } from 'binparse/build/src/string';
import { StrutParserContext, StrutParserInput } from 'binparse/build/src/type';
import { StrutTypeStringFixed } from 'binparse/build/src/string.js';
import { StrutParserContext, StrutParserInput } from 'binparse/build/src/type.js';

/**
* Generally item codes are its 3 letters plus a space eg `cap `
Expand Down
2 changes: 1 addition & 1 deletion packages/bintools/src/lang/__test__/lang.reader.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs';
import o from 'ospec';
import { LangReader } from '../lang.reader';
import { LangReader } from '../lang.reader.js';

o.spec('LangReader', () => {
o('should read v1 language tables', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/bintools/src/mpq.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Mpq } from '@diablo2/mpq';
import { StrutInfer, StrutType } from 'binparse';
import { promises as fs } from 'fs';
import * as path from 'path';
import { ItemFileParser } from './item/item.reader';
import { LangReader } from './lang/lang.reader';
import { Logger } from './log.type';
import { MonsterReader, MonsterReader2 } from './monster/monster.stat.reader';
import { ItemFileParser } from './item/item.reader.js';
import { LangReader } from './lang/lang.reader.js';
import { Logger } from './log.type.js';
import { MonsterReader, MonsterReader2 } from './monster/monster.stat.reader.js';

async function readAndParse<T extends StrutType<any>>(
mpq: Mpq,
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__test__/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Diablo2Version } from '@diablo2/data';
import o from 'ospec';
import 'source-map-support/register';
import { Diablo2Client } from '../client';
import 'source-map-support/register.js';
import { Diablo2Client } from '../client.js';

const GamePackets = [
{ data: [175, 1] },
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/__test__/packets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import url from 'url';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
import { Diablo2Version } from '@diablo2/data';
import * as fs from 'fs';
import * as path from 'path';
import 'source-map-support/register';
import { Diablo2Client } from '../client';
import 'source-map-support/register.js';
import { Diablo2Client } from '../client.js';

const packetPath = path.join(__dirname, '..', '..', '..', '..', 'test-data', '2020-08-24T05:51-packets.json');

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Diablo2MpqLoader, Logger } from '@diablo2/bintools';
import { Diablo2Mpq, Diablo2MpqData, Diablo2Version } from '@diablo2/data';
import { Diablo2PacketFactory, getDiabloPackets } from '@diablo2/packets';
import { Diablo2GameSession } from './game.state';
import { Diablo2GameSession } from './game.state.js';

export class Diablo2Client {
mpq: Diablo2MpqData = Diablo2Mpq;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/game.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Logger } from '@diablo2/bintools';
import { ItemQuality } from '@diablo2/data';
import { PacketsPod } from '@diablo2/packets';
import { ulid } from 'ulid';
import { Diablo2Client } from './client';
import { Diablo2PacketParser } from './packet.parser';
import { Diablo2State } from './state/game';
import { ItemJson } from './state/json';
import { Diablo2Client } from './client.js';
import { Diablo2PacketParser } from './packet.parser.js';
import { Diablo2State } from './state/game.js';
import { ItemJson } from './state/json.js';

const { client, server } = PacketsPod;

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Diablo2Client } from './client';
export { Diablo2GameSession } from './game.state';
export type { GameStateJson, NpcJson, ItemJson, KillJson } from './state/json';
export { Diablo2Client } from './client.js';
export { Diablo2GameSession } from './game.state.js';
export { Diablo2State } from './state/game.js';
export type { GameStateJson, NpcJson, ItemJson, KillJson } from './state/json.js';
2 changes: 1 addition & 1 deletion packages/core/src/packet.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toHex } from '@diablo2/data';
import { Huffman } from '@diablo2/huffman';
import { Diablo2Packet, Diablo2ParsedPacket } from '@diablo2/packets';
import { EventEmitter } from 'events';
import { Diablo2Client } from './client';
import { Diablo2Client } from './client.js';

/** Buffer packets until the required amount have been received */
export class PacketBuffer {
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/state/game.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@diablo2/bintools';
import { Difficulty } from '@diablo2/data';
import { Diablo2ParsedPacket } from '@diablo2/packets';
import * as GameJson from './json';
import * as GameJson from './json.js';

type OnCloseEvent = (game: Diablo2State) => void;
const MaxAgeMs = 5 * 60_000;
Expand Down Expand Up @@ -142,18 +142,21 @@ export class Diablo2State {
if (this.isMe(id)) this.player.level = level;
}

movePlayer(pkt: Diablo2ParsedPacket<unknown>, id: number, x: number, y: number): void {
movePlayer(pkt: Diablo2ParsedPacket<unknown> | undefined, id: number, x: number, y: number): void {
// console.log('MovePlayer', pkt.packet.name, id, x, y);
if (x === 0 || y === 0) return;
const player = this.players.get(id);
if (player == null) return;
this.log.debug({ packet: pkt.packet.name, player: player.name, id, x, y }, 'Player:Move');

player.updatedAt = Date.now();
if (player.x === x && player.y === y) return;
this.log.debug({ packet: pkt?.packet.name, player: player.name, id, x, y }, 'Player:Move');

player.x = x;
player.y = y;
player.updatedAt = Date.now();
this.dirty();
}

trackKill(u: GameJson.NpcJson): void {
if (u.code === -1) return;
let kill = this.kills.get(u.code);
Expand Down
1 change: 1 addition & 0 deletions packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum Attribute {
Stam = 10,
StamMax = 11,
CurrentLevel = 12,
Experience = 12,
Experience = 13,
GoldInPlayer = 14,
GoldInStash = 15,
}
22 changes: 11 additions & 11 deletions packages/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export { Attribute } from './attribute';
export { Act } from './act';
export { Difficulty } from './difficulty';
export { UnitType, UnitVisibility } from './unit';
export { GameObjectMode, GameObjectInteraction } from './game.object';
export { ItemActionType, ItemCategory, ItemDestination, ItemQuality, ItemContainer } from './item';
export { PlayerClass } from './player.class';
export { WarpType } from './warp';
export { Diablo2Mpq, Diablo2MpqData } from './mpq/mpq';
export { Diablo2Version, getDiabloVersion } from './version';
export { NpcFlags, NpcEnchant } from './npc';
export { Attribute } from './attribute.js';
export { Act } from './act.js';
export { Difficulty } from './difficulty.js';
export { UnitType, UnitVisibility } from './unit.js';
export { GameObjectMode, GameObjectInteraction } from './game.object.js';
export { ItemActionType, ItemCategory, ItemDestination, ItemQuality, ItemContainer } from './item.js';
export { PlayerClass } from './player.class.js';
export { WarpType } from './warp.js';
export { Diablo2Mpq, Diablo2MpqData } from './mpq/mpq.js';
export { Diablo2Version, getDiabloVersion } from './version.js';
export { NpcFlags, NpcEnchant } from './npc.js';

export { toHex } from 'binparse';
2 changes: 1 addition & 1 deletion packages/data/src/mpq/mpq.item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Diablo2MpqData } from './mpq';
import { Diablo2MpqData } from './mpq.js';

export interface Diablo2Item {
code: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/mpq/mpq.lang.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Diablo2MpqData } from './mpq';
import { Diablo2MpqData } from './mpq.js';

export const ExpansionOffset = 20000;
export const PatchOffset = 10000;
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/mpq/mpq.monster.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Diablo2MpqData } from './mpq';
import { Diablo2MpqData } from './mpq.js';

export interface Diablo2MpqMonster {
id: number;
Expand Down
6 changes: 3 additions & 3 deletions packages/data/src/mpq/mpq.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Diablo2MpqItem } from './mpq.item';
import { Diablo2MpqLang } from './mpq.lang';
import { Diablo2MpqMonsters } from './mpq.monster';
import { Diablo2MpqItem } from './mpq.item.js';
import { Diablo2MpqLang } from './mpq.lang.js';
import { Diablo2MpqMonsters } from './mpq.monster.js';

export class Diablo2MpqData {
/** Last path to load MPQ data from */
Expand Down
1 change: 1 addition & 0 deletions packages/huffman/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/huffman/src/__test__/decompress.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import o from 'ospec';
import { Huffman } from '..';
import { Huffman } from './../index.js';

const PacketDataRaw = `8a8CyEODRn35rWxlBuZHZfCshXMkoE52PBDDuBVAdGjXNK2MsEYiBINj48h+XRuPQqkIPZgYhpRA
VQBuaJ+aVsZYIRECQbHx5QpQfSAKBDDuBVAG5obc0rYyyCEQJAeHx5QpYAgqrR7bAqgaNDfmkgIy
Expand Down
1 change: 1 addition & 0 deletions packages/map/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
2 changes: 1 addition & 1 deletion packages/map/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ FROM tianon/wine

RUN apt-get update -yq \
&& apt-get install curl gnupg -yq \
&& curl -sL https://deb.nodesource.com/setup_12.x | bash \
&& curl -sL https://deb.nodesource.com/setup_16.x | bash \
&& apt-get install nodejs -yq

ENV WINEARCH=win32
Expand Down
24 changes: 0 additions & 24 deletions packages/map/map.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/map/map/offset.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const char *dlls[] = {
"Fog.DLL",
"BNClient.DLL",
"Storm.DLL",
"D2Cmp.DLL"};
"D2Cmp.DLL"
};

DWORD GetDllOffset(const char *DllName, int Offset) {
try {
Expand Down
4 changes: 3 additions & 1 deletion packages/map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"name": "Blayne Chard",
"email": "[email protected]"
},
"type": "module",
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand All @@ -34,6 +35,7 @@
"@types/cors": "^2.8.7",
"@types/express": "^4.17.8",
"@types/node": "^14.10.1",
"esbuild": "^0.11.11"
"esbuild": "^0.11.11",
"maplibre-gl": "^2.0.0-pre.1"
}
}
12 changes: 6 additions & 6 deletions packages/map/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import cors from 'cors';
import * as express from 'express';
import * as fs from 'fs';
import 'source-map-support/register';
import 'source-map-support/register.js';
import * as ulid from 'ulid';
import { Log } from './logger';
import { Diablo2Path, MapCommand, MapProcess } from './map/map.process';
import { HttpError, Request, Response, Route } from './route';
import { HealthRoute } from './routes/health';
import { MapRoute } from './routes/map';
import { Log } from './logger.js';
import { Diablo2Path, MapCommand, MapProcess } from './map/map.process.js';
import { HttpError, Request, Response, Route } from './route.js';
import { HealthRoute } from './routes/health.js';
import { MapRoute } from './routes/map.js';

if (!fs.existsSync(MapCommand)) {
Log.fatal(`Cannot find ${MapCommand}`);
Expand Down
17 changes: 9 additions & 8 deletions packages/map/src/map/lru.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
export class LruCache<T> {
private values: Map<string, T> = new Map<string, T>();
private valuesOld: Map<string, T> = new Map<string, T>();
private maxEntries: number;

constructor(maxEntries: number) {
this.maxEntries = maxEntries;
}

public get(key: string): T | undefined {
const entry = this.values.get(key);
if (entry != null) {
// peek the entry, re-insert for LRU strategy
this.values.delete(key);
let entry = this.values.get(key);
if (entry == null) {
entry = this.valuesOld.get(key);
if (entry == null) return undefined;
this.valuesOld.delete(key);
this.values.set(key, entry);
}

return entry;
}

public put(key: string, value: T): void {
public set(key: string, value: T): void {
if (this.values.size >= this.maxEntries) {
// least-recently used cache eviction strategy
const keyToDelete = this.values.keys().next().value;
this.values.delete(keyToDelete);
this.valuesOld = this.values;
this.values = new Map();
}

this.values.set(key, value);
Expand Down
8 changes: 4 additions & 4 deletions packages/map/src/map/map.process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ChildProcess, spawn, spawnSync } from 'child_process';
import { EventEmitter } from 'events';
import PLimit from 'p-limit';
import { createInterface } from 'readline';
import { Log, LogType } from '../logger';
import { LruCache } from './lru';
import { Diablo2Map, Diablo2MapGenMessage, MapGenMessageInfo, MapGenMessageMap } from './map';
import { Log, LogType } from '../logger.js';
import { LruCache } from './lru.js';
import { Diablo2Map, Diablo2MapGenMessage, MapGenMessageInfo, MapGenMessageMap } from './map.js';

export const MapCommand = './bin/d2-map.exe';
export const Diablo2Path = '/app/game';
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Diablo2MapProcess {
if (cacheData != null) return Promise.resolve(cacheData);
return this.q(async () => {
const mapResult = await this.getMaps(seed, difficulty, log);
this.cache.put(mapKey, mapResult);
this.cache.set(mapKey, mapResult);
return mapResult;
});
}
Expand Down
13 changes: 12 additions & 1 deletion packages/map/src/map/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface Diablo2Map {
id: string;
id: number;
map: number[][];
objects: { id: number; type: 'object'; x: number; y: number }[];
offset: { x: number; y: number };
size: { width: number; height: number };
}
export interface MapGenMessageInit {
type: 'init';
Expand All @@ -20,3 +24,10 @@ export interface MapGenMessageDone {
}

export type Diablo2MapGenMessage = MapGenMessageDone | MapGenMessageMap | MapGenMessageInfo | MapGenMessageInit;

export interface MapRouteResponse {
id: string;
seed: number;
difficulty: number;
maps: Record<number, Diablo2Map>;
}
Empty file.
Loading

0 comments on commit 4763715

Please sign in to comment.