task.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // 获取任务
  2. const server = "http://xjf.lianyou.fun:8095/v1";
  3. const index = 1;
  4. const StatusModifyDevice = 2;//獲取賬號
  5. const StatusGetStartButtonOfSpirit = 6; //获取按键小精灵开始脚本按钮
  6. const StatusStartSpirit = 7; //开始脚本
  7. const StatusPushSuperUserFile = 8; //推送root权限文件
  8. var computer = "";//hema 4005 yunshouji 9991
  9. var game_id = "";
  10. var NET_TIMEOUT_START_TIME_MINUTE=0;//统计网络失败的开始时间是第几分钟
  11. var SIMULATOR_NAME = "";
  12. var ACCOUNT = "";
  13. var URL_GET_TASK = util.format("%s/task/get_task?computer_name=%s&game_id=%s", server, computer, game_id);
  14. var URL_GET_ACCOUNT = util.format("%s/task/get_account?computer_name=%s&game_id=%s", server, computer, game_id);
  15. var URL_GET_RUN_STATUS = util.format("%s/pc_simulator/get_status?game_id=%s&simulator_name=", server, game_id);
  16. var URL_LOG = util.format("%s/device/setAccountLog?game_id=%s", server, game_id);
  17. var URL_SET_RUN_STATUS = util.format("%s/pc_simulator/set_status", server);
  18. var URL_ADD_SIMULATOR = util.format("%s/pc_simulator/add", server);
  19. function Task() {
  20. let packageNameSpirit;
  21. let deviceAccount;
  22. let simulator_name;
  23. let urlGetRunningStatus
  24. this.readParam=function(){
  25. computer=files.read("/sdcard/computer.txt")
  26. game_id=files.read("/sdcard/game_id.txt")
  27. URL_GET_TASK = util.format("%s/task/get_task?computer_name=%s&game_id=%s", server, computer, game_id);
  28. URL_GET_ACCOUNT = util.format("%s/task/get_account?computer_name=%s&game_id=%s", server, computer, game_id);
  29. URL_GET_RUN_STATUS = util.format("%s/pc_simulator/get_status?game_id=%s&simulator_name=", server, game_id);
  30. }
  31. this.main = function () {
  32. if (this.checkRunning()) {//正在執行
  33. return
  34. }
  35. if (!this.getTask()) {//獲取任務
  36. return
  37. }
  38. if (!this.getDeviceAccount()) {//獲取賬號
  39. return
  40. }
  41. this.stopPackage(this.packageNameSpirit);
  42. this.home();//
  43. this.addSimulator();
  44. this.pushAccount();//推送模擬器名稱到硬盤
  45. this.luanchSpirit();//啟動按鍵精靈app
  46. sleep(1000 * 50)
  47. this.startSpirit();//點擊音量小
  48. }
  49. this.getTask = function GetTask() {
  50. let task = this.httpGet(URL_GET_TASK);
  51. if (!task) {
  52. return false
  53. }
  54. if (task.Status == "stop") {
  55. return false
  56. }
  57. toastLog(task.Status + task.PackageNameSpirit);
  58. this.packageNameSpirit = task.PackageNameSpirit;
  59. return true
  60. }
  61. this.getDeviceAccount = function () {
  62. let deviceAccount = this.httpGet(URL_GET_ACCOUNT);
  63. if (!deviceAccount) {
  64. return false
  65. }
  66. this.simulator_name = util.format("%s_%d_%d_%s", computer, index, game_id, deviceAccount.account);
  67. this.urlGetRunningStatus = URL_GET_RUN_STATUS + this.simulator_name
  68. SIMULATOR_NAME = this.simulator_name
  69. ACCOUNT = deviceAccount.account
  70. this.setRunningStatus(StatusModifyDevice);
  71. return true
  72. }
  73. this.checkRunning = function () {
  74. this.log("check_running", "");
  75. this.urlGetRunningStatus = URL_GET_RUN_STATUS + SIMULATOR_NAME
  76. let runStatus = this.httpGet(this.urlGetRunningStatus)
  77. if (!runStatus) {
  78. return false
  79. }
  80. if (runStatus.Status == "stop") {
  81. return false
  82. }
  83. if (runStatus.Status == "") {
  84. return false
  85. }
  86. return true
  87. }
  88. this.setRunningStatus = function (status) {
  89. let url = util.format("%s?status=%d&simulator_name=%s", URL_SET_RUN_STATUS, status, SIMULATOR_NAME);
  90. this.httpGet(url);
  91. }
  92. this.addSimulator = function () {
  93. let url = util.format("%s?simulator_name=%s", URL_ADD_SIMULATOR, SIMULATOR_NAME);
  94. this.httpGet(url);
  95. }
  96. this.log = function (action, action_result) {
  97. let url = util.format("%s&account=%s&action=%s&action_result=%s", URL_LOG, ACCOUNT, action, action_result);
  98. this.httpGet(url);
  99. }
  100. this.pushAccount = function () {
  101. files.write("/sdcard/simulator_uuid.txt", this.simulator_name);
  102. this.setRunningStatus(StatusPushSuperUserFile);
  103. this.log("push_account", "");
  104. }
  105. this.luanchSpirit = function () {
  106. app.launchPackage(this.packageNameSpirit)
  107. this.setRunningStatus(StatusGetStartButtonOfSpirit);
  108. this.log("spirit_launch", "");
  109. }
  110. this.startSpirit = function () {
  111. // shell("input keyevent KEYCODE_VOLUME_DOWN", true);
  112. this.tap(349, 1214);//启动 349,1214
  113. sleep(3000)
  114. this.tap(712, 399);//旁边 712,399
  115. sleep(1000)
  116. this.tap(273, 405);//伸出来 273,405
  117. this.setRunningStatus(StatusStartSpirit);
  118. this.log("spirit_start", "");
  119. }
  120. this.tap = function (x, y) {
  121. let cmd = util.format("input tap %d %d", x, y);
  122. shell(cmd, true);
  123. }
  124. this.stopPackage = function (package) {
  125. let cmd = util.format("am force-stop %s", package);
  126. shell(cmd, true);
  127. this.log("spirit_stop", "");
  128. }
  129. this.home = function () {
  130. shell("input keyevent KEYCODE_HOME", true);
  131. this.log("home", "");
  132. }
  133. this.changeDevice = function () {
  134. this.exec()
  135. sleep(10000)
  136. }
  137. this.changeIP = function () {
  138. this.exec()
  139. sleep(10000)
  140. }
  141. /*-------公共使用函数--------*/
  142. this.httpGet = function (url) {
  143. console.info(url);
  144. let obj;//
  145. let res;
  146. try{
  147. res = http.get(url);
  148. } catch (error) {
  149. console.error(error);
  150. if (this.checkIfChangeVPN()){
  151. console.info("要切换VPN");
  152. this.changeVPN();
  153. NET_TIMEOUT_START_TIME_MINUTE=0;
  154. }else{
  155. console.info("还不需要切换VPN");
  156. }
  157. return false;
  158. }
  159. NET_TIMEOUT_START_TIME_MINUTE=0;
  160. try {
  161. if (res.statusCode != 200) {
  162. toast("请求失败: " + res.statusCode + " " + res.statusMessage);
  163. return false;
  164. }
  165. obj = res.body.json();
  166. } catch (error) {
  167. console.error(error);
  168. return false;
  169. }
  170. console.log(obj);
  171. return obj;
  172. }
  173. this.checkIfChangeVPN=function(){
  174. let minute=new Date().getMinutes();
  175. console.info("检测是否切换VPN");
  176. console.info(minute);
  177. console.info(NET_TIMEOUT_START_TIME_MINUTE);
  178. if (NET_TIMEOUT_START_TIME_MINUTE==0){
  179. NET_TIMEOUT_START_TIME_MINUTE=minute
  180. return false;
  181. }
  182. if (minute-NET_TIMEOUT_START_TIME_MINUTE<4){
  183. return false;
  184. }
  185. return true;
  186. }
  187. this.changeVPN=function(){
  188. shell("ipclient 2a521e4f7bf54ffb9dad5368c48dd783 0", true);
  189. console.info("关闭VPN");
  190. sleep(3000);
  191. shell("ipclient 2a521e4f7bf54ffb9dad5368c48dd783 1", true);
  192. console.info("切换VPN");
  193. sleep(2000);
  194. }
  195. this.exec = function (cmd) {
  196. var result = shell(cmd, true);
  197. log(result);
  198. console.show();
  199. if (result.code == 0) {
  200. toast("执行成功");
  201. } else {
  202. toast("执行失败!请到控制台查看错误信息");
  203. }
  204. }
  205. }
  206. module.exports = Task