当前位置: 首页 > news >正文

微服务: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);}
}

终极流程

相关文章:

微服务:Bot代码执行

每次要多传一个bot_id 判网关的时候判127.0.0.1所以最好改localhost 创建SpringCloud的子项目 BotRunningSystem 在BotRunningSystem项目中添加依赖&#xff1a; joor-java-8 可动态编译Java代码 2. 修改前端&#xff0c;传入对Bot的选择操作 package com.kob.botrunningsy…...

Python 导入Excel三维坐标数据 生成三维曲面地形图(面) 3、线条平滑曲面但有条纹

环境和包: 环境 python:python-3.12.0-amd64包: matplotlib 3.8.2 pandas 2.1.4 openpyxl 3.1.2 scipy 1.12.0 代码: import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import griddata imp…...

Vue.js+SpringBoot开发数字化社区网格管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块三、开发背景四、系统展示五、核心源码5.1 查询企事业单位5.2 查询流动人口5.3 查询精准扶贫5.4 查询案件5.5 查询人口 六、免责说明 一、摘要 1.1 项目介绍 基于JAVAVueSpringBootMySQL的数字化社区网格管理系统&#xf…...

java SSM农产品订购网站系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计

一、源码特点 java SSM农产品订购网站系统是一套完善的web设计系统&#xff08;系统采用SSM框架进行设计开发&#xff0c;springspringMVCmybatis&#xff09;&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采…...

vsto快速在excel中查找某个字符串

是的&#xff0c;使用foreach循环遍历 Excel.Range 可能会较慢&#xff0c;特别是在大型数据集上。为了提高效率&#xff0c;你可以考虑使用 Value 属性一次性获取整个范围的值&#xff0c;然后在内存中搜索文本。这样可以减少与 Excel 之间的交互次数&#xff0c;提高性能。 …...

Unity类银河恶魔城学习记录10-1 10-2 P89,90 Character stats - Stat script源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释&#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码 【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili Stat.cs using System.Collections; using System.Collections.Generic; us…...

西门子TIA中配置Anybus PROFINET IO Slave 模块

1、所需产品 Siemens S7 PLC CPU 315-2 PN/DP 6ES7 315-2EH-0AB0 Siemens PLC 编程电缆 n.a. n.a. PC ,并安装Siemens PLC编程软件 TIA Portal V11 X-gateway Slave 接口的GSDML文件 根据网关的软件版本而定 Anybus Communicator GSD文件 GSDML-V1.0-HMS-ABCPRT-20050317.xl…...

在 Rust 中使用 Serde 处理json

在 Rust 中使用 Serde 处理json 在本文中&#xff0c;我们将讨论 Serde、如何在 Rust 应用程序中使用它以及一些更高级的提示和技巧。 什么是serde&#xff1f; Rust中的serde crate用于高效地序列化和反序列化多种格式的数据。它通过提供两个可以使用的traits来实现这一点&a…...

【数据库】数据库介绍

文章目录 一、数据库介绍二、SQL分类 一、数据库介绍 什么是数据库 存储数据用文件就可以了&#xff0c;为什么还要弄个数据库? 文件保存数据有以下几个缺点&#xff1a; 文件的安全性问题 文件不利于数据查询和管理 文件不利于存储海量数据 文件在程序中控制不方便 数据库存…...

python 第三方库(PyPinyin\shortuuid\json)

PyPinyin库 简介 PyPinyin库是一个支持中文转拼音输出的Python第三方库&#xff0c;它可以根据词组智能匹配最正确的拼音&#xff0c;并且支持多音字&#xff0c;简单的繁体, 注音&#xff0c;多种不同拼音/注音风格的转换。 安装 (framework-learn) C:\Users\zzg>pip …...

一文解读ISO26262安全标准:术语(二)

一文解读ISO26262安全标准&#xff1a;术语&#xff08;二&#xff09; 本文继续补充一些标准中的术语&#xff0c;方便后续文章内容的有效理解。 分支覆盖率 branch coverage 控制流分支覆盖的比率. 100%分支覆盖率意味着100%语句覆盖率&#xff0c;比如&#xff0c;一个if语句…...

【Datawhale学习笔记】从大模型到AgentScope

从大模型到AgentScope AgentScope是一款全新的Multi-Agent框架&#xff0c;专为应用开发者打造&#xff0c;旨在提供高易用、高可靠的编程体验&#xff01; 高易用&#xff1a;AgentScope支持纯Python编程&#xff0c;提供多种语法工具实现灵活的应用流程编排&#xff0c;内置…...

QWebEngineView添加自定义网址协议UrlScheme

QWebEngineView可以和js交互需要使用QWebChannel&#xff0c;如果不使用的话&#xff0c;js可以请求自定义网址协议&#xff0c;相当于请求服务器&#xff0c;但是不用Qt专门做服务器&#xff0c;不占用系统端口。 如果结合系统自定义URL注册&#xff0c;可以达到访问自定义UR…...

react中使用腾讯地图

腾讯文档 申请好对应key 配置限额 https://lbs.qq.com/service/webService/webServiceGuide/webServiceQuota 代码 用到的服务端接口 1.逆地址解析 2.关键词输入提示 import React, { Component } from react; import styles from ./map.less import { Form, Row, Col, I…...

deepin23beta中SQLite3数据库安装与使用

SQLite 是一个嵌入式 SQL 数据库引擎&#xff0c;它实现了一个自包含、无服务器、零配置、事务性 SQL 数据库引擎。 SQLite 的代码属于公共领域&#xff0c;因此可以免费用于任何商业或私人目的。 SQLite 是世界上部署最广泛的数据库&#xff0c;其应用程序数量之多&#xff0c…...

前后端分离项目环境搭建

1. 使用到的技术和工具 springboot vue项目的搭建 工具 idea&#xff0c;mavennodejs 2. 后端框架搭建 利用maven创建springboot项目 3. 前端项目搭建 1. 安装相关工具 nodejs&#xff1a; 一个开源、跨平台的 JavaScript 运行时环境&#xff0c;可以理解成java当中需要…...

HTML静态网页成品作业(HTML+CSS)——家乡漳州介绍设计制作(1个页面)

&#x1f389;不定期分享源码&#xff0c;关注不丢失哦 文章目录 一、作品介绍二、作品演示三、代码目录四、网站代码HTML部分代码 五、源码获取 一、作品介绍 &#x1f3f7;️本套采用HTMLCSS&#xff0c;未使用Javacsript代码&#xff0c;共有1个页面。 二、作品演示 三、代…...

世界第二对海信到底有多重要?

作者 | 辰纹 来源 | 洞见新研社 不久前&#xff0c;全球权威市场研究机构Omdia公布了2023年全球电视销量排名&#xff0c;数据显示TCL电视全球销量达到了2526万台&#xff0c;位居全球第二&#xff0c;中国第一。 可是&#xff0c;同样是根据Omdia的数据&#xff0c;海信的官…...

多站合一的音乐搜索下载助手PHP源码l亲测

源码获取方式 回复&#xff1a;031601 搭建教程&#xff1a; 将源码下载上传至宝塔面板&#xff0c;直接运行即可~ 说明&#xff1a; 该源码进行测试&#xff0c;测试成功源码无加密优化相关其他采集问题。...

webserver烂大街?还有必要做么?

目录 什么是 Web Server&#xff1f; 如何提供 HTTP 服务&#xff1f; HTTP协议 简介 工作原理 工作步骤 HTTP请求报文格式 HTTP响应报文格式 HTTP请求方法 HTTP状态码 ​总结 都说webserver是C选手人手必备的烂大街项目&#xff0c;那么webserver 还有必要做么&…...

3.Redis命令

Redis命令 Redis 根据命令所操作对象的不同&#xff0c; 可以分为三大类&#xff1a; 对 Redis 进行基础性操作的命令&#xff0c;对 Key 的操作命令&#xff0c;对 Value 的操作命令。 1.1 Redis 首先通过 redis-cli 命令进入到 Redis 命令行客户端&#xff0c;然后再运行下…...

xray问题排查,curl: (35) Encountered end of file(已解决)

经过了好几次排查&#xff0c;都没找到问题&#xff0c;先说问题的排查过程&#xff0c;多次确认了user信息&#xff0c;包括用户id和alterid&#xff0c;都没问题&#xff0c;头大的一逼 问题排查过程 确保本地的xray服务是正常的 [rootk8s-master01 xray]# systemctl stat…...

【数据库】Oracle内存结构与参数调优

Oracle内存结构与参数调优 Oracle 内存结构概览oracle参数配置概览重要参数&#xff08;系统运行前配置&#xff09;:次要参数&#xff08;可在系统运行后再优化调整&#xff09;: Oracle数据库服务器参数如何调整OLTP内存分配操作系统核心参数配置Disabling ASMM&#xff08;禁…...

PS学习-抠图-蒙版-冰块酒杯等透明物体

选中图&#xff0c;ctrlA 全选 ctrlC复制 创建一个蒙版图层 选中蒙版Alt 点击进入 ctrlv 复制 ctrli 反转 原图层 ctrldelete填充为白色 添加一个背景&#xff0c;这个方法通用 首选创建一个 拖到最底部 给它填充颜色 这个可能是我图片的原因。视频是这样做的...

绝赞春招拯救计划 -- 操作系统,组成原理,计网

进程和线程 进程 一个在内存中运行的应用程序。每个进程都有自己独立的一块内存空间&#xff0c;一个进程可以有多个线程 线程 进程中的一个执行任务&#xff08;控制单元&#xff09;&#xff0c;负责当前进程中程序的执行。一个进程至少有一个线程&#xff0c;一个进程可以…...

c语言:于龙加

于龙加 任务描述 于龙同学设计了一个特别的加法规则&#xff0c;加法被重新定义了&#xff0c;我们称为于龙加。 两个非负整数的于龙加的意义是将两个整数按前后顺序连接合并形成一个新整数。 于龙想编程解决于龙加问题&#xff0c;可是对下面的程序他没有思路&#xff01; …...

AcWing 790:数的三次方根 ← 浮点数二分

【题目来源】https://www.acwing.com/problem/content/792/【题目描述】 给定一个浮点数 n&#xff0c;求它的三次方根。【输入格式】 共一行&#xff0c;包含一个浮点数 n。【输出格式】 共一行&#xff0c;包含一个浮点数&#xff0c;表示问题的解。 注意&#xff0c;结果保留…...

【LLM】LLama2模型(RMSNorm、SwiGLU、RoPE位置编码)

note 预训练语言模型除了自回归&#xff08;Autoregressive&#xff09;模型GPT&#xff0c;还有自编码模型&#xff08;Autoencoding&#xff09;BERT[1]、编-解码&#xff08;Encoder-Decoder&#xff09;模型BART[67]&#xff0c;以及融合上述三种方法的自回归填空&#xf…...

【力扣白嫖日记】1934.确认率

前言 练习sql语句&#xff0c;所有题目来自于力扣&#xff08;https://leetcode.cn/problemset/database/&#xff09;的免费数据库练习题。 今日题目&#xff1a; 1934.确认率 表&#xff1a;Signups 列名类型user_idinttime_stampdatetime User_id是该表的主键。每一行都…...

TinTin Web3 动态精选:以太坊坎昆升级利好 Layer2,比特币减半进入倒计时

TinTin 快讯由 TinTinLand 开发者技术社区打造&#xff0c;旨在为开发者提供最新的 Web3 新闻、市场时讯和技术更新。TinTin 快讯将以周为单位&#xff0c; 汇集当周内的行业热点并以快讯的形式排列成文。掌握一手的技术资讯和市场动态&#xff0c;将有助于 TinTinLand 社区的开…...