parent
371e68cfdd
commit
1044fa0ff6
@ -1,22 +0,0 @@
|
|||||||
package com.kob.backend.controller.pk;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/pk/")
|
|
||||||
public class BotInfoController {
|
|
||||||
|
|
||||||
@RequestMapping("getbotinfo/")
|
|
||||||
public Map<String, String> getBotInfor()
|
|
||||||
{
|
|
||||||
Map<String, String> bot1 = new HashMap<>();
|
|
||||||
bot1.put("name", "apple");
|
|
||||||
bot1.put("rating", "1500");
|
|
||||||
return bot1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package com.kob.backend.controller.pk;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class IndexController {
|
|
||||||
|
|
||||||
@RequestMapping( "/")
|
|
||||||
public String index()
|
|
||||||
{
|
|
||||||
return "pk/index.html";
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.kob.backend.controller.pk;
|
||||||
|
|
||||||
|
import com.kob.backend.service.pk.ReceiveBotMoveService;
|
||||||
|
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 ReceiveBotMoveController {
|
||||||
|
@Autowired
|
||||||
|
private ReceiveBotMoveService receiveBotMoveService;
|
||||||
|
|
||||||
|
@PostMapping("/pk/receive/bot/move/")
|
||||||
|
public String receiveBotMove(@RequestParam MultiValueMap<String,String> data){
|
||||||
|
Integer userId = Integer.parseInt(Objects.requireNonNull(data.getFirst("user_id")));
|
||||||
|
Integer direction = Integer.parseInt(Objects.requireNonNull(data.getFirst("direction")));
|
||||||
|
return receiveBotMoveService.receiveBotMove(userId,direction);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.kob.backend.service.impl.pk;
|
||||||
|
|
||||||
|
import com.kob.backend.consumer.WebSocketServer;
|
||||||
|
import com.kob.backend.consumer.utils.Game;
|
||||||
|
import com.kob.backend.service.pk.ReceiveBotMoveService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ReceiveBotMoveServiceImpl implements ReceiveBotMoveService {
|
||||||
|
@Override
|
||||||
|
public String receiveBotMove(Integer userId, Integer direction) {
|
||||||
|
|
||||||
|
System.out.println("receive bot move: " + userId + " " +direction);
|
||||||
|
if(WebSocketServer.users.get(userId)!=null){
|
||||||
|
Game game = WebSocketServer.users.get(userId).game;
|
||||||
|
if(game != null){
|
||||||
|
if(game.getPlayerA().getId().equals(userId)){//当前链接是A用户
|
||||||
|
game.setNextStepA(direction);
|
||||||
|
} else if(game.getPlayerB().getId().equals(userId)){//当前链接是B用户
|
||||||
|
game.setNextStepB(direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "receive bot move success";
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.kob.backend.service.impl.bot;
|
package com.kob.backend.service.impl.user.bot;
|
||||||
|
|
||||||
import com.kob.backend.mapper.BotMapper;
|
import com.kob.backend.mapper.BotMapper;
|
||||||
import com.kob.backend.pojo.Bot;
|
import com.kob.backend.pojo.Bot;
|
@ -1,4 +1,4 @@
|
|||||||
package com.kob.backend.service.impl.bot;
|
package com.kob.backend.service.impl.user.bot;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.kob.backend.mapper.BotMapper;
|
import com.kob.backend.mapper.BotMapper;
|
||||||
import com.kob.backend.pojo.Bot;
|
import com.kob.backend.pojo.Bot;
|
@ -1,4 +1,4 @@
|
|||||||
package com.kob.backend.service.impl.bot;
|
package com.kob.backend.service.impl.user.bot;
|
||||||
|
|
||||||
import com.kob.backend.mapper.BotMapper;
|
import com.kob.backend.mapper.BotMapper;
|
||||||
import com.kob.backend.pojo.Bot;
|
import com.kob.backend.pojo.Bot;
|
@ -1,4 +1,4 @@
|
|||||||
package com.kob.backend.service.impl.bot;
|
package com.kob.backend.service.impl.user.bot;
|
||||||
|
|
||||||
import com.kob.backend.mapper.BotMapper;
|
import com.kob.backend.mapper.BotMapper;
|
||||||
import com.kob.backend.pojo.Bot;
|
import com.kob.backend.pojo.Bot;
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.kob.backend.service.pk;
|
||||||
|
|
||||||
|
public interface ReceiveBotMoveService {
|
||||||
|
public String receiveBotMove(Integer userId,Integer direction);
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package com.kob.backend.service.pk;
|
package com.kob.backend.service.pk;
|
||||||
|
|
||||||
public interface StartGameService {
|
public interface StartGameService {
|
||||||
public String startGame(Integer aId,Integer bId);
|
public String startGame(Integer aId,Integer aBotId,Integer bId,Integer bBotId);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>backendcloud</artifactId>
|
||||||
|
<groupId>com.kob</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>botrunningsystem</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.jooq/joor-java-8 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jooq</groupId>
|
||||||
|
<artifactId>joor-java-8</artifactId>
|
||||||
|
<version>0.9.14</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.24</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
<version>2.7.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.3</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
|||||||
|
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 {
|
||||||
|
@Bean
|
||||||
|
public RestTemplate getRestTemplate(){
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
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 {
|
||||||
|
@Override
|
||||||
|
protected 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();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
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 {
|
||||||
|
@Autowired
|
||||||
|
private 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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.kob.botrunningsystem.service;
|
||||||
|
|
||||||
|
public interface BotRunningService {
|
||||||
|
public String addBot(Integer userId,String botCode,String input);
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
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();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public 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";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.kob.botrunningsystem.service.impl.utils;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Bot {
|
||||||
|
private Integer userId;
|
||||||
|
private String botCode;
|
||||||
|
private String input;
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.kob.botrunningsystem.service.impl.utils;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
public class BotPool extends Thread{
|
||||||
|
|
||||||
|
private final ReentrantLock lock = new ReentrantLock();
|
||||||
|
private final Condition condition = lock.newCondition();//条件变量
|
||||||
|
private final Queue<Bot> bots = new LinkedList<>();//消息队列-->add,remove-->锁
|
||||||
|
|
||||||
|
//添加一个任务-Bot
|
||||||
|
public void addBot(Integer userId,String botCode,String input){
|
||||||
|
lock.lock();//涉及到bots
|
||||||
|
try {
|
||||||
|
bots.add(new Bot(userId,botCode,input));
|
||||||
|
condition.signalAll();
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
//消费一个任务
|
||||||
|
private void consume(Bot bot){
|
||||||
|
Consumer consumer = new Consumer();
|
||||||
|
consumer.startTimeout(2000,bot);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true){
|
||||||
|
lock.lock();
|
||||||
|
if(bots.isEmpty()){//空
|
||||||
|
try {
|
||||||
|
condition.await();//【消息队列空】阻塞当前线程,直到被唤醒(默认包含解锁)
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
lock.unlock();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Bot bot = bots.remove();//取出当前任务+移除
|
||||||
|
lock.unlock();
|
||||||
|
consume(bot);//消耗任务,用时长,执行代码
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
|
||||||
|
|
||||||
|
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 static RestTemplate restTemplate ;
|
||||||
|
@Autowired
|
||||||
|
public void setRestTemplate(RestTemplate restTemplate){
|
||||||
|
Consumer.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
private Bot bot;
|
||||||
|
private final static String receiveBotMoveUrl = "http://127.0.0.1:3000/pk/receive/bot/move/";
|
||||||
|
|
||||||
|
|
||||||
|
public void startTimeout(long timeout,Bot bot){
|
||||||
|
this.bot = bot;
|
||||||
|
this.start();
|
||||||
|
try {
|
||||||
|
this.join(timeout);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
this.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String addUid(String code,String uid){
|
||||||
|
int k = code.indexOf(" implements BotInterface");
|
||||||
|
return code.substring(0,k) + uid +code.substring(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.kob.botrunningsystem.utils;
|
||||||
|
|
||||||
|
public class Bot implements com.kob.botrunningsystem.utils.BotInterface {
|
||||||
|
@Override
|
||||||
|
public Integer nextMove(String input) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.kob.botrunningsystem.utils;
|
||||||
|
|
||||||
|
public interface BotInterface {
|
||||||
|
public Integer nextMove(String input);//下一步方向
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
server.port=3002
|
@ -1,6 +1,6 @@
|
|||||||
package com.kob.matchingsystem.service;
|
package com.kob.matchingsystem.service;
|
||||||
|
|
||||||
public interface MatchingService {
|
public interface MatchingService {
|
||||||
public String addPlayer(Integer userId,Integer rating);
|
public String addPlayer(Integer userId,Integer rating,Integer botId);
|
||||||
public String removePlayer(Integer userId);
|
public String removePlayer(Integer userId);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue