StallBoard/LSL/raw/KVPRestore.lsl

163 lines
6.6 KiB
Text
Executable file

#include "variables.lsl"
#include "functions.lsl"
string g_sDate;
integer g_iTotal = 0;
integer g_iTotalDB;
integer g_iProcessed = 0;
GetKVP(integer iOffset)
{
UpdateDSRequest(NULL, llHTTPRequest("https://api.zontreck.com/zni/SLKVPStore.php", [HTTP_METHOD,"POST", HTTP_MIMETYPE, "application/json"], llList2Json(JSON_OBJECT,["op","get","offset",iOffset])), SetDSMeta(["get", iOffset]));
}
integer Percent(integer I, integer M)
{
return I*100/M;
}
string Progress(integer iProg){
// 64 chars max, must account for the brackets, percent sign, and the numbers.
integer iMax = 32;
iMax -= 2; // Brackets
iMax -= 6; // Numbers, spaces, % sign
integer i=0;
string sFinal = "[";
float fProg = (float)("0."+(string)iProg);
if(iProg>=100)fProg=1.0;
if(iProg<10)fProg=(float)("0.0"+(string)iProg);
integer pos = llFloor(iMax * fProg);
for(i=0;i<=iMax;i++){
if(i < pos) sFinal += "█";
else if(i == pos)sFinal += ">";
else sFinal += "░";
}
sFinal += "] "+(string)iProg+"%";
return sFinal;
}
text(){
llSetText("Downloading KVP from ZNI\n \n"+Progress(Percent(g_iProcessed,g_iTotal)), <0,1,0>,1);
}
bootstrap()
{
llSleep(2);
UpdateDSRequest(NULL, llHTTPRequest("https://api.zontreck.com/zni/SLKVPStore.php", [HTTP_METHOD,"POST", HTTP_MIMETYPE, "application/json"], llList2Json(JSON_OBJECT,["op", "get", "offset", g_iProcessed])), SetDSMeta(["download"]));
}
default
{
state_entry()
{
llSetText("ZNI KVP Restore Terminal", <0,1,1>,1);
}
touch_start(integer total_number)
{
if(llGetOwner()!=llDetectedKey(0))return;
llListen(929, "", llGetOwner(), "");
llTextBox(llGetOwner(), "What date to restore?", 929);
}
listen(integer c,string n,key i,string m){
g_sDate = m;
llSay(0, "Preparing to restore date: "+g_sDate);
UpdateDSRequest(NULL, llHTTPRequest("https://api.zontreck.com/zni/SLKVPStore.php", [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/json"], llList2Json(JSON_OBJECT,["op", "restore", "date", g_sDate])), SetDSMeta(["restore"]));
}
http_response(key k, integer s,list m,string b){
if(HasDSRequest(k)!=-1){
//llSay(0, "["+(string)s+"]: "+b);
list lMeta = GetMetaList(k);
DeleteDSReq(k);
string sOp = llList2String(lMeta,0);
list lParam = llParseStringKeepNulls(b,[";;",";"],[]);
//llSay(0, "HTTP Response: "+b);
if(sOp == "restore"){
if(llList2String(lParam,1) == "OK"){
g_iTotalDB = (integer)llList2String(lParam,2);
llSay(0, "Restore from file to database, successful, preparing to download.\n \n[ Total KVP in database: "+(string)g_iTotalDB+" ]");
llSay(0, "Preparing to delete all data...");
UpdateDSRequest(NULL, llKeyCountKeyValue(), SetDSMeta(["count"]));
}else {
llSay(0, "Fatal: Data could not be restored");
}
}else if(sOp == "download")
{
g_iProcessed++;
text();
string sJson = llList2String(lParam,2);
//llSay(0, "SERVER: "+sJson);
/*if(llJsonGetValue(sJson,["expire"])!="-1" && llJsonValueType(sJson,["expire"]) == JSON_NUMBER){
integer newExpireTime = llGetUnixTime();
newExpireTime = (newExpireTime+((60*60*24)*30));
sJson = llJsonSetValue(sJson, ["expire"], (string)newExpireTime);
//llSay(0, "Record is a stallboard, resetting expiretimer");
//llSay(0, "NEW: "+sJson);
}*/
//llSay(0, "Set Key ["+llList2String(lParam,1)+"] with value: "+sJson);
llUpdateKeyValue(llList2String(lParam,1), sJson, FALSE, "");
if(g_iProcessed>=g_iTotal){
llSay(0, "Finished restoring all data from ZNI Central control. Cooling down before resetting scripts!");
llSleep(120);
llRegionSay(5, "restore9f");
llSay(0, "Completed!\n \n[ Have a nice day ]");
llResetScript();
return;
}
bootstrap();
}
}
}
dataserver(key kID, string sData)
{
if(HasDSRequest(kID)!=-1)
{
list lMeta = GetMetaList(kID);
DeleteDSReq(kID);
string sValue = llGetSubString(sData,2,-1);
llSleep(1);
if(llList2String(lMeta,0)=="count"){
g_iTotal = (integer)sValue;
if(g_iTotal == 0){
g_iTotal = 1;
g_iProcessed=1;
llSetText("Erasing KVP Storage\n \n"+Progress(Percent(g_iProcessed,g_iTotal)), <1,0,0>,1);
llRegionSay(5, "restore9f");
llSay(0, "No Database keys\n \n[ Downloading backup from ZNI KVP Server ]");
llSleep(30);
g_iTotal = g_iTotalDB;
g_iProcessed=0;
text();
bootstrap();
return;
}
g_iProcessed = 0;
llSetText("Erasing SL KVP Storage\n \n"+Progress(Percent(g_iProcessed,g_iTotal)), <1,0,0>,1);
UpdateDSRequest(NULL, llKeysKeyValue(0, 1), SetDSMeta(["getkey", 0]));
} else if(llList2String(lMeta,0) == "getkey"){
integer iKVP = (integer)llList2String(lMeta,1);
iKVP++;
UpdateDSRequest(NULL, llDeleteKeyValue(sValue), SetDSMeta(["delkey", iKVP]));
} else if(llList2String(lMeta,0) == "delkey"){
integer iKVP = (integer)llList2String(lMeta,1);
g_iProcessed=iKVP;
llSleep(1);
llDeleteKeyValue(sValue);
if(g_iProcessed >= g_iTotal)
{
llSetText("Erasing SL KVP Storage\n \n"+Progress(Percent(g_iProcessed,g_iTotal)), <1,0,0>,1);
llSay(0, "Finished SL erasing all data\n \n[ Rechecking KVP ]");
UpdateDSRequest(NULL, llKeyCountKeyValue(), SetDSMeta(["count"]));
return;
}
llSetText("Erasing SL KVP Storage\n \n"+Progress(Percent(g_iProcessed,g_iTotal)), <1,0,0>,1);
UpdateDSRequest(NULL, llKeysKeyValue(0, 1), SetDSMeta(["getkey", iKVP]));
}
}
}
}