summaryrefslogtreecommitdiff
path: root/backend/src/Models/RangedWeapon.zig
blob: 14884d29b912052eeef0c207fe2605c2a10ff172 (plain)
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
const std = @import("std");
const pg = @import("pg");

pub const RangedWeaponType = struct {
    Id: []const u8,
    Name: []const u8,

    WeaponType: []const u8,
    Accuracy: i32,
    Concealability: []const u8,
    Avaliability: []const u8,
    Damage: []const u8,
    Ammunition: []const u8,
    NumberOfShots: u31,
    RateOfFire: u32,
    Reliability: []const u8,

    CreatedAt: i64,
    UpdatedAt: i64,

    pub fn GetCompactNotation(self: *RangedWeaponType, separator: u8) ![]const u8 {
        var b: [64]u8 = undefined;
        return try std.fmt.bufPrint(&b, 
            "{s}{c}{d}{c}{s}{c}{s}{c}{s}({s}){c}{d}{c}{d}{c}{s}", 
            .{
                self.WeaponType, separator,
                self.Accuracy, separator,
                self.Concealability, separator,
                self.Avaliability, separator,
                self.Damage,
                self.Ammunition, separator,
                self.NumberOfShots, separator,
                self.RateOfFire, separator,
                self.Reliability,
            });
    }

    // Parses the db row and returns the result. You are expected to use the
    // actual DB schema and use * as a fields selector
    pub fn Map(row: *const pg.Row) !RangedWeaponType {
        return RangedWeaponType {
            .Id = try row.get([]const u8, 0),
            .Name = try row.get([]const u8, 1),
            .WeaponType = try row.get([]const u8, 2),
            .Accuracy = try row.get(i32, 3),
            .Concealability = try row.get([]const u8, 4),
            .Avaliability = try row.get([]const u8, 5),
            .Damage = try row.get([]const u8, 6),
            .Ammunition = try row.get([]const u8, 7),
            .NumberOfShots = @intCast(try row.get(i32, 8)),
            .RateOfFire = @intCast(try row.get(i32, 9)),
            .Reliability = try row.get([]const u8, 10),
            .CreatedAt = try row.get(i64, 11),
            .UpdatedAt = try row.get(i64, 12),
        };
    }

};

// ==================== tests ====================

fn getTestType() RangedWeaponType {
    return .{
        .Id = "any",
        .Name = "any",
        .WeaponType = "P",
        .Accuracy = -1,
        .Concealability = "P",
        .Avaliability = "E",
        .Damage = "1D6",
        .Ammunition = "5mm",
        .NumberOfShots = 8,
        .RateOfFire = 2,
        .Reliability = "ST",
        .CreatedAt = 0,
        .UpdatedAt = 0,
    };
}

test "CompactNotation" {
    var testType = getTestType();

    try std.testing.expectEqualStrings(
        "P/-1/P/E/1D6(5mm)/8/2/ST", 
        try testType.GetCompactNotation('/'));
}