微服务:Bot代码执行

每次要多传一个bot_id 

判网关的时候判127.0.0.1所以最好改localhost

创建SpringCloud的子项目 BotRunningSystem
在BotRunningSystem项目中添加依赖:
joor-java-8

可动态编译Java代码
2. 修改前端,传入对Bot的选择操作

package com.kob.botrunningsystem.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}
package com.kob.botrunningsystem.config;import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/bot/add/").hasIpAddress("127.0.0.1").antMatchers(HttpMethod.OPTIONS).permitAll().anyRequest().authenticated();}
}
package com.kob.botrunningsystem.controller;import com.kob.botrunningsystem.service.BotRunningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Objects;@RestController
public class BotRunningController {@Autowiredprivate BotRunningService botRunningService;@PostMapping("/bot/add/")public String addBot(@RequestParam MultiValueMap<String, String> data) {Integer userId = Integer.parseInt(Objects.requireNonNull(data.getFirst("user_id")));String botCode = data.getFirst("bot_code");String input = data.getFirst("input");return botRunningService.addBot(userId, botCode, input);}
}
package com.kob.botrunningsystem.service.impl.utils;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Bot {Integer userId;String botCode;String input;
}

        

 

package com.kob.botrunningsystem.service.impl.utils;import com.kob.botrunningsystem.utils.BotInterface;
import org.joor.Reflect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;import java.util.UUID;@Component
public class Consumer extends Thread {private Bot bot;private static RestTemplate restTemplate;private final static String receiveBotMoveUrl = "http://127.0.0.1:8080/pk/receive/bot/move/";@Autowiredpublic void setRestTemplate(RestTemplate restTemplate) {Consumer.restTemplate = restTemplate;}public void startTimeout(long timeout, Bot bot) {this.bot = bot;this.start();try {this.join(timeout);  // 最多等待timeout秒} catch (InterruptedException e) {e.printStackTrace();} finally {this.interrupt();  // 终端当前线程}}private String addUid(String code, String uid) {  // 在code中的Bot类名后添加uidint k = code.indexOf(" implements com.kob.botrunningsystem.utils.BotInterface");return code.substring(0, k) + uid + code.substring(k);}@Overridepublic void run() {UUID uuid = UUID.randomUUID();String uid = uuid.toString().substring(0, 8);BotInterface botInterface = Reflect.compile("com.kob.botrunningsystem.utils.Bot" + uid,addUid(bot.getBotCode(), uid)).create().get();Integer direction = botInterface.nextMove(bot.getInput());System.out.println("move-direction: " + bot.getUserId() + " " + direction);MultiValueMap<String, String> data = new LinkedMultiValueMap<>();data.add("user_id", bot.getUserId().toString());data.add("direction", direction.toString());restTemplate.postForObject(receiveBotMoveUrl, data, String.class);}
}
package com.kob.botrunningsystem.service.impl;import com.kob.botrunningsystem.service.BotRunningService;
import com.kob.botrunningsystem.service.impl.utils.BotPool;
import org.springframework.stereotype.Service;@Service
public class BotRunningServiceImpl implements BotRunningService {public final static BotPool botPool = new BotPool();@Overridepublic String addBot(Integer userId, String botCode, String input) {System.out.println("add bot: " + userId + " " + botCode + " " + input);botPool.addBot(userId, botCode, input);return "add bot success";}
}

 

package com.kob.botrunningsystem.service;public interface BotRunningService {String addBot(Integer userId, String botCode, String input);
}

 

package com.kob.botrunningsystem.utils;import java.util.ArrayList;
import java.util.List;public class Bot implements com.kob.botrunningsystem.utils.BotInterface {static class Cell {public int x, y;public Cell(int x, int y) {this.x = x;this.y = y;}}private boolean check_tail_increasing(int step) {  // 检验当前回合,蛇的长度是否增加if (step <= 10) return true;return step % 3 == 1;}public List<Cell> getCells(int sx, int sy, String steps) {steps = steps.substring(1, steps.length() - 1);List<Cell> res = new ArrayList<>();int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};int x = sx, y = sy;int step = 0;res.add(new Cell(x, y));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)) {res.remove(0);}}return res;}@Overridepublic Integer nextMove(String input) {String[] strs = input.split("#");int[][] g = new int[15][16];for (int i = 0, k = 0; i < 15; i ++ ) {for (int j = 0; j < 16; j ++, k ++ ) {if (strs[0].charAt(k) == '1') {g[i][j] = 1;}}}int aSx = Integer.parseInt(strs[1]), aSy = Integer.parseInt(strs[2]);int bSx = Integer.parseInt(strs[4]), bSy = Integer.parseInt(strs[5]);List<Cell> aCells = getCells(aSx, aSy, strs[3]);List<Cell> 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;int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};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 < 15 && y >= 0 && y < 16 && g[x][y] == 0) {return i;}}return 0;}
}

 

package com.kob.botrunningsystem.utils;public interface BotInterface {Integer nextMove(String input);
}

 

package com.kob.botrunningsystem;import com.kob.botrunningsystem.service.impl.BotRunningServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class BotRunningSystemApplication {public static void main(String[] args) {BotRunningServiceImpl.botPool.start();SpringApplication.run(BotRunningSystemApplication.class, args);}
}

终极流程

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2868812.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

STM32定时器预分频系数和自动重装载系数

现以一个图开始&#xff1a; 预分频器和计数器最大值都为65535&#xff08;从0开始&#xff09; 预分配器&#xff1a;比如输入的是72MHZ的频率&#xff0c;&#xff08;预分频系数为0&#xff09;不分频的话就是一秒数72000000次&#xff0c;如果预分频系数为&#xff08;72…

基于springboot实现小区物业管理系统项目【项目源码+论文说明】

基于springboot实现小区物业管理系统演示 摘要 随着城镇人口居住的集中化加剧 &#xff0c;传统人工小区管理模式逐渐跟不上时代的潮流。这就要求我们提供一个专门的管理系统。来提高物管的工作效率、为住户提供更好的服务。 物业管理系统运用现代化的计算机管理手段,使物业的…

FPGA和ASIC

前言 大家好&#xff0c;我是jiantaoyab&#xff0c;这是我所总结作为学习的笔记第16篇,在本篇文章给大家介绍FPGA和ASIC。 一个四核i7的CPU的晶体管中有20亿的晶体管&#xff0c;需要链接起20亿的晶体管可不是一件容易的事情&#xff0c;所以设计一个CPU需要用年来算&#x…

MySQL:SQL优化

1. 插入优化 使用insert语句单条单条数据插入效率偏低&#xff0c;建议使用insert批量插入数据&#xff0c;批量控制在500-1000条数据较为合适&#xff0c;当面对数以百万的数据时&#xff0c;可以使用load指令&#xff0c;提升插入数据效率 相关指令 #客户端连接服务端加上参…

Java-PriorityQueue源码分析

PriorityQueue 源码分析 Java中的PriorityQueue采用的是堆这种数据结构来实现的,而存储堆采用的则是数组。 堆是一个完全二叉树,堆中每一个节点的值都必须大于等于(或小于等于)其子树中每个节点的值,对于每个节点的值都大于等于子树中每个节点值的堆&#xff0c;我们叫做大顶…

一学就会 | ChatGPT提示词-[简历指令库]-有爱AI实战教程(八)

演示站点&#xff1a; https://ai.uaai.cn 对话模块 官方论坛&#xff1a; www.jingyuai.com 京娱AI 一、导读&#xff1a; 在使用 ChatGPT 时&#xff0c;当你给的指令越精确&#xff0c;它的回答会越到位&#xff0c;举例来说&#xff0c;假如你要请它帮忙写文案&#xf…

SpringBoot打造企业级进销存储系统 第五讲

package com.java1234.repository;import com.java1234.entity.Menu; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query;import java.util.List;/*** 菜单Repository接口*/ public interface MenuReposit…

ISIS接口认证实验简述

默认情况下&#xff0c;ISIS接口认证通过在ISIS协议数据单元&#xff08;PDU&#xff09;中添加认证字段&#xff0c;例如&#xff1a;一个密钥或密码&#xff0c;用于验证发送方的身份。 ISIS接口认证防止未经授权的设备加入到网络中&#xff0c;并确保邻居之间的通信是可信的…

无限自动出兵-入门版【war3地图编辑器】

文章目录 1、创建单位和地区2、新事件开端3、动作3.1、创建单位3.2、选取单位3.2.1、发布指令 4、最终 1、创建单位和地区 2、新事件开端 创建新的触发器→新事件开端→时间→时间周期事件 3、动作 3.1、创建单位 3.2、选取单位 单位组→选取单位组内单位做动作 矩形区域内的…

数据结构:基于数组实现简单的数据缓存区(简单队列)

1 前言 在我们使用CAN或者以太网调试时&#xff0c;经常需要缓存最近n次收到的数据&#xff0c;以便于我们对数据进行分析。 实现这一想法我们很容易就会想到队列&#xff0c;队列就是一种先进先出的数据结构&#xff0c;之前在《数据结构&#xff1a;基于数组的环形队列&…

EtherCAT 开源主站 IGH 在 linux 开发板的移植和伺服通信测试

手边有一套正点原子linux开发板imax6ul&#xff0c;一直在吃灰&#xff0c;周末业余时间无聊&#xff0c;把EtherCAT的开源IGH主站移植到开发板上玩玩儿&#xff0c;搞点事情做。顺便学习研究下EtherCAT总线协议及其对伺服驱动器的运动控制过程。实验很有意思&#xff0c;这里总…

2核4G云服务器并发能支持多少用户在线?

腾讯云轻量2核4G5M带宽服务器支持多少人在线访问&#xff1f;5M带宽下载速度峰值可达640KB/秒&#xff0c;阿腾云以搭建网站为例&#xff0c;假设优化后平均大小为60KB&#xff0c;则5M带宽可支撑10个用户同时在1秒内打开网站&#xff0c;并发数为10&#xff0c;经阿腾云测试&a…

学点Java打小工_Day4_数组_冒泡排序

1 数组基本概念 程序算法数据结构 算法&#xff1a;解决程序的流程步骤 数据结构&#xff1a;将数据按照某种特定的结构来存储 设计良好的数据结构会导致良好的算法。 ArrayList、LinkedList 数组是最简单的数据结构。 数组&#xff1a;存放同一种类型数据的集合&#xff0c;在…

桌面待办,电脑桌面怎么设置待办事项

在忙碌的工作生活中&#xff0c;我们经常会有许多事情需要处理&#xff0c;为了提高工作效率和管理时间&#xff0c;很多人都有一套自己的桌面待办事项管理方法。那么&#xff0c;如何利用电脑桌面待办事项来提高工作效率&#xff0c;电脑桌面怎么设置待办事项呢&#xff1f; …

【Poi-tl Documentation】自定义占位符来设置图片大小

前置说明&#xff1a; <dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version> </dependency>模板文件&#xff1a; image_test.docx package run.siyuan.poi.tl.policy;imp…

Internet协议的安全性

Internet协议的安全性 文章目录 Internet协议的安全性1. 网络层1. IP*62. ARP*33. ICMP * 3 2. 传输层协议1. TCP1. * SYN-Flood攻击攻击检测* 防御 2. TCP序号攻击攻击 3. 拥塞机制攻击 2. UDP 3. 应用层协议1. DNS攻击*3防范*3: 2. FTP3. TELNET: 改用ssh4. 电子邮件1. 攻击2…

set与zset数据类型

set类型基础 redis集合(set)类型和list列表类型类似&#xff0c;都可以用来存储多个字符串元素的 集合。但是和list不同的是set集合当中不允许重复的元素。而且set集合当中元素是没有顺序的&#xff0c;不存在元素下标。 redis的set类型是使用哈希表构造的&#xff0c;因此复…

每日OJ题_简单多问题dp⑥_力扣714. 买卖股票的最佳时机含手续费

目录 力扣714. 买卖股票的最佳时机含手续费 状态机分析 解析代码 力扣714. 买卖股票的最佳时机含手续费 714. 买卖股票的最佳时机含手续费 难度 中等 给定一个整数数组 prices&#xff0c;其中 prices[i]表示第 i 天的股票价格 &#xff1b;整数 fee 代表了交易股票的手续…

Linux远程连接本地数据库(docker)

1. 安装docker 参考上一篇文章 CentOS安装Docker 2. Linux中安装Mysql 2.1 docker拉取mysql镜像 拉取镜像 docker pull mysql查看镜像列表 docker images2.2 运行mysql容器 运行一个名字为mysql的mysql容器&#xff0c;其连接端口号为3306&#xff0c;密码为123456 docker r…

口腔管理平台 |基于springboot框架+ Mysql+Java+B/S结构的口腔管理平台 设计与实现(可运行源码+数据库+lw文档)

推荐阅读100套最新项目 最新ssmjava项目文档视频演示可运行源码分享 最新jspjava项目文档视频演示可运行源码分享 最新Spring Boot项目文档视频演示可运行源码分享 目录 前台功能效果图 管理员功能登录前台功能效果图 会员功能 系统功能设计 数据库E-R图设计 lunwen参考…