Skip to content

Instantly share code, notes, and snippets.

@Yardanico

Yardanico/tt.zig Secret

Created September 6, 2019 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yardanico/ce7202ee0de5c512ce9c0cd9c895ae0b to your computer and use it in GitHub Desktop.
Save Yardanico/ce7202ee0de5c512ce9c0cd9c895ae0b to your computer and use it in GitHub Desktop.
const ReplayAction = struct {
time_since_previous: i64 = undefined,
x: f32 = undefined,
y: f32 = undefined,
key_mouse_set: i32 = undefined,
fn init(data: []const u8) !ReplayAction {
var res = ReplayAction{};
// w | x | y | z
var i: usize = 0;
var current_var: usize = 0;
while (i < data.len) {
const start = i;
while (i < data.len and isDigit(data[i])) : (i += 1) {}
// Parse the neccesary field
if (start != i) {
switch (current_var) {
0 => res.time_since_previous = try fmt.parseInt(i64, data[start..i], 10),
1 => res.x = try fmt.parseFloat(f32, data[start..i]),
2 => res.y = try fmt.parseFloat(f32, data[start..i]),
3 => res.key_mouse_set = try fmt.parseInt(i32, data[start..i], 10),
else => return error.InvalidReplayFile,
}
current_var += 1;
}
i += 1;
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment