summaryrefslogtreecommitdiff
path: root/backend/src/Models/RangedWeapon.zig
blob: 9b654096b41101a5d2047dbbd24278edf63d1258 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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: u32,
    RateOfFire: u32,
    Reliability: []const u8,

    CreatedAt: i64,
    UpdatedAt: i64,

    // The source of the weapom. For example, corebook or an adventure
    Origin: []const u8,

    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),
            .Origin = try row.get([]const u8, 13),
        };
    }

    // 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 MapWithAlloc(allocator: std.mem.Allocator, row: *const pg.Row) !RangedWeaponType {
        return RangedWeaponType {
            .Id = try allocator.dupe(u8, try row.get([]const u8, 0)),
            .Name = try allocator.dupe(u8, try row.get([]const u8, 1)),
            .WeaponType = try allocator.dupe(u8, try row.get([]const u8, 2)),
            .Accuracy = try row.get(i32, 3),
            .Concealability = try allocator.dupe(u8, try row.get([]const u8, 4)),
            .Avaliability = try allocator.dupe(u8, try row.get([]const u8, 5)),
            .Damage = try allocator.dupe(u8, try row.get([]const u8, 6)),
            .Ammunition = try allocator.dupe(u8, try row.get([]const u8, 7)),
            .NumberOfShots = @intCast(try row.get(i32, 8)),
            .RateOfFire = @intCast(try row.get(i32, 9)),
            .Reliability = try allocator.dupe(u8, try row.get([]const u8, 10)),
            .CreatedAt = try row.get(i64, 11),
            .UpdatedAt = try row.get(i64, 12),
            .Origin = try allocator.dupe(u8, try row.get([]const u8, 13)),
        };
    }

};


// The full model is causing error.MissingFields if we don't specify readonly
// CreatedAt and UpdatedAt fields
pub const RequestBody = 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,
    Origin: []const u8,

    pub fn ToModel(this: RequestBody) RangedWeaponType {
        return RangedWeaponType {
            .Id = this.Id,
            .Name = this.Name,
            .WeaponType = this.WeaponType,
            .Accuracy = this.Accuracy,
            .Concealability = this.Concealability,
            .Avaliability = this.Avaliability,
            .Damage = this.Damage,
            .Ammunition = this.Ammunition,
            .NumberOfShots = this.NumberOfShots,
            .RateOfFire = this.RateOfFire,
            .Reliability = this.Reliability,
            .CreatedAt = 0,
            .UpdatedAt = 0,
            .Origin = this.Origin,
        };
    }
};

// ==================== 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,
        .Origin = "corebook",
    };
}

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

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

test "Request to model" {
    var req: RequestBody = .{
        .Id = "any",
        .Name = "any",
        .WeaponType = "P",
        .Accuracy = -1,
        .Concealability = "P",
        .Avaliability = "E",
        .Damage = "1D6",
        .Ammunition = "5mm",
        .NumberOfShots = 8,
        .RateOfFire = 2,
        .Reliability = "ST",
        .Origin = "corebook"
    };

    const expected = getTestType();
    const result = req.ToModel();

    try std.testing.expectEqualStrings(expected.Id, result.Id);
    try std.testing.expectEqualStrings(expected.Name, result.Name);
    try std.testing.expectEqual(expected.Accuracy, result.Accuracy);
    try std.testing.expectEqual(0, result.CreatedAt);
    try std.testing.expectEqual(0, result.UpdatedAt);
}