alles fertig
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
|
||||
Default to using Bun instead of Node.js.
|
||||
|
||||
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
|
||||
- Use `bun test` instead of `jest` or `vitest`
|
||||
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
|
||||
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
|
||||
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
|
||||
- Bun automatically loads .env, so don't use dotenv.
|
||||
|
||||
## APIs
|
||||
|
||||
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
|
||||
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
|
||||
- `Bun.redis` for Redis. Don't use `ioredis`.
|
||||
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
|
||||
- `WebSocket` is built-in. Don't use `ws`.
|
||||
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
|
||||
- Bun.$`ls` instead of execa.
|
||||
|
||||
## Testing
|
||||
|
||||
Use `bun test` to run tests.
|
||||
|
||||
```ts#index.test.ts
|
||||
import { test, expect } from "bun:test";
|
||||
|
||||
test("hello world", () => {
|
||||
expect(1).toBe(1);
|
||||
});
|
||||
```
|
||||
|
||||
## Frontend
|
||||
|
||||
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
|
||||
|
||||
Server:
|
||||
|
||||
```ts#index.ts
|
||||
import index from "./index.html"
|
||||
|
||||
Bun.serve({
|
||||
routes: {
|
||||
"/": index,
|
||||
"/api/users/:id": {
|
||||
GET: (req) => {
|
||||
return new Response(JSON.stringify({ id: req.params.id }));
|
||||
},
|
||||
},
|
||||
},
|
||||
// optional websocket support
|
||||
websocket: {
|
||||
open: (ws) => {
|
||||
ws.send("Hello, world!");
|
||||
},
|
||||
message: (ws, message) => {
|
||||
ws.send(message);
|
||||
},
|
||||
close: (ws) => {
|
||||
// handle close
|
||||
}
|
||||
},
|
||||
development: {
|
||||
hmr: true,
|
||||
console: true,
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
|
||||
|
||||
```html#index.html
|
||||
<html>
|
||||
<body>
|
||||
<h1>Hello, world!</h1>
|
||||
<script type="module" src="./frontend.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
With the following `frontend.tsx`:
|
||||
|
||||
```tsx#frontend.tsx
|
||||
import React from "react";
|
||||
|
||||
// import .css files directly and it works
|
||||
import './index.css';
|
||||
|
||||
import { createRoot } from "react-dom/client";
|
||||
|
||||
const root = createRoot(document.body);
|
||||
|
||||
export default function Frontend() {
|
||||
return <h1>Hello, world!</h1>;
|
||||
}
|
||||
|
||||
root.render(<Frontend />);
|
||||
```
|
||||
|
||||
Then, run index.ts
|
||||
|
||||
```sh
|
||||
bun --hot ./index.ts
|
||||
```
|
||||
|
||||
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.
|
||||
|
||||
## Coding Style Preferences
|
||||
|
||||
- Write everything WITHOUT comments.
|
||||
@@ -1,8 +0,0 @@
|
||||
# Projekt-Dokumentation: Entwicklungsreihenfolge
|
||||
|
||||
1. Projekt init
|
||||
2. Docker compose mit redis
|
||||
3. Einfache verbindung
|
||||
4. Redis getestet (mit set, get und incr)
|
||||
5. Init-Skript (db/init.ts) mit ~100 statischen Entitäten erstellt (für reproduzierbare Tests)
|
||||
6. Client (index.ts) aktualisiert Player-Daten live und liest statische Gegner-Daten
|
||||
@@ -31,11 +31,11 @@ async function readSingle(_i: number) {
|
||||
}
|
||||
|
||||
async function readRegion(_i: number) {
|
||||
await redis.hget("region:1", "name");
|
||||
const enemyIds = await redis.smembers("region:1:enemies");
|
||||
const itemIds = await redis.smembers("region:1:items");
|
||||
for (const id of enemyIds) await redis.hgetall(`enemy:${id}`);
|
||||
for (const id of itemIds) await redis.hgetall(`item:${id}`);
|
||||
await redis.hget("bench:region:1", "name");
|
||||
const enemyIds = await redis.smembers("bench:region:1:enemies");
|
||||
const itemIds = await redis.smembers("bench:region:1:items");
|
||||
for (const id of enemyIds) await redis.hgetall(`bench:enemy:${id}`);
|
||||
for (const id of itemIds) await redis.hgetall(`bench:item:${id}`);
|
||||
}
|
||||
|
||||
async function writeRegion(_i: number) {
|
||||
|
||||
+115
-114
@@ -2,21 +2,21 @@ import { RedisClient } from "bun";
|
||||
const redis = new RedisClient("redis://localhost:40379");
|
||||
|
||||
const colors = {
|
||||
reset: "\x1b[0m",
|
||||
cyan: "\x1b[36m",
|
||||
green: "\x1b[32m",
|
||||
yellow: "\x1b[33m",
|
||||
magenta: "\x1b[35m",
|
||||
red: "\x1b[31m",
|
||||
gray: "\x1b[90m",
|
||||
bold: "\x1b[1m",
|
||||
reset: "\x1b[0m",
|
||||
cyan: "\x1b[36m",
|
||||
green: "\x1b[32m",
|
||||
yellow: "\x1b[33m",
|
||||
magenta: "\x1b[35m",
|
||||
red: "\x1b[31m",
|
||||
gray: "\x1b[90m",
|
||||
bold: "\x1b[1m",
|
||||
};
|
||||
|
||||
let totalRequests = 0;
|
||||
|
||||
function log(msg: string, color: string = colors.reset) {
|
||||
const timestamp = new Date().toISOString().split('T')[1].slice(0, -1);
|
||||
console.log(`${colors.gray}[${timestamp}]${colors.reset} ${color}${msg}${colors.reset}`);
|
||||
const timestamp = new Date().toISOString().split('T')[1].slice(0, -1);
|
||||
console.log(`${colors.gray}[${timestamp}]${colors.reset} ${color}${msg}${colors.reset}`);
|
||||
}
|
||||
|
||||
const introMessage = "Starting Ringstats Desktop Application...";
|
||||
@@ -51,110 +51,111 @@ const CHANCE_TARGET_LOCK = 0.30;
|
||||
log("Entering live sync loop (press Ctrl+C to exit)...", colors.gray);
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
// 1. Lebens- und Ausdauerwerte simulieren
|
||||
playerHealth = Math.floor(Math.max(10, playerHealth - (Math.random() * 10) + 5));
|
||||
playerStamina = Math.floor(Math.max(10, playerStamina - (Math.random() * 20) + 10));
|
||||
|
||||
if (Math.random() < CHANCE_LEVEL_UP) {
|
||||
playerLevel += 1;
|
||||
try {
|
||||
// 1. Lebens- und Ausdauerwerte simulieren
|
||||
playerHealth = Math.floor(Math.max(10, playerHealth - (Math.random() * 10) + 5));
|
||||
playerStamina = Math.floor(Math.max(10, playerStamina - (Math.random() * 20) + 10));
|
||||
|
||||
if (Math.random() < CHANCE_LEVEL_UP) {
|
||||
playerLevel += 1;
|
||||
}
|
||||
|
||||
// 2. Zufälliges Event: Schnellreise in eine andere Region
|
||||
if (Math.random() < CHANCE_FAST_TRAVEL) {
|
||||
currentRegionId = Math.floor(Math.random() * 16) + 1;
|
||||
|
||||
const startTravel = performance.now();
|
||||
const regionName = await redis.hget(`region:${currentRegionId}`, "name");
|
||||
const regionEnemiesIds = await redis.smembers(`region:${currentRegionId}:enemies`); // [293, 1, 31]
|
||||
const regionItemsIds = await redis.smembers(`region:${currentRegionId}:items`); // [1, 2, 3, 4]
|
||||
|
||||
// ([1, 2, 3, 4]) => name
|
||||
const enemyNames = await Promise.all(regionEnemiesIds.map(id => redis.hget(`enemy:${id}`, "name")));
|
||||
const itemNames = await Promise.all(regionItemsIds.map(id => redis.hget(`item:${id}`, "name")));
|
||||
|
||||
const queries = 3 + regionEnemiesIds.length + regionItemsIds.length;
|
||||
totalRequests += queries;
|
||||
const travelTime = (performance.now() - startTravel).toFixed(2);
|
||||
|
||||
console.log();
|
||||
log(`>>> Fast Traveled to: [ID: ${currentRegionId}] ${regionName} <<<`, colors.magenta + colors.bold);
|
||||
log(`[Region Data] Enemies nearby: ${enemyNames.join(", ") || "None"}`, colors.magenta);
|
||||
log(`[Region Data] Items nearby: ${itemNames.join(", ") || "None"}`, colors.magenta);
|
||||
log(`[Stats] Query Time: ${travelTime}ms | Reqs in Block: ${queries} | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
|
||||
// 3. Zufälliges Event: Ausrüstung wechseln
|
||||
if (Math.random() < CHANCE_GEAR_SWAP) {
|
||||
const newWeaponId = Math.floor(Math.random() * 10) + 1; // 1 to 10
|
||||
const newArmorId = Math.floor(Math.random() * 10) + 11; // 11 to 20
|
||||
const newTalismanId = Math.floor(Math.random() * 5) + 26; // 26 to 30
|
||||
|
||||
const startSwap = performance.now();
|
||||
await redis.del("player:1:weapon", "player:1:armor", "player:1:talisman");
|
||||
await redis.sadd("player:1:weapon", newWeaponId.toString());
|
||||
await redis.sadd("player:1:armor", newArmorId.toString());
|
||||
await redis.sadd("player:1:talisman", newTalismanId.toString());
|
||||
|
||||
const weaponName = await redis.hget(`item:${newWeaponId}`, "name");
|
||||
const armorName = await redis.hget(`item:${newArmorId}`, "name");
|
||||
const talismanName = await redis.hget(`item:${newTalismanId}`, "name");
|
||||
|
||||
totalRequests += 7; // 1 del + 3 sadd + 3 hget
|
||||
const swapTime = (performance.now() - startSwap).toFixed(2);
|
||||
|
||||
console.log();
|
||||
log(`>>> Gear Swapped <<<`, colors.cyan + colors.bold);
|
||||
log(`Equipped: ${weaponName} | ${armorName} | ${talismanName}`, colors.cyan);
|
||||
log(`[Stats] Query Time: ${swapTime}ms | Reqs in Block: 7 | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
|
||||
// 4. Aktuelle Spielerdaten an Redis senden
|
||||
const startSync = performance.now();
|
||||
await redis.hset("player:1", {
|
||||
id: 1,
|
||||
name: "Jonas",
|
||||
health: playerHealth,
|
||||
stamina: playerStamina,
|
||||
level: playerLevel,
|
||||
region_id: currentRegionId
|
||||
});
|
||||
totalRequests += 1;
|
||||
const syncTime = (performance.now() - startSync).toFixed(2);
|
||||
|
||||
log(`[Live Sync] [ID: 1] Jonas - Lvl: ${playerLevel} | HP: ${playerHealth} | Stamina: ${playerStamina}`, colors.green);
|
||||
log(`[Stats] Query Time: ${syncTime}ms | Reqs in Block: 1 | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
|
||||
// 5. Zufälliges Event: Gegner anvisieren
|
||||
if (Math.random() < CHANCE_TARGET_LOCK) {
|
||||
const startLock = performance.now();
|
||||
const randomEnemyId = await redis.srandmember(`region:${currentRegionId}:enemies`);
|
||||
totalRequests += 1;
|
||||
|
||||
if (randomEnemyId) {
|
||||
console.log();
|
||||
log("--- Target Lock Detected ---", colors.red + colors.bold);
|
||||
const enemyInfo = await redis.hgetall(`enemy:${randomEnemyId}`);
|
||||
|
||||
const dropItemIds = await redis.smembers(`enemy:${randomEnemyId}:items`);
|
||||
const dropItemNames = await Promise.all(dropItemIds.map(id => redis.hget(`item:${id}`, "name")));
|
||||
|
||||
const queries = 2 + dropItemIds.length;
|
||||
totalRequests += queries;
|
||||
const lockTime = (performance.now() - startLock).toFixed(2);
|
||||
|
||||
log(`Overlay -> [ID: ${enemyInfo.id}] ${enemyInfo.name} | Lvl: ${enemyInfo.level} | HP: ${enemyInfo.health} | DMG: ${enemyInfo.damage}`, colors.yellow);
|
||||
log(`Drops -> ${dropItemNames.length > 0 ? dropItemNames.join(", ") : "None"}`, colors.yellow);
|
||||
log(`[Stats] Query Time: ${lockTime}ms | Reqs in Block: ${queries + 1} | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
log("Datenbankfehler! Verbindung zu Redis unterbrochen.", colors.red);
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// 2. Zufälliges Event: Schnellreise in eine andere Region
|
||||
if (Math.random() < CHANCE_FAST_TRAVEL) {
|
||||
currentRegionId = Math.floor(Math.random() * 16) + 1;
|
||||
|
||||
const startTravel = performance.now();
|
||||
const regionName = await redis.hget(`region:${currentRegionId}`, "name");
|
||||
const regionEnemiesIds = await redis.smembers(`region:${currentRegionId}:enemies`);
|
||||
const regionItemsIds = await redis.smembers(`region:${currentRegionId}:items`);
|
||||
|
||||
const enemyNames = await Promise.all(regionEnemiesIds.map(id => redis.hget(`enemy:${id}`, "name")));
|
||||
const itemNames = await Promise.all(regionItemsIds.map(id => redis.hget(`item:${id}`, "name")));
|
||||
|
||||
const queries = 3 + regionEnemiesIds.length + regionItemsIds.length;
|
||||
totalRequests += queries;
|
||||
const travelTime = (performance.now() - startTravel).toFixed(2);
|
||||
|
||||
console.log();
|
||||
log(`>>> Fast Traveled to: [ID: ${currentRegionId}] ${regionName} <<<`, colors.magenta + colors.bold);
|
||||
log(`[Region Data] Enemies nearby: ${enemyNames.join(", ") || "None"}`, colors.magenta);
|
||||
log(`[Region Data] Items nearby: ${itemNames.join(", ") || "None"}`, colors.magenta);
|
||||
log(`[Stats] Query Time: ${travelTime}ms | Reqs in Block: ${queries} | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
|
||||
// 3. Zufälliges Event: Ausrüstung wechseln
|
||||
if (Math.random() < CHANCE_GEAR_SWAP) {
|
||||
const newWeaponId = Math.floor(Math.random() * 10) + 1; // 1 to 10
|
||||
const newArmorId = Math.floor(Math.random() * 10) + 11; // 11 to 20
|
||||
const newTalismanId = Math.floor(Math.random() * 5) + 26; // 26 to 30
|
||||
|
||||
const startSwap = performance.now();
|
||||
await redis.del("player:1:weapon", "player:1:armor", "player:1:talisman");
|
||||
await redis.sadd("player:1:weapon", newWeaponId.toString());
|
||||
await redis.sadd("player:1:armor", newArmorId.toString());
|
||||
await redis.sadd("player:1:talisman", newTalismanId.toString());
|
||||
|
||||
const weaponName = await redis.hget(`item:${newWeaponId}`, "name");
|
||||
const armorName = await redis.hget(`item:${newArmorId}`, "name");
|
||||
const talismanName = await redis.hget(`item:${newTalismanId}`, "name");
|
||||
|
||||
totalRequests += 7; // 1 del + 3 sadd + 3 hget
|
||||
const swapTime = (performance.now() - startSwap).toFixed(2);
|
||||
|
||||
console.log();
|
||||
log(`>>> Gear Swapped <<<`, colors.cyan + colors.bold);
|
||||
log(`Equipped: ${weaponName} | ${armorName} | ${talismanName}`, colors.cyan);
|
||||
log(`[Stats] Query Time: ${swapTime}ms | Reqs in Block: 7 | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
|
||||
// 4. Aktuelle Spielerdaten an Redis senden
|
||||
const startSync = performance.now();
|
||||
await redis.hset("player:1", {
|
||||
id: 1,
|
||||
name: "Jonas",
|
||||
health: playerHealth,
|
||||
stamina: playerStamina,
|
||||
level: playerLevel,
|
||||
region_id: currentRegionId
|
||||
});
|
||||
totalRequests += 1;
|
||||
const syncTime = (performance.now() - startSync).toFixed(2);
|
||||
|
||||
log(`[Live Sync] [ID: 1] Jonas - Lvl: ${playerLevel} | HP: ${playerHealth} | Stamina: ${playerStamina}`, colors.green);
|
||||
log(`[Stats] Query Time: ${syncTime}ms | Reqs in Block: 1 | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
|
||||
// 5. Zufälliges Event: Gegner anvisieren
|
||||
if (Math.random() < CHANCE_TARGET_LOCK) {
|
||||
const startLock = performance.now();
|
||||
const randomEnemyId = await redis.srandmember(`region:${currentRegionId}:enemies`);
|
||||
totalRequests += 1;
|
||||
|
||||
if (randomEnemyId) {
|
||||
console.log();
|
||||
log("--- Target Lock Detected ---", colors.red + colors.bold);
|
||||
const enemyInfo = await redis.hgetall(`enemy:${randomEnemyId}`);
|
||||
|
||||
const dropItemIds = await redis.smembers(`enemy:${randomEnemyId}:items`);
|
||||
const dropItemNames = await Promise.all(dropItemIds.map(id => redis.hget(`item:${id}`, "name")));
|
||||
|
||||
const queries = 2 + dropItemIds.length;
|
||||
totalRequests += queries;
|
||||
const lockTime = (performance.now() - startLock).toFixed(2);
|
||||
|
||||
log(`Overlay -> [ID: ${enemyInfo.id}] ${enemyInfo.name} | Lvl: ${enemyInfo.level} | HP: ${enemyInfo.health} | DMG: ${enemyInfo.damage}`, colors.yellow);
|
||||
log(`Drops -> ${dropItemNames.length > 0 ? dropItemNames.join(", ") : "None"}`, colors.yellow);
|
||||
log(`[Stats] Query Time: ${lockTime}ms | Reqs in Block: ${queries + 1} | Total Reqs: ${totalRequests}`, colors.gray);
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
log("Datenbankfehler! Verbindung zu Redis unterbrochen.", colors.red);
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
await Bun.sleep(2000);
|
||||
await Bun.sleep(2000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user