game_validator 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/game_validator.gemspec +1 -1
- data/lib/game_validator.rb +3 -2
- data/lib/game_validator/validator.rb +2 -2
- data/lib/game_validator/validator/base.rb +15 -3
- data/lib/game_validator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd90aaf91b77cf3e07547bab0e7b2ab215d91247a61dd1b2db3cc1970a6ddc51
|
4
|
+
data.tar.gz: d4f13f99c8cc2fd30b1fb6cbc12d2a7dec41d69030cb73b7aaf6f0367da74248
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e1f12c87026ecbe1386fde7ff5a1c8afb8fa6340b952a08707a2fab65124a01d984fe50a61f3d2259cf21b8d12ae652fd6eba4121b5bb4a062eb8efda424b55
|
7
|
+
data.tar.gz: 7d06c2c9f5939529f97b0d084da7021233c0598e0b4218aeb8570849edf20b118f00dbed51f3e18faed079e8199fe04c6e11b05d6dc25fa81429c3cba89a5715
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -79,9 +79,10 @@ full_validator = {
|
|
79
79
|
In this fashion, you create the full validator as follows
|
80
80
|
```
|
81
81
|
GameValidator::Validator::new(
|
82
|
-
validate_player_action_and_user: GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1),
|
82
|
+
validate_player_action_and_user: GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1, last_action_id: 1),
|
83
83
|
full_validator_for: full_validator)
|
84
84
|
```
|
85
|
+
Note the last_action_id option, to make sure that you are validating against the most recent action in the game.
|
85
86
|
|
86
87
|
You can also wrap successful results from the validator, using GameValidator::Validator::ValidateToAction and GameValidator::Validator::Result
|
87
88
|
```
|
@@ -99,7 +100,6 @@ GameValidator::Validator::ValidateToAction::new(
|
|
99
100
|
execute: DoStuff::new)})
|
100
101
|
```
|
101
102
|
|
102
|
-
|
103
103
|
## Development
|
104
104
|
|
105
105
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/game_validator.gemspec
CHANGED
@@ -2,7 +2,7 @@ lib = File.expand_path("lib", __dir__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
require "game_validator/version"
|
4
4
|
|
5
|
-
Gem::Specification.new 'game_validator', '0.
|
5
|
+
Gem::Specification.new 'game_validator', '0.6.0' do |spec|
|
6
6
|
spec.name = 'game_validator'
|
7
7
|
spec.version = GameValidator::VERSION
|
8
8
|
spec.authors = ['Colin Horner']
|
data/lib/game_validator.rb
CHANGED
@@ -8,8 +8,9 @@ Types::Callable = Types.Constructor(Method) do |value|
|
|
8
8
|
callable.call(value) # raises Dry::Types::ConstraintError if object doesn't respond to #call
|
9
9
|
value.is_a?(Method) || value.is_a?(Proc) ? value : value.method(:call)
|
10
10
|
end
|
11
|
-
Types::ArrayOfCallable =
|
12
|
-
Types::ArrayOfStrictInteger =
|
11
|
+
Types::ArrayOfCallable = Types::Array::of(Types::Callable)
|
12
|
+
Types::ArrayOfStrictInteger = Types::Array::of(Types::Strict::Integer)
|
13
|
+
Types::ArrayOfString = Types::Array::of(Types::String)
|
13
14
|
|
14
15
|
require 'game_validator/version'
|
15
16
|
require 'game_validator/validator'
|
@@ -6,9 +6,9 @@ require 'game_validator/validator/full_validator_coercer'
|
|
6
6
|
module GameValidator
|
7
7
|
class Validator
|
8
8
|
extend Dry::Initializer
|
9
|
-
option :validate_player_action_and_user, type: Types
|
9
|
+
option :validate_player_action_and_user, type: Types::Callable
|
10
10
|
option :full_validator_for, type: Types::Hash
|
11
|
-
option :build_failure, type: Types
|
11
|
+
option :build_failure, type: Types::Callable,
|
12
12
|
default: ->{GameValidator::Validator::Result::Failure::Builder::new}
|
13
13
|
|
14
14
|
def call(action_hash:, user:)
|
@@ -7,12 +7,14 @@ Dry::Validation.load_extensions(:monads)
|
|
7
7
|
module GameValidator
|
8
8
|
class Validator
|
9
9
|
class Base < Dry::Validation::Contract
|
10
|
-
option :legal_options, type: Types::
|
11
|
-
option :
|
10
|
+
option :legal_options, type: Types::ArrayOfString
|
11
|
+
option :current_player_id, type: Types::Coercible::Integer
|
12
|
+
option :last_action_id, type: Types::Coercible::Integer
|
12
13
|
|
13
14
|
params do
|
14
15
|
required(:player_action).filled(:string)
|
15
16
|
required(:user).filled(Types.Interface(:id, :admin?))
|
17
|
+
required(:last_action_id).filled(:integer)
|
16
18
|
end
|
17
19
|
|
18
20
|
rule(:player_action) do
|
@@ -22,10 +24,20 @@ module GameValidator
|
|
22
24
|
end
|
23
25
|
|
24
26
|
rule(:user) do
|
25
|
-
if !values[:user].id.eql?(
|
27
|
+
if !values[:user].id.eql?(current_player_id) && !values[:user].admin?
|
26
28
|
key.failure({text: 'Not logged in', status: 401})
|
27
29
|
end
|
28
30
|
end
|
31
|
+
|
32
|
+
rule(:last_action_id) do
|
33
|
+
if values[:last_action_id] < last_action_id
|
34
|
+
key.failure({text: "last_action_id '#{values[:last_action_id]}' is obsolete",status: 409})
|
35
|
+
end
|
36
|
+
|
37
|
+
if values[:last_action_id] > last_action_id
|
38
|
+
key.failure({text: "last_action_id '#{values[:last_action_id]}' not a match",status: 400})
|
39
|
+
end
|
40
|
+
end
|
29
41
|
end
|
30
42
|
end
|
31
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: game_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Horner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|