generated from cepdnaclk/eYY-3yp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from udayaKavinda/main
ux updated
- Loading branch information
Showing
20 changed files
with
655 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,85 @@ | ||
console.log("udaya") | ||
const express = require("express"); | ||
const app = express(); | ||
const http = require("http"); | ||
const { mongoose } = require("mongoose"); | ||
const { Socket } = require("socket.io"); | ||
const Room = require("./models/room"); | ||
|
||
const port=process.env.PORT || 3000; | ||
var server =http.createServer(app); | ||
|
||
var io=require("socket.io")(server); | ||
|
||
app.use(express.json()); | ||
|
||
// database | ||
const DB="mongodb+srv://namal:[email protected]/?retryWrites=true&w=majority"; | ||
mongoose.connect(DB).then(()=>{ | ||
console.log("DB connection established"); | ||
}).catch(err => console.log(err) ); | ||
//database | ||
|
||
io.on("connection",(socket)=>{ | ||
try{ | ||
console.log("IO connection established"); | ||
socket.on("createOrJoinRoom",async ()=>{ | ||
let room =await Room.findOne({isJoin:true}); | ||
if(room){ | ||
const roomId=room._id.toString(); | ||
room.isJoin = false; | ||
let player = { | ||
socketID:socket.id, | ||
playerType:"Black", | ||
} | ||
room.players.push(player); | ||
room = await room.save(); | ||
socket.join(roomId); | ||
const roomSockets = io.sockets.adapter.rooms.get(roomId); | ||
const allSocketIds = Array.from(roomSockets); | ||
const otherSocketId = allSocketIds.find(socketId => socketId !== socket.id); | ||
io.to(otherSocketId).emit("joinRoomSuccess",room); | ||
room.isWhite=false; | ||
io.to(socket.id).emit("joinRoomSuccess",room); | ||
console.log(roomId); | ||
}else{ | ||
room =new Room(); | ||
const roomId=room._id.toString(); | ||
let player ={ | ||
socketID:socket.id, | ||
playerType:"White", | ||
} | ||
room.players.push(player); | ||
room.isWhite=true; | ||
room=await room.save(); | ||
socket.join(roomId); | ||
io.to(roomId).emit("createRoomSuccess",room); | ||
console.log(roomId); | ||
} | ||
}); | ||
}catch(e){ | ||
console.log(e); | ||
} | ||
|
||
|
||
socket.on('chessMove', (data) => { | ||
try{ | ||
console.log(data); | ||
socket.join(data.roomId); | ||
io.to(data.roomId).emit('chessMove', data); | ||
}catch(e){ | ||
console.log(e); | ||
} | ||
}); | ||
|
||
|
||
}); | ||
|
||
|
||
|
||
// app.get("/", function(req, res) { | ||
// res.send("no"+process.env.PORT); | ||
// }); | ||
|
||
server.listen(port, "0.0.0.0", () => { | ||
console.log("udaya " +port); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const playerSchema = new mongoose.Schema({ | ||
nickname: { | ||
type:String, | ||
trim:true, | ||
}, | ||
socketID: { | ||
type:String, | ||
}, | ||
points:{ | ||
type:Number, | ||
default:0, | ||
}, | ||
playerType: { | ||
required:true, | ||
type:String, | ||
} | ||
|
||
}); | ||
|
||
module.exports =playerSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const mongoose = require('mongoose'); | ||
const playerSchema = require('./players'); | ||
|
||
const roomSchema =new mongoose.Schema({ | ||
gameModeOnline:{ | ||
required:true, | ||
type:Boolean, | ||
default:true, | ||
}, | ||
players:[playerSchema], | ||
|
||
isJoin:{ | ||
type:Boolean, | ||
default:true, | ||
}, | ||
isWhite:{ | ||
type:Boolean, | ||
default:true, | ||
}, | ||
|
||
}); | ||
|
||
const roomModel = mongoose.model('Room',roomSchema); | ||
module.exports =roomModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Player { | ||
String nickname; | ||
String socketID; | ||
int points; | ||
String playerType; | ||
|
||
Player({ | ||
required this.nickname, | ||
required this.socketID, | ||
this.points = 0, | ||
required this.playerType, | ||
}); | ||
|
||
factory Player.fromJson(Map<String, dynamic> json) { | ||
return Player( | ||
nickname: json['nickname'] ?? "udaya", | ||
socketID: json['socketID'], | ||
points: json['points'] ?? 0, | ||
playerType: json['playerType'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:smartchessboard/models/player.dart'; | ||
|
||
class Room { | ||
bool gameModeOnline; | ||
List<Player>? players; | ||
bool? isJoin; | ||
bool isWhite; | ||
String roomId; | ||
|
||
Room({ | ||
required this.gameModeOnline, | ||
this.players, | ||
this.isJoin, | ||
this.isWhite = true, | ||
this.roomId = "", | ||
}); | ||
|
||
factory Room.fromJson(Map<String, dynamic> json) { | ||
return Room( | ||
roomId: json["_id"], | ||
gameModeOnline: json['gameModeOnline'], | ||
players: (json['players'] as List<dynamic>) | ||
.map((playerJson) => Player.fromJson(playerJson)) | ||
.toList(), | ||
isJoin: json['isJoin'], | ||
isWhite: json['isWhite'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class ShortMove { | ||
String from; | ||
String to; | ||
String nextPlayer; | ||
|
||
ShortMove({ | ||
required this.from, | ||
required this.to, | ||
this.nextPlayer = "white", | ||
}); | ||
|
||
factory ShortMove.fromJson(Map<String, dynamic> json) { | ||
return ShortMove( | ||
from: json['from'], | ||
to: json['to'], | ||
nextPlayer: json['nextPlayer'] ?? "white", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:smartchessboard/models/short_move.dart'; | ||
|
||
class MoveDataProvider extends ChangeNotifier { | ||
ShortMove _shortMoveData = ShortMove(from: "a1", to: "b1"); // | ||
|
||
ShortMove? get shortMoveData => _shortMoveData; | ||
|
||
void updateMoveData(Map<String, dynamic> data) { | ||
_shortMoveData = ShortMove.fromJson(data); | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:smartchessboard/models/room.dart'; | ||
|
||
class RoomDataProvider extends ChangeNotifier { | ||
Room? _roomData; // Assuming Room is a class representing your room structure | ||
|
||
Room? get roomData => _roomData; | ||
|
||
void updateRoomData(Map<String, dynamic> data) { | ||
_roomData = Room.fromJson(data); | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// ignore: library_prefixes | ||
import 'package:socket_io_client/socket_io_client.dart' as IO; | ||
|
||
class SocketClient { | ||
IO.Socket? socket; | ||
static SocketClient? _instance; | ||
|
||
SocketClient._internal() { | ||
socket = IO.io('http://192.168.1.100:3000', <String, dynamic>{ | ||
'transports': ['websocket'], | ||
'autoconnect': false, | ||
}); | ||
socket!.connect(); | ||
} | ||
static SocketClient get instance { | ||
_instance ??= SocketClient._internal(); | ||
return _instance!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:smartchessboard/models/short_move.dart'; | ||
import 'package:smartchessboard/provider/move_data_provider.dart'; | ||
import 'package:smartchessboard/provider/room_data_provider.dart'; | ||
import 'package:smartchessboard/resources/socket_client.dart'; | ||
import 'package:smartchessboard/screens/game_screen.dart'; | ||
import 'package:smartchessboard/utils/utils.dart'; | ||
|
||
class SocketMethods { | ||
final _socketClient = SocketClient.instance.socket!; | ||
|
||
void createOrJoinRoom() { | ||
_socketClient.emit("createOrJoinRoom", { | ||
'nickname': "online", | ||
}); | ||
} | ||
|
||
// void joinRoom(String nickname, String roomId) { | ||
// if (nickname.isNotEmpty && roomId.isNotEmpty) { | ||
// _socketClient.emit('joinRoom', { | ||
// 'nickname': nickname, | ||
// 'roomId': roomId, | ||
// }); | ||
// } | ||
// } | ||
|
||
void createRoomSuccessListener(BuildContext context) { | ||
_socketClient.on('createRoomSuccess', (room) { | ||
print("createRoomSuccess"); | ||
}); | ||
} | ||
|
||
void joinRoomSuccessListener(BuildContext context) { | ||
_socketClient.on('joinRoomSuccess', (room) { | ||
Provider.of<RoomDataProvider>(context, listen: false) | ||
.updateRoomData(room); | ||
print("joinRoomSuccess"); | ||
Navigator.pushNamed(context, GameScreen.routeName); | ||
}); | ||
} | ||
|
||
void errorOccuredListener(BuildContext context) { | ||
_socketClient.on('errorOccurred', (data) { | ||
showSnackBar(context, data); | ||
}); | ||
} | ||
|
||
void listenChessMoves(BuildContext context) { | ||
_socketClient.on('chessMove', (data) { | ||
Provider.of<MoveDataProvider>(context, listen: false) | ||
.updateMoveData(data); | ||
}); | ||
} | ||
|
||
void sendChessMove(String from, String to, String roomId, String nextPlayer) { | ||
_socketClient.emit('chessMove', { | ||
'roomId': roomId, | ||
'from': from, | ||
'to': to, | ||
'nextPlayer': nextPlayer, | ||
}); | ||
} | ||
|
||
void disposeChessMoveSockets() { | ||
_socketClient.off('chessMove'); | ||
} | ||
|
||
void disposeCrateJoinSockets() { | ||
_socketClient.off('createRoomSuccess'); | ||
_socketClient.off('joinRoomSuccess'); | ||
} | ||
} |
Oops, something went wrong.