diff options
author | gumi <git@gumi.ca> | 2020-05-16 17:18:17 -0400 |
---|---|---|
committer | gumi <git@gumi.ca> | 2020-05-16 17:18:17 -0400 |
commit | 25a3c3fefae87d55da172f2aaef6a27987f439fd (patch) | |
tree | c410cdfd73c7cf9101f7f6dab1c8fa1772dd05c9 | |
parent | 1af05ee74d0b2ec4e132617ab71bedd30fbfe753 (diff) | |
download | tools-25a3c3fefae87d55da172f2aaef6a27987f439fd.tar.gz tools-25a3c3fefae87d55da172f2aaef6a27987f439fd.tar.bz2 tools-25a3c3fefae87d55da172f2aaef6a27987f439fd.tar.xz tools-25a3c3fefae87d55da172f2aaef6a27987f439fd.zip |
[frob] update for deno 1.0
-rw-r--r-- | server/frob/accreg.ts | 10 | ||||
-rw-r--r-- | server/frob/char.ts | 46 | ||||
-rw-r--r-- | server/frob/index.ts | 25 | ||||
-rw-r--r-- | server/frob/itemdb.ts | 2 | ||||
-rw-r--r-- | server/frob/login.ts | 10 | ||||
-rw-r--r-- | server/frob/party.ts | 10 | ||||
-rw-r--r-- | server/frob/sql.ts | 26 | ||||
-rw-r--r-- | server/frob/storage.ts | 38 |
8 files changed, 100 insertions, 67 deletions
diff --git a/server/frob/accreg.ts b/server/frob/accreg.ts index d951d77..3e0fb19 100644 --- a/server/frob/accreg.ts +++ b/server/frob/accreg.ts @@ -1,3 +1,5 @@ +import { SQLHandler } from "./sql.ts"; + class AccregParser { private reg_line = "^" + @@ -7,7 +9,7 @@ class AccregParser { private vars_line = "(?<var_name>[^,]+),(?<value>[-0-9]+) "; private reg_regex: RegExp; private reg_regex_vars: RegExp; - private encoder; + private encoder: TextEncoder; constructor () { this.reg_regex = new RegExp(this.reg_line); @@ -51,7 +53,7 @@ class AccregParser { while (true) { const nread = await Deno.read(file.rid, buf); - if (nread === Deno.EOF) { + if (nread === null) { break; } @@ -82,9 +84,9 @@ class AccregParser { } class AccregSQL { - private sql; + private sql: SQLHandler; - constructor (sql) { + constructor (sql: SQLHandler) { this.sql = sql; } diff --git a/server/frob/char.ts b/server/frob/char.ts index dfb6837..2d9fb0e 100644 --- a/server/frob/char.ts +++ b/server/frob/char.ts @@ -1,3 +1,5 @@ +import { SQLHandler } from "./sql.ts"; + class CharParser { private char_line = "^" + @@ -28,7 +30,7 @@ class CharParser { private char_regex_items: RegExp; private char_regex_skills: RegExp; private char_regex_vars: RegExp; - private encoder; + private encoder: TextEncoder; constructor () { this.char_regex = new RegExp(this.char_line); @@ -84,7 +86,7 @@ class CharParser { groups.variables2 = variables; - Deno.write(Deno.stdout.rid, this.encoder.encode(`\r⌛ processing char ${groups.char_id}...`)); + Deno.write(Deno.stdout.rid, this.encoder.encode(`\r \r⌛ processing char ${groups.char_id}...`)); return groups; } @@ -98,7 +100,7 @@ class CharParser { while (true) { const nread = await Deno.read(file.rid, buf); - if (nread === Deno.EOF) { + if (nread === null) { break; } @@ -121,21 +123,28 @@ class CharParser { } class CharWriter { - private file; + private file?: Deno.File; private highest: number = 0; - private encoder; + private encoder: TextEncoder; constructor () { + this.encoder = new TextEncoder(); + } + + async init () { try { - Deno.removeSync("world/save/athena.txt.tmp"); + await Deno.remove("world/save/athena.txt.tmp"); } catch { // ignore this } - this.file = Deno.openSync("world/save/athena.txt.tmp", "a+"); - this.encoder = new TextEncoder(); + this.file = await Deno.open("world/save/athena.txt.tmp", {append: true, create: true}); } async write (char: any) { + if (!this.file) { + return; + } + let line = `${char.char_id}\t` + `${char.account_id},${char.char_num}\t` + @@ -170,25 +179,26 @@ class CharWriter { } } - async finalize(dry_run: boolean = false) { + async finalize () { console.info("\rappending %newid%... "); + + if (!this.file) { + return; + } + await Deno.write(this.file.rid, this.encoder.encode(`${this.highest + 1}\t%newid%\n`)); this.file.close(); - if (dry_run) { - Deno.removeSync("world/save/athena.txt.tmp"); - } else { - console.info("overwriting athena.txt..."); - await Deno.rename("world/save/athena.txt", "world/save/athena.txt_pre-frob"); - await Deno.rename("world/save/athena.txt.tmp", "world/save/athena.txt"); - } + console.info("overwriting athena.txt..."); + await Deno.rename("world/save/athena.txt", "world/save/athena.txt_pre-frob"); + await Deno.rename("world/save/athena.txt.tmp", "world/save/athena.txt"); } } class CharSQL { - private sql; + private sql: SQLHandler; - constructor (sql) { + constructor (sql: SQLHandler) { this.sql = sql; } diff --git a/server/frob/index.ts b/server/frob/index.ts index fc3ac09..df06975 100644 --- a/server/frob/index.ts +++ b/server/frob/index.ts @@ -91,7 +91,7 @@ const flags = { } // flag parsing - for (let opt of args) { + for (const opt of args.slice()) { if (opt.startsWith("--")) { switch (opt.slice(2)) { case "dry": @@ -101,18 +101,18 @@ const flags = { case "dump": case "sql": flags.sql = true; + break; case "clean": case "clean-only": - args.length = 0; break; default: throw new SyntaxError(`unknown flag: ${opt}`); } args.shift(); + } else { + break; // no more flags } - - break; // no more flags } // item parsing @@ -163,6 +163,10 @@ const flags = { } } + if (!flags.dry_run) { + char_writer.init(); + } + // inventory: for await (let char of char_parser.readDB()) { let items_filtered = []; // this is not a Set because some items don't stack @@ -196,7 +200,7 @@ const flags = { await char_SQL.write(char); } - await char_writer.finalize(!!flags.dry_run); + await char_writer.finalize(); // char-server-bound account variables if (flags.sql) { @@ -212,6 +216,10 @@ const flags = { } } + if (!flags.dry_run) { + storage_writer.init(); + } + // storage: for await (let storage of storage_parser.readDB()) { let items_filtered = []; // this is not a Set because some items don't stack @@ -252,14 +260,17 @@ const flags = { if (storage.storage_amount >= 1) { await storage_writer.write(storage); - await storage_SQL.write(storage); + + if (flags.sql) { + await storage_SQL.write(storage); + } } else { console.log(`storage of account ${storage.account_id} is now empty: removing it from the storage db`); stats.storage.wiped++; } } - await storage_writer.finalize(!!flags.dry_run); + await storage_writer.finalize(); await sql.close(); console.info("\r \n=== all done ==="); diff --git a/server/frob/itemdb.ts b/server/frob/itemdb.ts index 5ff943d..721965a 100644 --- a/server/frob/itemdb.ts +++ b/server/frob/itemdb.ts @@ -26,7 +26,7 @@ class ItemDB { this.item_regex = new RegExp(this.item_line); } - private parseLine (line) { + private parseLine (line: string) { const match = this.item_regex.exec(line); if (!(match instanceof Object) || !Reflect.has(match, "groups")) { diff --git a/server/frob/login.ts b/server/frob/login.ts index fa130cc..ccf6d37 100644 --- a/server/frob/login.ts +++ b/server/frob/login.ts @@ -1,3 +1,5 @@ +import { SQLHandler } from "./sql.ts"; + class LoginParser { private login_line = "^" + @@ -16,7 +18,7 @@ class LoginParser { "(?<ban_until_time>[0-9]+)\t" + "$"; private login_regex: RegExp; - private encoder; + private encoder: TextEncoder; constructor () { this.login_regex = new RegExp(this.login_line); @@ -79,7 +81,7 @@ class LoginParser { while (true) { const nread = await Deno.read(file.rid, buf); - if (nread === Deno.EOF) { + if (nread === null) { break; } @@ -114,9 +116,9 @@ class LoginParser { } class LoginSQL { - private sql; + private sql: SQLHandler; - constructor (sql) { + constructor (sql: SQLHandler) { this.sql = sql; } diff --git a/server/frob/party.ts b/server/frob/party.ts index cdc0580..a15336b 100644 --- a/server/frob/party.ts +++ b/server/frob/party.ts @@ -1,3 +1,5 @@ +import { SQLHandler } from "./sql.ts"; + class PartyParser { private party_line = "^" + @@ -9,7 +11,7 @@ class PartyParser { private member_line = "(?<account_id>[0-9]+),(?<leader>[01])\t(?<char_name>[^\t]+)\t"; private party_regex: RegExp; private party_regex_members: RegExp; - private encoder; + private encoder: TextEncoder; constructor () { this.party_regex = new RegExp(this.party_line); @@ -65,7 +67,7 @@ class PartyParser { while (true) { const nread = await Deno.read(file.rid, buf); - if (nread === Deno.EOF) { + if (nread === null) { break; } @@ -96,9 +98,9 @@ class PartyParser { } class PartySQL { - private sql; + private sql: SQLHandler; - constructor (sql) { + constructor (sql: SQLHandler) { this.sql = sql; } diff --git a/server/frob/sql.ts b/server/frob/sql.ts index 60c4bbe..a7ab414 100644 --- a/server/frob/sql.ts +++ b/server/frob/sql.ts @@ -1,11 +1,11 @@ -import { Client } from "https://deno.land/x/mysql/mod.ts"; +import { Client, Connection } from "https://deno.land/x/mysql/mod.ts"; class SQLHandler { - private client; - private hostname; - private username; - private password; - private backslash; + private client: Client; + private hostname: string; + private username: string; + private password: string; + private backslash: RegExp; constructor (hostname: string = "127.0.0.1", username: string = "evol", password: string = "evol") { this.client = new Client(); @@ -154,21 +154,21 @@ class SQLHandler { `); // some old parties have weird names } - escape (str) { + escape (str: string) { // for some reason the deno sql module doesn't escape backslashes return str.replace(this.backslash, "\\\\"); } - async transaction (fn) { - return await this.client.transaction(fn); + async transaction (processor: (c: Connection) => Promise<any>) { + return await this.client.transaction(processor); } - async query (...args) { - return await this.client.query(...args); + async query (sql: string, params?: any[]) { + return await this.client.query(sql, params); } - async do (...args) { - return await this.client.execute(...args); + async do (sql: string, params?: any[]) { + return await this.client.execute(sql, params); } diff --git a/server/frob/storage.ts b/server/frob/storage.ts index 04559d6..ab44ba1 100644 --- a/server/frob/storage.ts +++ b/server/frob/storage.ts @@ -1,3 +1,5 @@ +import { SQLHandler } from "./sql.ts"; + class StorageParser { private storage_line = "^" + @@ -6,7 +8,7 @@ class StorageParser { private storage_items_line = "[0-9]+,(?<nameid>[0-9]+),(?<amount>[0-9]+),(?<equip>[0-9]+),[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+ "; private storage_regex: RegExp; private storage_regex_items: RegExp; - private encoder; + private encoder: TextEncoder; constructor () { this.storage_regex = new RegExp(this.storage_line); @@ -36,7 +38,7 @@ class StorageParser { groups.items = items; - Deno.write(Deno.stdout.rid, this.encoder.encode(`\r⌛ processing storage of account ${groups.account_id}... `)); + Deno.write(Deno.stdout.rid, this.encoder.encode(`\r \r⌛ processing storage of account ${groups.account_id}...`)); return groups; } @@ -50,7 +52,7 @@ class StorageParser { while (true) { const nread = await Deno.read(file.rid, buf); - if (nread === Deno.EOF) { + if (nread === null) { break; } @@ -81,20 +83,27 @@ class StorageParser { } class StorageWriter { - private file; - private encoder; + private file?: Deno.File; + private encoder: TextEncoder; constructor () { + this.encoder = new TextEncoder(); + } + + async init () { try { - Deno.removeSync("world/save/storage.txt.tmp"); + await Deno.remove("world/save/storage.txt.tmp"); } catch { // ignore this } - this.file = Deno.openSync("world/save/storage.txt.tmp", "a+"); - this.encoder = new TextEncoder(); + this.file = await Deno.open("world/save/storage.txt.tmp", {append: true, create: true}); } async write (storage: any) { + if (!this.file) { + return; + } + let line = `${storage.account_id},${storage.storage_amount}\t`; for (const item of storage.items) { @@ -107,12 +116,9 @@ class StorageWriter { await Deno.write(this.file.rid, this.encoder.encode(line)); } - async finalize(dry_run: boolean = false) { - this.file.close(); - - if (dry_run) { - Deno.removeSync("world/save/storage.txt.tmp"); - } else { + async finalize () { + if (this.file) { + this.file.close(); console.info("\roverwriting storage.txt... "); await Deno.rename("world/save/storage.txt", "world/save/storage.txt_pre-frob"); await Deno.rename("world/save/storage.txt.tmp", "world/save/storage.txt"); @@ -121,9 +127,9 @@ class StorageWriter { } class StorageSQL { - private sql; + private sql: SQLHandler; - constructor (sql) { + constructor (sql: SQLHandler) { this.sql = sql; } |