m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--curling.rb11
-rw-r--r--engine.rb5
2 files changed, 14 insertions, 2 deletions
diff --git a/curling.rb b/curling.rb
index 4b03e8d..e688a01 100644
--- a/curling.rb
+++ b/curling.rb
@@ -61,7 +61,16 @@ To restart the game, run `rm k` to remove the cookie that stores your game state
end
get '/:command' do |command|
- engine = Hex.new cookies.to_h
+ begin
+ engine = Hex.new cookies.to_h
+ rescue InvalidChecksumError
+ halt 422, <<~MSG
+ Invalid state, checksum does not match!
+ If you lost your last valid game state, you'll have to remove your current
+ cookie and start over.
+ MSG
+ end
+
engine.step command
engine.state_h.each_pair do |key, value|
cookies[key] = value
diff --git a/engine.rb b/engine.rb
index b04e2f2..7b9ddf0 100644
--- a/engine.rb
+++ b/engine.rb
@@ -1,3 +1,6 @@
+class InvalidChecksumError < StandardError
+end
+
class Engine
def initialize hash
validate_checksum hash
@@ -6,7 +9,7 @@ class Engine
def validate_checksum hash
if !checksum_valid? hash
- throw 'Invalid state, checksum does not match!'
+ raise InvalidChecksumError
end
end