Open source

This commit is contained in:
Kaan Soral
2023-10-15 13:06:58 +03:00
parent 256ce800e7
commit 553b121f17
2633 changed files with 769945 additions and 454 deletions
BIN
View File
Binary file not shown.
+109
View File
@@ -0,0 +1,109 @@
var { workerData, parentPort } = require('worker_threads');
if(!workerData) // for chrome://inspect with "node --expose-gc --inspect character.js"
{
workerData={
"name":"NewWarrior",
"region":"EU",
"server":"I",
"code":"cli_tests",
"auth":"5818821692620800-gjxqztPovvpiaKTGnjEtT",
};
parentPort={postMessage:function(){},on:function(){}};
//jsdomDevtoolsFormatter = require('jsdom-devtools-formatter');
//jsdomDevtoolsFormatter.install();
}
// In CODE, you can: parent.CLI_OUT.push({"type":"custom_type",data:{}}) - it's propagated to the below routine, and if not handled, to master.js
console.log(workerData.name);
global.gc();
var jsdom,request,localstorager,localStorage=null;
try{
jsdom=require("jsdom");
}catch(e){
console.log("You need to run: npm install jsdom");
process.exit();
}
var { JSDOM }=jsdom;
try{
request=require('request');
}catch(e){
console.log("You need to run: npm install request");
process.exit();
}
try{
localstorager=require('node-localstorage').LocalStorage;
localStorage=new localstorager("./storage");
}catch(e){
console.log("You need to run: npm install node-localstorage for functioning localStorage support!");
}
var base_url="http://thegame.com";
var url=base_url+"/character/"+workerData.name+"/in/"+workerData.region+"/"+workerData.server+"/?no_html=bot&is_bot=1&is_cli=1&code="+workerData.code+"&auth="+workerData.auth;
var dom=false,code_active=false;
request(url, (error, response, body) => {
var virtualConsole=new jsdom.VirtualConsole();
virtualConsole.sendTo(console);
var options={
url:url,
pretendToBeVisual:true,
includeNodeLocations: true,
resources:'usable',
runScripts:'dangerously',
virtualConsole:virtualConsole,
beforeParse:function(window){}
}
dom=new JSDOM(body,options);
dom.name=workerData.name;
if(localStorage && !dom.window.ls_emulation) dom.window.ls_emulation=localStorage;
dom.window.cli_require=require;
//console.log(dom.window.document.body.innerHTML);
});
setInterval(function(){
if(!dom || !dom.window.game_logs) return;
if(dom.window.character) dom.name=dom.window.character.name;
dom.window.game_logs.forEach(function(l){
console.log(dom.name+"[LOG] "+l[0]);
});
dom.window.game_logs=[];
dom.window.game_chats.forEach(function(c){
console.log(dom.name+"[CHAT] "+c[0]+": "+c[1]);
});
dom.window.game_chats=[];
dom.window.CLI_OUT.forEach(function(m){
if(m.type=="kill") process.exit(1);
else if(m.type=="time_kill")
{
console.log("[CLI] You need to purchase CLI time from the Main Menu (currently: parent.api_call(\"cli_time\"))");
parentPort.postMessage({"type":"kill"});
}
else parentPort.postMessage(m);
});
dom.window.CLI_OUT=[];
if(!code_active && dom.window.code_active) code_active=true,parentPort.postMessage({"type":"code_active"});
},10);
parentPort.on("message",function(data){
dom.window.CLI_IN.push(data);
});
setInterval(function(){
if(dom && dom.window.character && dom.window.socket && (new Date()-dom.window.last_ping)<30000)
parentPort.postMessage({type:"update"});
},1000);
setInterval(function(){
if (process.memoryUsage().heapUsed > 150000000) {
global.gc(true);
console.log("["+workerData.name+"] Manual GC");
}
},20000);
process.on('unhandledRejection', (reason, p) => {
// console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
+35
View File
@@ -0,0 +1,35 @@
var puppeteer = require('puppeteer');
var { workerData, parentPort } = require('worker_threads');
if(!workerData) // for chrome://inspect with "node --expose-gc --inspect character.js"
{
workerData={
"name":"Wizard",
"region":"EU",
"server":"I",
"code":"PhoenixHunter",
"auth":"5818821692620800-gjxqztPovvpiaKTGnjEtT",
};
parentPort={postMessage:function(){},on:function(){}};
//jsdomDevtoolsFormatter = require('jsdom-devtools-formatter');
//jsdomDevtoolsFormatter.install();
}
// In CODE, you can: parent.CLI_OUT.push({"type":"custom_type",data:{}}) - it's propagated to the below routine, and if not handled, to master.js
var base_url="http://thegame.com";
var url=base_url+"/character/"+workerData.name+"/in/"+workerData.region+"/"+workerData.server+"/?no_html=1&code="+workerData.code+"&auth="+workerData.auth;
var dom=false,code_active=false;
(async () => {
console.log(workerData.name);
const browser = await puppeteer.launch({args:["--disable-gpu","--disable-gpu-compositing","--disable-accelerated-2d-canvas","--ignore-gpu-blacklist","--headless"]});
const page = await browser.newPage();
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
await page.goto(url);
console.log("Loaded page.");
})();
setInterval(function(){
},1);
+218
View File
@@ -0,0 +1,218 @@
var is_server=0;
var character={
x:0, real_x:0,
y:0, real_y:0,
map:"main",
base:{h: 8, v: 7, vn: 2},
processor:true,
}
var smart={
moving:false,
map:"main",x:0,y:0,
on_done:function(){},
plot:null,
edge:20, // getting 20px close to the target is enough
baby_edge:80, // start treading lightly when 60px close to the target or starting point
try_exact_spot: false,
use_town:false,
prune:{
smooth:true,
map:true,
},
flags:{}
};
function game_log(x)
{
console.log("[MGL]: "+x);
}
var queue=[],visited={},start=0,best=null;
var moves=[[0,15],[0,-15],[15,0],[-15,0]];
var baby_steps=[[0,5],[0,-5],[5,0],[-5,0]];
// baby_steps is a new logic, used just around the target or starting point, to get out of tough spots [08/03/19]
function plot(index)
{
if(index==-1) return;
plot(queue[index].i); // Recursively back-tracks the path we came from
smart.plot.push(queue[index]);
}
function qpush(node)
{
// If we haven't visited this location, adds the location to the queue
if(smart.prune.map && smart.flags.map && node.map!=smart.map) return;
if(visited[node.map+"-"+node.x+"-"+node.y]) return;
if(!node.i) node.i=start; // set the index, to aid the plot function
queue.push(node);
visited[node.map+"-"+node.x+"-"+node.y]=true;
}
function smooth_path()
{
var i=0,j;
while(i<smart.plot.length)
{
// Assume the path ahead is [i] [i+1] [i+2] - This routine checks whether [i+1] could be skipped
// The resulting path is smooth rather than rectangular and bumpy
// Try adding "function smooth_path(){}" or "smart.prune.smooth=false;" to your Code
// [06/07/18]: (!smart.plot[i+2] || !smart.plot[i+2].transport) - without this condition, in "winterland", move(-160,-660), smart_move("main") fails
while(i+2<smart.plot.length && smart.plot[i].map==smart.plot[i+1].map && smart.plot[i].map==smart.plot[i+1].map && (!smart.plot[i+2] || !smart.plot[i+2].transport) &&
can_move({map:smart.plot[i].map,x:smart.plot[i].x,y:smart.plot[i].y,going_x:smart.plot[i+2].x,going_y:smart.plot[i+2].y,base:character.base}))
smart.plot.splice(i+1,1);
i++;
}
}
function bfs()
{
var timer=new Date(),result=null,optimal=true;
while(start<queue.length)
{
var current=queue[start];
// game_log(current.x+" "+current.y);
var map=G.maps[current.map];
var c_moves=moves,qlist=[];
if(current.map==smart.map)
{
var c_dist=abs(current.x-smart.x)+abs(current.y-smart.y);
var s_dist=abs(current.x-smart.start_x)+abs(current.y-smart.start_y);
smart.flags.map=true;
if(c_dist<smart.baby_edge || s_dist<smart.baby_edge) c_moves=baby_steps;
if(c_dist<smart.edge)
{
result=start;
break;
}
else if(best===null || abs(current.x-smart.x)+abs(current.y-smart.y)<abs(queue[best].x-smart.x)+abs(queue[best].y-smart.y))
{
best=start;
}
}
else if(current.map!=smart.map)
{
if(smart.prune.map && smart.flags.map) {start++; continue;}
map.doors.forEach(function(door){
// if(simple_distance({x:map.spawns[door[6]][0],y:map.spawns[door[6]][1]},{x:current.x,y:current.y})<30)
if(smart.map!="bank" && door[4]=="bank" && !G.maps[current.map].mount || door[8]=="complicated") return; // manually patch the bank shortcut
if(is_door_close(current.map,door,current.x,current.y) && can_use_door(current.map,door,current.x,current.y))
qlist.push({map:door[4],x:G.maps[door[4]].spawns[door[5]||0][0],y:G.maps[door[4]].spawns[door[5]||0][1],transport:true,s:door[5]||0});
});
map.npcs.forEach(function(npc){
if(npc.id=="transporter" && simple_distance({x:npc.position[0],y:npc.position[1]},{x:current.x,y:current.y})<75)
{
for(var place in G.npcs.transporter.places)
{
qlist.push({map:place,x:G.maps[place].spawns[G.npcs.transporter.places[place]][0],y:G.maps[place].spawns[G.npcs.transporter.places[place]][1],transport:true,s:G.npcs.transporter.places[place]});
}
}
});
}
if(smart.use_town) qpush({map:current.map,x:map.spawns[0][0],y:map.spawns[0][1],town:true}); // "town"
shuffle(c_moves);
c_moves.forEach(function(m){
var new_x=current.x+m[0],new_y=current.y+m[1];
// game_log(new_x+" "+new_y);
// utilise can_move - game itself uses can_move too - smart_move is slow as can_move checks all the lines at each step
if(can_move({map:current.map,x:current.x,y:current.y,going_x:new_x,going_y:new_y,base:character.base}))
qpush({map:current.map,x:new_x,y:new_y});
});
qlist.forEach(function(q){qpush(q);}); // So regular move's are priotised
start++;
}
if(result===null)
{
result=best,optimal=false;
game_log("Path not found!","#CF575F");
smart.moving=false;
smart.success=false;
}
else
{
plot(result);
if(1) // [08/03/19] - to attempt and move to the actual coordinates
{
var last=smart.plot[smart.plot.length-1]; if(!last) last={map:character.map,x:character.real_x,y:character.real_y};
if(smart.x!=last.x || smart.y!=last.y)
{
smart.try_exact_spot=true;
smart.plot.push({map:last.map,x:smart.x,y:smart.y});
}
}
smart.found=true;
if(smart.prune.smooth) smooth_path();
if(optimal) game_log("Path found!","#C882D1");
else game_log("Path found~","#C882D1");
// game_log(queue.length);
smart.moving=false;
smart.success=true;
}
}
function start_pathfinding()
{
smart.try_exact_spot=false;
smart.searching=true;
smart.start_x=character.real_x;
smart.start_y=character.real_y;
queue=[],visited={},start=0,best=null;
qpush({x:character.real_x,y:character.real_y,map:character.map,i:-1});
game_log("Searching for a path...","#89D4A2");
bfs();
}
function continue_pathfinding()
{
bfs();
}
function smart_move_logic()
{
if(!smart.moving) return;
if(!smart.searching && !smart.found)
{
start_pathfinding();
}
else if(!smart.found)
{
continue_pathfinding();
}
else if(!character.moving && can_walk(character) && !is_transporting(character))
{
if(!smart.plot.length)
{
smart.moving=false;
smart.on_done(true);
return;
}
var current=smart.plot[0];
smart.plot.splice(0,1);
// game_log(JSON.stringify(current));
if(current.town)
{
use("town");
}
else if(current.transport)
{
parent.socket.emit("transport",{to:current.map,s:current.s});
parent.push_deferred("transport");
// use("transporter",current.map);
}
else if(character.map==current.map && (smart.try_exact_spot && !smart.plot.length || can_move_to(current.x,current.y)))
{
// game_log("S "+current.x+" "+current.y);
move(current.x,current.y);
}
else
{
game_log("Lost the path...","#CF5B5B");
smart_move({map:smart.map,x:smart.x,y:smart.y},smart.on_done);
}
}
}
+119
View File
@@ -0,0 +1,119 @@
const { Worker, SHARE_ENV } = require('worker_threads');
// parent.api_call("cli_time") to purchase 7 days for 29 shells. The rates will go up
// IMPORTANT: RUN AS
// node --expose-gc master.js
// for global.gc();
var G=null;
var fs=require("fs");
eval(""+fs.readFileSync("../../js/common_functions.js")); // Manually copy/paste common_functions.js from libraries/ to this directory
eval(""+fs.readFileSync("functions.js"));
var auth="5818821692620800-gjxqztPovvpiaKTGnjEtT"; // show_json(parent.Cookies.get("auth"))
var characters=[
{
"name":"GG",
"region":"EU",
"server":"I",
"code":"local_cm_receiver",
},
{
"name":"MERC",
"region":"EU",
"server":"I",
"code":"local_cm_sender",
},
];
function run_character(workerData)
{
console.log(workerData);
const worker = new Worker('./character.js', { workerData:workerData, env: SHARE_ENV, execArgv:["expose-gc","inspect"]});
worker.on('message',function(data){
if(data.type=="update")
worker.info.last_update=new Date();
else if(data.type=="smart_move")
{
G=data.G;
console.log("[CLI] SMART_MOVE");
smart.moving=true;
smart.plot=[]; smart.flags={}; smart.searching=smart.found=false;
character.x=character.real_x=data.start_x;
character.y=character.real_y=data.start_y;
character.map=data.start_map;
smart.x=data.x;
smart.y=data.y;
smart.map=data.map;
start_pathfinding();
console.log("[CLI] SMART_MOVE_RESULT: "+smart.found);
console.log(smart.plot);
worker.postMessage({type:"smart_move",found:smart.found,plot:smart.plot});
}
else if(data.type=="cm")
{
var sent=false;
characters.forEach(function(char_info){
if(char_info.run && char_info.name==data['to'])
{
char_info.worker.postMessage(data);
sent=true;
}
});
if(!sent) console.log("[UNSENT CM] TO: "+data['to']);
}
else if(data.type=="code_active")
{
worker.info.code_active=true;
worker.postMessage({type:"hello","from":"master.js"});
}
else if(data.type=="kill") process.exit();
});
worker.on('error',function(data){
console.log("[MASTER] "+worker.info.name+" error: "+data);
});
worker.on('exit',function(code){
if (code !== 0)
console.log("[MASTER] "+worker.info.name+" stopped with code: "+code);
worker.info.run=false;
});
return worker;
}
setInterval(function(){
var current=new Date(),delay=false;
characters.forEach(function(info){
if(info.run && (current-info.last_update)>40000)
{
console.log("[MASTER] Reloading "+info.name);
info.run=false;
info.code_active=false;
info.worker.terminate();
delay=true;
}
});
if(delay) return; // worker.on('exit') needs to fire first
characters.forEach(function(info){
if(!info.run)
{
if(!info.auth) info.auth=auth;
info.run=true;
info.code_active=false;
info.last_update=new Date();
delete info.worker;
var workerData={}; Object.assign(workerData,info);
info.worker=run_character(info);
info.worker.info=info;
}
});
},100);
setInterval(function(){
if (process.memoryUsage().heapUsed > 300000000) {
global.gc();
console.log("[MASTER] Manual GC");
}
},20000);
+1
View File
@@ -0,0 +1 @@
{"heartbeat":{"GG":"Sun Jun 28 2020 19:58:06 GMT+0300 (GMT+03:00)"}}
+1
View File
@@ -0,0 +1 @@
{"heartbeat":{"MERC":"Sun Jun 28 2020 19:58:06 GMT+0300 (GMT+03:00)"}}
@@ -0,0 +1 @@
{"heartbeat":{"MERC":"Wed Jun 17 2020 21:33:13 GMT+0300 (GMT+03:00)"}}
+1
View File
@@ -0,0 +1 @@
{"heartbeat":{"Wizard":"Sun Jul 12 2020 14:09:42 GMT+0300 (GMT+03:00)"}}
+1
View File
@@ -0,0 +1 @@
10
+1
View File
@@ -0,0 +1 @@
{"time":"2020-06-24T18:53:30.947Z","ip":"192.168.1.125","port":"8090"}
+50
View File
@@ -0,0 +1,50 @@
var jsdom,request,localstorager,localStorage=null;
try{
jsdom=require("jsdom");
}catch(e){
console.log("You need to run: npm install jsdom");
process.exit();
}
var { JSDOM }=jsdom;
try{
request=require('request');
}catch(e){
console.log("You need to run: npm install request");
process.exit();
}
try{
localstorager=require('node-localstorage').LocalStorage;
localStorage=new localstorager("./storage");
}catch(e){
console.log("You need to run: npm install node-localstorage for functioning localStorage support!");
}
localStorage.setItem("gg",10);
//setInterval(function(){
// console.log(localStorage.getItem("gg"));
//},3000);
return;
function test1(){ var a=[]; for(var i=0;i<10000;i++) a.push(i); setTimeout(function(){ test1() },1); }
if(0)
{
for(var i=0;i<100;i++) test1();
}
if(1)
{
const jsdom=require("jsdom");
const { JSDOM }=jsdom;
var options={
url:"http://gg.com",
pretendToBeVisual:true,
includeNodeLocations: true,
resources:'usable',
runScripts:'dangerously',
beforeParse:function(window){}
}
dom=new JSDOM('<script>function test1(){ var t=setTimeout(function(){ test1() },1); }; test1(); </script>',options);
}