sdk:添加获取服务器列表、本地屏蔽词库
This commit is contained in:
+22
-12
@@ -1,10 +1,10 @@
|
||||
import * as request from "request-promise";
|
||||
import { RequestError } from "request-promise/errors";
|
||||
import { BANTU_VID_ADDR, BANTU_VID_APP_KEY, HTTP_METHOD } from '../consts';
|
||||
import { checkVidObjSign, get37CheckChatMd5Sign, get37Md5Sign, get37PostMd5Sign, getVidObjSign } from "./sdkUtil";
|
||||
import { checkVidObjSign, get37CheckChatMd5Sign, get37Md5SignA, get37Md5SignB, getVidObjSign } from "./sdkUtil";
|
||||
import { STATUS } from '../consts'
|
||||
import { resResult } from "./util";
|
||||
import { Chat37Params, CheckGuild37Params, CheckName37Params } from "../domain/sdk";
|
||||
import { Chat37Params, CheckGuild37Params, CheckName37Params, GetWordParam } from "../domain/sdk";
|
||||
|
||||
// 通用,请求http
|
||||
export async function httpRequest(url: string, method: string, body: any, headers?: any, timeout = 150) {
|
||||
@@ -150,13 +150,27 @@ export async function vidHttpRequest(addr: string, body: any) {
|
||||
// 37sdk请求
|
||||
|
||||
export async function request37(url: string, body: any, key: string) {
|
||||
body['sign'] = get37Md5Sign(body, key);
|
||||
body['sign'] = get37Md5SignA(body, key);
|
||||
|
||||
let result = await httpRequestForm(url, HTTP_METHOD.POST, body, 1000);
|
||||
console.log('******result', result)
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function request37Post(url: string, body: CheckName37Params|CheckGuild37Params, key: string) {
|
||||
body.setSign(get37Md5SignB(body.getBody(), key));
|
||||
|
||||
let result = await httpRequestForm(url, HTTP_METHOD.POST, body, 1 * 60 * 1000);
|
||||
console.log('*******', result != 1, result.code != STATUS.REQUEST_TIME_OUT.code)
|
||||
if(result.code == STATUS.REQUEST_TIME_OUT.code) {
|
||||
return STATUS.REQUEST_TIME_OUT.code;
|
||||
}
|
||||
if(result != 1) {
|
||||
return 0
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
export async function request37CheckChat(url: string, body: Chat37Params, key: string) {
|
||||
body.setSign(get37CheckChatMd5Sign(body, key));
|
||||
|
||||
@@ -167,13 +181,9 @@ export async function request37CheckChat(url: string, body: Chat37Params, key: s
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function request37Post(url: string, body: CheckName37Params|CheckGuild37Params, key: string) {
|
||||
body.setSign(get37PostMd5Sign(body.getBody(), key));
|
||||
export async function request37GetWord(url: string, body: GetWordParam, key: string) {
|
||||
body.setSign(get37Md5SignB(body.getBody(), key));
|
||||
|
||||
let result = await httpRequestForm(url, HTTP_METHOD.POST, body, 1 * 60 * 1000);
|
||||
console.log('*******', result != 1, result.code != STATUS.REQUEST_TIME_OUT.code)
|
||||
if(result != 1 && result.code != STATUS.REQUEST_TIME_OUT.code) {
|
||||
return false
|
||||
}
|
||||
return true;
|
||||
}
|
||||
let result = await httpRequestForm(url, HTTP_METHOD.GET, body, 5 * 60 * 1000);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { REDIS_KEY, SDK_37_ADDR, SDK_37_CONST } from '../consts';
|
||||
import { request37 } from './httpUtil';
|
||||
import { nowSeconds } from './timeUtil';
|
||||
import { LoginValidataReturn37, Chat37Params } from '../domain/sdk';
|
||||
import { LoginValidataReturn37, Chat37Params, GetServerListParam } from '../domain/sdk';
|
||||
import * as crypto from 'crypto'
|
||||
|
||||
// 通用加密方法
|
||||
@@ -39,21 +39,30 @@ export async function checkMd5Sign(obj: any, treatStr?: (str: string) => string)
|
||||
|
||||
/************** 37sdk **************/
|
||||
|
||||
export function get37Md5Sign(body: any, key: string) {
|
||||
export function get37Md5SignA(body: any, key: string) {
|
||||
return getMd5ObjSign(body, '&', (str) => `${str}&${key}`);
|
||||
}
|
||||
|
||||
export function get37CheckChatMd5Sign(body: Chat37Params, key: string) {
|
||||
let { game_key = '', sid = '', username = '', channel = '', to_username = '', ip = '', time } = body;
|
||||
let str = `${game_key}${sid}${username}${to_username}${channel}${ip}${time}${key}`;
|
||||
console.log('** origin str', body, str);
|
||||
return md5(str);
|
||||
let sign = md5(str);
|
||||
console.log('** origin str', body, str, sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
export function get37PostMd5Sign(body: any, key: string) {
|
||||
export function get37Md5SignB(body: any, key: string) {
|
||||
return getMd5ObjSign(body, '', (str) => `${str}${key}`);
|
||||
}
|
||||
|
||||
export function get37GetServerMd5Sign(body: GetServerListParam, key: string) {
|
||||
let { time, appid, gid } = body;
|
||||
let str = `${time}${appid}${gid}${key}`;
|
||||
let sign = md5(str);
|
||||
console.log('** origin str', body, str, sign);
|
||||
return sign
|
||||
}
|
||||
|
||||
export async function loginValidate37(clientId: string, pst: string, platformAppid: string = SDK_37_CONST.PID, childGameId: number = SDK_37_CONST.FX_C_GAME_ID) {
|
||||
let result: string = await request37(SDK_37_ADDR.LOGIN, {
|
||||
clientid: clientId,
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
function Trie(this: any) {
|
||||
this.root = new TrieNode(null);
|
||||
this.hitStr = '';
|
||||
}
|
||||
|
||||
function TrieNode(this: any, key) {
|
||||
this.key = key; // 节点字符
|
||||
this.children = []; // 子节点集合
|
||||
this.end = false;
|
||||
}
|
||||
|
||||
Trie.prototype = {
|
||||
insertData: function (stringData) {
|
||||
this.insert(stringData, this.root);
|
||||
// console.log(JSON.stringify(this.root, null, 2))
|
||||
},
|
||||
// 递归判断插入
|
||||
insert: function (stringData, node) {
|
||||
if (stringData == '') {
|
||||
return;
|
||||
}
|
||||
let children = node.children;
|
||||
let haveData = null;
|
||||
for (let i in children) {
|
||||
if (children[i].key == stringData[0]) {
|
||||
haveData = children[i];
|
||||
}
|
||||
}
|
||||
if (haveData) {
|
||||
this.insert(stringData.substring(1), haveData); //说明找到了对应的节点
|
||||
if (stringData.substring(1).length == 0) haveData.end = true;
|
||||
} else { //那如果没有找到则插入
|
||||
if (children.length == 0) { //当前节点没有子节点
|
||||
let node = new TrieNode(stringData[0]);
|
||||
children.push(node);
|
||||
this.insert(stringData.substring(1), node); //将该字符节点插入节点的children中
|
||||
if (stringData.substring(1).length == 0) node.end = true;
|
||||
} else { //当前节点存在子节点,需要查找一个合适的位置去插入新节点
|
||||
let validPosition = 0;
|
||||
for (let j in children) {
|
||||
if (children[j].key < stringData[0]) {
|
||||
validPosition++;
|
||||
}
|
||||
}
|
||||
let node = new TrieNode(stringData[0]);
|
||||
children.splice(validPosition, 0, node);
|
||||
this.insert(stringData.substring(1), node); //将该字符节点插入节点的children中
|
||||
if (stringData.substring(1).length == 0) node.end = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
// 查询字符串
|
||||
search: function (queryData) {
|
||||
if (queryData == '' || this.root.children.length == 0) {
|
||||
return false;
|
||||
}
|
||||
for (let j = 0; j < queryData.length; j++) {
|
||||
for (let i in this.root.children) {
|
||||
this.hitStr = '';
|
||||
if (this.searchNext(this.root.children[i], queryData.substring(j))) {
|
||||
console.log('hitStr:' + this.hitStr);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
// 获取命中的关键词
|
||||
getHitStr: function (queryData) {
|
||||
this.hitStr = '';
|
||||
this.search(queryData)
|
||||
return this.hitStr;
|
||||
},
|
||||
// 递归查询判断
|
||||
searchNext: function (node, stringData) {
|
||||
// 若字符与节点key不相等,则不匹配
|
||||
if (stringData[0] != node.key) {
|
||||
return false;
|
||||
} else { // 若与key相等,继续判断
|
||||
this.hitStr += node.key;
|
||||
let children = node.children;
|
||||
if (children.length == 0) { // 叶子节点,最后一个字符,则完全匹配
|
||||
// if (children.length == 0) { // 叶子节点,最后一个字符,则完全匹配
|
||||
return true;
|
||||
} else if (node.end) {
|
||||
return true;
|
||||
} else if (children.length > 0 && stringData.length > 1) { // 既不是叶子节点,也不是最后一个字符,则继续递归查找
|
||||
for (let i in children) {
|
||||
if (children[i].key == stringData[1]) {
|
||||
return this.searchNext(children[i], stringData.substring(1)); // 记得return 递归函数,否则获取的返回值为undefined
|
||||
}
|
||||
}
|
||||
} else { // C1:叶子节点,C2:最后一个字符;若只满足其中一个条件,则不匹配
|
||||
return false;
|
||||
}
|
||||
}
|
||||
},
|
||||
// 删除字符串
|
||||
delete: function (stringData) {
|
||||
if (this.search(stringData)) { // 判断是否存在该单词(字符串)
|
||||
for (let i in this.root.children) {
|
||||
if (this.delNext(this.root, i, stringData, stringData)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
},
|
||||
/**
|
||||
* 先递归查找到字符串的叶子节点,然后从字符串的叶子节点逐级向根节点递归删除叶子节点,直到删除字符串
|
||||
* @param parent 父节点
|
||||
* @param index 子节点在父节点children数组中的索引位置
|
||||
* @param stringData 递归遍历中的字符串
|
||||
* @param delStr 调用delete方法时的原始字符串
|
||||
*/
|
||||
delNext: function (parent, index, stringData, delStr) {
|
||||
//当前节点对象
|
||||
let node = parent.children[index];
|
||||
// 若字符与节点key不相等,则不匹配
|
||||
if (stringData[0] != node.key) {
|
||||
return false;
|
||||
} else { // 若与key相等,继续判断
|
||||
let children = node.children;
|
||||
if (children.length == 0 && stringData.length == 1) { // 叶子节点,最后一个字符,则完全匹配
|
||||
// 删除叶子节点,利用父节点删除子节点原理
|
||||
parent.children.splice(index, 1);
|
||||
// 字符串从尾部移除一个字符后,继续遍历删除方法
|
||||
this.delete(delStr.substring(0, delStr.length - 1));
|
||||
} else if (children.length > 0 && stringData.length > 1) { // 既不是叶子节点,也不是最后一个字符,则继续递归查找
|
||||
for (let i in children) {
|
||||
if (children[i].key == stringData[1]) {
|
||||
return this.delNext(node, i, stringData.substring(1), delStr); // 记得return 递归函数,否则获取的返回值为undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// 打印字符串
|
||||
printData: function () {
|
||||
for (let i in this.root.children) {
|
||||
this.printHelper(this.root.children[i], [this.root.children[i].key]);
|
||||
}
|
||||
},
|
||||
// 递归输出字符串
|
||||
printHelper: function (node, data) {
|
||||
if (node.children.length == 0) {
|
||||
console.log('>', data.join(''));
|
||||
return;
|
||||
}
|
||||
for (let i in node.children) {
|
||||
data.push(node.children[i].key);
|
||||
this.printHelper(node.children[i], data);
|
||||
// if (node.end) console.log(data);
|
||||
data.pop(); // 注意,找打一个单词后,返回下一个初始节点继续遍历
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Trie;
|
||||
@@ -433,6 +433,25 @@ export function readWarJsonFileList() {
|
||||
});
|
||||
}
|
||||
|
||||
export function readWordTxt(fileName: string) {
|
||||
try {
|
||||
let file = fs.readFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`)).toString('utf8').replace(/^\uFEFF/, '');
|
||||
return file;
|
||||
} catch(e) {
|
||||
console.log(e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function writeWordTxt(fileName: string, words: string) {
|
||||
try {
|
||||
let file = fs.writeFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`), words);
|
||||
return file;
|
||||
} catch(e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function readTsFile(fileName: string) {
|
||||
try {
|
||||
let file = fs.readFileSync(path.resolve(__dirname, `../pubUtils/${fileName}.js`)).toString('utf8').replace(/^\uFEFF/, '');
|
||||
|
||||
Reference in New Issue
Block a user