-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cr
93 lines (77 loc) · 2.34 KB
/
app.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require "kemal"
require "./l_tree"
require "./battle_snake/**"
require "./strategy/**"
require "dotenv"
dev_env = Kemal.config.env == "development"
Dotenv.load if File.exists?(".env") && dev_env
test_env = Kemal.config.env == "test"
Dotenv.load(path: ".env.test") if File.exists?(".env.test") && test_env
require "../config/**"
require "./models/**"
require "./jobs/**"
add_context_storage_type(Strategy::Base)
persist_to_db = ENV["DISABLE_DB_PERSIST"]?.nil?
not_found_response = "
<p style='margin-top: 100px;'>
<center>
Strategy Not Found.
<br><br>
<a href='https://github.com/fdocr/crystalsnake'>Read usage details</a>
</center>
</p>
"
macro persist_turn!
PersistTurnJob.new(
path: env.request.path,
context_json: env.params.json.to_json
).enqueue if persist_to_db
end
def truncate_uuid(str)
"#{str[0..7]}...#{str[24..32]}"
end
before_all do |env|
next if env.request.path =~ /\/games?/
env.response.content_type = "application/json"
next if env.params.json.empty?
context = BattleSnake::Context.from_json(env.params.json.to_json)
strategy = Strategy.build(env.params.url["strategy"], context)
halt env, status_code: 404, response: not_found_response if strategy.nil?
env.set("strategy", strategy)
end
get "/" do |env|
env.redirect "https://github.com/fdocr/CrystalSnake"
end
# Battlesnake API Endpoints
get "/:strategy" do |env|
{
"apiversion": "1",
"author": "fdocr",
"color": ENV["SNAKE_COLOR"] ||= "#e3dada",
"head": ENV["SNAKE_HEAD"] ||= "default",
"tail": ENV["SNAKE_TAIL"] ||= "default",
"version": {{ `shards version "#{__DIR__}"`.chomp.stringify }}
}.to_json
end
post "/:strategy/start" do |env|
persist_turn!
end
post "/:strategy/move" do |env|
persist_turn!
move = env.get("strategy").as(Strategy::Base).move
res = { "move": move, "shout": "Moving #{move}!" }
res.to_json
end
post "/:strategy/end" do |env|
persist_turn!
end
# DB-persisted games
get "/games/:strategy" do |env|
strategy = env.params.url["strategy"]
offset = (env.params.query["page"]? || 0).to_i * 50
count = Turn.where { _path == "/#{strategy}/end" }.count
end_turns = Turn.where { _path == "/#{strategy}/end" }.limit(50).offset(offset).order(id: :desc)
render "src/views/games.ecr", "src/views/layout.ecr"
end
spawn { Mosquito::Runner.start } unless ENV["EMBED_WORKER"]?.nil?
Kemal.run