feat: add trading over the same network - #339
Open
JunSungSoo wants to merge 2 commits into
Open
JunSungSoo wants to merge 2 commits into
JunSungSoo wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When several people run PokeTokenBar on the same network while working or
studying together, being able to trade the Pokémon they raised makes the whole
thing more fun. This adds a Trade tab that lets two users on the same
Wi-Fi/LAN swap either a graduated Dex entry or the Pokémon they are currently
raising.
to the computer name, editable in Settings) and a permanent 4-character trade
code. A manual
ip:portcode is the fallback when discovery is blocked.rarities, and approves. The trade commits only when both sides approve.
approval screen warns by name and progress first (e.g. "Vulpix (82%) will be
lost"). Every commit first snapshots the save to
companion-state.pre-trade-<timestamp>.json, and the Trade tab shows wherethat file is and how to restore from it.
Implementation is behind a
TradeTransportprotocol with two implementations:MultipeerTradeTransport(MultipeerConnectivity,encryptionPreference: .required, Bonjour_ptb-trade._tcp) andManualTradeTransport(a directNWConnectionwith 4-byte length-prefixed frames and a size cap). Multipeerallows many peers, so the 1:1 rule is enforced in that layer, with a
deterministic tiebreak for simultaneous cross-invitations.
TradeSessionisthe transport-agnostic state machine above it (hello → offer → both-accept →
commit), and
CompanionStore.applyTradeCommit(sending:receiving:)performs thebackup, removal and insert. Incoming payloads are clamped in
TradeItem.sanitized()at the trust boundary, following the existingSaveTransfer.sanitizedprecedent.Bluetooth-only trading is out of scope here. The transport layer is
protocol-based so it can be added later; this PR is Wi-Fi/LAN only.
Type of change
UI changes
While a trade is connected the popover no longer closes on an outside click,
so a trade cannot be lost mid-approval.
Checklist
swift buildandswift testpass locallyTests added: item model, message codec, session state machine, TCP framing,
both transports, and the commit/backup path. One integration test drives the
real MultipeerConnectivity stack and skips rather than fails when no local
network is available. Also verified manually end to end with two app instances
on one Mac — a Dex entry and an in-progress Pokémon were swapped, the overwrite
warning appeared, and both sides produced a pre-trade backup file.