parent
6c1744c37e
commit
521ba31fa8
@ -0,0 +1,15 @@
|
|||||||
|
package com.kob.backend.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
|
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
package com.kob.backend.consumer.utils;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
|
||||||
|
final private Integer rows;
|
||||||
|
final private Integer cols;
|
||||||
|
final private Integer inner_walls_count;
|
||||||
|
final private int[][] g;
|
||||||
|
final private static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
|
||||||
|
|
||||||
|
//构造map规模
|
||||||
|
public Game(Integer rows, Integer cols, Integer inner_walls_count) {
|
||||||
|
this.rows = rows;
|
||||||
|
this.cols = cols;
|
||||||
|
this.inner_walls_count = inner_walls_count;
|
||||||
|
this.g = new int[rows][cols];
|
||||||
|
}
|
||||||
|
|
||||||
|
//货物gamemap
|
||||||
|
public int[][] getG()
|
||||||
|
{
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
//地图是否连通
|
||||||
|
private boolean check_connectivity(int sx, int sy, int tx, int ty)
|
||||||
|
{
|
||||||
|
if (sx == tx && sy == ty) return true;
|
||||||
|
g[sx][sy] = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i ++ ) {
|
||||||
|
int x = sx + dx[i], y = sy + dy[i];
|
||||||
|
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols && g[x][y] == 0)
|
||||||
|
{
|
||||||
|
if (check_connectivity(x, y, tx, ty))
|
||||||
|
{
|
||||||
|
g[sx][sy] = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g[sx][sy] = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean draw() // 画地图
|
||||||
|
{
|
||||||
|
for (int i = 0; i < this.rows; i ++ )
|
||||||
|
{
|
||||||
|
for (int j = 0; j < this.cols; j ++ )
|
||||||
|
{
|
||||||
|
g[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//边
|
||||||
|
for (int r = 0; r < this.rows; r ++ ) {
|
||||||
|
g[r][0] = g[r][this.cols - 1] = 1;
|
||||||
|
}
|
||||||
|
//边
|
||||||
|
for (int c = 0; c < this.cols; c ++ ) {
|
||||||
|
g[0][c] = g[this.rows - 1][c] = 1;
|
||||||
|
}
|
||||||
|
//随机画
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i = 0; i < this.inner_walls_count / 2; i ++ )
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 1000; j ++ )
|
||||||
|
{
|
||||||
|
int r = random.nextInt(this.rows);
|
||||||
|
int c = random.nextInt(this.cols);
|
||||||
|
|
||||||
|
if (g[r][c] == 1 || g[this.rows - 1 - r][this.cols - 1 - c] == 1)
|
||||||
|
continue;
|
||||||
|
if (r == this.rows - 2 && c == 1 || r == 1 && c == this.cols - 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
g[r][c] = g[this.rows - 1 - r][this.cols - 1 - c] = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return check_connectivity(this.rows - 2, 1, 1, this.cols - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void createMap()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 1000; i ++ )
|
||||||
|
{
|
||||||
|
if (draw()) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.kob.backend.consumer.utils;
|
||||||
|
|
||||||
|
import com.kob.backend.utils.JwtUtil;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
|
||||||
|
public class JwtAuthentication {
|
||||||
|
//根据token返回userId
|
||||||
|
public static Integer getUserId(String token)//不用实例访问
|
||||||
|
{
|
||||||
|
int userid = -1;
|
||||||
|
try {
|
||||||
|
Claims claims = JwtUtil.parseJWT(token);
|
||||||
|
userid = Integer.parseInt(claims.getSubject());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return userid;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
<template>
|
||||||
|
<div class="matchground">
|
||||||
|
<div class="row">
|
||||||
|
|
||||||
|
<!-- 自己 -->
|
||||||
|
<div class="col-6">
|
||||||
|
<div class="user-photo">
|
||||||
|
<img :src="$store.state.user.photo" alt="">
|
||||||
|
</div>
|
||||||
|
<div class="user-username">
|
||||||
|
{{$store.state.user.username}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 对手 -->
|
||||||
|
<div class="col-6">
|
||||||
|
<div class="user-photo">
|
||||||
|
<img :src="$store.state.pk.opponent_photo" alt="">
|
||||||
|
</div>
|
||||||
|
<div class="user-username">
|
||||||
|
{{$store.state.pk.opponent_username}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-12" style="text-align : center; padding-top : 12vh;">
|
||||||
|
<button type="button" class="btn btn-warning btn-lg" @click="click_match_btn">{{match_btn_info}}</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { ref } from "vue"
|
||||||
|
import { useStore } from "vuex"
|
||||||
|
export default {
|
||||||
|
setup(){
|
||||||
|
const store = useStore();
|
||||||
|
|
||||||
|
let match_btn_info = ref("开始匹配")
|
||||||
|
|
||||||
|
const click_match_btn = ( )=>{
|
||||||
|
if(match_btn_info.value === "开始匹配"){
|
||||||
|
match_btn_info.value = "取消";
|
||||||
|
|
||||||
|
//向后端发送信息
|
||||||
|
store.state.pk.socket.send(JSON.stringify({
|
||||||
|
event:"start-matching",
|
||||||
|
}));
|
||||||
|
} else if(match_btn_info.value === "取消"){
|
||||||
|
match_btn_info.value = "开始匹配";
|
||||||
|
|
||||||
|
store.state.pk.socket.send(JSON.stringify({
|
||||||
|
event:"stop-matching",
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
match_btn_info,
|
||||||
|
click_match_btn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
|
||||||
|
div.matchground {
|
||||||
|
width: 60vw;
|
||||||
|
height: 70vh;
|
||||||
|
background-color:rgba(50 ,50 ,50 ,0.5);
|
||||||
|
margin: 40px auto;
|
||||||
|
}
|
||||||
|
div.user-photo {
|
||||||
|
text-align: center;
|
||||||
|
padding-top: 10vh;
|
||||||
|
}
|
||||||
|
div.user-photo > img{
|
||||||
|
border-radius: 50%;
|
||||||
|
width: 20vh;
|
||||||
|
}
|
||||||
|
div.user-username {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: white;
|
||||||
|
margin-top: 2vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</style>
|
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="talk">
|
||||||
|
<input v-model="username" type="text" class="left" placeholder="请输入用户名">
|
||||||
|
<button type="button" class="btn btn-warning" @click="sendMessage">发送</button>
|
||||||
|
<div>{{$store.state.pk.message}}</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
div.talk {
|
||||||
|
width: 30vw;
|
||||||
|
height: 10vh;
|
||||||
|
background-color: lightblue;
|
||||||
|
/* display: flex; */
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.left {
|
||||||
|
display: inline;
|
||||||
|
width: 370px;
|
||||||
|
height: 28px;
|
||||||
|
}
|
||||||
|
</style>
|
@ -0,0 +1,32 @@
|
|||||||
|
// import $ from "jquery"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
state: {
|
||||||
|
status:"matching",//matching匹配页面 playing对战页面
|
||||||
|
socket:null,
|
||||||
|
opponent_username:"",
|
||||||
|
opponent_photo:"",
|
||||||
|
gamemap:null,
|
||||||
|
},
|
||||||
|
getters: {
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
updateSocket(state,socket){
|
||||||
|
state.socket = socket;
|
||||||
|
},
|
||||||
|
updateOpponent(state,opponent){
|
||||||
|
state.opponent_username = opponent.username;
|
||||||
|
state.opponent_photo = opponent.photo;
|
||||||
|
},
|
||||||
|
updateStatus(state,status){
|
||||||
|
state.status = status;
|
||||||
|
},
|
||||||
|
updateGamemap(state,gamemap){
|
||||||
|
state.gamemap = gamemap ;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
},
|
||||||
|
modules: {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue