Commit 0bb9debe authored by Chunchi Che's avatar Chunchi Che

rerange some code

parent 1b0f132d
// Collection of APIs provided by MyCard
export * from "./account";
export * from "./match";
export * from "./options";
export * from "./room";
export * from "./user";
/* MC服房间的选项 */
export interface Options {
mode: number;
rule: number;
start_lp: number;
start_lp_tag: number;
start_hand: number;
draw_count: number;
duel_rule: number;
no_check_deck: boolean;
no_shuffle_deck: boolean;
lflist?: number;
time_limit?: number;
auto_death: boolean;
}
/* 一些MC服创建/加入房间相关的通用函数 */
import { Options } from "./options";
export interface Room {
id?: string;
title?: string;
users?: { username: string; position: number; avatar?: string }[];
options: Options;
}
// 通过房间ID和external_id加密得出房间密码
//
// 用于加入MC服房间
export function getEncryptedPasswd(
roomID: string,
external_id: number,
): string {
const optionsBuffer = new Uint8Array(6);
optionsBuffer[1] = 3 << 4;
let checksum = 0;
for (let i = 1; i < optionsBuffer.length; i++) {
checksum -= optionsBuffer[i];
}
optionsBuffer[0] = checksum & 0xff;
const secret = (external_id % 65535) + 1;
for (let i = 0; i < optionsBuffer.length; i += 2) {
const value = (optionsBuffer[i + 1] << 8) | optionsBuffer[i];
const xorResult = value ^ secret;
optionsBuffer[i + 1] = (xorResult >> 8) & 0xff;
optionsBuffer[i] = xorResult & 0xff;
}
const base64String = btoa(String.fromCharCode(...optionsBuffer));
return base64String + roomID;
}
......@@ -5,7 +5,7 @@ import React, { useState } from "react";
import useWebSocket, { ReadyState } from "react-use-websocket";
import { proxy, useSnapshot } from "valtio";
import { getUserInfo } from "@/api";
import { getUserInfo, Room } from "@/api";
import { useConfig } from "@/config";
import { ScrollableArea } from "../Shared";
......@@ -18,28 +18,6 @@ interface Info {
data: Room | Room[] | string;
}
interface Room {
id?: string;
title?: string;
users?: { username: string; position: number; avatar?: string }[];
options: Options;
}
interface Options {
mode: number;
rule: number;
start_lp: number;
start_lp_tag: number;
start_hand: number;
draw_count: number;
duel_rule: number;
no_check_deck: boolean;
no_shuffle_deck: boolean;
lflist?: number;
time_limit?: number;
auto_death: boolean;
}
export const watchStore = proxy<{ watchID: string | undefined }>({
watchID: undefined,
});
......
......@@ -9,7 +9,7 @@ import { useEffect, useState } from "react";
import { LoaderFunction, useNavigate } from "react-router-dom";
import { useSnapshot } from "valtio";
import { match } from "@/api";
import { getEncryptedPasswd, match } from "@/api";
import { useConfig } from "@/config";
import { accountStore, deckStore, resetUniverse, roomStore } from "@/stores";
import { Background, IconFont, Select } from "@/ui/Shared";
......@@ -17,7 +17,7 @@ import { Background, IconFont, Select } from "@/ui/Shared";
import styles from "./index.module.scss";
import { MatchModal, matchStore } from "./MatchModal";
import { ReplayModal, replayOpen } from "./ReplayModal";
import { connectSrvpro, getEncryptedPasswd } from "./util";
import { connectSrvpro } from "./util";
import { WatchContent, watchStore } from "./WatchContent";
const { servers: serverList } = useConfig();
......@@ -87,7 +87,10 @@ export const Component: React.FC = () => {
(server) => server.name === "mycard-athletic",
);
if (mcServer) {
const passWd = getEncryptedPasswd(watchStore.watchID, user);
const passWd = getEncryptedPasswd(
watchStore.watchID,
user.external_id,
);
await connectSrvpro({
ip: mcServer.ip + ":" + mcServer.port,
player: user.username,
......
......@@ -4,7 +4,6 @@ import { initStrings } from "@/api";
import { useConfig } from "@/config";
import socketMiddleWare, { socketCmd } from "@/middleware/socket";
import sqliteMiddleWare, { sqliteCmd } from "@/middleware/sqlite";
import { User } from "@/stores";
const NeosConfig = useConfig();
......@@ -53,28 +52,3 @@ export const connectSrvpro = async (params: {
});
}
};
export function getEncryptedPasswd(roomID: string, user: User): string {
const optionsBuffer = new Uint8Array(6);
optionsBuffer[1] = 3 << 4;
let checksum = 0;
for (let i = 1; i < optionsBuffer.length; i++) {
checksum -= optionsBuffer[i];
}
optionsBuffer[0] = checksum & 0xff;
const secret = (user.external_id % 65535) + 1;
for (let i = 0; i < optionsBuffer.length; i += 2) {
const value = (optionsBuffer[i + 1] << 8) | optionsBuffer[i];
const xorResult = value ^ secret;
optionsBuffer[i + 1] = (xorResult >> 8) & 0xff;
optionsBuffer[i] = xorResult & 0xff;
}
const base64String = btoa(String.fromCharCode(...optionsBuffer));
return base64String + roomID;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment