task.js 6.9 KB

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