Skip to content

Instantly share code, notes, and snippets.

@andrewxhill andrewxhill/README.md
Last active Jun 11, 2019

Embed
What would you like to do?

Setup a new game of cmd-line tag!

Create a new game

sh new-game.sh

sh new-game.sh "Game 1"

Output

{
  "block_count": 1,
  "head": "QmQEPTjFeugPFuJYVCVpLhYNJnxemv2AdAi9SLmrKD7nME",
  "head_block": {
    "author": "12D3KooWFq4GuoQBMkXSrVDePuhWpFn2VD6WpxdVgshBzTHyHfdb",
    "date": "2019-06-10T20:20:42.195264Z",
    "id": "QmQEPTjFeugPFuJYVCVpLhYNJnxemv2AdAi9SLmrKD7nME",
    "parents": [],
    "thread": "12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG",
    "type": "JOIN",
    "user": {
      "address": "P5J8iTwJHZKtEWr5udeSc1KKoSAZsxPX7UjreoE5d4VYKCsG",
      "name": "P5J8iTw"
    }
  },
  "id": "12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG",
  "initiator": "P5J8iTwJHZKtEWr5udeSc1KKoSAZsxPX7UjreoE5d4VYKCsG",
  "key": "textile-cmd-line-tag-v0.0-game_1",
  "name": "Game 1",
  "peer_count": 1,
  "schema": "QmcSccsSZPZW4GkXSAqUpyvKWPxGGuNcQqPjpbPHBPqPL8",
  "schema_node": {
    "json_schema": {
      "description": "Possible events in cmd line tag.",
      "properties": {
        "event": {
          "description": "event type identifier",
          "type": "string"
        },
        "extra": {
          "description": "extra information",
          "type": "string"
        },
        "target": {
          "description": "peer-id of that the event modifies",
          "type": "string"
        }
      },
      "required": [
        "event"
      ],
      "title": "CMD Line Tag Mechanics",
      "type": "object"
    },
    "mill": "/json",
    "name": "cmd-line-tag"
  },
  "sharing": "INVITE_ONLY",
  "sk": "<secret>",
  "state": "LOADED",
  "type": "PUBLIC",
  "whitelist": []
}

Invite a peer

textile invites create --thread="12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG" --address="P69vwxHTh1p5Tv5HrRyUGGzzQLXjUQThNKgGmsvTq1up1nvA"

List peers

textile thread peer -t 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG

Start the game

sh start-game.sh

Tag the new peer

sh tag-peer.sh

sh tag-peer.sh 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG P69vwxHTh1p5Tv5HrRyUGGzzQLXjUQThNKgGmsvTq1up1nvA

Are you it?

sh am-i-it.sh

sh am-i-it.sh 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG
1. -carson- tagged you
2. you tagged -carson-

Run! -carson- is it!
#!/usr/bin/env bash
set -e # abort on any error
ID=${1?param missing - threadId.}
# textile threads get 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG
ADDRESS=$(textile account address)
if THREAD=$(textile threads get $ID 2>&1); then
# Only the game creator should start the game
INITIATOR=$(echo $THREAD | jq -r .initiator)
# TODO: Should hit the start-game script here to ensure it's started if not
declare -i STARTED
STARTED=0
# Get all existing blocks created by current user
for row in $( textile file list -t $ID -l100000 | jq --arg INITIATOR "$INITIATOR" -r '.items[] | select(.user.address == $INITIATOR) | .files[0].file.hash' ); do
START=$( textile file get $row --content | jq 'select(.event == "start")')
# If we got any results back from Start events, we can skip creating it again
if [ ! -z "$START" ]; then
STARTED+=1
fi
done
if [ "$STARTED" -eq 0 ]; then
echo "Error: Game not started"
exit
fi
# Game always started with initiator it
TAGGED=$INITIATOR
declare -i N
N=1
for row in $( textile feed -l1000000 -t $ID | jq --unbuffered -cr ".items | reverse | .[] | select(.payload.\"@type\" == \"/Files\") | @base64" ); do
# Only the person that is Tagged creates updates we care about
DECODED=$(echo $row | base64 --decode)
AUTHOR=$(echo $DECODED | jq -r ".payload.user.address")
if [ "$AUTHOR" == "$TAGGED" ]; then
HASH=$(echo $DECODED | jq -r '.payload.files[0].file.hash')
TAG=$( textile file get $HASH --content | jq 'select(.event == "tag")' )
# We are just lookking for tag events
if [ ! -z "${TAG// }" ]; then
NOWIT=$( textile file get $HASH --content | jq -r '.target' )
if [ $NOWIT == $ADDRESS ]; then
CONTACT=$(textile contacts get $TAGGED | jq -r '.name')
echo "$N. $CONTACT tagged you"
elif [ $TAGGED == $ADDRESS ]; then
CONTACT=$(textile contacts get $NOWIT | jq -r '.name')
echo "$N. you tagged $CONTACT"
else
CONTACT_A=$(textile contacts get $TAGGED | jq -r '.name')
CONTACT_B=$(textile contacts get $NOWIT | jq -r '.name')
echo "$N. $CONTACT_A tagged $CONTACT_B"
fi
N+=1
# Hand off the known tagged
TAGGED=$NOWIT
fi
fi
done
echo ""
if [ $TAGGED == $ADDRESS ]; then
echo "Oh no, you are it!"
else
CONTACT=$(textile contacts get $TAGGED | jq -r '.name')
echo "Run! $CONTACT is it!"
fi
else
echo "Error: Incorrect thread ID"
fi
{
"name": "cmd-line-tag",
"mill": "/json",
"plaintext": true,
"json_schema": {
"title": "CMD Line Tag Mechanics",
"description": "Possible events in cmd line tag.",
"type": "object",
"required": [ "event" ],
"properties": {
"event": {
"type": "string",
"description": "event type identifier"
},
"target": {
"type": "string",
"description": "peer-id of that the event modifies"
},
"extra": {
"type": "string",
"description": "extra information"
}
}
}
}
#!/usr/bin/env bash
set -e # abort on any error
NAME=${1?param missing - name.}
KEY='textile-cmd-line-tag-v0.0-'${NAME// /_}
if result=$(textile threads add $NAME --key=$KEY --schema-file='game-of-tag.json' --type="open" --sharing="invite_only" 2>&1); then
RESULT=$result
else
echo $result
# Print any existing threads with this name
echo "Known threads:"
textile threads list | jq --arg KEY "$KEY" '.items[] | select(.key == $KEY)'
echo "hint: game names must be unique"
exit
fi
jq '.' <<< $RESULT
ID=${1?param missing - threadId.}
# textile threads get 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG
ADDRESS=$(textile account address)
# Get our Thread
if THREAD=$(textile threads get $ID 2>&1); then
# Only the game creator should start the game
INITIATOR=$(echo $THREAD | jq -r .initiator)
# Is current user the game creator?
if [ ! "$INITIATOR" == "$ADDRESS" ]; then
echo "Error: Only game creator can start the game."
exit
fi
# Get all existing blocks created by current user
for row in $( textile file list -t $ID -l100000 | jq --arg ADDRESS "$ADDRESS" -r '.items[] | select(.user.address == $ADDRESS) | .files[0].file.hash' ); do
START=$( textile file get $row --content | jq 'select(.event == "start")')
# If we got any results back from Start events, we can skip creating it again
if [ ! -z "$START" ]; then
echo "Game on!"
exit
fi
done
# If game isn't started, let's kick it off!
START='{ "event": "start", "target": "'$ADDRESS'" }'
echo $START | jq '.' | textile file add -t "$ID"
echo "Game started! Tag you're it!"
else
echo "Error; Incorrect thread ID"
fi
#!/usr/bin/env bash
set -e # abort on any error
ID=${1?param missing - threadId.}
PEER=${2?param missing - peer.}
# textile threads get 12D3KooWRhJkorg7tKrp6qUHjrbCVXLYY5juYLyVhC2Wo6ncZyjG
ADDRESS=$(textile account address)
if THREAD=$(textile threads get $ID 2>&1); then
# Only the game creator should start the game
INITIATOR=$(echo $THREAD | jq -r .initiator)
TAGGED=$INITIATOR
KNOWN=$( textile thread peers -t $ID | jq --arg PEER "$PEER" '.items[] | select(.address == $PEER)')
# Check if Known is empty
if [ -z "${KNOWN// }" ]; then
echo "Error: Peer hasn't joined game yet"
echo 'hint: textile invites create --thread="'$ID'" --address="'$PEER'"'
exit
fi
for row in $( textile feed -l1000000 -t $ID | jq -c '.items | reverse | .[] | select(.payload."@type" == "/Files")' ); do
# Only the person that is Tagged creates updates we care about
AUTHOR=$(echo $row | jq -r '.payload.user.address')
if [ "$AUTHOR" == "$TAGGED" ]; then
HASH=$(echo $row | jq -r '.payload.files[0].file.hash')
TAG=$( textile file get $HASH --content | jq 'select(.event == "tag")' )
# We are just lookking for tag events
if [ ! -z "${TAG// }" ]; then
# Hand off the known tagged
TAGGED=$( textile file get $HASH --content | jq -r '.target' )
fi
fi
done
if [ ! "$TAGGED" == "$ADDRESS" ]; then
echo "Error: Sorry, wait your turn."
exit
fi
# All good, make the tag real
START='{ "event": "tag", "target": "'$PEER'" }'
echo $START | jq '.' | textile file add -t "$ID"
echo "Boom, you've made it to safety"
else
echo "Error: Incorrect thread ID"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.