{"id":48037926,"url":"https://github.com/zients/lua-online-tic-tac-toe-server","last_synced_at":"2026-04-04T14:00:55.453Z","repository":{"id":104598951,"uuid":"225153340","full_name":"zients/lua-online-tic-tac-toe-server","owner":"zients","description":null,"archived":false,"fork":false,"pushed_at":"2020-08-24T15:33:07.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-23T18:30:56.822Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zients.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2019-12-01T11:54:08.000Z","updated_at":"2023-06-12T04:37:46.000Z","dependencies_parsed_at":"2023-05-04T14:16:21.223Z","dependency_job_id":null,"html_url":"https://github.com/zients/lua-online-tic-tac-toe-server","commit_stats":null,"previous_names":["zients/lua-online-tic-tac-toe-server"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/zients/lua-online-tic-tac-toe-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zients%2Flua-online-tic-tac-toe-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zients%2Flua-online-tic-tac-toe-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zients%2Flua-online-tic-tac-toe-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zients%2Flua-online-tic-tac-toe-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zients","download_url":"https://codeload.github.com/zients/lua-online-tic-tac-toe-server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zients%2Flua-online-tic-tac-toe-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31402277,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T10:20:44.708Z","status":"ssl_error","status_checked_at":"2026-04-04T10:20:06.846Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":"2026-04-04T14:00:31.466Z","updated_at":"2026-04-04T14:00:55.437Z","avatar_url":"https://github.com/zients.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Online TicTacToe Server\n\nThis is lua runtime for nakama game server.\n\nThese lua file is in the nakama folder  ./data/modules.\n\nTicTacToe Unity Client : https://github.com/tsen1220/UnityOnlineTicTacToeClient\n\nWe need to export the lua modules to let nakama server run the specific match handler API.\n\n## Start Database\nWe need to start the cockroachDB. This is my start setting.\n\n```\ncockroach start --insecure --store=path=\"./cdb-store1/\" --listen-addr=localhost --background\n```\n\n\n## Nakama Match API\n\nWe need to set the nakama module to use the nakama features and table M to export the module by returning M.\n\n### Lua Modules\n```\n\nlocal nakama = require(\"nakama\")\n\nlocal M ={}\n\n...\n...\n...\n...\n...\n...\n...\n...\n...\n\n---- return M to export.\n\nreturn M\n\n```\n\n### Match_Create\n\nIf nakama matchmaker matched, it would create the match room with matchID and setupState (ex:room rule, room presences...).\n\nAnd players can join the match by matchID.\n\n```\nlocal function CreateMatchID(context, match_user) \n  \n--[[\n  match_user data cotains the match info.\n  setupState table uses for create the match.\n]]\n\n  local setupState={\n    invited = match_user\n  }\n\n\n--[[\n\n  The matchmaker matched hook must return a match ID or nil.\n\n  if the match should proceed as relayed multiplayer.\n\n  For Lua it should be the module name.\n   In this example it'd be a file named MatchRun.lua, so the match module is MatchRun.\n]]\n\n---------------- My runtime module is MatchRun\n\n  local module = \"MatchRun\"\n  local matchID;\n\n---------------- Create the match with module and setupState.\n\n  matchID = nakama.match_create(module, setupState)\n\n  return matchID;\nend\n\n\n---------------- This RPC is for creating the match.\n\n\nnakama.register_matchmaker_matched(CreateMatchID)\n\n\n```\n\n## Match Handler API\n\nThen , use the nakama match handler API to ensure the online game smoothly.\n\nWe need to use Nakama match handler API.\n\n### Match_Init\n\nM.match_init(context, setupState)\n\n```\n\nfunction M.match_init(context, setupState)\n\n    --[[\n        When you succeed to create the match. Then you will initiate the match. \n\n\n        You must return three values.\n        \n        (table) - The initial in-memory state of the match. May be any non-nil Lua term, or nil to end the match. This variable comes from setupState of match_create. You can customize.\n        \n        (number) - Tick rate representing the desired number of match_loop() calls per second. Must be between 1 and 30, inclusive.\n        \n        (string) - A string label that can be used to filter matches in listing operations. Must be between 0 and 2048 bytes long. This is used in match listing to filter matches.\n    ]]\n\n\n    local gamestate = setupState      or      { setupState, setupState.invited[1].presence, setupState.invited[2].presence }\n    local tickrate = 2\n    local label = \"TicTacToe\"\n\n    return gamestate, tickrate, label\n\nend\n\n```\n\n### Match_Join_Attempt\n\nM.match_join_attempt(context, dispatcher, tick, state, presence, metadata)\n\n```\nfunction M.match_join_attempt(context, dispatcher, tick, state, presence, metadata)\n\n      --[[\n          After initiating , check the user join attempt.\n\n          We need to return 2 values , 1 is optional.\n          \n          (table) - An (optionally) updated state. May be any non-nil Lua term, or nil to end the match.\n          \n          (boolean) - True if the join attempt should be allowed, false otherwise.\n          \n          (string) *optionally - If the join attempt should be rejected, an optional string rejection reason can be returned to the client.\n      ]]\n\n      --[[ \n          which comes from nakama server\n\n          Presence format(     Presence data format    ):\n          {\n            user_id = \"user unique ID\",\n            session_id = \"session ID of the user's current connection\",\n            username = \"user's unique username\",\n            node = \"name of the Nakama node the user is connected to\"\n          }\n         ]]\n\n    local acceptUser = true\n\n    return state , acceptUser\n\nend\n\n```\n\n### Match_Join\n\nM.match_join(context, dispatcher, tick, state, presences)\n\n```\nfunction M.match_join(context, dispatcher, tick, state, presences)\n\n    --[[\n        Join the room.\n\n        We need to return 1 value.\n        (table) - An (optionally) updated state. May be any non-nil Lua term, or nil to end the match.\n    ]]    \n\n\n  return state\n\nend\n\n```\n\n### Match_Loop\n\nM.match_loop(context, dispatcher, tick, state, messages)\n\n```\nfunction M.match_loop(context, dispatcher, tick, state, messages)\n\n    --[[\n        This is the server runtime during the match.\n        Messages is the data which comes from the client.\n        return nil  ===========\u003e stop the match loop.\n   ]] \n\n   for _,msg in ipairs(messages) do \n      .....\n      .....\n      .....\n\n    --[[\n      This section want to send data to the client. To keep match working.\n    ]]\n      dispatcher.broadcast_message(op_code, data, presences, sender)\n\n   end\n\n\n    return state\n\nend\n```\n\n### Match_Leave\n\nM.match_leave(context, dispatcher, tick, state, presences)\n\n```\nfunction M.match_leave(context, dispatcher, tick, state, presences)\n\n  --[[\n    When someone leave the match, exit match_loop function and execute match_leave.\n    \n      We need to return 1 value.\n      (table) - An (optionally) updated state. May be any non-nil Lua term or nil to end the match.\n  ]]  \n\n\n  return state\nend\n```\n\n\n### Match Runtime API\n\nThe Nakama match handler API contains dispatcher which is match runtime API to send the messages to client .\n\nRecommend that use this in function match_loop.\n\nWe need to use it to complete the online game.\n\n\n```\ndispatcher.broadcast_message(op_code, data, presences, sender):\n\n1. op_code is number.\n2. data is json_encode data.\n3. presences is table which contains presence(s) data.  ex: {presences[1],presences[2]} or {presences[1]}\n4. sender is table which contains presence(s) data.\n\n\nFor example, function is in match_loop ===========\u003e dispatcher.broadcast_message(1, Nakama.json_encode(data), { state.presences[1] }, { state.presences[2] })\n\n        Presence format:\n        presence  {\n            user_id = \"user unique ID\",\n            session_id = \"session ID of the user's current connection\",\n            username = \"user's unique username\",\n            node = \"name of the Nakama node the user is connected to\"\n          }\n\n\n    |Param    |\tType\t|                                                   Description                                                  |\n    |---------|---------|----------------------------------------------------------------------------------------------------------------|\n    |op_code  |\tnumber\t|Numeric message op code.                                                                                        |\n    |data     |\tstring\t|Data payload string, or nil.                                                                                    |\n    |presences|\ttable\t|List of presences (a subset of match participants) to use as message targets, or nil to send to the whole match.|\n    |sender   |\ttable\t|A presence to tag on the message as the 'sender', or nil.                                                       |\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzients%2Flua-online-tic-tac-toe-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzients%2Flua-online-tic-tac-toe-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzients%2Flua-online-tic-tac-toe-server/lists"}