Skip to content

Commit

Permalink
Initial work to track the high score
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 29, 2023
1 parent 54284e4 commit 0332119
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
112 changes: 112 additions & 0 deletions src/Crypto.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const std = @import("std");
const raylib = @import("raylib");
const Shared = @import("./Shared.zig").Shared;

pub const Crypto = struct {
const key = [_]u8{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};

const AES = std.crypto.core.aes.Aes256;

pub inline fn GetIV() [16]u8 {
var buf: [16]u8 = undefined;
Shared.Random.Get().bytes(buf[0..]);
return buf;
}

pub inline fn Encrypt(
iv: [16]u8,
value: []const u8,
out: []u8,
) void {
var _iv: [16]u8 = iv;
std.mem.copy(u8, out[0..16], _iv[0..]);

var ctx = AES.initEnc(key[0..].*);

var i: usize = 0;
while (i < value.len) : (i += 16) {
var block: [16]u8 = undefined;
std.mem.copy(u8, block[0..], value[i .. i + 16]);

// block[i] xor iv
var j: usize = 0;
while (j < 16) : (j += 1) {
block[j] ^= _iv[j];
}

var block2: [16]u8 = undefined;
ctx.encrypt(&block2, &block);
std.mem.copy(u8, out[i + 16 .. i + 32], block2[0..]);
std.mem.copy(u8, _iv[0..], block2[0..]);
}
}

pub inline fn Decrypt(
value: []const u8,
out: []u8,
) void {
var iv: [16]u8 = value[0..16].*;
var ctx = AES.initDec(key[0..].*);

var i: usize = 16;
while (i < value.len) : (i += 16) {
var block: [16]u8 = undefined;
std.mem.copy(u8, block[0..], value[i .. i + 16]);
var block2: [16]u8 = undefined;

ctx.decrypt(&block2, &block);

// block[i] xor iv
var j: usize = 0;
while (j < 16) : (j += 1) {
block2[j] ^= iv[j];
}

std.mem.copy(u8, out[i - 16 .. i], block2[0..]);
std.mem.copy(u8, iv[0..], block[0..]);
}
}
};

test "round trip" {
Shared.Random.init();

const iv = Crypto.GetIV();
var encryptedOut: [32]u8 = undefined;
const in = "abcdefghjklmnopq";
var decryptedOut: [32]u8 = undefined;

Crypto.Encrypt(iv, in[0..], encryptedOut[0..]);

try std.testing.expectEqualSlices(u8, iv[0..], encryptedOut[0..16]);

Crypto.Decrypt(encryptedOut[0..], decryptedOut[0..]);

try std.testing.expectEqualSlices(u8, in, decryptedOut[0..in.len]);
}

test "crypto number" {
Shared.Random.init();

const iv = Crypto.GetIV();
var encryptedOut: [48]u8 = undefined;
var decryptedOut: [32]u8 = undefined;
const in = 10;

_ = try std.fmt.bufPrint(&decryptedOut, "{d}", .{in});
Crypto.Encrypt(iv, decryptedOut[0..], encryptedOut[0..]);

try std.testing.expectEqualSlices(u8, iv[0..], encryptedOut[0..16]);

for (0..5) |_| {
Shared.Random.init();
Crypto.Decrypt(encryptedOut[0..], decryptedOut[0..]);

var trimValue: [1]u8 = undefined;
var out = try std.fmt.parseInt(u64, std.mem.trimRight(u8, &decryptedOut, &trimValue), 10);
try std.testing.expect(in == out);
}
}
13 changes: 8 additions & 5 deletions src/Shared.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Helpers_ = @import("Helpers.zig").Helpers;
const RndGen = std.rand.DefaultPrng;
const Sprites = @import("Sprite.zig").Sprite;
const CameraController = @import("Camera.zig").Camera;
const Crypto_ = @import("Crypto.zig").Crypto;

pub const Shared = struct {
const Alloc = struct {
Expand Down Expand Up @@ -70,6 +71,8 @@ pub const Shared = struct {

pub const Helpers = Helpers_;

pub const Crypto = Crypto_;

pub const Time = struct {
extern fn WASMTimestamp() i64;

Expand Down Expand Up @@ -196,14 +199,14 @@ pub const Shared = struct {

pub const Settings = struct {
var loaded_settings: ?SettingsManager = null;
pub fn GetSettings() SettingsManager {
pub inline fn GetSettings() SettingsManager {
if (loaded_settings == null) {
loaded_settings = SettingsManager.load(Alloc.allocator);
}
return loaded_settings.?;
}

pub fn UpdateSettings(newValue: anytype) void {
pub inline fn UpdateSettings(newValue: anytype) void {
const original_settings = GetSettings();
loaded_settings = SettingsManager.update(original_settings, newValue);

Expand All @@ -221,7 +224,7 @@ pub const Shared = struct {
}
}

pub fn SaveNow() void {
pub inline fn SaveNow() void {
_ = loaded_settings.?.save(Alloc.allocator);
}
};
Expand Down Expand Up @@ -266,9 +269,9 @@ pub const Shared = struct {
return vl.Views.PausedView;
}

pub inline fn GameOver() vl.Views {
pub inline fn GameOver(Score: u64, HighScore: u64) vl.Views {
const gameover_vm = GameOverViewModel.GetVM();
gameover_vm.GameOver();
gameover_vm.GameOver(Score, HighScore);
return vl.Views.GameOverView;
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/ViewModels/GameOverViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ pub const GameOverViewModel = Shared.View.ViewModel.Create(
struct {
pub var BackgroundTexture: ?raylib.Texture = null;
pub var startTime: i64 = 0;
pub var score: u64 = 0;
pub var highScore: u64 = 0;

pub inline fn GameOver() void {
pub inline fn GameOver(Score: u64, HighScore: u64) void {
score = Score;
highScore = HighScore;
raylib.endDrawing();
const img = raylib.loadImageFromScreen();
defer img.unload();
Expand Down
2 changes: 1 addition & 1 deletion src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn DrawWithCamera() Shared.View.Views {
}

if (vm.shieldLevel == 0) {
return Shared.View.GameOver();
return Shared.View.GameOver(vm.score, 0);
}

return result;
Expand Down

0 comments on commit 0332119

Please sign in to comment.