Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client/client.rb

Overview

Networking part of the game clientside

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console, cfg, gui) ⇒ Client



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
# File 'lib/client/client.rb', line 20

def initialize(console, cfg, gui)
  @id = nil
  @tick = 0
  @state = STATE_MENU
  @cfg = cfg
  @gui = gui

  @console = console
  @console.log "LOAD #{@s}"

  @s = nil # network socket (set in connect() method)

  @recording_ticks = []
  @recording_ticks_len = 0
  @recording_file = 'autorec.txt'
  @is_recording = false

  @server_version = nil

  @game_map = nil

  # @force_send
  # used by client to send data regardless of what the gui
  # wanted to send
  @force_send = nil

  # state variables
  @req_playerlist = Time.now - 8

  # return values
  @players = []
  @flags = { skip: false, state: @state, gamestate: 'g', id: nil }
  @extra = nil # currently only used for download progress array
end

Instance Attribute Details

#game_mapObject (readonly)

Returns the value of attribute game_map.



18
19
20
# File 'lib/client/client.rb', line 18

def game_map
  @game_map
end

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/client/client.rb', line 18

def id
  @id
end

#server_versionObject (readonly)

Returns the value of attribute server_version.



18
19
20
# File 'lib/client/client.rb', line 18

def server_version
  @server_version
end

#stateObject (readonly)

Returns the value of attribute state.



18
19
20
# File 'lib/client/client.rb', line 18

def state
  @state
end

Instance Method Details

#connect(ip, port) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/client/client.rb', line 63

def connect(ip, port)
  reset
  @s = TCPSocket.open(ip, port)
  @s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) # nagle's algorithm-
  update_state(STATE_CONNECTING)
  start_recording if @cfg.data['autorecord']
end

#disconnectObject



71
72
73
74
75
76
77
78
79
# File 'lib/client/client.rb', line 71

def disconnect
  return if @state == STATE_MENU
  return if @s.nil?

  @console.log 'disconnecting from server.'
  @s.close
  @s = nil
  reset
end

#load_recording(recording_file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/client/client.rb', line 81

def load_recording(recording_file)
  recording_file = "#{@cfg.chichilku3_dir}recordings/#{recording_file}"
  @recording_ticks = []
  @tick = 0
  update_state(STATE_REC_PLAYBACK)
  File.readlines(recording_file).each do |data|
    @recording_ticks << data[0..-2] # cut off newline
  end
  # TODO: check if this .length lookup eats performance in ruby
  @recording_ticks_len = @recording_ticks.length
  @console.log "loaded recording containing #{@recording_ticks.size} ticks"
end

#recording_playback_tickObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/client/client.rb', line 108

def recording_playback_tick
  if @recording_ticks_len <= @tick
    @console.log 'finished playing back recording'
    update_state(STATE_MENU)
    return [[], @flags, nil]
  end
  data = @recording_ticks[@tick]

  @tick += 1
  @flags[:skip] = false

  # save protocol and cut it off
  msg = handle_protocol(data[0].to_i, data[1], data[2..-1])
  [@players, @flags, msg, [@tick, @recording_ticks_len]]
end

#recording_record_tick(data) ⇒ Object



101
102
103
104
105
106
# File 'lib/client/client.rb', line 101

def recording_record_tick(data)
  return unless @is_recording

  recording_file = "#{@cfg.chichilku3_dir}recordings/#{@recording_file}"
  IO.write(recording_file, "#{data}\n", mode: 'a')
end

#resetObject



55
56
57
58
59
60
61
# File 'lib/client/client.rb', line 55

def reset
  @id = nil
  @tick = 0
  @state = STATE_MENU
  @players = []
  @flags = { skip: false, state: @state, gamestate: 'g', id: nil }
end

#start_recordingObject



94
95
96
97
98
99
# File 'lib/client/client.rb', line 94

def start_recording
  @recording_file = 'autorec.txt'
  rec_file = "#{@cfg.chichilku3_dir}recordings/#{@recording_file}"
  @is_recording = true
  File.delete(rec_file) if File.exist? rec_file
end

#tick(client_data, protocol, tick) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/client/client.rb', line 124

def tick(client_data, protocol, tick)
  return nil if @state == STATE_MENU

  # sleep(1)
  @tick = tick
  @flags[:skip] = false

  # send data to the server
  send_data(client_data, protocol)

  # get data from the server + implicit return
  data = fetch_server_data
  return nil if data.nil?

  recording_record_tick(data)

  # save protocol and cut it off
  msg = handle_protocol(data[0].to_i, data[1], data[2..-1])
  [@players, @flags, msg, @extra]
end