Skip to content

Commit

Permalink
short
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese committed Jul 28, 2023
1 parent 9b8e617 commit 9277156
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"scripts": {
"lint": "eslint . --ext .ts",
"webviewer": "tsc && node ./webviewer/server.js"
"webviewer": "tsc && node webviewer/server.js"
},
"main": "index.js",
"repository": {
Expand Down
19 changes: 5 additions & 14 deletions src/hyper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@ class VectorN {

type GeneralSpace = VectorN[];

function gen_patterns(d: string): number[] {
return d.split('').map((value) => (value === '0' ? -1 : 1));
}

function symmetry_points(rank: number, point: VectorN): GeneralSpace {
const res: GeneralSpace = [];
for (let i = 0; i < 2 ** rank; i++) {
let m = i.toString(2);
m = '0'.repeat(rank - m.length) + m;
const x = gen_patterns(m);
const y = point.map((y, i) => y * x[i]);
res.push(y);
}
for (let i = 0; i < 1 << rank; ++i)
res.push(point.map((y, j) => ((i >> (rank - j - 1)) & 1 ? y : -y)));
return res;
}

Expand All @@ -56,9 +47,9 @@ function symmetry(rank: number, space: GeneralSpace): GeneralSpace {

function test_ball(radius: number) {
const result = [];
for (let x = 0; x <= radius; x++) {
for (let y = 0; y <= radius; y++) {
for (let z = 0; z <= radius; z++) {
for (let x = 0; x <= radius; ++x) {
for (let y = 0; y <= radius; ++y) {
for (let z = 0; z <= radius; ++z) {
if (x * x + y * y + z * z <= radius * radius) {
result.push(new VectorN(x, y, z));
}
Expand Down
50 changes: 45 additions & 5 deletions webviewer/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as ws from 'ws';
import readline from 'readline';
import { stdin, stdout } from 'process';
import express from 'express';
import path, { resolve } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { dirname, join, extname, resolve } from 'path';
import { readdirSync, renameSync, unlinkSync, mkdirSync, statSync, existsSync } from 'fs';
import {
Vec3,
vec3,
Expand All @@ -20,7 +20,7 @@ import {
Turtle2D,
Turtle3D,
Symmetry
} from '../index.js';
} from './dist/index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -32,6 +32,44 @@ function pack(bs) {
};
}

function rmdirSync(dirpath) {
if (existsSync(dirpath) && statSync(dirpath).isDirectory()) {
readdirSync(dirpath).forEach(function (file) {
const curPath = join(dirpath, file);
if (statSync(curPath).isDirectory()) {
rmdirSync(curPath);
} else {
unlinkSync(curPath);
}
});
rmdirSync(dirpath);
}
}

function removeCache() {
rmdirSync('./dist');
}

function moveScripts(sourceDir, targetDir, excludeDirs) {
const entries = readdirSync(sourceDir, { withFileTypes: true });

entries.forEach((entry) => {
const sourcePath = join(sourceDir, entry.name);
const targetPath = join(targetDir, entry.name);

if (entry.isDirectory()) {
if (!excludeDirs.includes(entry.name)) {
moveScripts(sourcePath, targetPath, excludeDirs);
}
} else if (entry.name.endsWith('.js') || entry.name.endsWith('.d.ts')) {
if (!existsSync(targetDir)) {
mkdirSync(targetDir, { recursive: true });
}
renameSync(sourcePath, targetPath);
}
});
}

function handleWebSocket() {
const wss = new ws.WebSocketServer({ port: 2333 });

Expand Down Expand Up @@ -67,13 +105,15 @@ function handleWebSocket() {
function handleHttpServer() {
const app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.use('/node_modules', express.static(path.join(resolve(__dirname, '../'), 'node_modules')));
app.use(express.static(join(__dirname, 'public')));
app.use('/node_modules', express.static(join(resolve(__dirname, '../'), 'node_modules')));

app.listen(8080, () => {
console.log('Server is running. You can access it at http://localhost:8080.');
});
}

removeCache();
moveScripts(resolve(__dirname, '..'), resolve(__dirname, 'dist'), ['node_modules', 'webviewer']);
handleWebSocket();
handleHttpServer();

0 comments on commit 9277156

Please sign in to comment.