-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.rb
executable file
·148 lines (127 loc) · 2.91 KB
/
server.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'digest/sha1'
require './lib/dungeon'
$salt = rand
$ids = {}
$num = 0
$rooms = create_dungeon
helpers do
def disconnect id
$ids.delete id
end
def cleanup_timeouts
$ids.each { |id,val| disconnect(id) if Time.now - val[:last_conn] > 5 }
end
def transform_params params
params.each do |param|
instance_variable_set("@" + param[0],param[1])
end
end
end
before do
transform_params params
end
get '/add_player/?' do
return "Name can't be blank" if @name.strip.empty?
return "Name is too long (> 25 characters)" if @name.length > 25
id = Digest::SHA1.hexdigest(($num + $salt).to_s)
$ids[id] = {:x => rand($rooms[0][:width]-40),
:y => rand($rooms[0][:height]-40),
:color => [@r.to_i,@g.to_i,@b.to_i],
:last_conn => Time.now,
:name => @name,
:num => $num,
:room => 0,
:width => 20,
:height => 20,
:rooms_visted => [0],
:angle => 0,
}
$num += 1
id
end
get '/list_ids/?' do
if @pass == "awesome"
$ids.keys.join "<br/>\n"
else
redirect "/404"
end
end
get '*/?' do
if @id == nil
return "No id"
elsif $ids[@id] == nil
return "Incorrect id"
else
@player = $ids[@id]
pass
end
end
get '/list_players/?' do
cleanup_timeouts
hash = {}
$ids.each_value do |val|
hash[val[:num]] = [val[:x], val[:y], val[:angle], val[:color], val[:name]] unless @player[:room] != val[:room]
end
hash[-1] = [$rooms[@player[:room]][:width]/2-10, $rooms[@player[:room]][:height]/2-10, 0, [255,200,100], ""] if $rooms[@player[:room]][:last]
hash.to_s
end
get '/num/?' do
@player[:num].to_s
end
get '/alive/?' do
@player[:last_conn] = Time.now
end
get '/x/?' do
@player[:x].to_s
end
get '/y/?' do
@player[:y].to_s
end
get '/update_player' do
limit = 10
unless @x == nil or @y == nil or @angle == nil
@x = @x.to_i
@x = limit if @x > limit
@x = -limit if @x < -limit
@player[:x] = @player[:x]+@x
@player[:x] = 0 if @player[:x] < 0
room = @player[:room]
@player[:x] = $rooms[room][:width]-@player[:width] if @player[:x] > $rooms[room][:width]-@player[:width]
@y = @y.to_i
@y = limit if @y > limit
@y = -limit if @y < -limit
@player[:y] = @player[:y]+@y
@player[:y] = 0 if @player[:y] < 0
room = @player[:room]
@player[:y] = $rooms[room][:height]-@player[:height] if @player[:y] > $rooms[room][:height]-@player[:height]
@player[:angle] = @angle.to_i
return "#{@player[:x]},#{@player[:y]},#{@player[:angle]}"
end
""
end
get '/room/?' do
return $rooms[@player[:room]].to_s
end
get '/change_room/:room/?' do
@room = params[:room]
unless params[:room] == nil
@player[:x], @player[:y] = $rooms[@player[:room]][:doors][@room.to_i][1]
@player[:room] = @room.to_i
@player[:rooms_visted] += [@room.to_i]
end
""
end
get '/map/?' do
visted = {}
@player[:rooms_visted].each do |room|
visted[room] = $rooms[room]
end
return visted.to_s
end
get '/disconnect/?' do
disconnect @id
""
end