Compare commits
12 Commits
93ffcb67a7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5304120464 | ||
|
|
2459262cef | ||
|
|
a54b315e5d | ||
|
|
bd072c8604 | ||
|
|
10a48c4038 | ||
|
|
cfa1afe4cb | ||
|
|
480e9d41f8 | ||
|
|
24ed3afbaf | ||
|
|
ae4471dec9 | ||
|
|
544160f916 | ||
| b7d21decaf | |||
|
|
43c28329a2 |
25
Dockerfile
25
Dockerfile
@@ -1,12 +1,25 @@
|
||||
FROM golang:alpine AS builder
|
||||
FROM node:alpine AS frontend_builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN go build -o main .
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
COPY src ./src
|
||||
COPY public ./public
|
||||
COPY tsconfig.json ./
|
||||
COPY index.html ./
|
||||
RUN npm run build
|
||||
|
||||
FROM golang:alpine AS server_builder
|
||||
WORKDIR /app
|
||||
COPY go.mod ./
|
||||
RUN go mod download
|
||||
COPY main.go ./
|
||||
COPY --from=frontend_builder /app/dist ./dist
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main
|
||||
|
||||
FROM alpine:latest
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/main /app/main
|
||||
COPY --from=builder /app/dist /app/dist
|
||||
RUN apk add --no-cache ca-certificates
|
||||
COPY --from=server_builder /app/main ./main
|
||||
COPY --from=server_builder /app/dist ./dist
|
||||
EXPOSE 8181
|
||||
CMD ["/app/main"]
|
||||
CMD ["./main"]
|
||||
|
||||
68
dist/assets/index--TwMq_B1.js
vendored
Normal file
68
dist/assets/index--TwMq_B1.js
vendored
Normal file
File diff suppressed because one or more lines are too long
13
dist/index.html
vendored
Normal file
13
dist/index.html
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>tma-front</title>
|
||||
<script type="module" crossorigin src="/assets/index--TwMq_B1.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
</html>
|
||||
1
dist/vite.svg
vendored
Normal file
1
dist/vite.svg
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
21
index.html
21
index.html
@@ -1,13 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>tma-front</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>tma-front</title>
|
||||
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -2,12 +2,29 @@ import Phaser from "phaser";
|
||||
|
||||
export class RemotePlayer {
|
||||
sprite: Phaser.GameObjects.Rectangle;
|
||||
label: Phaser.GameObjects.Text;
|
||||
|
||||
targetX: number;
|
||||
targetY: number;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, y: number, color: number) {
|
||||
constructor(
|
||||
scene: Phaser.Scene,
|
||||
x: number,
|
||||
y: number,
|
||||
name: string,
|
||||
color: number,
|
||||
) {
|
||||
this.sprite = scene.add.rectangle(x, y, 32, 32, color);
|
||||
|
||||
this.label = scene.add
|
||||
.text(x, y - 26, name, {
|
||||
fontSize: "12px",
|
||||
color: "#ffffff",
|
||||
stroke: "#000000",
|
||||
strokeThickness: 3,
|
||||
})
|
||||
.setOrigin(0.5);
|
||||
|
||||
this.targetX = x;
|
||||
this.targetY = y;
|
||||
}
|
||||
@@ -18,12 +35,17 @@ export class RemotePlayer {
|
||||
}
|
||||
|
||||
update() {
|
||||
// магия сглаживания
|
||||
// интерполяция позиции
|
||||
this.sprite.x = Phaser.Math.Linear(this.sprite.x, this.targetX, 0.15);
|
||||
this.sprite.y = Phaser.Math.Linear(this.sprite.y, this.targetY, 0.15);
|
||||
|
||||
// ник всегда над игроком
|
||||
this.label.x = this.sprite.x;
|
||||
this.label.y = this.sprite.y - 26;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.sprite.destroy();
|
||||
this.label.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,23 +4,63 @@ export class TouchInput {
|
||||
dx = 0;
|
||||
dy = 0;
|
||||
|
||||
constructor(scene: Phaser.Scene) {
|
||||
let start: Phaser.Math.Vector2 | null = null;
|
||||
private base?: Phaser.GameObjects.Arc;
|
||||
private knob?: Phaser.GameObjects.Arc;
|
||||
|
||||
private start?: Phaser.Math.Vector2;
|
||||
private readonly radius = 50;
|
||||
|
||||
constructor(scene: Phaser.Scene) {
|
||||
scene.input.on("pointerdown", (pointer: Phaser.Input.Pointer) => {
|
||||
start = new Phaser.Math.Vector2(pointer.x, pointer.y);
|
||||
this.start = new Phaser.Math.Vector2(pointer.x, pointer.y);
|
||||
|
||||
// основание
|
||||
this.base = scene.add
|
||||
.circle(pointer.x, pointer.y, this.radius, 0x000000, 0.35)
|
||||
.setDepth(1000);
|
||||
|
||||
// ручка
|
||||
this.knob = scene.add
|
||||
.circle(pointer.x, pointer.y, this.radius / 2, 0xffffff, 0.8)
|
||||
.setDepth(1001);
|
||||
});
|
||||
|
||||
scene.input.on("pointermove", (pointer: Phaser.Input.Pointer) => {
|
||||
if (!start) return;
|
||||
this.dx = Phaser.Math.Clamp((pointer.x - start.x) / 50, -1, 1);
|
||||
this.dy = Phaser.Math.Clamp((pointer.y - start.y) / 50, -1, 1);
|
||||
if (!this.start || !this.knob) return;
|
||||
|
||||
const dx = pointer.x - this.start.x;
|
||||
const dy = pointer.y - this.start.y;
|
||||
|
||||
const distance = Math.min(this.radius, Math.hypot(dx, dy));
|
||||
const angle = Math.atan2(dy, dx);
|
||||
|
||||
const knobX = this.start.x + Math.cos(angle) * distance;
|
||||
const knobY = this.start.y + Math.sin(angle) * distance;
|
||||
|
||||
this.knob.setPosition(knobX, knobY);
|
||||
|
||||
this.dx = Phaser.Math.Clamp(dx / this.radius, -1, 1);
|
||||
this.dy = Phaser.Math.Clamp(dy / this.radius, -1, 1);
|
||||
});
|
||||
|
||||
scene.input.on("pointerup", () => {
|
||||
start = null;
|
||||
this.dx = 0;
|
||||
this.dy = 0;
|
||||
this.reset();
|
||||
});
|
||||
|
||||
scene.input.on("pointerout", () => {
|
||||
this.reset();
|
||||
});
|
||||
}
|
||||
|
||||
private reset() {
|
||||
this.dx = 0;
|
||||
this.dy = 0;
|
||||
this.start = undefined;
|
||||
|
||||
this.base?.destroy();
|
||||
this.knob?.destroy();
|
||||
|
||||
this.base = undefined;
|
||||
this.knob = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
declare global {
|
||||
interface Window {
|
||||
Telegram?: {
|
||||
WebApp: {
|
||||
initData: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class GameSocket {
|
||||
private socket: WebSocket;
|
||||
playerId: string | null = null;
|
||||
@@ -6,6 +16,17 @@ export class GameSocket {
|
||||
constructor(url: string) {
|
||||
this.socket = new WebSocket(url);
|
||||
|
||||
this.socket.onopen = () => {
|
||||
const initData = window.Telegram?.WebApp?.initData ?? "";
|
||||
|
||||
this.socket.send(
|
||||
JSON.stringify({
|
||||
type: "auth",
|
||||
initData,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
this.socket.onmessage = (e) => {
|
||||
const msg = JSON.parse(e.data);
|
||||
|
||||
@@ -13,10 +34,14 @@ export class GameSocket {
|
||||
this.playerId = msg.payload.id;
|
||||
}
|
||||
|
||||
if (msg.type === "state") {
|
||||
if (msg.type === "input") {
|
||||
this.onState?.(msg.payload);
|
||||
}
|
||||
};
|
||||
|
||||
this.socket.onerror = (e) => {
|
||||
console.error("WebSocket error", e);
|
||||
};
|
||||
}
|
||||
|
||||
sendInput(dx: number, dy: number) {
|
||||
@@ -24,8 +49,8 @@ export class GameSocket {
|
||||
|
||||
this.socket.send(
|
||||
JSON.stringify({
|
||||
dx,
|
||||
dy,
|
||||
type: "input",
|
||||
payload: { dx, dy },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
import Phaser from "phaser";
|
||||
import { RemotePlayer } from "../entities/RemotePlayer";
|
||||
import { TouchInput } from "../input/TouchInput";
|
||||
import { GameSocket } from "../net/Socket";
|
||||
|
||||
let localAddr: string = "ws://localhost:8080/ws";
|
||||
let remoteAddr: string = "wss://tma-api.quizer.space/ws";
|
||||
let isLocal: boolean = false;
|
||||
|
||||
type ServerPlayerState = {
|
||||
x: number;
|
||||
y: number;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export class GameScene extends Phaser.Scene {
|
||||
players = new Map<string, RemotePlayer>();
|
||||
socket!: GameSocket;
|
||||
@@ -9,7 +20,7 @@ export class GameScene extends Phaser.Scene {
|
||||
|
||||
create() {
|
||||
this.inputCtrl = new TouchInput(this);
|
||||
this.socket = new GameSocket("ws://localhost:8080/ws");
|
||||
this.socket = new GameSocket(isLocal ? localAddr : remoteAddr);
|
||||
|
||||
this.socket.onState = (state) => {
|
||||
this.syncPlayers(state.players);
|
||||
@@ -24,7 +35,8 @@ export class GameScene extends Phaser.Scene {
|
||||
}
|
||||
}
|
||||
|
||||
syncPlayers(serverPlayers: Record<string, { x: number; y: number }>) {
|
||||
syncPlayers(serverPlayers: Record<string, ServerPlayerState>) {
|
||||
// добавление / обновление
|
||||
for (const id in serverPlayers) {
|
||||
const data = serverPlayers[id];
|
||||
let player = this.players.get(id);
|
||||
@@ -32,13 +44,22 @@ export class GameScene extends Phaser.Scene {
|
||||
if (!player) {
|
||||
const isMe = id === this.socket.playerId;
|
||||
const color = isMe ? 0x00ff00 : 0xff4444;
|
||||
player = new RemotePlayer(this, data.x, data.y, color);
|
||||
|
||||
player = new RemotePlayer(
|
||||
this,
|
||||
data.x,
|
||||
data.y,
|
||||
data.name, // 👈 username от сервера
|
||||
color,
|
||||
);
|
||||
|
||||
this.players.set(id, player);
|
||||
}
|
||||
|
||||
player.setTarget(data.x, data.y);
|
||||
}
|
||||
|
||||
// удаление отключившихся
|
||||
for (const [id, player] of this.players) {
|
||||
if (!serverPlayers[id]) {
|
||||
player.destroy();
|
||||
|
||||
Reference in New Issue
Block a user