-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_setup.rb
104 lines (93 loc) · 2.18 KB
/
db_setup.rb
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
94
95
96
97
98
99
100
101
102
require 'rubygems'
require 'ostruct'
require 'couchrest'
require 'json'
if ARGV.length==1
cw_config=JSON.parse(File.read(ARGV[0]))
else
cw_config={
"_id"=> "config",
"arena"=> {
"max_program_length"=> 40,
"max_player_threads"=> 64,
"min_dist"=> 100,
"num_cycles"=> 10000,
"num_rounds"=> 50,
"num_cells"=> 2400
},
"interpreter"=> {
"num_threads"=> 2,
"blacklist"=>[]
},
"output"=>{
"file"=>"www/scores.html",
"generate_every_update" => "false",
"round_time" => 15.0
}
}
end
$config=OpenStruct.new
$config.db=CouchRest.database!("http://127.0.0.1:5984/codewars")
begin
$config.db.delete!
$config.db=CouchRest.database!("http://127.0.0.1:5984/codewars")
rescue
end
begin
if res=$config.db.get("_design/codewars")
$config.db.delete_doc(res)
end
rescue
end
$config.db.save_doc(cw_config)
$config.db.save_doc({
"_id" => "_design/codewars",
:views => {
:scores=>{
:map => %{
function(doc){
if (doc.scores){
for(var w in doc.scores){
emit([doc._id,w],doc.scores[w])
}
}
}
}
},
:active_bots=>{
:map => %{
function(doc){
if(doc.code){
emit([doc.group,doc._id],{"_id":doc._id,"timestamp":doc.timestamp})
}
}
},
:reduce=>%{
function(key,values,rereduce){
var max_timestamp=0
var retval=0
for(var v in values){
if(values[v].timestamp>max_timestamp){
max_timestamp=values[v].timestamp
max_id={"_id":values[v]._id,"timestamp":values[v].timestamp}
}
}
return(max_id);
}
}
},
:highscores=>{
:map=>%{
function(doc){
if (doc.scores){
var sum=0
for(var other in doc.scores){
sum+=3*doc.scores[other]["wins"]+doc.scores[other]["ties"]
}
emit(sum,doc._id)
}
}
}
}
}#end views
})