From b313f11eb418bbf278c9bf5e74bc85706af72ae0 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 11 Mar 2023 18:44:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=B6=88=E6=81=AF=E4=BC=A0=E9=80=92?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=94=B9=E4=B8=BA=E6=96=87=E4=BB=B6=E4=BC=A0?= =?UTF-8?q?=E8=BE=93,=20=E4=BB=A5=E4=BE=BF=E4=BA=8E=20docker=20=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E4=B9=8B=E9=97=B4=E4=BC=A0=E8=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/utils/Consumer.java | 36 ++++++++++++++++++ .../com/kob/botrunningsystem/utils/Bot.java | 37 ++++++++++++++----- backendcloud/input.txt | 1 + 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 backendcloud/input.txt diff --git a/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/service/impl/utils/Consumer.java b/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/service/impl/utils/Consumer.java index f5f88af..fc44fe1 100644 --- a/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/service/impl/utils/Consumer.java +++ b/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/service/impl/utils/Consumer.java @@ -8,7 +8,12 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; import java.util.UUID; +import java.util.function.Supplier; @Component public class Consumer extends Thread { @@ -41,11 +46,19 @@ public class Consumer extends Thread { } } +/* // 在 Code 中的 Bot 类名后面," implements com.kob.botrunningsystem.utils.BotInterface" 之前加上 uid private String addUid(String code, String uid) { int k = code.indexOf(" implements com.kob.botrunningsystem.utils.BotInterface"); return code.substring(0, k) + uid + code.substring(k); } +*/ + + // 在 Code 中的 Bot 类名后面," implements com.kob.botrunningsystem.utils.BotInterface" 之前加上 uid + private String addUid(String code, String uid) { + int k = code.indexOf(" implements java.util.function.Supplier"); + return code.substring(0, k) + uid + code.substring(k); + } @Override public void run() { @@ -56,14 +69,37 @@ public class Consumer extends Thread { // Reflect 来自 JOOR 依赖: 可以动态的编译 java 代码 // 重名类只会编译一遍,为了每次新线程都重新编译, // 在类名后加一个随机字符串 UUID(每次返回一个不一样的ID) + /* BotInterface botInterface = Reflect.compile( "com.kob.botrunningsystem.utils.Bot" + uid, addUid(bot.getBotCode(), uid) ).create().get(); // compile(编译) 完这个类之后 create(创建) 一个类,并 get(获取) 到这个类 +*/ + // 替换接口类型:修改为文件传输形式 + Supplier botInterface = Reflect.compile( + "com.kob.botrunningsystem.utils.Bot" + uid, + addUid(bot.getBotCode(), uid) + ).create().get(); // compile(编译) 完这个类之后 create(创建) 一个类,并 get(获取) 到这个类 + + // 将微服务之间的数据包通过文件传输,便于在 docker 容器之间进行数据交互 + File file = new File("input.txt"); // 创建输出文件 + try(PrintWriter fileOut = new PrintWriter(file)){ + fileOut.println(bot.getInput()); // 将 bot.getInput() 输出出去 + fileOut.flush(); // 清空缓冲区 + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + +/* // 存储获取到的方向 Integer direction = botInterface.nextMove(bot.getInput()); System.out.println("move-direction: " + bot.getUserId() + " " + direction); // 根据动态编译结果返回输入 +*/ + + // 同步修改为文件传输 + Integer direction = botInterface.get(); + System.out.println("move-direction: " + bot.getUserId() + " " + direction); // 根据动态编译结果返回输入 // 打包信息 MultiValueMap data = new LinkedMultiValueMap<>(); diff --git a/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/utils/Bot.java b/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/utils/Bot.java index 435ccd4..ab012c7 100644 --- a/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/utils/Bot.java +++ b/backendcloud/botrunningsystem/src/main/java/com/kob/botrunningsystem/utils/Bot.java @@ -1,12 +1,18 @@ -// Bot 自动化代码示例 +// Bot 自动化代码示例: 用于写前端 Bot 代码的辅助类, 在后端不参与运行 package com.kob.botrunningsystem.utils; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; +import java.util.Scanner; -public class Bot implements com.kob.botrunningsystem.utils.BotInterface { +//public class Bot implements com.kob.botrunningsystem.utils.BotInterface { +// 修改接口 +public class Bot implements java.util.function.Supplier { static class Cell { public int x, y; + public Cell(int x, int y) { this.x = x; this.y = y; @@ -26,26 +32,26 @@ public class Bot implements com.kob.botrunningsystem.utils.BotInterface { int x = sx, y = sy; int step = 0; res.add(new Cell(x, y)); - for (int i = 0; i < steps.length(); i ++ ) { + for (int i = 0; i < steps.length(); i++) { int d = steps.charAt(i) - '0'; x += dx[d]; y += dy[d]; res.add(new Cell(x, y)); - if (!check_tail_increasing( ++ step)) { + if (!check_tail_increasing(++step)) { res.remove(0); } } return res; } - @Override + // @Override public Integer nextMove(String input) { String[] strs = input.split("#"); int rows = 18; int cols = 19; int[][] g = new int[rows][cols]; - for (int i = 0, k = 0; i < rows; i ++ ) { - for (int j = 0; j < cols; j ++, k ++ ) { + for (int i = 0, k = 0; i < rows; i++) { + for (int j = 0; j < cols; j++, k++) { if (strs[0].charAt(k) == '1') { g[i][j] = 1; } @@ -58,11 +64,11 @@ public class Bot implements com.kob.botrunningsystem.utils.BotInterface { List aCells = getCells(aSx, aSy, strs[3]); List bCells = getCells(bSx, bSy, strs[6]); - for (Cell c: aCells) g[c.x][c.y] = 1; - for (Cell c: bCells) g[c.x][c.y] = 1; + for (Cell c : aCells) g[c.x][c.y] = 1; + for (Cell c : bCells) g[c.x][c.y] = 1; int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1}; - for (int i = 0; i < 4; i ++ ) { + for (int i = 0; i < 4; i++) { int x = aCells.get(aCells.size() - 1).x + dx[i]; int y = aCells.get(aCells.size() - 1).y + dy[i]; if (x >= 0 && x < rows && y >= 0 && y < cols && g[x][y] == 0) { @@ -72,4 +78,15 @@ public class Bot implements com.kob.botrunningsystem.utils.BotInterface { return 0; } + + @Override + public Integer get() { + File file = new File("input.txt"); + try { + Scanner sc = new Scanner(file); + return nextMove(sc.next()); // 读入字符串 + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } } diff --git a/backendcloud/input.txt b/backendcloud/input.txt new file mode 100644 index 0000000..97876c1 --- /dev/null +++ b/backendcloud/input.txt @@ -0,0 +1 @@ +11111111111111100000000000011100001001100110010010000101110000000000011100010000000110000000000001100000001000111000000000001110100001001001100110010000111000000000000111111111111111#1#12#(22222300303221221221)#11#1#(00000100000111111111)