Module: Alchemist::EventMachine::ClientProtocol

Defined in:
lib/alchemist/event_machine/client_protocol.rb

Instance Method Summary collapse

Instance Method Details

#appearObject



28
29
30
# File 'lib/alchemist/event_machine/client_protocol.rb', line 28

def appear
  send_data "appear\n"
end

#create(element) ⇒ Object



85
86
87
# File 'lib/alchemist/event_machine/client_protocol.rb', line 85

def create(element)
  send_data "create #{element}\n"
end

#describe(symbol) ⇒ Object



93
94
95
# File 'lib/alchemist/event_machine/client_protocol.rb', line 93

def describe(symbol)
  send_data "describe #{symbol}\n"
end

#dispatch_command(command, data = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/alchemist/event_machine/client_protocol.rb', line 146

def dispatch_command(command, data = nil)
  case command
  when "Welcome" then handle_welcome
  when "hello" then handle_hello
  when "appeared" then handle_appeared
  when "see" then handle_see data
  when "inventory" then handle_inventory data
  when "basics" then handle_basics data
  when "compounds" then handle_compounds data
  when "error" then handle_error data
  when "messages" then handle_messages data
  when "avatars" then handle_avatars data
  when "location" then handle_location data
  when "element" then handle_element data
  when "noelement" then handle_noelement data
  end
end

#eastObject



73
74
75
# File 'lib/alchemist/event_machine/client_protocol.rb', line 73

def east
  move 'east'
end

#element(symbol, name) ⇒ Object



89
90
91
# File 'lib/alchemist/event_machine/client_protocol.rb', line 89

def element(symbol, name)
  send_data "element #{symbol} #{name}\n"
end

#forge(ingred_1, ingred_2, result) ⇒ Object



97
98
99
# File 'lib/alchemist/event_machine/client_protocol.rb', line 97

def forge(ingred_1, ingred_2, result)
  send_data "forge #{ingred_1} #{ingred_2} #{result}\n"
end

#formulate(ingred_1, ingred_2, result, name) ⇒ Object



101
102
103
# File 'lib/alchemist/event_machine/client_protocol.rb', line 101

def formulate(ingred_1, ingred_2, result, name)
  send_data "formulate #{ingred_1} #{ingred_2} #{result} #{name}\n"
end

#handle_appearedObject



9
# File 'lib/alchemist/event_machine/client_protocol.rb', line 9

def handle_appeared;end

#handle_avatars(message) ⇒ Object



15
# File 'lib/alchemist/event_machine/client_protocol.rb', line 15

def handle_avatars(message);end

#handle_basics(message) ⇒ Object



12
# File 'lib/alchemist/event_machine/client_protocol.rb', line 12

def handle_basics(message);end

#handle_compounds(message) ⇒ Object



13
# File 'lib/alchemist/event_machine/client_protocol.rb', line 13

def handle_compounds(message);end

#handle_element(message) ⇒ Object



18
# File 'lib/alchemist/event_machine/client_protocol.rb', line 18

def handle_element(message);end

#handle_error(message) ⇒ Object



17
# File 'lib/alchemist/event_machine/client_protocol.rb', line 17

def handle_error(message);end

#handle_helloObject



8
# File 'lib/alchemist/event_machine/client_protocol.rb', line 8

def handle_hello;end

#handle_inventory(message) ⇒ Object



11
# File 'lib/alchemist/event_machine/client_protocol.rb', line 11

def handle_inventory(message);end

#handle_location(message) ⇒ Object



16
# File 'lib/alchemist/event_machine/client_protocol.rb', line 16

def handle_location(message);end

#handle_messages(message) ⇒ Object



14
# File 'lib/alchemist/event_machine/client_protocol.rb', line 14

def handle_messages(message);end

#handle_noelement(message) ⇒ Object



19
# File 'lib/alchemist/event_machine/client_protocol.rb', line 19

def handle_noelement(message);end

#handle_see(message) ⇒ Object



10
# File 'lib/alchemist/event_machine/client_protocol.rb', line 10

def handle_see(message);end

#handle_welcomeObject

Clients should implement the various handle_* methods to take action in response to serve updates



7
# File 'lib/alchemist/event_machine/client_protocol.rb', line 7

def handle_welcome;end

#login(name) ⇒ Object

Clients can call these methods to send commands to the server



23
24
25
26
# File 'lib/alchemist/event_machine/client_protocol.rb', line 23

def (name)
  send_data name
  send_data "\n"
end

#lookObject



36
37
38
# File 'lib/alchemist/event_machine/client_protocol.rb', line 36

def look
  send_data "look\n"
end

#message(number, string) ⇒ Object



105
106
107
# File 'lib/alchemist/event_machine/client_protocol.rb', line 105

def message(number, string)
  send_data "message #{number} #{string}\n"
end

#move(direction) ⇒ Object



60
61
62
63
# File 'lib/alchemist/event_machine/client_protocol.rb', line 60

def move(direction)
  send_data direction
  send_data "\n"
end

#northObject



65
66
67
# File 'lib/alchemist/event_machine/client_protocol.rb', line 65

def north
  move 'north'
end

#put(element) ⇒ Object



81
82
83
# File 'lib/alchemist/event_machine/client_protocol.rb', line 81

def put(element)
  send_data "put #{element}\n"
end

#readObject



40
41
42
# File 'lib/alchemist/event_machine/client_protocol.rb', line 40

def read
  send_data "read\n"
end

#receive_data(data) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/alchemist/event_machine/client_protocol.rb', line 113

def receive_data(data)
  @buffer ||= ''
  @buffer << data

  while @buffer =~ /^.*?\n/
    line = $&
    receive_line line
    @buffer[0,line.length] = ''
  end
end

#receive_line(line) ⇒ Object

Command framing and dispatch



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/alchemist/event_machine/client_protocol.rb', line 126

def receive_line(line)
  if @lines_to_read
    @response << line
    @lines_to_read -= 1

    if @lines_to_read <= 0
      @lines_to_read = nil
      dispatch_command @command, @response
      @command = nil
      @response = nil
    end
  elsif line =~ /^(see|messages|avatars) (\d+)$/
    @command = $1
    @lines_to_read = $2.to_i
    @response = ''
  else
    dispatch_command *line.split(' ',2)
  end
end

#request_basicsObject



48
49
50
# File 'lib/alchemist/event_machine/client_protocol.rb', line 48

def request_basics
  send_data "basics\n"
end

#request_compoundsObject



52
53
54
# File 'lib/alchemist/event_machine/client_protocol.rb', line 52

def request_compounds
  send_data "compounds\n"
end

#request_inventoryObject



44
45
46
# File 'lib/alchemist/event_machine/client_protocol.rb', line 44

def request_inventory
  send_data "inventory\n"
end

#request_locationObject



32
33
34
# File 'lib/alchemist/event_machine/client_protocol.rb', line 32

def request_location
  send_data "location\n"
end

#southObject



69
70
71
# File 'lib/alchemist/event_machine/client_protocol.rb', line 69

def south
  move 'south'
end

#takeObject



109
110
111
# File 'lib/alchemist/event_machine/client_protocol.rb', line 109

def take
  send_data "take\n"
end

#westObject



77
78
79
# File 'lib/alchemist/event_machine/client_protocol.rb', line 77

def west
  move 'west'
end

#whoObject



56
57
58
# File 'lib/alchemist/event_machine/client_protocol.rb', line 56

def who
  send_data "who\n"
end