From 7c732ef0063ad3a9a0d5d1c41ce04b1da5b3362d Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sun, 9 Jul 2023 13:39:39 +0000 Subject: [PATCH] Add wallet principal to user and player --- app/models/play.rb | 1 + app/models/player.rb | 17 +++++++ app/models/soft_token_principal.rb | 2 + app/models/user.rb | 17 +++++++ app/views/tables/_seat.html.haml | 1 + app/views/visitors/index.html.haml | 3 +- config/deploy/production_ext.rb | 46 +++++++++++++++++++ ...30709122324_create_soft_token_principal.rb | 8 ++++ db/schema.rb | 7 ++- 9 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 app/models/soft_token_principal.rb create mode 100644 config/deploy/production_ext.rb create mode 100644 db/migrate/20230709122324_create_soft_token_principal.rb diff --git a/app/models/play.rb b/app/models/play.rb index bd712e71..439e9b57 100644 --- a/app/models/play.rb +++ b/app/models/play.rb @@ -18,6 +18,7 @@ class Play < ActiveRecord::Base if self.game.over? self.game.winner_player_id = self.player.id self.game.status = "Player #{self.player.display_name} wins!" + # TODO Pay out crypto self.game.save end end diff --git a/app/models/player.rb b/app/models/player.rb index 40c85500..98d221a0 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -10,6 +10,23 @@ class Player < ActiveRecord::Base #validates :user, presence: true validates :game, presence: true + def principal + stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize + if stp.principal.nil? + #TODO use principal + `dfx identity use anonymous` + if `dfx identity use #{soft_token}; echo $?`.to_i == 255 + `dfx identity new #{soft_token} --disable-encryption` + `dfx identity use #{soft_token}` + @principal = `dfx identity get-principal` + `dfx identity use anonymous` + stp.principal = @principal + stp.save + end + end + return stp.principal + end + def inventory player_cards.where(play_id: nil).order(:value) end diff --git a/app/models/soft_token_principal.rb b/app/models/soft_token_principal.rb new file mode 100644 index 00000000..f69ce581 --- /dev/null +++ b/app/models/soft_token_principal.rb @@ -0,0 +1,2 @@ +class SoftTokenPrincipal < ActiveRecord::Base +end diff --git a/app/models/user.rb b/app/models/user.rb index 8ae9a6d1..7e36b25b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,23 @@ class User < ActiveRecord::Base validates :email, presence: true, uniqueness: true validates :soft_token, presence: true, uniqueness: true + def principal + stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize + if stp.principal.nil? + #TODO use principal + `dfx identity use anonymous` + if `dfx identity use #{soft_token}; echo $?`.to_i == 255 + `dfx identity new #{soft_token} --disable-encryption` + `dfx identity use #{soft_token}` + @principal = `dfx identity get-principal` + `dfx identity use anonymous` + stp.principal = @principal + stp.save + end + end + return stp.principal + end + def set_default_role self.role ||= :user end diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index 0fce0bce..ea31c409 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -26,6 +26,7 @@ .player{id: "player-#{seat.player.id}", class: @game.controlling_player == seat.player ? "taking-turn" : "", data: {soft_token: seat.player.soft_token} } .user-name= seat.player.display_name + .user-principal= seat.player.principal - if @game.started? .inventory-count Cards: diff --git a/app/views/visitors/index.html.haml b/app/views/visitors/index.html.haml index c9515540..b1446ed4 100644 --- a/app/views/visitors/index.html.haml +++ b/app/views/visitors/index.html.haml @@ -28,9 +28,8 @@ - 10.times do |i| .eyecandy = image_tag "ICYPEES-coin.png" -- -- Dir.glob("app/assets/images/cards/png/**").each do |card_img| +-# - Dir.glob("app/assets/images/cards/png/**").each do |card_img| - puts card_img = image_tag diff --git a/config/deploy/production_ext.rb b/config/deploy/production_ext.rb new file mode 100644 index 00000000..9ce66990 --- /dev/null +++ b/config/deploy/production_ext.rb @@ -0,0 +1,46 @@ +set :stage, :production + +set :nginx_server_name, '13.tendy.zone' # optional, defaults to :application + +# Simple Role Syntax +# ================== +# server in each group is considered to be the first +# unless any hosts have the primary property set. +role :app, %w{thirteen@13.tendy.zone} +# Supports bulk-adding hosts to roles, the primary +role :web, %w{thirteen@13.tendy.zone} +role :db, %w{thirteen@13.tendy.zone} + +# Extended Server Syntax +# ====================== +# This can be used to drop a more detailed server +# definition into the server list. The second argument +# something that quacks like a hash can be used to set +# extended properties on the server. +server '13.tendy.zone', user: 'thirteen', roles: %w{web app db}, my_property: :my_value + +set :ssh_options, { :verbose => :debug } + + + +# you can set custom ssh options +# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options +# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start) +# set it globally + set :ssh_options, { keys: %w(/home/bort/.ssh/id_ed25519), forward_agent: true, auth_methods: %w(publickey)} +# you can set custom ssh options +# it's possible to pass +# and/or per server +# server 'example.com', +# user: 'user_name', +# roles: %w{web app}, +# ssh_options: { +# user: 'user_name', # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: 'please use keys' +# } +# setting per server overrides global ssh_options + +# fetch(:default_env).merge!(rails_env: :production) diff --git a/db/migrate/20230709122324_create_soft_token_principal.rb b/db/migrate/20230709122324_create_soft_token_principal.rb new file mode 100644 index 00000000..3290f396 --- /dev/null +++ b/db/migrate/20230709122324_create_soft_token_principal.rb @@ -0,0 +1,8 @@ +class CreateSoftTokenPrincipal < ActiveRecord::Migration[5.2] + def change + create_table :soft_token_principals do |t| + t.string :soft_token + t.string :principal + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 14b1266f..f380fff9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_05_10_103647) do +ActiveRecord::Schema.define(version: 2023_07_09_122324) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -91,6 +91,11 @@ ActiveRecord::Schema.define(version: 2023_05_10_103647) do t.index ["user_id"], name: "index_seats_on_user_id" end + create_table "soft_token_principals", force: :cascade do |t| + t.string "soft_token" + t.string "principal" + end + create_table "tables", force: :cascade do |t| t.string "title" t.datetime "created_at"