[mod] 先優化為Class
This commit is contained in:
106
src/api/slot/spin.ts
Normal file
106
src/api/slot/spin.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { INetRequest } from "../../script/Engine/CatanEngine/NetManagerV2/Core/INetRequest";
|
||||
import { INetResponse } from "../../script/Engine/CatanEngine/NetManagerV2/Core/INetResponse";
|
||||
import { ClientData } from "../../shared/protocols/define/interface";
|
||||
import { RpcSlot1SpinRequest, RpcSlot1SpinResponse } from "../../shared/protocols/Slot1Request";
|
||||
import { RandomEx } from "../../Utils/Number/RandomEx";
|
||||
|
||||
|
||||
export default class SlotBase {
|
||||
protected temps: string[];
|
||||
protected freeTemps: string[];
|
||||
|
||||
public *spin(clientData: ClientData, req: INetRequest<RpcSlot1SpinRequest>): IterableIterator<any> {
|
||||
const data: RpcSlot1SpinRequest = req.Data
|
||||
clientData.free = null;
|
||||
|
||||
const slotData: any = JSON.parse(this.temps[RandomEx.GetInt(0, this.temps.length)]);
|
||||
|
||||
let totalGet: number = 0;
|
||||
if (slotData.line) {
|
||||
totalGet += this.getLineGet(slotData.line);
|
||||
}
|
||||
if (slotData.scatter) {
|
||||
totalGet += this.getScatterGet(slotData.scatter);
|
||||
}
|
||||
if (slotData.free) {
|
||||
const count = slotData.free[1];
|
||||
const { freeData, totalFreeGet } = this.getFree(count);
|
||||
totalGet += totalFreeGet;
|
||||
clientData.free = { count: 0, freeData };
|
||||
}
|
||||
if (totalGet) {
|
||||
slotData.get = [[1, totalGet]];
|
||||
}
|
||||
|
||||
clientData.money -= data.pay;
|
||||
if (slotData.get) {
|
||||
clientData.money += slotData.get[0][1];
|
||||
}
|
||||
slotData["pay"] = [[1, -data.pay]];
|
||||
slotData["money"] = clientData.money;
|
||||
|
||||
const response: INetResponse<RpcSlot1SpinResponse> = {
|
||||
Status: 0,
|
||||
Method: req.Method,
|
||||
Data: slotData,
|
||||
IsValid: true
|
||||
};
|
||||
return response;
|
||||
}
|
||||
|
||||
protected getFree(count: number) {
|
||||
const freeData = [];
|
||||
let totalFreeGet = 0;
|
||||
for (let i: number = 0; i < count; i++) {
|
||||
const slotData: any = JSON.parse(this.freeTemps[RandomEx.GetInt(0, this.freeTemps.length)]);
|
||||
freeData.push(slotData);
|
||||
if (slotData.line) {
|
||||
totalFreeGet += this.getLineGet(slotData.line);
|
||||
}
|
||||
}
|
||||
return { freeData, totalFreeGet }
|
||||
}
|
||||
|
||||
getLineGet(lines: number[][]): number {
|
||||
let totalGet: number = 0;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
totalGet += line[2]
|
||||
}
|
||||
return totalGet;
|
||||
}
|
||||
|
||||
getScatterGet(scatter: number[][]): number {
|
||||
let totalGet: number = scatter[0][1];
|
||||
return totalGet;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// // 原始的编码字符串
|
||||
// // // {"slot":[4,4,1,12,14,9,10,1,6,4,8,9,1,14,10],"scatter":[[[2,7,12],100]],"pay":[[1,-100]],"get":[[1,100]],"money":9990624}
|
||||
// // const encodedStr = 'H4sIAAAAAAAAA1WOMQ+CMBCF/0vnG9paRdkcSRwMjoahQjE1hZIWooTw371TSLS54d3ru7tvYpylE4vO97mJg+uztvbkNNq2WcVSAax2/rnIe/BDR5qj7UNDUipgtq3M62Rjz9KrUrBNgMMGSxbw2b1+gQIBQoJQcADBsdmhtaeGPMGLGZhutRujjbkxLq50Zz0uaJh4dJexuXm3wlY2mJIiX16kOdI5CQkeo5Wx/B/oKMp/oshZB2PQnPG9ARdrnfUWAQAA';
|
||||
|
||||
// // {"slot":[5,6,14,6,5,11,12,3,14,11,6,2,11,2,12],"line":[[[5,11,12,13,9],161,1500]],"pay":[[1,-100]],"get":[[1,1500]],"money":9991524}
|
||||
// const encodedStr = 'H4sIAAAAAAAAA11Pu27DMAz8F84cQjlSEG9FpwAdinQMMqi2XKiQJcOykRqB/72kH0OrQTreEXenJxygfEIOabi6PIbhEpskTGt9vNRQEkIT0mODX30aO8EHplPfClRHBB9r9/Pm8wDlrdBIZ6QjFoSk77iYb5pGI4pB3mFVYSEjQ4NKHr7UfUaw0YYp+3x1LuS94Lud9nZkaGGn9jMFbsbVqjRGjtAIDx/q13VSCMFHt7Q3JMNWY02nAs9csLMT63rm4O/uY/Hcg2rfu0qS1y/zP1/EQDrm6u9q93/pxNZN7xyzM59fbBqv0msBAAA=';
|
||||
|
||||
// // 解码 Base64
|
||||
// const decodedBytes = Buffer.from(encodedStr, 'base64');
|
||||
|
||||
// // 解压缩 zlib 数据
|
||||
// gunzip(decodedBytes, (err, decompressedBytes) => {
|
||||
// if (err) {
|
||||
// console.error('解压缩失败:', err);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // 解析 JSON 数据
|
||||
// try {
|
||||
// const jsonData = JSON.parse(decompressedBytes.toString());
|
||||
// console.log(JSON.stringify(jsonData, null, 4));
|
||||
// } catch (parseErr) {
|
||||
// console.error('JSON 解析失败:', parseErr);
|
||||
// }
|
||||
// });
|
||||
@@ -1,86 +1,15 @@
|
||||
import { INetRequest } from "../../script/Engine/CatanEngine/NetManagerV2/Core/INetRequest";
|
||||
import { INetResponse } from "../../script/Engine/CatanEngine/NetManagerV2/Core/INetResponse";
|
||||
import { ClientData } from "../../shared/protocols/define/interface";
|
||||
import { RpcSlot1SpinRequest, RpcSlot1SpinResponse } from "../../shared/protocols/Slot1Request";
|
||||
import { RandomEx } from "../../Utils/Number/RandomEx";
|
||||
|
||||
export default function* (clientData: ClientData, req: INetRequest<RpcSlot1SpinRequest>): IterableIterator<any> {
|
||||
const data: RpcSlot1SpinRequest = req.Data
|
||||
clientData.free = null;
|
||||
import SlotBase from "../slot/spin";
|
||||
|
||||
|
||||
// // 原始的编码字符串
|
||||
// // // {"slot":[4,4,1,12,14,9,10,1,6,4,8,9,1,14,10],"scatter":[[[2,7,12],100]],"pay":[[1,-100]],"get":[[1,100]],"money":9990624}
|
||||
// // const encodedStr = 'H4sIAAAAAAAAA1WOMQ+CMBCF/0vnG9paRdkcSRwMjoahQjE1hZIWooTw371TSLS54d3ru7tvYpylE4vO97mJg+uztvbkNNq2WcVSAax2/rnIe/BDR5qj7UNDUipgtq3M62Rjz9KrUrBNgMMGSxbw2b1+gQIBQoJQcADBsdmhtaeGPMGLGZhutRujjbkxLq50Zz0uaJh4dJexuXm3wlY2mJIiX16kOdI5CQkeo5Wx/B/oKMp/oshZB2PQnPG9ARdrnfUWAQAA';
|
||||
|
||||
// // {"slot":[5,6,14,6,5,11,12,3,14,11,6,2,11,2,12],"line":[[[5,11,12,13,9],161,1500]],"pay":[[1,-100]],"get":[[1,1500]],"money":9991524}
|
||||
// const encodedStr = 'H4sIAAAAAAAAA11Pu27DMAz8F84cQjlSEG9FpwAdinQMMqi2XKiQJcOykRqB/72kH0OrQTreEXenJxygfEIOabi6PIbhEpskTGt9vNRQEkIT0mODX30aO8EHplPfClRHBB9r9/Pm8wDlrdBIZ6QjFoSk77iYb5pGI4pB3mFVYSEjQ4NKHr7UfUaw0YYp+3x1LuS94Lud9nZkaGGn9jMFbsbVqjRGjtAIDx/q13VSCMFHt7Q3JMNWY02nAs9csLMT63rm4O/uY/Hcg2rfu0qS1y/zP1/EQDrm6u9q93/pxNZN7xyzM59fbBqv0msBAAA=';
|
||||
|
||||
// // 解码 Base64
|
||||
// const decodedBytes = Buffer.from(encodedStr, 'base64');
|
||||
|
||||
// // 解压缩 zlib 数据
|
||||
// gunzip(decodedBytes, (err, decompressedBytes) => {
|
||||
// if (err) {
|
||||
// console.error('解压缩失败:', err);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // 解析 JSON 数据
|
||||
// try {
|
||||
// const jsonData = JSON.parse(decompressedBytes.toString());
|
||||
// console.log(JSON.stringify(jsonData, null, 4));
|
||||
// } catch (parseErr) {
|
||||
// console.error('JSON 解析失败:', parseErr);
|
||||
// }
|
||||
// });
|
||||
|
||||
const temps: string[] = [
|
||||
export default class Slot1 extends SlotBase {
|
||||
protected temps: string[] = [
|
||||
`{"slot":[11,4,8,9,5,2,13,10,7,9,10,6,6,12,4],"line":[[[5,11,12],161,2000]]}`,
|
||||
|
||||
`{"slot":[9,6,2,5,4,14,10,9,13,10,4,5,5,2,2]}`,
|
||||
|
||||
`{"slot":[4,3,3,3,9,10,14,14,9,4,7,8,8,5,10],"free":[[1,2,3],3],"scatter":[[[1,2,3],3000]]}`,
|
||||
];
|
||||
const slotData: any = JSON.parse(temps[RandomEx.GetInt(0, temps.length)]);
|
||||
|
||||
let totalGet: number = 0;
|
||||
if (slotData.line) {
|
||||
totalGet += getLineGet(slotData.line);
|
||||
}
|
||||
if (slotData.scatter) {
|
||||
totalGet += getScatterGet(slotData.scatter);
|
||||
}
|
||||
if (slotData.free) {
|
||||
const count = slotData.free[1];
|
||||
const { freeData, totalFreeGet } = getFree(count);
|
||||
totalGet += totalFreeGet;
|
||||
clientData.free = { count: 0, freeData };
|
||||
}
|
||||
if (totalGet) {
|
||||
slotData.get = [[1, totalGet]];
|
||||
}
|
||||
|
||||
clientData.money -= data.pay;
|
||||
if (slotData.get) {
|
||||
clientData.money += slotData.get[0][1];
|
||||
}
|
||||
slotData["pay"] = [[1, -data.pay]];
|
||||
slotData["money"] = clientData.money;
|
||||
|
||||
const response: INetResponse<RpcSlot1SpinResponse> = {
|
||||
Status: 0,
|
||||
Method: req.Method,
|
||||
Data: slotData,
|
||||
IsValid: true
|
||||
};
|
||||
return response;
|
||||
}
|
||||
|
||||
function getFree(count: number) {
|
||||
const freeData = [];
|
||||
let totalFreeGet = 0;
|
||||
const freeTemps: string[] = [
|
||||
protected freeTemps: string[] = [
|
||||
`{"slot":[13,9,5,14,4,9,4,12,12,11,4,14,8,7,6],"line":[[[10,6],195,300],[[10,6],213,300]]}`,
|
||||
`{"slot":[9,9,10,13,4,5,4,3,4,11,10,14,14,14,5]}`,
|
||||
`{"slot":[12,6,8,12,11,7,12,11,5,5,11,5,3,10,9]}`,
|
||||
@@ -92,26 +21,4 @@ function getFree(count: number) {
|
||||
`{"slot":[10,14,13,9,8,6,6,6,4,13,13,12,9,14,4],"line":[[[5,6,7],122,3000]]}`,
|
||||
`{"slot":[11,13,9,8,5,8,5,5,13,9,14,9,12,4,7]}`,
|
||||
];
|
||||
for (let i: number = 0; i < count; i++) {
|
||||
const slotData: any = JSON.parse(freeTemps[RandomEx.GetInt(0, freeTemps.length)]);
|
||||
freeData.push(slotData);
|
||||
if (slotData.line) {
|
||||
totalFreeGet += getLineGet(slotData.line);
|
||||
}
|
||||
}
|
||||
return { freeData, totalFreeGet }
|
||||
}
|
||||
|
||||
function getLineGet(lines: number[][]): number {
|
||||
let totalGet: number = 0;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
totalGet += line[2]
|
||||
}
|
||||
return totalGet;
|
||||
}
|
||||
|
||||
function getScatterGet(scatter: number[][]): number {
|
||||
let totalGet: number = scatter[0][1];
|
||||
return totalGet;
|
||||
}
|
||||
@@ -58,33 +58,56 @@ export class NetConnector {
|
||||
try {
|
||||
// 动态导入文件
|
||||
const module = await import(`../../../../api/${req.Method.replace(".", "/")}`);
|
||||
const isClass = typeof module.default === 'function' && module.default.prototype && Object.getOwnPropertyNames(module.default.prototype).includes('constructor');
|
||||
|
||||
// 调用导入模块中的处理函数
|
||||
if (module.default) {
|
||||
let AsyncFunction: () => IterableIterator<any> = function* (): IterableIterator<any> {
|
||||
const clientData: ClientData = NetConnector.clients.get(socket);
|
||||
const response: INetResponse<any> = yield* module.default(clientData, req);
|
||||
if (response) {
|
||||
NetConnector.Send(socket, response);
|
||||
}
|
||||
};
|
||||
CoroutineV2.Single(AsyncFunction()).Start();
|
||||
if (isClass) {
|
||||
const moduleClass = new module.default();
|
||||
let AsyncFunction: () => IterableIterator<any> = function* (): IterableIterator<any> {
|
||||
const clientData: ClientData = NetConnector.clients.get(socket);
|
||||
const method: string = req.Method.split(".")[1];
|
||||
try {
|
||||
const response: INetResponse<any> = yield* moduleClass[method](clientData, req);
|
||||
if (response) {
|
||||
NetConnector.Send(socket, response);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error handling request ${req.Method}: ${error.message}`);
|
||||
NetConnector.sendError(socket, req);
|
||||
}
|
||||
};
|
||||
CoroutineV2.Single(AsyncFunction()).Start();
|
||||
} else {
|
||||
let AsyncFunction: () => IterableIterator<any> = function* (): IterableIterator<any> {
|
||||
const clientData: ClientData = NetConnector.clients.get(socket);
|
||||
const response: INetResponse<any> = yield* module.default(clientData, req);
|
||||
if (response) {
|
||||
NetConnector.Send(socket, response);
|
||||
}
|
||||
};
|
||||
CoroutineV2.Single(AsyncFunction()).Start();
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Module for ${req.Method} does not export a default function.`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error handling request ${req.Method}: ${error.message}`);
|
||||
const response: INetResponse<any> = {
|
||||
Status: -1,
|
||||
Method: req.Method,
|
||||
Data: null,
|
||||
IsValid: false
|
||||
};
|
||||
NetConnector.Send(socket, response);
|
||||
NetConnector.sendError(socket, req);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static sendError(socket: WebSocket, req: INetRequest<any>) {
|
||||
const response: INetResponse<any> = {
|
||||
Status: -1,
|
||||
Method: req.Method,
|
||||
Data: null,
|
||||
IsValid: false
|
||||
};
|
||||
NetConnector.Send(socket, response);
|
||||
}
|
||||
|
||||
public static OnWebSocketClose() {
|
||||
console.log('Client disconnected');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user