2020-01-13 06:01:31 +08:00
|
|
|
const express = require("express");
|
|
|
|
const cors = require("cors");
|
2020-01-13 15:55:10 +08:00
|
|
|
const fs = require("fs");
|
2020-01-13 06:01:31 +08:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const port = process.env.PORT || 10080;
|
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
const server = app.listen(port, () => {
|
|
|
|
console.log(`Server is running on port: ${port}`);
|
2020-01-13 15:55:10 +08:00
|
|
|
});
|
2020-01-13 06:01:31 +08:00
|
|
|
|
|
|
|
const socket = require("socket.io");
|
|
|
|
const io = socket(server);
|
|
|
|
|
2020-01-13 15:55:10 +08:00
|
|
|
let testDoudizhuData = null;
|
|
|
|
|
2020-01-13 06:01:31 +08:00
|
|
|
io.on("connection", socket => {
|
2020-01-13 15:55:10 +08:00
|
|
|
console.log("successfully connected to rlcard showdown frontend");
|
|
|
|
socket.emit("getMessage", "successfully connected to rlcard showdown node server");
|
2020-01-13 06:01:31 +08:00
|
|
|
socket.on("getMessage", message => {
|
2020-01-14 15:18:36 +08:00
|
|
|
let res = null;
|
2020-01-13 15:55:10 +08:00
|
|
|
if(message){
|
|
|
|
switch(message.type){
|
|
|
|
case(0):
|
2020-01-14 15:18:36 +08:00
|
|
|
res = {
|
2020-01-13 15:55:10 +08:00
|
|
|
type: 0,
|
|
|
|
message: {
|
|
|
|
playerInfo: testDoudizhuData.playerInfo,
|
2020-01-15 03:19:43 +08:00
|
|
|
initHands: testDoudizhuData.initHands
|
2020-01-13 15:55:10 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
socket.emit("getMessage", res);
|
|
|
|
break;
|
2020-01-14 15:18:36 +08:00
|
|
|
case(1):
|
|
|
|
console.log(message);
|
|
|
|
if(message.message.turn >= testDoudizhuData.moveHistory.length){
|
|
|
|
// todo: process end of game
|
|
|
|
}else{
|
|
|
|
res = {
|
|
|
|
type: 1,
|
|
|
|
message: {
|
|
|
|
turn: message.message.turn,
|
|
|
|
playerIdx: testDoudizhuData.moveHistory[message.message.turn].playerIdx,
|
|
|
|
move: testDoudizhuData.moveHistory[message.message.turn].move
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
socket.emit("getMessage", res);
|
|
|
|
break;
|
2020-01-13 15:55:10 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 06:01:31 +08:00
|
|
|
})
|
2020-01-13 15:55:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
function getGameHistory(){
|
|
|
|
fs.readFile("./sample_data/sample_doudizhu.json", (err, data) => {
|
|
|
|
if (err) throw err;
|
|
|
|
testDoudizhuData = JSON.parse(data);
|
|
|
|
console.log(testDoudizhuData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getGameHistory();
|