{"id":23276242,"url":"https://github.com/dinoscapeprogramming/game-package","last_synced_at":"2025-04-06T11:45:00.806Z","repository":{"id":59240980,"uuid":"535788452","full_name":"DinoscapeProgramming/Game-Package","owner":"DinoscapeProgramming","description":"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","archived":false,"fork":false,"pushed_at":"2024-05-22T15:22:09.000Z","size":49,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-14T14:17:13.082Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DinoscapeProgramming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-12T17:58:09.000Z","updated_at":"2024-05-22T15:22:12.000Z","dependencies_parsed_at":"2025-02-12T17:36:48.193Z","dependency_job_id":null,"html_url":"https://github.com/DinoscapeProgramming/Game-Package","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DinoscapeProgramming%2FGame-Package","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DinoscapeProgramming%2FGame-Package/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DinoscapeProgramming%2FGame-Package/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DinoscapeProgramming%2FGame-Package/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DinoscapeProgramming","download_url":"https://codeload.github.com/DinoscapeProgramming/Game-Package/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478251,"owners_count":20945262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-19T21:30:02.090Z","updated_at":"2025-04-06T11:45:00.780Z","avatar_url":"https://github.com/DinoscapeProgramming.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Game Package\r\nA package you can use to create your own game platform with an implemented match- and user-system with default games like TicTacToe using socket.io\r\n\r\n## Documentation\r\n### Configuration\r\n```js\r\nconst express = require('express');\r\nconst app = express();\r\nconst http = require('http').Server(app);\r\nconst io = require('socket.io')(http);\r\nconst path = require('path');\r\nconst socketGames = require('socket.io-games');\r\n\r\nsocketGames.config.config(app, {\r\n  file: \"./db.json\"\r\n}).then(() =\u003e {\r\n  socketGames.config.clearDatabaseSync();\r\n});\r\napp.use(express.static(\"pages\"));\r\napp.use(express.static(\"public\"));\r\n\r\nio.on('connection', (socket, name) =\u003e {});\r\n\r\napp.all('/', (req, res) =\u003e {\r\n  res.sendFile(\"pages/home/index.html\");\r\n});\r\n\r\nhttp.listen(3000, () =\u003e {\r\n  console.log(\"Server is ready\");\r\n});\r\n```\r\n\r\n### Create Match (TicTacToe)\r\n```js\r\nsocket.on(\"createMatch\", (options) =\u003e {\r\n  socketGames.games.tictactoe.createMatch(socket.id, options).then((result) =\u003e {\r\n    if (!result.err) {\r\n      io.of(\"/\").sockets.get(result.playerId).emit(\"newMatch\", result);\r\n    }\r\n  });\r\n});\r\n```\r\n\r\n### Create Match (Connect Four)\r\n```js\r\nsocket.on(\"createMatch\", (options) =\u003e {\r\n  socketGames.games.connectfour.createMatch(socket.id, options).then((result) =\u003e {\r\n    if (!result.err) {\r\n      io.of(\"/\").sockets.get(result.playerId).emit(\"newMatch\", result);\r\n    }\r\n  });\r\n});\r\n```\r\n\r\n### Accept Match\r\n```js\r\nsocket.on(\"acceptMatch\", (options) =\u003e {\r\n  socketGames.matches.acceptMatch(socket.id, options).then((result) =\u003e {\r\n    if (!result.err) {\r\n      socket.emit(\"acceptMatch\", result);\r\n    }\r\n  });\r\n});\r\n```\r\n\r\n### Reject Match\r\n```js\r\nsocket.on(\"rejectMatch\", (options) =\u003e {\r\n  if (socketGames.config.readFileSync().data.matches[options.matchId]?.status === \"pending\") {\r\n    socketGames.matches.rejectMatch(socket.id, options).then((result) =\u003e {\r\n      if (!result.err) {\r\n        socket.emit(\"rejectMatch\", result);\r\n      }\r\n    });\r\n  }\r\n});\r\n```\r\n\r\n### Open Match\r\n```js\r\nsocket.on(\"openMatch\", (options) =\u003e {\r\n  try {\r\n    socketGames.config.readFileSync().data?.matches?.[options.matchId]?.players.forEach((player) =\u003e {\r\n      io.of(\"/\").sockets.get(player).emit(\"openMatch\", {\r\n        matchId: options.matchId,\r\n        turn: socketGames.config.readFileSync().data?.matches?.[options.matchId]?.data.turn,\r\n        players: socketGames.config.readFileSync().data?.matches?.[options.matchId]?.data?.players,\r\n        users: socketGames.users.allUsersSync().users\r\n      });\r\n    });\r\n  } catch (err) {}\r\n});\r\n```\r\n\r\n### Place Field (TicTacToe)\r\n```js\r\nsocket.on(\"placeField\", (options) =\u003e {\r\n  try {\r\n    socketGames.games.tictactoe.placeField(socket.id, options).then((result) =\u003e {\r\n      if (!result.err) {\r\n        Object.keys(result.players).forEach((player) =\u003e {\r\n          io.of(\"/\").sockets.get(player).emit(\"placeField\", {\r\n            ...result,\r\n            ...{\r\n              users: socketGames.users.allUsersSync().users\r\n            }\r\n          });\r\n        });\r\n      }\r\n    });\r\n  } catch (err) {}\r\n});\r\n```\r\n\r\n### Place Field (Connect Four)\r\n```js\r\nsocket.on(\"placeField\", (options) =\u003e {\r\n  try {\r\n    socketGames.games.connectfour.placeField(socket.id, options).then((result) =\u003e {\r\n      if (!result.err) {\r\n        Object.keys(result.players).forEach((player) =\u003e {\r\n          io.of(\"/\").sockets.get(player).emit(\"placeField\", {\r\n            ...result,\r\n            ...{\r\n              users: socketGames.users.allUsersSync().users\r\n            }\r\n          });\r\n        });\r\n      }\r\n    });\r\n  } catch (err) {}\r\n});\r\n```\r\n\r\n### Create User\r\n```js\r\nsocket.on(\"createUser\", (options) =\u003e {\r\n  socketGames.users.createUser(socket.id, options).then(() =\u003e {\r\n    socketGames.users.allUsers().then((result) =\u003e {\r\n      io.emit(\"userUpdate\", result.users);\r\n    });\r\n  });\r\n});\r\n```\r\n\r\n### Delete User\r\n```js\r\nsocket.on(\"disconnect\", () =\u003e {\r\n  socketGames.users.deleteUser(socket.id, io).then((deletedUser) =\u003e {\r\n    if (!deletedUser.err) {\r\n      socketGames.users.allUsers().then((result) =\u003e {\r\n        io.emit(\"userUpdate\", result.users);\r\n        deletedUser.matches.forEach((match) =\u003e {\r\n          match[1].players.filter((player) =\u003e player !== socket.id).forEach((player) =\u003e {\r\n            io.of(\"/\").sockets.get(player).emit(\"roomLeave\", {\r\n              username: result.users[player].username\r\n            });\r\n          });\r\n        });\r\n      });\r\n    }\r\n  });\r\n});\r\n```\r\n\r\n## Example\r\n```js\r\nconst express = require('express');\r\nconst app = express();\r\nconst http = require('http').Server(app);\r\nconst io = require('socket.io')(http);\r\nconst path = require('path');\r\nconst socketGames = require('socket.io-games');\r\n\r\nsocketGames.config.config(app, {\r\n  file: \"./db.json\"\r\n}).then(() =\u003e {\r\n  socketGames.config.clearDatabaseSync();\r\n});\r\napp.use(express.static(\"pages\"));\r\napp.use(express.static(\"public\"));\r\n\r\nio.on('connection', (socket, name) =\u003e {\r\n  socket.on(\"createUser\", (options) =\u003e {\r\n    socketGames.users.createUser(socket.id, options).then(() =\u003e {\r\n      socketGames.users.allUsers().then((result) =\u003e {\r\n        io.emit(\"userUpdate\", result.users);\r\n      });\r\n    });\r\n  });\r\n  socket.on(\"createMatch\", (options) =\u003e {\r\n    socketGames.games.tictactoe.createMatch(socket.id, options).then((result) =\u003e {\r\n      if (!result.err) {\r\n        io.of(\"/\").sockets.get(result.playerId).emit(\"newMatch\", result);\r\n      }\r\n    });\r\n  });\r\n  socket.on(\"acceptMatch\", (options) =\u003e {\r\n    socketGames.matches.acceptMatch(socket.id, options).then((result) =\u003e {\r\n      if (!result.err) {\r\n        socket.emit(\"acceptMatch\", result);\r\n      }\r\n    });\r\n  });\r\n  socket.on(\"rejectMatch\", (options) =\u003e {\r\n    if (socketGames.config.readFileSync().data.matches[options.matchId]?.status === \"pending\") {\r\n      socketGames.matches.rejectMatch(socket.id, options).then((result) =\u003e {\r\n        if (!result.err) {\r\n          socket.emit(\"rejectMatch\", result);\r\n        }\r\n      });\r\n    }\r\n  });\r\n  socket.on(\"openMatch\", (options) =\u003e {\r\n    try {\r\n      socketGames.config.readFileSync().data?.matches?.[options.matchId]?.players.forEach((player) =\u003e {\r\n        io.of(\"/\").sockets.get(player).emit(\"openMatch\", {\r\n          matchId: options.matchId,\r\n          turn: socketGames.config.readFileSync().data?.matches?.[options.matchId]?.data.turn,\r\n          players: socketGames.config.readFileSync().data?.matches?.[options.matchId]?.data?.players,\r\n          users: socketGames.users.allUsersSync().users\r\n        });\r\n      });\r\n    } catch (err) {}\r\n  });\r\n  socket.on(\"placeField\", (options) =\u003e {\r\n    try {\r\n      socketGames.games.tictactoe.placeField(socket.id, options).then((result) =\u003e {\r\n        if (!result.err) {\r\n          Object.keys(result.players).forEach((player) =\u003e {\r\n            io.of(\"/\").sockets.get(player).emit(\"placeField\", {\r\n              ...result,\r\n              ...{\r\n                users: socketGames.users.allUsersSync().users\r\n              }\r\n            });\r\n          });\r\n        }\r\n      });\r\n    } catch (err) {}\r\n  });\r\n  socket.on(\"disconnect\", () =\u003e {\r\n    socketGames.users.deleteUser(socket.id, io).then((deletedUser) =\u003e {\r\n      if (!deletedUser.err) {\r\n        socketGames.users.allUsers().then((result) =\u003e {\r\n          io.emit(\"userUpdate\", result.users);\r\n          deletedUser.matches.forEach((match) =\u003e {\r\n            match[1].players.filter((player) =\u003e player !== socket.id).forEach((player) =\u003e {\r\n              io.of(\"/\").sockets.get(player).emit(\"roomLeave\", {\r\n                username: result.users[player].username\r\n              });\r\n            });\r\n          });\r\n        });\r\n      }\r\n    });\r\n  });\r\n});\r\n\r\napp.all('/', (req, res) =\u003e {\r\n  res.sendFile(\"pages/home/index.html\");\r\n});\r\n\r\nhttp.listen(3000, () =\u003e {\r\n  console.log(\"Server is ready\");\r\n});\r\n```\r\n\r\n## Client Side Documentation\r\n### Configuration\r\n```html\r\n\u003cscript src=\"/socket.io/socket.io.js\"\u003e\r\n```\r\n```js\r\nconst socket = io();\r\n```\r\n\r\n### Actions\r\n#### Create User\r\n```js\r\nsocket.emit(\"createUser\", {\r\n  username: \"My Username\"\r\n});\r\n```\r\n\r\n#### Create Match\r\n```js\r\nsocket.emit(\"createMatch\", {\r\n  playerId: \"player id\"\r\n});\r\n```\r\n\r\n#### Accept Match\r\n```js\r\nsocket.emit(\"acceptMatch\", {\r\n  matchId: \"match id\"\r\n});\r\n```\r\n\r\n#### Reject Match\r\n```js\r\nsocket.emit(\"rejectMatch\", {\r\n  matchId: \"match id\"\r\n});\r\n```\r\n\r\n#### Open Match\r\n```js\r\nsocket.emit(\"openMatch\", {\r\n  matchId: \"match id\"\r\n});\r\n```\r\n\r\n#### Place Field\r\n```js\r\nsocket.emit(\"placeField\", {\r\n  matchId: \"match id\",\r\n  field: \"1\" // number from 1 to 9\r\n})\r\n```\r\n\r\n### Events\r\n#### User Update\r\n```js\r\nsocket.on(\"userUpdate\", (options) =\u003e {\r\n  console.log(options.users); // do some stuff with the users\r\n});\r\n```\r\n\r\n#### New Match\r\n```js\r\nsocket.on(\"newMatch\", (options) =\u003e {\r\n  console.log(options.username, options.matchId); // do some stuff with the match request\r\n});\r\n```\r\n\r\n#### Accept Match\r\n```js\r\nsocket.on(\"acceptMatch\", (options) =\u003e {\r\n  console.log(options.matchId); // do some stuff with the accepted match\r\n  socket.emit(\"openMatch\", { matchId: options.matchId }); // open the accepted match\r\n});\r\n```\r\n\r\n#### Reject Match\r\n```js\r\nsocket.on(\"rejectMatch\", (options) =\u003e {\r\n  console.log(options.matchId); // do some stuff with the rejected match\r\n});\r\n```\r\n\r\n#### Open Match\r\n```js\r\nsocket.on(\"openMatch\", (options) =\u003e {\r\n  console.log(options.users[socket.id], options.users[Object.entries(options.players).filter((item) =\u003e item[0] !== socket.id)[0][0]].username, options.turn); // do some stuff with the opened match\r\n});\r\n```\r\n\r\n#### Place Field\r\n```js\r\nsocket.on(\"placeField\", (options) =\u003e {\r\n  console.log(options.field); // do some stuff with the placed field\r\n});\r\n```\r\n\r\n#### Room Leave\r\n```js\r\nsocket.on(\"roomLeave\", (options) =\u003e {\r\n  console.log(options.username); // do some stuff with the user who left\r\n});\r\n```\r\n\r\n## Example Site\r\nhttps://tictactoe.dinoscape.tk","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdinoscapeprogramming%2Fgame-package","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdinoscapeprogramming%2Fgame-package","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdinoscapeprogramming%2Fgame-package/lists"}