From db2164f498cd098328d7c289c5a777fc049695c6 Mon Sep 17 00:00:00 2001 From: zongor Date: Mon, 27 May 2024 20:19:37 -0400 Subject: [PATCH] add sql types --- client/types.h | 60 ++++----- server/components/Entity.js | 122 +++++++++--------- server/generate.sh | 2 +- server/schema.json | 60 ++++----- server/sql/types.sql | 2 + .../test/testgen.sh | 2 +- .../js-class-c-struct-transpiler/transpile.js | 53 ++++++-- 7 files changed, 164 insertions(+), 137 deletions(-) create mode 100644 server/sql/types.sql diff --git a/client/types.h b/client/types.h index 2910440..934b418 100644 --- a/client/types.h +++ b/client/types.h @@ -1,34 +1,34 @@ typedef struct Entity { char Name[24]; - int Strength; - int Endurance; - int Intelligence; - int Wisdom; - int Charisma; - int Faith; - int Artisan; - int Metalworking; - int Alchemy; - int Engineering; - int Culinary; - int Ranged; - int Melee; - int Acrobatics; - int Prestidigitation; - int Language; - int Medicine; - int Thaumitology; - int Theology; - int Fishing; - int Mining; - int Survival; - int Gardening; - int History; - int Perception; - int Luck; - int Stealth; - int Attractiveness; - int Affluence; - int Notoriety; + unsigned char Strength; + unsigned char Endurance; + unsigned char Intelligence; + unsigned char Wisdom; + unsigned char Charisma; + unsigned char Faith; + unsigned char Artisan; + unsigned char Metalworking; + unsigned char Alchemy; + unsigned char Engineering; + unsigned char Culinary; + unsigned char Ranged; + unsigned char Melee; + unsigned char Acrobatics; + unsigned char Prestidigitation; + unsigned char Language; + unsigned char Medicine; + unsigned char Thaumitology; + unsigned char Theology; + unsigned char Fishing; + unsigned char Mining; + unsigned char Survival; + unsigned char Gardening; + unsigned char History; + unsigned char Perception; + unsigned char Luck; + unsigned char Stealth; + unsigned char Attractiveness; + unsigned char Affluence; + unsigned char Notoriety; } Entity; diff --git a/server/components/Entity.js b/server/components/Entity.js index 9f79b2a..60ca8db 100644 --- a/server/components/Entity.js +++ b/server/components/Entity.js @@ -6,190 +6,190 @@ class Entity { this._data.set(this._encoder.encode(v), 0); } get Strength() { - return this._data.getInt32(24, true); + return this._data.getUint8(24, true); } set Strength(v) { - return this._data.setInt32(24, v, true); + return this._data.setUint8(24, v, true); } get Endurance() { - return this._data.getInt32(28, true); + return this._data.getUint8(25, true); } set Endurance(v) { - return this._data.setInt32(28, v, true); + return this._data.setUint8(25, v, true); } get Intelligence() { - return this._data.getInt32(32, true); + return this._data.getUint8(26, true); } set Intelligence(v) { - return this._data.setInt32(32, v, true); + return this._data.setUint8(26, v, true); } get Wisdom() { - return this._data.getInt32(36, true); + return this._data.getUint8(27, true); } set Wisdom(v) { - return this._data.setInt32(36, v, true); + return this._data.setUint8(27, v, true); } get Charisma() { - return this._data.getInt32(40, true); + return this._data.getUint8(28, true); } set Charisma(v) { - return this._data.setInt32(40, v, true); + return this._data.setUint8(28, v, true); } get Faith() { - return this._data.getInt32(44, true); + return this._data.getUint8(29, true); } set Faith(v) { - return this._data.setInt32(44, v, true); + return this._data.setUint8(29, v, true); } get Artisan() { - return this._data.getInt32(48, true); + return this._data.getUint8(30, true); } set Artisan(v) { - return this._data.setInt32(48, v, true); + return this._data.setUint8(30, v, true); } get Metalworking() { - return this._data.getInt32(52, true); + return this._data.getUint8(31, true); } set Metalworking(v) { - return this._data.setInt32(52, v, true); + return this._data.setUint8(31, v, true); } get Alchemy() { - return this._data.getInt32(56, true); + return this._data.getUint8(32, true); } set Alchemy(v) { - return this._data.setInt32(56, v, true); + return this._data.setUint8(32, v, true); } get Engineering() { - return this._data.getInt32(60, true); + return this._data.getUint8(33, true); } set Engineering(v) { - return this._data.setInt32(60, v, true); + return this._data.setUint8(33, v, true); } get Culinary() { - return this._data.getInt32(64, true); + return this._data.getUint8(34, true); } set Culinary(v) { - return this._data.setInt32(64, v, true); + return this._data.setUint8(34, v, true); } get Ranged() { - return this._data.getInt32(68, true); + return this._data.getUint8(35, true); } set Ranged(v) { - return this._data.setInt32(68, v, true); + return this._data.setUint8(35, v, true); } get Melee() { - return this._data.getInt32(72, true); + return this._data.getUint8(36, true); } set Melee(v) { - return this._data.setInt32(72, v, true); + return this._data.setUint8(36, v, true); } get Acrobatics() { - return this._data.getInt32(76, true); + return this._data.getUint8(37, true); } set Acrobatics(v) { - return this._data.setInt32(76, v, true); + return this._data.setUint8(37, v, true); } get Prestidigitation() { - return this._data.getInt32(80, true); + return this._data.getUint8(38, true); } set Prestidigitation(v) { - return this._data.setInt32(80, v, true); + return this._data.setUint8(38, v, true); } get Language() { - return this._data.getInt32(84, true); + return this._data.getUint8(39, true); } set Language(v) { - return this._data.setInt32(84, v, true); + return this._data.setUint8(39, v, true); } get Medicine() { - return this._data.getInt32(88, true); + return this._data.getUint8(40, true); } set Medicine(v) { - return this._data.setInt32(88, v, true); + return this._data.setUint8(40, v, true); } get Thaumitology() { - return this._data.getInt32(92, true); + return this._data.getUint8(41, true); } set Thaumitology(v) { - return this._data.setInt32(92, v, true); + return this._data.setUint8(41, v, true); } get Theology() { - return this._data.getInt32(96, true); + return this._data.getUint8(42, true); } set Theology(v) { - return this._data.setInt32(96, v, true); + return this._data.setUint8(42, v, true); } get Fishing() { - return this._data.getInt32(100, true); + return this._data.getUint8(43, true); } set Fishing(v) { - return this._data.setInt32(100, v, true); + return this._data.setUint8(43, v, true); } get Mining() { - return this._data.getInt32(104, true); + return this._data.getUint8(44, true); } set Mining(v) { - return this._data.setInt32(104, v, true); + return this._data.setUint8(44, v, true); } get Survival() { - return this._data.getInt32(108, true); + return this._data.getUint8(45, true); } set Survival(v) { - return this._data.setInt32(108, v, true); + return this._data.setUint8(45, v, true); } get Gardening() { - return this._data.getInt32(112, true); + return this._data.getUint8(46, true); } set Gardening(v) { - return this._data.setInt32(112, v, true); + return this._data.setUint8(46, v, true); } get History() { - return this._data.getInt32(116, true); + return this._data.getUint8(47, true); } set History(v) { - return this._data.setInt32(116, v, true); + return this._data.setUint8(47, v, true); } get Perception() { - return this._data.getInt32(120, true); + return this._data.getUint8(48, true); } set Perception(v) { - return this._data.setInt32(120, v, true); + return this._data.setUint8(48, v, true); } get Luck() { - return this._data.getInt32(124, true); + return this._data.getUint8(49, true); } set Luck(v) { - return this._data.setInt32(124, v, true); + return this._data.setUint8(49, v, true); } get Stealth() { - return this._data.getInt32(128, true); + return this._data.getUint8(50, true); } set Stealth(v) { - return this._data.setInt32(128, v, true); + return this._data.setUint8(50, v, true); } get Attractiveness() { - return this._data.getInt32(132, true); + return this._data.getUint8(51, true); } set Attractiveness(v) { - return this._data.setInt32(132, v, true); + return this._data.setUint8(51, v, true); } get Affluence() { - return this._data.getInt32(136, true); + return this._data.getUint8(52, true); } set Affluence(v) { - return this._data.setInt32(136, v, true); + return this._data.setUint8(52, v, true); } get Notoriety() { - return this._data.getInt32(140, true); + return this._data.getUint8(53, true); } set Notoriety(v) { - return this._data.setInt32(140, v, true); + return this._data.setUint8(53, v, true); } get bytes() { return new Uint8Array(this._ptr); } constructor(init = {}, ptr = undefined) { - this._size = 144; + this._size = 54; this._ptr = ptr.buffer || new ArrayBuffer(this._size); this._data = new DataView(this._ptr); diff --git a/server/generate.sh b/server/generate.sh index 4c1e7bf..d722317 100755 --- a/server/generate.sh +++ b/server/generate.sh @@ -1,2 +1,2 @@ #!/bin/bash -bun run ../tools/js-class-c-struct-transpiler/transpile.js -S schema.json -j components/ -c ../client/ +bun run ../tools/js-class-c-struct-transpiler/transpile.js -S schema.json -j components/ -c ../client/ -s sql/ diff --git a/server/schema.json b/server/schema.json index 82b74ee..bba9c35 100644 --- a/server/schema.json +++ b/server/schema.json @@ -6,123 +6,123 @@ "size": "24" }, "Strength": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Endurance": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Intelligence": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Wisdom": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Charisma": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Faith": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Artisan": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Metalworking": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Alchemy": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Engineering": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Culinary": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Ranged": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Melee": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Acrobatics": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Prestidigitation": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Language": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Medicine": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Thaumitology": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Theology": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Fishing": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Mining": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Survival": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Gardening": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "History": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Perception": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Luck": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Stealth": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Attractiveness": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Affluence": { - "type": "i32", + "type": "u8", "kind": "scalar" }, "Notoriety": { - "type": "i32", + "type": "u8", "kind": "scalar" } } diff --git a/server/sql/types.sql b/server/sql/types.sql new file mode 100644 index 0000000..2354704 --- /dev/null +++ b/server/sql/types.sql @@ -0,0 +1,2 @@ +CREATE TABLE Entity (entity_id INTEGER PRIMARY KEY AUTOINCREMENT, string TEXT, Strength INTEGER, Endurance INTEGER, Intelligence INTEGER, Wisdom INTEGER, Charisma INTEGER, Faith INTEGER, Artisan INTEGER, Metalworking INTEGER, Alchemy INTEGER, Engineering INTEGER, Culinary INTEGER, Ranged INTEGER, Melee INTEGER, Acrobatics INTEGER, Prestidigitation INTEGER, Language INTEGER, Medicine INTEGER, Thaumitology INTEGER, Theology INTEGER, Fishing INTEGER, Mining INTEGER, Survival INTEGER, Gardening INTEGER, History INTEGER, Perception INTEGER, Luck INTEGER, Stealth INTEGER, Attractiveness INTEGER, Affluence INTEGER, Notoriety INTEGER); + diff --git a/tools/js-class-c-struct-transpiler/test/testgen.sh b/tools/js-class-c-struct-transpiler/test/testgen.sh index 8e5617b..00acd90 100755 --- a/tools/js-class-c-struct-transpiler/test/testgen.sh +++ b/tools/js-class-c-struct-transpiler/test/testgen.sh @@ -1 +1 @@ -bun run transpile.js -S schema.json -j out/ -c out/ \ No newline at end of file +bun run ../transpile.js -S schema.json -j out/ -c out/ -s out/ \ No newline at end of file diff --git a/tools/js-class-c-struct-transpiler/transpile.js b/tools/js-class-c-struct-transpiler/transpile.js index 38f57d2..0153c42 100644 --- a/tools/js-class-c-struct-transpiler/transpile.js +++ b/tools/js-class-c-struct-transpiler/transpile.js @@ -4,41 +4,59 @@ let types = { u8: { c: "unsigned char", js: "Uint8", + sql: "INTEGER", size: 1, }, i8: { c: "char", js: "Int8", + sql: "INTEGER", size: 1, }, u16: { c: "unsigned short", js: "Uint16", + sql: "INTEGER", size: 2, }, i16: { c: "short", js: "Int16", + sql: "INTEGER", size: 2, }, u32: { c: "unisgned int", js: "Uint32", + sql: "INTEGER", size: 4, }, i32: { c: "int", js: "Int32", + sql: "INTEGER", size: 4, }, + i64: { + c: "long", + js: "BigInt64", + sql: "INTEGER" + }, + u64: { + c: "unsigned long", + js: "BigUint64", + sql: "INTEGER" + }, f32: { c: "float", js: "Float32", + sql: "REAL", size: 4, }, f64: { c: "double", js: "Float64", + sql: "REAL", size: 8, }, }; @@ -50,14 +68,18 @@ const { values } = parseArgs({ type: "string", short: "S", }, - jsout: { + javascript_out: { type: "string", short: "j", }, - cout: { + c_out: { type: "string", short: "c", }, + sqlite_out: { + type: "string", + short: "s" + } }, strict: true, allowPositionals: true, @@ -88,6 +110,7 @@ function jsStructConstructor(size, containsString) { const sFile = Bun.file(values.schema); const schema = await sFile.json(); let cData = ""; +let sqlData = ""; for (const type of Object.keys(schema)) { let containsString = false; @@ -95,7 +118,9 @@ for (const type of Object.keys(schema)) { let size = 0; let importStatements = ""; let jsData = ""; + let foreignKeys = ""; const props = schema[type]; + sqlData += `CREATE TABLE ${type} (${type.toLowerCase()}_id INTEGER PRIMARY KEY AUTOINCREMENT`; cData += `typedef struct ${type} {`; jsData += `class ${type} {`; for (const prop of Object.keys(props)) { @@ -117,7 +142,7 @@ for (const type of Object.keys(schema)) { set ${prop}(v) { this._data.set(this._encoder.encode(v), ${parseInt(offset)}); }`; - + sqlData += `, ${propType} TEXT`; cData += ` char ${prop}[${iSize}];`; break; @@ -132,7 +157,7 @@ for (const type of Object.keys(schema)) { offset )}, v, true); }`; - + sqlData += `, ${prop} ${types[propType].sql}`; cData += ` ${types[propType].c} ${prop};`; @@ -150,7 +175,11 @@ for (const type of Object.keys(schema)) { if (!importStatements.includes(importS)) { importStatements += importS; } + const foreignKey = `${propType.toLowerCase()}_id`; + const localKey = `${prop.toLowerCase()}_${foreignKey}`; + sqlData += `,${localKey} INTEGER`; + foreignKeys += `\nFOREIGN KEY(${localKey}) REFERENCES ${propType}(${foreignKey})` cData += `\n\t\t${types[propType].c} ${prop};`; break; case "array": @@ -174,18 +203,14 @@ for (const type of Object.keys(schema)) { }; jsData += jsStructConstructor(size, containsString); - jsData += ` -} - -export default ${type}`; - cData += ` -} ${type}; - -`; + jsData += `\n}\n\nexport default ${type}`; + cData += `\n} ${type};\n\n`; + sqlData += `)${foreignKeys};\n\n`; await Bun.write( - Bun.file(values.jsout + type + ".js"), + Bun.file(values.javascript_out + type + ".js"), importStatements + jsData ); } -await Bun.write(Bun.file(values.cout + "types.h"), cData); +await Bun.write(Bun.file(values.c_out + "types.h"), cData); +await Bun.write(Bun.file(values.sqlite_out + "types.sql"), sqlData);