Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 05-ping example #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lua/examples/05-ping/README.md
Original file line number Diff line number Diff line change
@@ -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 :)
12 changes: 12 additions & 0 deletions lua/examples/05-ping/build.sh
Original file line number Diff line number Diff line change
@@ -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
124 changes: 124 additions & 0 deletions lua/examples/05-ping/src/Example05Ping.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
local graphics <const> = playdate.graphics

local PdPortal <const> = PdPortal
local PortalCommand <const> = PdPortal.PortalCommand

class('Example05Ping').extends(PdPortal)
local Example05Ping <const> = 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
12 changes: 12 additions & 0 deletions lua/examples/05-ping/src/main.lua
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lua/examples/05-ping/src/pdxinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name=pdportal-05-ping
bundleID=com.strawdynamics.pdportal-05-ping
version=0.2.0
buildNumber=1