Class: Termworld::Models::User
- Inherits:
-
Object
- Object
- Termworld::Models::User
- Defined in:
- lib/termworld/models/user.rb
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#positionx ⇒ Object
readonly
Returns the value of attribute positionx.
-
#positiony ⇒ Object
readonly
Returns the value of attribute positiony.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes_without_id ⇒ Object
- #bind_local_by_name ⇒ Object
- #create ⇒ Object
- #current_map ⇒ Object
- #delete_local(by: nil) ⇒ Object
-
#initialize(params) ⇒ User
constructor
A new instance of User.
- #initialize_position ⇒ Object
- #move(direction) ⇒ Object
- #save_local ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(params) ⇒ User
Returns a new instance of User.
31 32 33 |
# File 'lib/termworld/models/user.rb', line 31 def initialize(params) params.each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#created ⇒ Object (readonly)
Returns the value of attribute created.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def created @created end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def name @name end |
#positionx ⇒ Object (readonly)
Returns the value of attribute positionx.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def positionx @positionx end |
#positiony ⇒ Object (readonly)
Returns the value of attribute positiony.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def positiony @positiony end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
4 5 6 |
# File 'lib/termworld/models/user.rb', line 4 def updated @updated end |
Class Method Details
.all ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/termworld/models/user.rb', line 16 def all res = $api_client.call_auth(:get, '/users') return [] if res.code != 200 JSON.parse(res.body, {symbolize_names: true})[:users].map do |user| self.new(user) end end |
.all_local ⇒ Object
24 25 26 27 28 |
# File 'lib/termworld/models/user.rb', line 24 def all_local $db[:users].all.map do |user| self.new(user) end end |
.create_table ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/termworld/models/user.rb', line 6 def create_table $db.create_table :users do primary_key :id String :name String :current_map_name Integer :positionx Integer :positiony end end |
Instance Method Details
#attributes ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/termworld/models/user.rb', line 79 def attributes { id: @id, name: @name, current_map_name: @current_map_name, positionx: @positionx, positiony: @positiony, } end |
#attributes_without_id ⇒ Object
88 89 90 |
# File 'lib/termworld/models/user.rb', line 88 def attributes_without_id attributes.reject { |k, _| k == :id } end |
#bind_local_by_name ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/termworld/models/user.rb', line 35 def bind_local_by_name record = $db[:users].where(name: @name).first return false if record.nil? record.reject { |key, _| key == :name }.each { |key, value| instance_variable_set("@#{key}", value) } true end |
#create ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/termworld/models/user.rb', line 44 def create validate return false unless @errors.empty? res = $api_client.call_auth(:post, '/users', {name: @name}) return false if res.code != 201 true end |
#current_map ⇒ Object
121 122 123 124 |
# File 'lib/termworld/models/user.rb', line 121 def current_map @current_map ||= Object.const_get("Termworld::Resources::Maps::#{@current_map_name.capitalize}").new @current_map end |
#delete_local(by: nil) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/termworld/models/user.rb', line 62 def delete_local(by: nil) if by == :id $db[:users].where(id: @id).delete elsif by == :name $db[:users].where(name: @name).delete end end |
#initialize_position ⇒ Object
92 93 94 95 96 |
# File 'lib/termworld/models/user.rb', line 92 def initialize_position @current_map_name = 'town' @positionx = 0 @positiony = 0 end |
#move(direction) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/termworld/models/user.rb', line 98 def move(direction) supposed_position = {x: @positionx, y: @positiony} case direction when :up supposed_position[:y] -= 1 when :down supposed_position[:y] += 1 when :left supposed_position[:x] -= 1 when :right supposed_position[:x] += 1 else return false end return false if supposed_position.any? { |_, v| v < 0 } chip = current_map.get_chip(supposed_position) return false if chip.nil? || !chip.movable @positionx = supposed_position[:x] @positiony = supposed_position[:y] save_local true end |
#save_local ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/termworld/models/user.rb', line 51 def save_local validate return false unless @errors.empty? if record = $db[:users].where(id: @id).first @created, @updated = false, true $db[:users].where(id: @id).update(attributes_without_id) else @created, @updated = true, false $db[:users].insert(attributes) end end |
#validate ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/termworld/models/user.rb', line 70 def validate @errors = [] if @name.nil? || @name.empty? @errors << "Name is required" elsif !@name.scan(/[^0-9a-z]+/i).empty? @errors << "Name must be only alphanumeric" end end |