Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.4.4-cache improve #47

Open
wants to merge 17 commits into
base: dev-1.4.4
Choose a base branch
from
8 changes: 8 additions & 0 deletions .github/mergify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pull_request_rules:
- name: Automatic merge ⬇️ on approval ✔
conditions:
- "#approved-reviews-by>=1"
- label!=work-in-progress
actions:
merge:
method: rebase
Binary file modified plugins/csgowiki-pack.smx
Binary file not shown.
17 changes: 13 additions & 4 deletions scripting/csgowiki-pack.sp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Plugin:myinfo = {
name = "[CSGOWiki] Plugin-Pack",
author = "CarOL",
description = "An Sourcemod Instance For [CSGOWiki-Web] Service",
version = "v1.4.3",
version = "v1.4.4",
url = "https://docs.csgowiki.top/plugins"
};

Expand Down Expand Up @@ -59,7 +59,10 @@ public OnPluginStart() {
g_hOnUtilityWiki = FindOrCreateConvar("sm_utility_wiki_on", "1", "Set module: <utility_wiki> on/off.");
g_hCSGOWikiToken = FindOrCreateConvar("sm_csgowiki_token", "", "Make sure csgowiki token valid. Some modules will be disabled if csgowiki token invalid", -1.0, -1.0, true);
g_hWikiReqLimit = FindOrCreateConvar("sm_wiki_request_limit", "1", "Limit cooling time(second) for each player's `!wiki` request. Set 0 to unlimit", 0.0, 10.0);
g_hApiHost = FindOrCreateConvar("sm_csgowiki_apihost", "https://apiproxy.mycsgolab.com:5555", "Alternative option for this setting is `https://api.mycsgolab.com` which source in Hongkong");
g_hApiHost = FindOrCreateConvar("sm_csgowiki_apihost", "http://121.40.123.93:2333", "Alternative option for this setting is `https://api.mycsgolab.com` which source in Hongkong");
g_hLocalCacheEnable = FindOrCreateConvar("sm_csgowiki_cache_enable", "1", "Enable local cache.");
g_hLocalCacheFileLimit = FindOrCreateConvar("sm_csgowiki_cache_limit","10","Set the limit of the cache files. set -1 for unlimit.");


HookOpConVarChange();

Expand Down Expand Up @@ -89,9 +92,14 @@ public OnMapStart() {
// init collection
GetAllCollection();

// initialize cache
EnforceDirExists("data/csgowiki");
EnforceDirExists("data/csgowiki/replays");
EnforceDirExists("data/csgowiki/path");
EnforceDirExists("data/csgowiki/cache");
EnforceDirExists("data/csgowiki/cache/replays");
EnforceDirExists("data/csgowiki/cache/path");
if (check_function_on(g_hLocalCacheEnable, "")) {
EnforceDirExists("data/csgowiki/cache/detail");
}
}

public OnMapEnd() {
Expand Down Expand Up @@ -171,5 +179,6 @@ public Action OnClientSayCommand(int client, const char[] command, const char[]
return Plugin_Handled;
}
}

return Plugin_Continue;
}
60 changes: 49 additions & 11 deletions scripting/csgowiki/replay.sp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ void UploadPlayBack(int client, char utid[LENGTH_UTILITY_ID]) {
return;
}

char apiHost[LENGTH_TOKEN];
char apiHost[LENGTH_TOKEN] = "https://api.mycsgolab.com";
char token[LENGTH_TOKEN];
char url[LENGTH_URL];
GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
// GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
GetConVarString(g_hCSGOWikiToken, token, LENGTH_TOKEN);
PrintToChat(client, "%s \x04开始上传录像:%s", PREFIX, utid);

Format(url, sizeof(url), "%s/v2/utility/upload-playback-put/%s/?token=%s", apiHost, utid, token);
Format(url, sizeof(url), "%s/v2/utility/upload-playback/%s/?token=%s", apiHost, utid, token);
HTTPRequest request = new HTTPRequest(url);
DataPack pack = new DataPack();
pack.WriteCell(client);
Expand Down Expand Up @@ -93,11 +93,18 @@ void BotMimicUploadCallback(HTTPStatus status, DataPack pack) {
bool StartRequestReplayFile(int client, char utility_id[LENGTH_UTILITY_ID], char utid[LENGTH_UTILITY_ID]) {
if (!IsPlayer(client)) return false;
char filepath[84];
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/replays/%s.rec", utid);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/replays/%s.rec", utid);
if (FileExists(filepath)) { // bug
PrintToChat(client, "%s \x04命中缓存,开始播放录像", PREFIX);
StartReplay(client, utid);
return false;
int timestamp = GetFileTime(filepath, FileTime_Created);
int currentTimestamp = GetTime();

if ((currentTimestamp - timestamp) < 432000) {
PrintToChat(client, "%s \x04命中缓存,开始播放录像", PREFIX);
StartReplay(client, utid);
return false;
} else {
DeleteFile(filepath);
}
}
if (IsPlayer(client))
PrintToChat(client, "%s \x09正在请求录像文件...", PREFIX);
Expand Down Expand Up @@ -157,22 +164,53 @@ public void StartReplay(int client, char utid[LENGTH_UTILITY_ID]) {
DataPack fpack = new DataPack();
fpack.WriteCell(client);
char filepath[84];
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/replays/%s.rec", utid);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/replays/%s.rec", utid);
fpack.WriteString(filepath);
RequestFrame(BotMimicStartReplay, fpack);
}

void DeleteReplayFileFromUtid(char utid[LENGTH_UTILITY_ID], bool type=true) {
char filepath[84];
char directoryPath[84];
if (type) {
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/replays/%s.rec", utid);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/replays/%s.rec", utid);
BuildPath(Path_SM, directoryPath, sizeof(directoryPath), "data/csgowiki/cache/replays");

int limit = GetConVarInt(g_hLocalCacheFileLimit);
if (limit != -1) {
DirectoryListing dL = OpenDirectory(directoryPath);
char buffer[64];
char fileName[64];
dL.GetNext(buffer, sizeof(buffer));
dL.GetNext(buffer, sizeof(buffer));
int timestamp = 0;
int fileCounter = 0;
while(dL.GetNext(buffer, sizeof(buffer))) {
char path[84];
BuildPath(Path_SM, path, sizeof(path), "data/csgowiki/cache/replays/%s", buffer);
int currentTimestamp = GetFileTime(path, FileTime_Created);
if (fileCounter == 0) {timestamp = currentTimestamp;}
else if (currentTimestamp < timestamp) {
timestamp = currentTimestamp;
fileName = buffer;
}
fileCounter++;
}

if (fileCounter >= limit) {
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/replays/%s", fileName);
DeleteFile(filepath);
}

}
}
else {
BuildPath(Path_SM, filepath, sizeof(filepath), "data/botmimic/csgowiki/%s/%s.rec", g_sCurrentMap, utid);
}
if (FileExists(filepath)) {
DeleteFile(filepath);
}



}

public void BotMimicStartReplay(DataPack pack) {
Expand Down
16 changes: 8 additions & 8 deletions scripting/csgowiki/utility_submit.sp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ void TriggerWikiPost(client) {

// request
char url[LENGTH_MESSAGE];
char apiHost[LENGTH_TOKEN];
GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
char apiHost[LENGTH_TOKEN] = "https://api.mycsgolab.com";
// GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
Format(url, sizeof(url), "%s/v2/utility/submit/?token=%s", apiHost, token);
HTTPRequest httpRequest = new HTTPRequest(url);
httpRequest.SetHeader("Content-Type", "application/json");
Expand Down Expand Up @@ -264,7 +264,7 @@ void SaveUtilityPath(int client, char filename[LENGTH_UTILITY_ID]) {
}

char filepath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/path/%s.path", filename);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/path/%s.path", filename);
File hFile = OpenFile(filepath, "wb");
if(hFile == null) {
LogError("Can't open the record file for writing! (%s)", filepath);
Expand All @@ -285,19 +285,19 @@ void SaveUtilityPath(int client, char filename[LENGTH_UTILITY_ID]) {

void UploadUtilityPath(int client, char utid[LENGTH_UTILITY_ID]) {
char filepath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/path/%s.path", utid);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/path/%s.path", utid);
if (!FileExists(filepath)) {
PrintToChat(client, "%s \x02待上传文件不存在", PREFIX);
return;
}

char apiHost[LENGTH_TOKEN];
char apiHost[LENGTH_TOKEN] = "https://api.mycsgolab.com";
char token[LENGTH_TOKEN];
char url[LENGTH_URL];
GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
// GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
GetConVarString(g_hCSGOWikiToken, token, LENGTH_TOKEN);
PrintToChat(client, "%s \x04开始上传路径文件:%s", PREFIX, utid);
Format(url, sizeof(url), "%s/v2/utility/upload-path-put/%s/?token=%s", apiHost, utid, token);
Format(url, sizeof(url), "%s/v2/utility/upload-path/%s/?token=%s", apiHost, utid, token);
HTTPRequest request = new HTTPRequest(url);

DataPack pack = new DataPack();
Expand All @@ -315,7 +315,7 @@ void UploadUtilityPathCallback(HTTPStatus status, DataPack pack) {

// filepath
char filepath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/path/%s.path", utid);
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/path/%s.path", utid);

if (status != HTTPStatus_OK) {
PrintToChat(client, "%s \x02路径文件上传失败:%d", PREFIX, status);
Expand Down
125 changes: 96 additions & 29 deletions scripting/csgowiki/utility_wiki.sp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void GetAllCollection(client=-1) {
char url[LENGTH_MESSAGE];
Format(url, sizeof(url), "%s/v2/utility/filter", apiHost);
HTTPRequest AllCollectionRequest = new HTTPRequest(url);
AllCollectionRequest.SetHeader("Content-Type", "application/json");
AllCollectionRequest.AppendQueryParam("token", token);
AllCollectionRequest.AppendQueryParam("mapname", g_sCurrentMap);
AllCollectionRequest.AppendQueryParam("tickrate", "%d", g_iServerTickrate);
Expand Down Expand Up @@ -100,34 +101,58 @@ void GetUtilityDetail(client, char[] utId) {
CreateTimer(fWikiLimit, ReqLockTimerCallback, client);
}
}

if (check_function_on(g_hLocalCacheEnable,"")) {
char path[256];
BuildPath(Path_SM, path, sizeof(path), "data/csgowiki/cache/detail/%s.json", utId);
if (FileExists(path)) {
Handle hFile = OpenFile(path, "r");
char content[1024];
ReadFileString(hFile, content, sizeof(content));
JSONObject resp_json = JSONObject.FromString(content);

char token[LENGTH_TOKEN];
GetConVarString(g_hCSGOWikiToken, token, LENGTH_TOKEN);
char apiHost[LENGTH_TOKEN];
GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
char steamid_[LENGTH_STEAMID64];
GetClientAuthId(client, AuthId_SteamID64, steamid_, LENGTH_STEAMID64);
char player_name[LENGTH_NAME];
GetClientName(client, player_name, sizeof(player_name));
char url[LENGTH_MESSAGE];
Format(url, sizeof(url), "%s/v2/utility/detail", apiHost);
// timestamp
int currentTimestamp = GetTime();
int timestamp = GetFileTime(path, FileTime_Created);

if ( (currentTimestamp - timestamp) < 432000 ) { // 5 days
JSONObject json_obj = view_as<JSONObject>(resp_json.Get("utility_detail"));
ShowUtilityDetail(client, json_obj);
CloseHandle(hFile);
return;
} else {
DeleteFile(path);
}
}
}

HTTPRequest httpRequest = new HTTPRequest(url);
httpRequest.AppendQueryParam("token", token);
httpRequest.AppendQueryParam("article_id", utId);
httpRequest.Get(UtilityDetailResponseCallback, client);
char token[LENGTH_TOKEN];
GetConVarString(g_hCSGOWikiToken, token, LENGTH_TOKEN);
char apiHost[LENGTH_TOKEN];
GetConVarString(g_hApiHost, apiHost, sizeof(apiHost));
char steamid_[LENGTH_STEAMID64];
GetClientAuthId(client, AuthId_SteamID64, steamid_, LENGTH_STEAMID64);
char player_name[LENGTH_NAME];
GetClientName(client, player_name, sizeof(player_name));
char url[LENGTH_MESSAGE];
Format(url, sizeof(url), "%s/v2/utility/detail", apiHost);

HTTPRequest httpRequest = new HTTPRequest(url);
httpRequest.AppendQueryParam("token", token);
httpRequest.AppendQueryParam("article_id", utId);
httpRequest.Get(UtilityDetailResponseCallback, client);

// =====================================================
// HTTPRequest postRequest = new HTTPRequest("http://ci.csgowiki.top:2333/trigger/wiki-player");
// postRequest.SetHeader("Content-Type", "application/json");
// =====================================================
// HTTPRequest postRequest = new HTTPRequest("http://ci.csgowiki.top:2333/trigger/wiki-player");
// postRequest.SetHeader("Content-Type", "application/json");

// JSONObject postData = new JSONObject();
// postData.SetString("map_name", g_sCurrentMap);
// postData.SetString("steamid", steamid_);
// postData.SetString("player_name", player_name);
// postRequest.Post(postData, WikiPlayerTriggerResponseCallback);
// delete postData;
// JSONObject postData = new JSONObject();
// postData.SetString("map_name", g_sCurrentMap);
// postData.SetString("steamid", steamid_);
// postData.SetString("player_name", player_name);
// postRequest.Post(postData, WikiPlayerTriggerResponseCallback);
// delete postData;
}

void ResetSingleClientWikiState(client, bool force_del=false) {
Expand Down Expand Up @@ -243,6 +268,48 @@ void UtilityDetailResponseCallback(HTTPResponse response, int client) {
else if (StrEqual(status, "ok")) {
JSONObject json_obj = view_as<JSONObject>(resp_json.Get("utility_detail"));
ShowUtilityDetail(client, json_obj);

if (check_function_on(g_hLocalCacheEnable,"")) {
char utId[LENGTH_UTILITY_ID];
json_obj.GetString("id", utId, sizeof(utId));
char path[256];
BuildPath(Path_SM, path, sizeof(path), "data/csgowiki/cache/detail/%s.json", utId);
Handle hFile = OpenFile(path,"w");
WriteFileString(hFile,detail,false);
CloseHandle(hFile);

// LRU
char directoryPath[84];
char filepath[84];
BuildPath(Path_SM, directoryPath, sizeof(directoryPath), "data/csgowiki/cache/detail");

int limit = GetConVarInt(g_hLocalCacheFileLimit);
if (limit != -1) {
DirectoryListing dL = OpenDirectory(directoryPath);
char buffer[64];
char fileName[64];
dL.GetNext(buffer, sizeof(buffer));
dL.GetNext(buffer, sizeof(buffer));
int timestamp = 0;
int fileCounter = 0;
while(dL.GetNext(buffer, sizeof(buffer))) {
char path[84];
BuildPath(Path_SM, path, sizeof(path), "data/csgowiki/cache/detail/%s", buffer);
int currentTimestamp = GetFileTime(path, FileTime_Created);
if (fileCounter == 0) {timestamp = currentTimestamp;}
else if (currentTimestamp < timestamp) {
timestamp = currentTimestamp;
fileName = buffer;
}
fileCounter++;
}

if (fileCounter >= limit) {
BuildPath(Path_SM, filepath, sizeof(filepath), "data/csgowiki/cache/detail/%s", fileName);
DeleteFile(filepath);
}
}
}
}
delete resp_json;
}
Expand All @@ -252,11 +319,11 @@ void UtilityDetailResponseCallback(HTTPResponse response, int client) {
}


void WikiPlayerTriggerResponseCallback(HTTPResponse response, any data) {
if (response.Status != HTTPStatus_OK) {
PrintToServer("wiki-player trigger error: %d", response.Status);
}
}
// void WikiPlayerTriggerResponseCallback(HTTPResponse response, any data) {
// if (response.Status != HTTPStatus_OK) {
// PrintToServer("wiki-player trigger error: %d", response.Status);
// }
// }

void ShowUtilityDetail(client, JSONObject detail_json) {
if (!IsPlayer(client)) return;
Expand Down
2 changes: 1 addition & 1 deletion scripting/csgowiki/utils.sp
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,4 @@ void TextMsgFix(DataPack hPack) {

EndMessage();
}
}
}
4 changes: 4 additions & 0 deletions scripting/include/csgowiki.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ Handle g_hOnUtilitySubmit = INVALID_HANDLE;
Handle g_hOnUtilityWiki = INVALID_HANDLE;
Handle g_hWikiReqLimit = INVALID_HANDLE;
Handle g_hWikiAutoKicker = INVALID_HANDLE;
Handle g_hLocalCacheEnable = INVALID_HANDLE;
Handle g_hLocalCacheFileLimit = INVALID_HANDLE;

// wiki token
Handle g_hCSGOWikiToken;

// api host
Handle g_hApiHost = INVALID_HANDLE;

Expand Down