Completed
Push — master ( 101101...2095ec )
by Oleksandr
01:53
created

_get_unit_type()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
cc 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4286
1
# -*- coding: utf-8 -*-
2
3 1
from il2fb.commons.spatial import Point2D
4 1
from il2fb.commons.structures import BaseStructure
5
6 1
from ..converters import to_belligerent, to_skill, to_unit_type
7 1
from . import CollectingParser
8
9
10 1
class ChiefsSectionParser(CollectingParser):
11
    """
12
    Parses ``Chiefs`` section.
13
    View :ref:`detailed description <chiefs-section>`.
14
    """
15
16 1
    def check_section_name(self, section_name):
17 1
        return section_name == "Chiefs"
18
19 1
    def parse_line(self, line):
20 1
        params = line.split()
21 1
        (uid, type__code, belligerent), params = params[0:3], params[3:]
22
23 1
        type_code, unit_code = type__code.split('.')
24 1
        unit_type = self._get_unit_type(type_code)
25
26 1
        unit = {
27
            'id': uid,
28
            'code': unit_code,
29
            'type': unit_type,
30
            'belligerent': to_belligerent(belligerent),
31
        }
32 1
        if params:
33 1
            hibernation, skill, recharge_time = params
34 1
            unit.update({
35
                'hibernation': int(hibernation),
36
                'skill': to_skill(skill),
37
                'recharge_time': float(recharge_time),
38
            })
39 1
        self.data.append(unit)
40
41 1
    @staticmethod
42
    def _get_unit_type(type_code):
43 1
        try:
44 1
            return to_unit_type(type_code)
45 1
        except:
46
            # Use original string as unit type
47 1
            return type_code
48
49 1
    def clean(self):
50 1
        return {'moving_units': self.data, }
51
52
53 1
class GroundRoutePoint(BaseStructure):
54 1
    __slots__ = ['pos', 'is_checkpoint', 'delay', 'section_length', 'speed', ]
55
56 1
    def __init__(self, pos, is_checkpoint, delay=None, section_length=None,
57
                 speed=None):
58 1
        self.pos = pos
59 1
        self.is_checkpoint = is_checkpoint
60 1
        self.delay = delay
61 1
        self.section_length = section_length
62 1
        self.speed = speed
63
64 1
    def __repr__(self):
65 1
        return "<GroundRoutePoint '{0};{1}'>".format(self.pos.x, self.pos.y)
66
67
68 1
class ChiefRoadSectionParser(CollectingParser):
69
    """
70
    Parses ``N_Chief_Road`` section.
71
    View :ref:`detailed description <chief-road-section>`.
72
    """
73 1
    id_suffix = "_Chief"
74 1
    section_suffix = "_Road"
75 1
    input_suffix = id_suffix + section_suffix
76 1
    output_prefix = 'route_'
77
78 1
    def check_section_name(self, section_name):
79 1
        if not section_name.endswith(self.input_suffix):
80 1
            return False
81 1
        unit_id = self._extract_unit_id(section_name)
82 1
        stop = unit_id.index(self.id_suffix)
83 1
        return unit_id[:stop].isdigit()
84
85 1
    def init_parser(self, section_name):
86 1
        super(ChiefRoadSectionParser, self).init_parser(section_name)
87 1
        unit_id = self._extract_unit_id(section_name)
88 1
        self.output_key = "{}{}".format(self.output_prefix, unit_id)
89
90 1
    def _extract_unit_id(self, section_name):
91 1
        stop = section_name.index(self.section_suffix)
92 1
        return section_name[:stop]
93
94 1
    def parse_line(self, line):
95 1
        params = line.split()
96 1
        pos, params = params[0:2], params[3:]
97
98 1
        args = {
99
            'pos': Point2D(*pos),
100
        }
101 1
        is_checkpoint = bool(params)
102 1
        args['is_checkpoint'] = is_checkpoint
103 1
        if is_checkpoint:
104 1
            args['delay'] = int(params[0])
105 1
            args['section_length'] = int(params[1])
106 1
            args['speed'] = float(params[2])
107
108 1
        point = GroundRoutePoint(**args)
109 1
        self.data.append(point)
110
111 1
    def clean(self):
112
        return {self.output_key: self.data}
113