inital commit
This commit is contained in:
@@ -7,6 +7,7 @@ import dotenv from 'dotenv';
|
||||
import { v1 } from 'uuid';
|
||||
|
||||
import { IJankenSelect, IJankenResult } from './models/message';
|
||||
import { IRoom } from './models';
|
||||
|
||||
dotenv.config();
|
||||
const port: number = 8080;
|
||||
@@ -157,13 +158,9 @@ httpServer.listen(port, () => {
|
||||
|
||||
// Janken Tool
|
||||
|
||||
let room: boolean = false;
|
||||
let playerOne: string = '';
|
||||
let playerTwo: string = '';
|
||||
let playerOneOption: string = '';
|
||||
let playerTwoOption: string = '';
|
||||
let stages: string[] = [];
|
||||
let currentIndex: number = 0;
|
||||
let rooms: Record<string, IRoom> = {};
|
||||
const defaultStages: string[] = ['cementery', 'prison', 'torture', 'inquisition', 'quadrangle', 'clock', 'spiral', 'empireo', 'cathedral', 'close', 'arrange'];
|
||||
|
||||
io.on('connection', (socket: socketIo.Socket) => {
|
||||
@@ -174,45 +171,74 @@ io.on('connection', (socket: socketIo.Socket) => {
|
||||
socket.on('exitGame', () => {
|
||||
console.log('exitGame');
|
||||
io.emit('exitGame');
|
||||
room = false;
|
||||
initRoom();
|
||||
rooms = {};
|
||||
Object.keys(io.sockets.sockets).forEach((s: string) => {
|
||||
io.sockets.sockets[s].disconnect(true);
|
||||
});
|
||||
});
|
||||
socket.on('janken', (data: IJankenSelect) => {
|
||||
console.log('janken', data);
|
||||
if (playerOne === data.userName) {
|
||||
playerOneOption = data.option;
|
||||
} else if (playerTwo === data.userName) {
|
||||
playerTwoOption = data.option;
|
||||
}
|
||||
console.log('playerOneOption', playerOneOption);
|
||||
console.log('playerTwoOption', playerTwoOption);
|
||||
if (playerOneOption !== '' && playerTwoOption !== '') {
|
||||
const message: IJankenResult = {
|
||||
playerOne,
|
||||
playerOneOption,
|
||||
playerTwo,
|
||||
playerTwoOption
|
||||
};
|
||||
io.emit('jankenResult', message);
|
||||
const currentRoom = rooms[data.room];
|
||||
if (currentRoom) {
|
||||
if (currentRoom.playerOne === data.userName) {
|
||||
currentRoom.playerOneOption = data.option;
|
||||
} else if (currentRoom.playerTwo === data.userName) {
|
||||
currentRoom.playerTwoOption = data.option;
|
||||
}
|
||||
console.log('playerOneOption', currentRoom.playerOneOption);
|
||||
console.log('playerTwoOption', currentRoom.playerTwoOption);
|
||||
if (currentRoom.playerOneOption !== '' && currentRoom.playerTwoOption !== '') {
|
||||
const message: IJankenResult = {
|
||||
playerOne: currentRoom.playerOne,
|
||||
playerOneOption: currentRoom.playerOneOption,
|
||||
playerTwo: currentRoom.playerTwo,
|
||||
playerTwoOption: currentRoom.playerTwoOption
|
||||
};
|
||||
io.to(data.room).emit('jankenResult', message);
|
||||
const sqlToPrepare = 'INSERT INTO `janken` (`match`, `result`) VALUES(?, ?);';
|
||||
const sql = mysql.format(sqlToPrepare, [data.room, JSON.stringify(message)]);
|
||||
connection.query(sql,
|
||||
(error: mysql.MysqlError | null, results: any) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
);
|
||||
if (currentRoom.playerOneOption !== currentRoom.playerTwoOption) {
|
||||
delete rooms[data.room];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
socket.on('registerUser', (data: string) => {
|
||||
socket.on('registerUser', (data: {username: string, room: string}) => {
|
||||
console.log('registerUser', data);
|
||||
if (playerOne === '') {
|
||||
playerOne = data;
|
||||
socket.emit('registerUser', data);
|
||||
} else if (playerTwo === '') {
|
||||
playerTwo = data;
|
||||
socket.emit('registerUser', data);
|
||||
let currentRoom: IRoom = {
|
||||
playerOne: '',
|
||||
playerTwo: '',
|
||||
playerOneOption: '',
|
||||
playerTwoOption: ''
|
||||
};
|
||||
if (rooms[data.room]) {
|
||||
currentRoom = rooms[data.room];
|
||||
} else {
|
||||
rooms = {
|
||||
...rooms,
|
||||
[data.room]: currentRoom
|
||||
};
|
||||
}
|
||||
socket.join(data.room);
|
||||
if (currentRoom.playerOne === '') {
|
||||
currentRoom.playerOne = data.username;
|
||||
socket.emit('registerUser', data.username);
|
||||
} else if (currentRoom.playerTwo === '') {
|
||||
currentRoom.playerTwo = data.username;
|
||||
socket.emit('registerUser', data.username);
|
||||
}
|
||||
const message = {
|
||||
playerOne,
|
||||
playerTwo
|
||||
playerOne: currentRoom.playerOne,
|
||||
playerTwo: currentRoom.playerTwo
|
||||
};
|
||||
io.emit('login', message);
|
||||
io.to(data.room).emit('login', message);
|
||||
});
|
||||
socket.on('moveToStage', () => {
|
||||
console.log('moveToStage');
|
||||
@@ -227,11 +253,14 @@ io.on('connection', (socket: socketIo.Socket) => {
|
||||
io.emit('updateIndex', currentIndex);
|
||||
}
|
||||
});
|
||||
socket.on('againGame', () => {
|
||||
console.log('againGame');
|
||||
io.emit('againGame');
|
||||
playerOneOption = '';
|
||||
playerTwoOption = '';
|
||||
socket.on('againGame', (room: string) => {
|
||||
const currentRoom = rooms[room];
|
||||
if (currentRoom) {
|
||||
console.log('againGame');
|
||||
io.to(room).emit('againGame');
|
||||
currentRoom.playerOneOption = '';
|
||||
currentRoom.playerTwoOption = '';
|
||||
}
|
||||
});
|
||||
socket.on('previousIndex', () => {
|
||||
console.log('previousIndex');
|
||||
@@ -240,6 +269,7 @@ io.on('connection', (socket: socketIo.Socket) => {
|
||||
io.emit('updateIndex', currentIndex);
|
||||
}
|
||||
});
|
||||
/*
|
||||
if (!room) {
|
||||
// create new room
|
||||
initRoom();
|
||||
@@ -264,15 +294,14 @@ io.on('connection', (socket: socketIo.Socket) => {
|
||||
socket.emit('stages', message);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
const initialmessage = {
|
||||
playerOne: '',
|
||||
playerTwo: '',
|
||||
};
|
||||
socket.emit('login', initialmessage);
|
||||
});
|
||||
const shuffle = (array: string[]): string[] => {
|
||||
return array.sort(() => Math.random() - 0.5);
|
||||
};
|
||||
|
||||
const initRoom = (): void => {
|
||||
playerOne = '';
|
||||
playerTwo = '';
|
||||
playerOneOption = '';
|
||||
playerTwoOption = '';
|
||||
};
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface IRoom {
|
||||
playerOne: string;
|
||||
playerTwo: string;
|
||||
playerOneOption: string;
|
||||
playerTwoOption: string;
|
||||
}
|
||||
|
||||
@@ -16,5 +16,6 @@ export interface ILogin {
|
||||
|
||||
export interface IJankenSelect {
|
||||
userName: string;
|
||||
room: string;
|
||||
option: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user