Android / Multiplayer / In development
Mini Game Platform
一处集合五子棋、中国象棋与在线房间的原生 Android 小游戏平台。
从一局棋开始,把客户端、游戏规则和联机系统串在一起。本地可以与 AI 对弈,也能通过房间和邀请进入由服务端裁定的在线对局。
- Play locally, connect online
- 五子棋与中国象棋 · 本地人机 · 在线房间与邀请
- One client, an authoritative server
- Compose 多模块客户端,Nakama / Lua 权威对局,PostgreSQL 持久化。
- Kotlin
- Jetpack Compose
- Nakama
- Lua
- PostgreSQL
function M.match_join(context, dispatcher, tick, state, presences) for _, presence in ipairs(presences) do state.players[presence.user_id] = presence end if player_count(state) == 2 and state.status == "reconnecting" then state.status = "playing" state.reconnect_deadline_tick = 0 elseif player_count(state) == 2 and state.status == "waiting" then state.status = "playing" state.turn_user_id = state.black_user_id state.reconnect_deadline_tick = 0 elseif state.status == "waiting" then state.status = "waiting" state.turn_user_id = "" end broadcast_state(dispatcher, state) return stateendSource excerpt / 重连恢复
两名玩家重新就位后恢复对局,清除重连倒计时,并广播同一份服务端状态。
local function has_five(board, x, y, piece) local directions = { { 1, 0 }, { 0, 1 }, { 1, 1 }, { 1, -1 }, } for _, direction in ipairs(directions) do local connected = 1 connected = connected + count_direction(board, x, y, direction[1], direction[2], piece) connected = connected + count_direction(board, x, y, -direction[1], -direction[2], piece) if connected >= 5 then return true end end return falseendSource excerpt / 胜负判定
沿横、纵和两条对角线双向累计连续棋子,由服务端判断是否形成五连。
local function decode_current_round(dispatcher, presence, state, message) local decoded_ok, payload = pcall(nk.json_decode, message.data or "{}") local round_id = decoded_ok and type(payload) == "table" and payload.round_id or nil if type(round_id) ~= "number" or round_id ~= math.floor(round_id) or round_id ~= state.round_id then send_error(dispatcher, presence, "STALE_ROUND", "round_id is invalid or stale") return false, nil end return true, payloadendSource excerpt / 回合校验
先安全解码消息,再核对整数 round_id,拒绝过期回合的操作。