A package you can use to create your own game platform using with an implemented match- and user-system with default games like TicTacToe socket.io
npm install socket.io-gamesjs
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const socketGames = require('socket.io-games');
socketGames.config.config(app, {
file: "./db.json",
usernameCharacterLength: {
minimum: 2, // default => 0 (no minimum)
maximum: 15 // default => 0 (no maximum)
}, // optional
pluginURL: "https://myPluginURL.com" // default => official plugin site
}).then(() => {
socketGames.config.clearDatabaseSync();
});
app.use(express.static("pages"));
app.use(express.static("public"));
io.on('connection', (socket, name) => {});
app.all('/', (req, res) => {
res.sendFile("pages/home/index.html");
});
http.listen(3000, () => {
console.log("Server is ready");
});
`
$3
#### Setup
`js
socketGames.config.config(app, {
file: "./db.json" // database
});
`
#### Read Database
`js
socketGames.config.readDatabase().then((result) => {
console.log(result.data);
});
`
#### Clear Database
`js
socketGames.config.clearDatabase().then((result) => {
console.log("Cleared the database");
});
`
$3
#### Create Match
`js
socket.on("createMatch", (options) => {
socketGames.matches.createMatch(socket.id, options).then((result) => {
if (!result.err) {
io.of("/").sockets.get(result.playerId).emit("newMatch", result);
}
});
});
`
#### Accept Match
`js
socket.on("acceptMatch", (options) => {
socketGames.matches.acceptMatch(socket.id, options).then((result) => {
if (!result.err) {
socket.emit("acceptMatch", result);
}
});
});
`
#### Reject Match
`js
socket.on("rejectMatch", (options) => {
if (socketGames.config.readDatabaseSync().data.matches[options.matchId]?.status === "pending") {
socketGames.matches.rejectMatch(socket.id, options).then((result) => {
if (!result.err) {
socket.emit("rejectMatch", result);
}
});
}
});
`
#### Open Match
`js
socket.on("openMatch", (options) => {
try {
socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.players.forEach((player) => {
io.of("/").sockets.get(player).emit("openMatch", {
matchId: options.matchId,
turn: socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.data.turn,
players: socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.data?.players,
users: socketGames.users.allUsersSync().users
});
});
} catch (err) {}
});
`
$3
#### Create User
`js
socket.on("createUser", (options) => {
socketGames.users.createUser(socket.id, options).then(() => {
socketGames.users.allUsers().then(({ users }) => {
io.emit("userUpdate", { users });
});
});
});
`
#### Edit User
`js
socket.on("editUser", (options) => {
socketGames.users.editUser(socket.id, options).then(() => {
socketGames.users.allUsers().then(({ users }) => {
io.emit("userUpdate", { users });
})
});
})
`
#### Delete User
`js
socket.on("disconnect", () => {
socketGames.users.deleteUser(socket.id, io).then((deletedUser) => {
if (!deletedUser.err) {
socketGames.users.allUsers().then(({ users }) => {
io.emit("userUpdate", { users });
deletedUser.matches.forEach((match) => {
match[1].players.filter((player) => player !== socket.id).forEach((player) => {
io.of("/").sockets.get(player).emit("roomLeave", {
username: result.users[player].username
});
});
});
});
}
});
});
`
$3
#### Create Match of TicTacToe
`js
socket.on("createMatch", (options) => {
socketGames.games.tictactoe.createMatch(socket.id, options).then((result) => {
if (!result.err) {
io.of("/").sockets.get(result.playerId).emit("newMatch", result);
}
});
});
`
#### Create Match of Connect Four
`js
socket.on("createMatch", (options) => {
socketGames.games.connectfour.createMatch(socket.id, options).then((result) => {
if (!result.err) {
io.of("/").sockets.get(result.playerId).emit("newMatch", result);
}
});
});
`
#### Place Field of TicTacToe
`js
socket.on("placeField", (options) => {
try {
socketGames.games.tictactoe.placeField(socket.id, options).then((result) => {
if (!result.err) {
Object.keys(result.players).forEach((player) => {
io.of("/").sockets.get(player).emit("placeField", {
...result,
...{
users: socketGames.users.allUsersSync().users
}
});
});
}
});
} catch (err) {}
});
`
#### Place Field of Connect Four
`js
socket.on("placeField", (options) => {
try {
socketGames.games.connectfour.placeField(socket.id, options).then((result) => {
if (!result.err) {
Object.keys(result.players).forEach((player) => {
io.of("/").sockets.get(player).emit("placeField", {
...result,
...{
users: socketGames.users.allUsersSync().users
}
});
});
}
});
} catch (err) {}
});
`
$3
#### Register File
`js
socketGames.gameRegistry.registerGames({
games: [
{
type: "file", // set type to file
game: "./game.js" // path to your game
}
]
});
`
#### Register URL
`js
socketGames.gameRegistry.registerGames({
games: [
{
type: "url", // set type to url
game: "https://raw.githubusercontent.com/username/repository/root/file" // url to your game
}
]
});
`
#### Register Plugin
`js
socketGames.gameRegistry.registerGames({
games: [
{
type: "plugin", // set type to plugin
game: "My Plugin Id" // plugin id
}
]
});
`
#### Unregister File
`js
socketGames.gameRegistry.unregisterGames({
games: [
{
type: "file", // set type to file
game: "./game.js" // path to your game
}
]
});
`
#### Unregister URL
`js
socketGames.gameRegistry.unregisterGames({
games: [
{
type: "url", // set type to url
game: "https://raw.githubusercontent.com/username/repository/root/file" // url to your game
}
]
});
`
#### Unregister Plugin
`js
socketGames.gameRegistry.unregisterGames({
games: [
{
type: "plugin", // set type to plugin
game: "My Plugin Id" // plugin id
}
]
});
`
$3
`js
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const socketGames = require('socket.io-games');
socketGames.config.config(app, {
file: "./db.json"
}).then(() => {
socketGames.config.clearDatabaseSync();
});
app.use(express.static("pages"));
app.use(express.static("public"));
io.on('connection', (socket, name) => {
socket.on("createUser", (options) => {
socketGames.users.createUser(socket.id, options).then(() => {
socketGames.users.allUsers().then((result) => {
io.emit("userUpdate", result.users);
});
});
});
socket.on("createMatch", (options) => {
socketGames.games.tictactoe.createMatch(socket.id, options).then((result) => {
if (!result.err) {
io.of("/").sockets.get(result.playerId).emit("newMatch", result);
}
});
});
socket.on("acceptMatch", (options) => {
socketGames.matches.acceptMatch(socket.id, options).then((result) => {
if (!result.err) {
socket.emit("acceptMatch", result);
}
});
});
socket.on("rejectMatch", (options) => {
if (socketGames.config.readDatabaseSync().data.matches[options.matchId]?.status === "pending") {
socketGames.matches.rejectMatch(socket.id, options).then((result) => {
if (!result.err) {
socket.emit("rejectMatch", result);
}
});
}
});
socket.on("openMatch", (options) => {
try {
socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.players.forEach((player) => {
io.of("/").sockets.get(player).emit("openMatch", {
matchId: options.matchId,
turn: socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.data.turn,
players: socketGames.config.readDatabaseSync().data?.matches?.[options.matchId]?.data?.players,
users: socketGames.users.allUsersSync().users
});
});
} catch (err) {}
});
socket.on("placeField", (options) => {
try {
socketGames.games.tictactoe.placeField(socket.id, options).then((result) => {
if (!result.err) {
Object.keys(result.players).forEach((player) => {
io.of("/").sockets.get(player).emit("placeField", {
...result,
...{
users: socketGames.users.allUsersSync().users
}
});
});
}
});
} catch (err) {}
});
socket.on("disconnect", () => {
socketGames.users.deleteUser(socket.id, io).then((deletedUser) => {
if (!deletedUser.err) {
socketGames.users.allUsers().then((result) => {
io.emit("userUpdate", result.users);
deletedUser.matches.forEach((match) => {
match[1].players.filter((player) => player !== socket.id).forEach((player) => {
io.of("/").sockets.get(player).emit("roomLeave", {
username: result.users[player].username
});
});
});
});
}
});
});
});
app.all('/', (req, res) => {
res.sendFile("pages/home/index.html");
});
http.listen(3000, () => {
console.log("Server is ready");
});
`
Client Side Documentation
$3
#### Setup
`html