add invite game mode
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
# Real-time matchmaking channel.
|
||||
#
|
||||
# Client flow:
|
||||
# 1. Connect: ws(s)://host/cable?token=<JWT>
|
||||
# 2. Subscribe to "MatchmakingChannel"
|
||||
# 3. Send { action: "start" } to enter the queue
|
||||
# 4. Receive { type: "matched", ... } or { type: "timeout" }
|
||||
# 5. Disconnecting (or sending { action: "stop" }) cancels the search.
|
||||
# Client flows:
|
||||
# 1. Random battle:
|
||||
# Connect -> Subscribe -> { action: "start" }
|
||||
# -> Receive { type: "matched", ... } or { type: "timeout" }
|
||||
# 2. Invite battle:
|
||||
# Host: Connect -> Subscribe -> { action: "create_invite" }
|
||||
# -> Receive { type: "invite_created", code: "ABCDE" }
|
||||
# -> Wait -> Receive { type: "matched", ... }
|
||||
# Guest: Connect -> Subscribe -> { action: "join_invite", code: "ABCDE" }
|
||||
# -> Receive { type: "matched", ... } or { type: "invite_error", ... }
|
||||
# 3. Cancellation:
|
||||
# Disconnecting or sending { action: "stop" } cancels active search or invite.
|
||||
class MatchmakingChannel < ApplicationCable::Channel
|
||||
def subscribed
|
||||
# NB: must mirror MatchmakingService.broadcast, which uses
|
||||
@@ -17,17 +23,36 @@ class MatchmakingChannel < ApplicationCable::Channel
|
||||
# (e.g. brief reconnect), deliver it now — exactly once.
|
||||
if match = MatchmakingService.claim(current_user.id)
|
||||
transmit(match)
|
||||
elsif code = MatchmakingService.current_invite_code(current_user.id)
|
||||
transmit({ type: "invite_created", code: code })
|
||||
end
|
||||
end
|
||||
|
||||
def start(_data)
|
||||
def start(_data = nil)
|
||||
MatchmakingService.enqueue(current_user.id, rating: current_user.rating)
|
||||
end
|
||||
|
||||
def create_invite
|
||||
code = MatchmakingService.create_invite(current_user.id)
|
||||
transmit({ type: "invite_created", code: code })
|
||||
end
|
||||
|
||||
def join_invite(data)
|
||||
code = data&.[]("code") || data&.[](:code)
|
||||
result = MatchmakingService.join_invite(current_user.id, code)
|
||||
if result[:error]
|
||||
transmit({ type: "invite_error", error: result[:error], message: result[:message] })
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
MatchmakingService.cancel(current_user.id)
|
||||
end
|
||||
|
||||
def cancel_invite
|
||||
MatchmakingService.cancel(current_user.id)
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
MatchmakingService.cancel(current_user.id)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user