diff --git a/lua/examples/05-ping/README.md b/lua/examples/05-ping/README.md new file mode 100644 index 0000000..c647d63 --- /dev/null +++ b/lua/examples/05-ping/README.md @@ -0,0 +1,7 @@ +# pdportal-05-ping + +How fast is this thing, anyway? + +## Installation + +`./build.sh d` will compile and automatically install the app to your connected Playdate. I'm sorry, the script currently only works on macOS, and even then can be a bit janky. Pull requests welcome :) diff --git a/lua/examples/05-ping/build.sh b/lua/examples/05-ping/build.sh new file mode 100755 index 0000000..f0baae9 --- /dev/null +++ b/lua/examples/05-ping/build.sh @@ -0,0 +1,12 @@ +pdxFile="pdportal-05-ping.pdx" + +cp ../../pdportal.lua ./src/pdportal.lua +pdc src "$pdxFile" + +runChoice=$1 + +if [ "$runChoice" = "s" ]; then + open "$pdxFile" +elif [ "$runChoice" = "d" ]; then + node ../../../scripts/uploadPdxToPlaydate.js "$pdxFile" +fi diff --git a/lua/examples/05-ping/src/Example05Ping.lua b/lua/examples/05-ping/src/Example05Ping.lua new file mode 100644 index 0000000..7b3eb5e --- /dev/null +++ b/lua/examples/05-ping/src/Example05Ping.lua @@ -0,0 +1,124 @@ +local graphics = playdate.graphics + +local PdPortal = PdPortal +local PortalCommand = PdPortal.PortalCommand + +class('Example05Ping').extends(PdPortal) +local Example05Ping = Example05Ping + +function Example05Ping:init() + -- If your subclass overrides the init method, make sure to call super! + Example05Ping.super.init(self) + + playdate.display.setRefreshRate(50) + + self:_initOwnProps() +end + +function Example05Ping:_initOwnProps() + self.connected = false + self.isPinging = false + self.pingDuration = -1 + self.pingStart = -1 + self.peerId = nil + self.remotePeerId = nil +end + +function Example05Ping:update() + -- If your subclass overrides the update method, make sure to call super! + Example05Ping.super.update(self) + + graphics.clear() + + playdate.drawFPS(10, 225) + + if self.connected then + if self.isPinging then + graphics.drawTextAligned( + 'Pinging…', + 200, + 100, + kTextAlignment.center + ) + elseif self.peerId == nil then + graphics.drawTextAligned( + 'Connecting to peer server…', + 200, + 100, + kTextAlignment.center + ) + elseif self.remotePeerId == nil then + graphics.drawTextAligned( + 'Connected as ' .. self.peerId ..', waiting for remote peer…', + 200, + 100, + kTextAlignment.center + ) + elseif self.pingDuration > -1 then + graphics.drawTextAligned( + 'Pong in ' .. self.pingDuration * 1000 ..'ms (Ⓐ ping again)', + 200, + 100, + kTextAlignment.center + ) + + if playdate.buttonJustPressed(playdate.kButtonA) then + self:beginPing() + end + else + graphics.drawTextAligned( + 'Ⓐ to ping', + 200, + 100, + kTextAlignment.center + ) + + if playdate.buttonJustPressed(playdate.kButtonA) then + self:beginPing() + end + end + else + graphics.drawTextAligned( + 'Disconnected', + 200, + 100, + kTextAlignment.center + ) + end +end + +function Example05Ping:beginPing() + self.pingStart = playdate.getElapsedTime() + self:sendToPeerConn(self.remotePeerId, 'ping') +end + +function Example05Ping:onConnect(portalVersion) + self.connected = true + self:sendCommand(PortalCommand.InitializePeer) +end + +function Example05Ping:onPeerOpen(peerId) + self.peerId = peerId +end + +function Example05Ping:onPeerConnection(remotePeerId) + self.remotePeerId = remotePeerId +end + +function Example05Ping:onPeerConnOpen(remotePeerId) + self.remotePeerId = remotePeerId +end + +function Example05Ping:onDisconnect() + self:_initOwnProps() +end + +function Example05Ping:onPeerConnData(remotePeerId, payload) + -- Note double quotes below because of peerjs JSON encoding. Typically, you'd just send an actual JSON object instead of a string + if payload == '"ping"' then + self:sendToPeerConn(self.remotePeerId, 'pong') + elseif payload == '"pong"' then + self.isPinging = false + self.pingDuration = playdate.getElapsedTime() - self.pingStart + end +end diff --git a/lua/examples/05-ping/src/main.lua b/lua/examples/05-ping/src/main.lua new file mode 100644 index 0000000..684aeb9 --- /dev/null +++ b/lua/examples/05-ping/src/main.lua @@ -0,0 +1,12 @@ +-- Copied during build, you wouldn't normally have to do that +import './pdportal' + +import 'CoreLibs/graphics' + +import 'Example05Ping' + +local app = Example05Ping() + +playdate.update = function() + app:update() +end diff --git a/lua/examples/05-ping/src/pdxinfo b/lua/examples/05-ping/src/pdxinfo new file mode 100644 index 0000000..c78a12e --- /dev/null +++ b/lua/examples/05-ping/src/pdxinfo @@ -0,0 +1,4 @@ +name=pdportal-05-ping +bundleID=com.strawdynamics.pdportal-05-ping +version=0.2.0 +buildNumber=1