first commit

This commit is contained in:
Soph :3 2024-07-16 23:54:30 +03:00
commit c6f462ca45
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
8 changed files with 1611 additions and 0 deletions

175
.gitignore vendored Normal file
View file

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

27
README.md Normal file
View file

@ -0,0 +1,27 @@
# pianoverse client for TS
## install
run `bun add https://git.sad.ovh/sophie/pianoverse`
then import `pianoverse`
## requirements
1. bun
2. a brain
## example
```ts
import { Client } from "pianoverse"
const client = new Client("https://pianoverse.net");
client.on("open", () => {
client.setRoom("Lobby")
client.setProfile("My Bot!")
})
client.on("message", (user, content) => {
if(content == "!help") {
client.message("Hi :P")
}
})
```

BIN
bun.lockb Executable file

Binary file not shown.

186
index.ts Normal file
View file

@ -0,0 +1,186 @@
import * as proto from "./pianoverse_pb";
import EventEmitter from "node:events";
import type TypedEmitter from "typed-emitter";
import UserAgent from "user-agents";
const CEventType = proto.ClientMessage_EventType;
const SEventType = proto.ServerMessage_EventType;
type MessageEvents = {
open: () => void;
close: () => void;
message: (
user: { id: string; name: string; color: string },
content: string
) => void;
welcome: () => void;
rooms: (rooms: proto.ServerMessage_Room[]) => void;
join: (join: proto.ServerMessage_Join) => void;
leave: (id: string) => void;
};
interface Player {
id: string;
name: string;
color: string;
role?: number;
x?: number;
y?: number;
}
export class Client extends (EventEmitter as new () => TypedEmitter<MessageEvents>) {
private ws: WebSocket;
me!: Player;
chatHistory: proto.ServerMessage_Chat[] = [];
room: {
name?: string;
owner?: string;
} = {};
rooms: proto.ServerMessage_Room[] = [];
players = new Map<string, proto.ServerMessage_Join>();
move(x: number, y: number) {
this.ws.send(
new proto.ClientMessage({
event: CEventType.MOVE,
move: {
x,
y,
},
}).toBinary()
);
}
message(chat: string) {
chat.match(/.{1,200}/gm)?.forEach((z, i) => {
setTimeout(() => {
this.ws.send(
new proto.ClientMessage({
event: CEventType.CHAT,
chat: z,
}).toBinary()
);
}, 500 * i);
});
}
keyDown(key: number, velocity: number) {
this.ws.send(
new proto.ClientMessage({
event: CEventType.PRESS,
press: {
key,
vel: velocity,
},
}).toBinary()
);
}
keyUp(key: number) {
this.ws.send(
new proto.ClientMessage({
event: CEventType.RELEASE,
release: {
key,
},
}).toBinary()
);
}
setProfile(name: string, color: string) {
this.ws.send(
new proto.ClientMessage({
event: CEventType.PROFILE,
profile: {
name,
color,
},
}).toBinary()
);
}
setRoom(room: string, priv?: boolean) {
this.ws.send(
new proto.ClientMessage({
event: CEventType.ROOM,
room: {
room: room,
private: priv,
},
}).toBinary()
);
}
constructor(url: string) {
super();
this.ws = new WebSocket(url.replace("http", "ws"), {
//@ts-expect-error
headers: {
Origin: url,
"User-Agent": new UserAgent().toString(),
},
protocol: "pianoverse",
});
this.ws.addEventListener("open", () => {
this.ws.binaryType = "arraybuffer";
setInterval(() => {
this.ws.send(
new proto.ClientMessage({
event: CEventType.HEARTBEAT,
}).toBinary()
);
}, 2000);
this.emit("open");
});
this.ws.addEventListener("message", (e) => {
let data = new Uint8Array(e.data);
let decode;
try {
decode = proto.ServerMessage.fromBinary(data);
} catch {
console.log("Could not decode data.");
console.log(data);
return;
}
if (decode.event == SEventType.RATELIMIT) {
console.log("Ratelimit reached! Type: " + decode.rateLimit);
}
if (decode.event == SEventType.JOIN) {
this.players.set(decode.join!.id, decode.join!);
this.emit("join", decode.join!);
}
if (decode.event == SEventType.LEAVE) {
this.emit("leave", decode.leave);
this.players.delete(decode.leave!);
}
if (decode.event == SEventType.ROOMS) {
this.rooms = decode.rooms;
this.emit("rooms", decode.rooms);
}
if (decode.event == SEventType.WELCOME) {
this.me = {
id: decode.welcome!.id,
name: decode.welcome!.name,
color: decode.welcome!.color,
};
this.room = {
name: decode.welcome!.room,
owner: decode.welcome!.owner,
};
this.chatHistory = decode.welcome!.chat;
this.emit("welcome");
}
if (decode.event == SEventType.CHAT) {
this.emit(
"message",
{
id: decode.chat!.id,
name: decode.chat!.name,
color: decode.chat!.color,
},
decode.chat!.content
);
}
});
}
}

24
package.json Normal file
View file

@ -0,0 +1,24 @@
{
"name": "pianoverse",
"module": "index.ts",
"type": "module",
"license": "GPL-3.0-only",
"homepage": "https://git.sad.ovh/sophie/pianoverse",
"bugs": "https://git.sad.ovh/sophie/pianoverse/issues",
"author": "Sophie",
"scripts": {
"build-proto": "protoc -I . --plugin ./node_modules/.bin/protoc-gen-es --es_out . --es_opt target=ts pianoverse.proto"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@bufbuild/protoc-gen-es": "^1.10.0",
"@types/user-agents": "^1.0.4",
"typed-emitter": "^2.1.0",
"user-agents": "^1.1.267"
}
}

149
pianoverse.proto Normal file
View file

@ -0,0 +1,149 @@
syntax = "proto3";
package pianoverse;
message ClientMessage {
enum EventType {
PING = 0;
ROOM = 1;
PROFILE = 2;
CHAT = 3;
MOVE = 4;
MUTE = 5;
UNMUTE = 6;
KICK = 7;
PRESS = 8;
RELEASE = 9;
SUSTAIN = 10;
HEARTBEAT = 11;
BAN = 12;
}
EventType event = 1;
Room room = 3;
message Room {
string room = 1;
bool private = 2;
}
Profile profile = 4;
string chat = 5;
Move move = 6;
string mute = 7;
string unmute = 8;
string kick = 9;
Press press = 10;
Release release = 11;
bool sustain = 12;
Heartbeat heartbeat = 13;
message Heartbeat {}
Ban ban = 14;
message Ban {
string id = 1;
uint32 hours = 2;
}
}
message ServerMessage {
enum EventType {
PONG = 0;
CHAT = 1;
ROOMS = 2;
WELCOME = 3;
MOVE = 4;
PRESS = 5;
RELEASE = 6;
SUSTAIN = 7;
PROFILE = 8;
JOIN = 9;
LEAVE = 10;
RATELIMIT = 11;
}
EventType event = 1;
Pong pong = 2;
message Pong {}
Chat chat = 3;
message Chat {
string id = 1;
string content = 2;
string name = 3;
string color = 4;
}
repeated Room rooms = 4;
message Room {
string room = 1;
uint32 count = 2;
}
Welcome welcome = 5;
message Welcome {
string id = 1;
string name = 2;
string color = 3;
string room = 4;
string owner = 5;
repeated Chat chat = 6;
}
Move move = 6;
Press press = 7;
Release release = 8;
Sustain sustain = 9;
message Sustain {
string id = 1;
bool enabled = 2;
}
Profile profile = 10;
Join join = 11;
message Join {
string id = 1;
string name = 2;
string color = 3;
int32 role = 4;
float x = 6;
float y = 7;
}
string leave = 12;
int32 rateLimit = 13;
}
message Profile {
string id = 1;
string name = 2;
string color = 3;
string sound = 4;
bool muted = 5;
float x = 6;
float y = 7;
}
message Move {
string id = 1;
float x = 2;
float y = 3;
}
message Press {
uint32 key = 2;
uint32 vel = 3;
}
message Release {
string id = 1;
uint32 key = 2;
}

1023
pianoverse_pb.ts Normal file

File diff suppressed because it is too large Load diff

27
tsconfig.json Normal file
View file

@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}