quake_parser 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6acd58b3769d942cd3c0f54164a89d01d8ff732968455c660fd16d9a23f1d798
4
- data.tar.gz: f65c02d52429694fdbece880d96bebd23bb6592ddcd5f4e5061eaaa9ef2cf6b7
3
+ metadata.gz: b0debba2b9d2fc5393e6c5389be892702f875d4660237b04983b40a094ccb32e
4
+ data.tar.gz: 35e618de009c2174e7484d23570c12476581b759343c1d5bca8f3beecb477089
5
5
  SHA512:
6
- metadata.gz: 6e8d737b01276c751fbb63c2699424a70d5ecda21bdf89fd302336c5848574e6ad3bd7e88f56093b9c88dbfc3ce5fee61101b7c66ebcf617d4bb4037d241ffe5
7
- data.tar.gz: e8a7a093fc223a761b7a45d02be27b442f4a51aafd187628bdf5b4dd028e13276b48a172d50f637c1e4c29530ee5b05c365ae0ab61881fbfe20bf6f9b6ca3564
6
+ metadata.gz: 2d1c97948995afe19fa7c3abbde6c105e3cb04fface10891151081cf636bc09de0245eddb0a8a86129c8d41be15c21ccecda1d8f120d2b100618c2e75c78ce30
7
+ data.tar.gz: 7468ad3d52329855024f88463aadd6f52852bccb17f64d5d155fd7a0a00702debf81952ce915de92b142b5f056d719b6044ed39d747e2ae26580d97e1499fe54
data/lib/game.rb ADDED
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module QuakeParser
6
+ class Game
7
+ attr_reader :kills, :kills_by_means, :players
8
+
9
+ DEATH_CAUSES = {
10
+ '0' => 'MOD_UNKNOWN', '1' => 'MOD_SHOTGUN', '2' => 'MOD_GAUNTLET', '3' => 'MOD_MACHINEGUN',
11
+ '4' => 'MOD_GRENADE', '5' => 'MOD_GRENADE_SPLASH', '6' => 'MOD_ROCKET', '7' => 'MOD_ROCKET_SPLASH',
12
+ '8' => 'MOD_PLASMA', '9' => 'MOD_PLASMA_SPLASH', '10' => 'MOD_RAILGUN', '11' => 'MOD_LIGHTNING',
13
+ '12' => 'MOD_BFG', '13' => 'MOD_BFG_SPLASH', '14' => 'MOD_WATER', '15' => 'MOD_SLIME',
14
+ '16' => 'MOD_LAVA', '17' => 'MOD_CRUSH', '18' => 'MOD_TELEFRAG', '19' => 'MOD_FALLING',
15
+ '20' => 'MOD_SUICIDE', '21' => 'MOD_TARGET_LASER', '22' => 'MOD_TRIGGER_HURT', '23' => 'MOD_NAIL',
16
+ '24' => 'MOD_CHAINGUN', '25' => 'MOD_PROXIMITY_MINE', '26' => 'MOD_KAMIKAZE', '27' => 'MOD_JUICED',
17
+ '28' => 'MOD_GRAPPLE'
18
+ }
19
+
20
+ def initialize(game)
21
+ @game = game
22
+ @total_kills = 0
23
+ @players = []
24
+ @kills = Hash.new(0)
25
+ @kills_by_means = Hash.new(0)
26
+ end
27
+
28
+ def add_player(name)
29
+ @players << name unless @players.include?(name)
30
+ @kills[name] = 0
31
+ end
32
+
33
+ def update_player_name(old_name, new_name)
34
+ @kills[new_name] = @kills.delete(old_name)
35
+ @players[@players.index(old_name)] = new_name
36
+ end
37
+
38
+ def increment_kill(killer, mod = '0')
39
+ @total_kills += 1
40
+ @kills[killer] += 1
41
+ @kills_by_means[DEATH_CAUSES[mod]] += 1
42
+ end
43
+
44
+ def decrement_kill(killed, mod = '0')
45
+ @total_kills += 1
46
+ @kills[killed] -= 1
47
+ @kills_by_means[DEATH_CAUSES[mod]] += 1
48
+ end
49
+
50
+ def convert_data_to_hash
51
+ {
52
+ 'game' => @game,
53
+ 'total_kills' => @total_kills,
54
+ 'players' => @players,
55
+ 'kills' => @kills,
56
+ 'kills_by_means' => @kills_by_means
57
+ }
58
+ end
59
+
60
+ def convert_data_to_json
61
+ JSON.pretty_generate(convert_data_to_hash)
62
+ end
63
+ end
64
+ end
data/lib/main.rb ADDED
@@ -0,0 +1,36 @@
1
+ require_relative 'parser'
2
+
3
+ module QuakeParser
4
+ class Main
5
+ def initialize(file_path)
6
+ raise Errno::ENOENT, "File not found or unreadable: #{file_path}" unless File.exist?(file_path)
7
+
8
+ @parser = QuakeParser::Parser.new(File.read(file_path))
9
+ end
10
+
11
+ def parser_and_print
12
+ @parser.parse_file
13
+ end
14
+
15
+ def get_games
16
+ games_data = []
17
+ @parser.games.each { |game| games_data << game.convert_data_to_hash if game }
18
+ games_data
19
+ end
20
+
21
+ def get_ranking
22
+ scores = Hash.new(0)
23
+
24
+ @parser.games.each do |game|
25
+ next unless game
26
+
27
+ game.kills.each do |key, value|
28
+ scores[key] += value
29
+ end
30
+ end
31
+
32
+ ranking = scores.sort_by { |_key, value| -value }.to_h
33
+ ranking
34
+ end
35
+ end
36
+ end
data/lib/parser.rb ADDED
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'game'
4
+
5
+ module QuakeParser
6
+ class Parser
7
+ attr_reader :games
8
+
9
+ WORLD = '1022'
10
+
11
+ def initialize(log)
12
+ @log = log
13
+ @games = []
14
+ end
15
+
16
+ def parse_file
17
+ @log.each_line { |line| parse_line(line) }
18
+ end
19
+
20
+ def start_game
21
+ @games << QuakeParser::Game.new(@games.size + 1)
22
+ @player_id_to_name_map = { WORLD => '<world>' }
23
+ end
24
+
25
+ def parse_line(line)
26
+ match = line.match(/\d+:\d+ \w+:/)
27
+ return unless match
28
+
29
+ task = match.string.split(' ')[1]
30
+ data = match.post_match.strip
31
+ parse_task(task, data)
32
+ end
33
+
34
+ def parse_task(task, data)
35
+ case task
36
+ when "InitGame:"
37
+ start_game
38
+ when "ClientUserinfoChanged:"
39
+ parse_update(data)
40
+ when "Kill:"
41
+ parse_kill(data)
42
+ when "ClientDisconnect:"
43
+ @player_id_to_name_map[data.match(/\d+/).to_s] = nil
44
+ end
45
+ end
46
+
47
+ def parse_update(data)
48
+ id = data.match(/\d+/).to_s
49
+ post_id = data.match(/\d+/).post_match.to_s
50
+ name = post_id.match(/\\.*?\\/).to_s[1..-2]
51
+
52
+ if @player_id_to_name_map[id]
53
+ @games.last.update_player_name(@player_id_to_name_map[id], name)
54
+ else
55
+ @games.last.add_player(name)
56
+ end
57
+ @player_id_to_name_map[id] = name
58
+ end
59
+
60
+ def parse_kill(data)
61
+ killer, killed, mod = data.match(/\d+ \d+ \d+/).to_s.split(" ")
62
+
63
+ if killer == WORLD
64
+ @games.last.decrement_kill(@player_id_to_name_map[killed], mod)
65
+ else
66
+ @games.last.increment_kill(@player_id_to_name_map[killer], mod)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'main'
2
+ require_relative 'parser'
3
+ require_relative 'game'
4
+
5
+ module QuakeParser
6
+ def self.run(file_path)
7
+ main = QuakeParser::Main.new(file_path)
8
+ main.parser_and_print
9
+ { games: main.get_games, ranking: main.get_ranking }
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quake_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
@@ -44,7 +44,11 @@ email:
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
- files: []
47
+ files:
48
+ - lib/game.rb
49
+ - lib/main.rb
50
+ - lib/parser.rb
51
+ - lib/quake_parser.rb
48
52
  homepage: https://github.com/yasmincrisostomo/quake_parser
49
53
  licenses:
50
54
  - MIT