initial commit
This commit is contained in:
		
						commit
						ce94208d0c
					
				
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							|  | @ -0,0 +1,235 @@ | |||
| const std = @import("std"); | ||||
| const print = std.debug.print; | ||||
| const io = std.io; | ||||
| const fs = std.fs; | ||||
| const json = std.json; | ||||
| const cwd = fs.cwd(); | ||||
| 
 | ||||
| const ImgList = struct { | ||||
|     small:          ?[]const u8,//string | ||||
|     normal:         ?[]const u8,//string | ||||
|     large:          ?[]const u8,//string | ||||
|     png:            ?[]const u8,//string | ||||
|     art_crop:       ?[]const u8,//string | ||||
|     border_crop:    ?[]const u8,//string | ||||
| }; | ||||
| 
 | ||||
| const Legalities = struct { | ||||
|         standard:   ?[]const u8,//string | ||||
|         future:     ?[]const u8,//string | ||||
|         historic:   ?[]const u8,//string | ||||
|         timeless:   ?[]const u8,//string | ||||
|         gladiator:  ?[]const u8,//string | ||||
|         pioneer:    ?[]const u8,//string | ||||
|         explorer:   ?[]const u8,//string | ||||
|         modern:     ?[]const u8,//string | ||||
|         legacy:     ?[]const u8,//string | ||||
|         pauper:     ?[]const u8,//string | ||||
|         vintage:    ?[]const u8,//string | ||||
|         penny:      ?[]const u8,//string | ||||
|         commander:  ?[]const u8,//string | ||||
|         oathbreaker:?[]const u8,//string | ||||
|         standardbrawl:?[]const u8,//string | ||||
|         brawl:      ?[]const u8,//string | ||||
|         alchemy:    ?[]const u8,//string | ||||
|         paupercommander:?[]const u8,//string | ||||
|         duel:       ?[]const u8,//string | ||||
|         oldschool:  ?[]const u8,//string | ||||
|         premodern:  ?[]const u8,//string | ||||
|         predh:      ?[]const u8,//string | ||||
| }; | ||||
| 
 | ||||
| const Prices = struct { | ||||
|     usd:        ?[]const u8,//string? but technically a float | ||||
|     usd_foil:   ?[]const u8,//same | ||||
|     usd_etched: ?[]const u8,//same | ||||
|     eur:        ?[]const u8,//same | ||||
|     eur_foil:   ?[]const u8,//same | ||||
|     tix:        ?[]const u8,//same | ||||
| }; | ||||
| 
 | ||||
| const RelatedUris = struct { | ||||
|     gatherer:                       ?[]const u8,//string | ||||
|     tcgplayer_infinite_articles:    ?[]const u8,//string | ||||
|     tcgplayer_infinite_decks:       ?[]const u8,//string | ||||
|     edhrec:                         ?[]const u8,//string | ||||
| }; | ||||
| 
 | ||||
| const PurchaseUris = struct { | ||||
|         tcgplayer:          ?[]const u8,//string | ||||
|         cardmarket:         ?[]const u8,//string | ||||
|         cardhoarder:        ?[]const u8,//string | ||||
| }; | ||||
| 
 | ||||
| const val_or_nothing = union(enum) { | ||||
|     none: void, | ||||
|     int: i32, | ||||
|     float: f64, | ||||
|     string: []const u8, | ||||
| }; | ||||
| 
 | ||||
| const Card = struct { | ||||
|     // object:                 ?[]const u8,//string | ||||
|     // id:                     ?[]const u8,//string | ||||
|     // oracle_id:              ?[]const u8,//string | ||||
|     // multiverse_ids:               ?[]u32,//array of int | ||||
|     // mtgo_id:                        ?u32,//int | ||||
|     // mtgo_foil_id:                   ?u32,//int | ||||
|     // tcgplayer_id:                   ?u32,//int | ||||
|     // cardmarket_id:                  ?u32,//int | ||||
|     name:                   ?[]const u8,//string | ||||
|     // lang:                   ?[]const u8,//string | ||||
|     // released_at:            ?[]const u8,//string | ||||
|     // uri:                    ?[]const u8,//string | ||||
|     // scryfall_uri:           ?[]const u8,//string | ||||
|     // layout:                 ?[]const u8,//string | ||||
|     // highres_image:                ?bool,//bool | ||||
|     // image_status:           ?[]const u8,//string | ||||
|     // image_uris:                ?ImgList,//obj (strings) | ||||
|     // mana_cost:              ?[]const u8,//string | ||||
|     cmc:                            ?f32,//technically a float? but I think we can always cast safely EDIT: NOPE | ||||
|     type_line:              ?[]const u8,//string | ||||
|     oracle_text:            ?[]const u8 = "",//string | ||||
|     // colors:                 json.Value,//?[]const u8,//array of Chars | ||||
|     color_identity:         json.Value,//?[]const u8,//array of Chars | ||||
|     keywords:             ?[][]const u8,//array of Strings | ||||
|     // legalities:             ?Legalities,//obj (strings) | ||||
|     // games:                ?[][]const u8,//array of Strings | ||||
|     // reserved:                     ?bool,//bool | ||||
|     // foil:                         ?bool,//bool | ||||
|     // nonfoil:                      ?bool,//bool | ||||
|     // finishes:             ?[][]const u8,//array of Strings | ||||
|     // oversized:                    ?bool,//bool | ||||
|     // promo:                        ?bool,//bool | ||||
|     // reprint:                      ?bool,//bool | ||||
|     // variation:                    ?bool,//bool | ||||
|     // set_id:                 ?[]const u8,//string | ||||
|     // set:                    ?[]const u8,//string | ||||
|     // set_name:               ?[]const u8,//string | ||||
|     // set_type:               ?[]const u8,//string | ||||
|     // set_uri:                ?[]const u8,//string | ||||
|     // set_search_uri:         ?[]const u8,//string | ||||
|     // scryfall_set_uri:       ?[]const u8,//string | ||||
|     // rulings_uri:            ?[]const u8,//string | ||||
|     // prints_search_uri:      ?[]const u8,//string | ||||
|     // collector_number:       ?[]const u8,//string | ||||
|     // digital:                      ?bool,//bool | ||||
|     // rarity:                 ?[]const u8,//string | ||||
|     // flavor_text:            ?[]const u8,//string | ||||
|     // card_back_id:           ?[]const u8,//string | ||||
|     // artist:                 ?[]const u8,//string | ||||
|     // artist_ids:           ?[][]const u8,//string | ||||
|     // illustration_id:        ?[]const u8,//string | ||||
|     // border_color:           ?[]const u8,//string | ||||
|     // frame:                  ?[]const u8,//string | ||||
|     // full_art:                     ?bool,//bool | ||||
|     // textless:                     ?bool,//bool | ||||
|     // booster:                      ?bool,//bool | ||||
|     // story_spotlight:              ?bool,//bool | ||||
|     // edhrec_rank:                    ?u32,//int | ||||
|     // prices:                     ?Prices,//obj (floats stored as strings) | ||||
|     // related_uris:         json.Value,//?RelatedUris,//obj (strings) | ||||
|     // purchase_uris:        json.Value,//?PurchaseUris,//obj (strings) | ||||
| }; | ||||
| 
 | ||||
| pub fn main() !void { | ||||
|    | ||||
|     //const stdin = io.getStdIn().reader(); | ||||
|     const stdout = io.getStdOut().writer(); | ||||
|     const args = try std.process.argsAlloc(std.heap.page_allocator); | ||||
|     if (args.len < 2) return error.ExpectedArgument; | ||||
|      | ||||
|     const listFileName: []const u8 = args[1]; | ||||
|     const listFileSize = (try cwd.statFile(listFileName)).size; | ||||
| 
 | ||||
|     const oracleFileName = "oracle-cards-20240205220208.js"; | ||||
|     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||
|     defer arena.deinit(); | ||||
|     const allocator = arena.allocator(); | ||||
| 
 | ||||
|      | ||||
|     //================================= | ||||
|     //      slice implementation | ||||
|     //================================= | ||||
| 
 | ||||
|     // const oracleFileSize = (try cwd.statFile(oracleFileName)).size; | ||||
|     // const oracleBuf = try allocator.alloc(u8, oracleFileSize); | ||||
|     // const oracleString = try fs.cwd().readFile(oracleFileName, oracleBuf); | ||||
|     // const parsedJson = json.parseFromSlice([]Card, allocator, oracleString, .{.ignore_unknown_fields = true}); | ||||
|     // try stdout.print("{any}", .{parsedJson}); | ||||
| 
 | ||||
|     //================================== | ||||
|     //          reader implementation | ||||
|     //================================== | ||||
|      | ||||
|      const oracleFile = try cwd.openFile(oracleFileName, .{}); | ||||
|      const oracleReader = oracleFile.reader(); | ||||
|      var diagnostics = json.Diagnostics{}; | ||||
|      // var jsonReader = json.reader(allocator, oracleReader); | ||||
|      var jsonReader = json.Reader(0x2000, @TypeOf(oracleReader)).init(allocator, oracleReader); | ||||
|      jsonReader.enableDiagnostics(&diagnostics); | ||||
|      const parsedJson = try json.parseFromTokenSource([]Card, allocator, &jsonReader, .{.ignore_unknown_fields = true}); | ||||
|      // if (parsedJson == error.InvalidCharacter) { | ||||
|      //     try stdout.print("{s}\n", .{jsonReader.buffer}); | ||||
|      //     try stdout.print("{any}\n", .{jsonReader.scanner.stack}); | ||||
|      //     const cursorPos = jsonReader.scanner.cursor; | ||||
|      //     try stdout.print("{s}\n", .{jsonReader.scanner.input}); | ||||
|      //     try stdout.print("{d}\n", .{cursorPos}); | ||||
|      //     try stdout.print("{c}\n", .{oracleString[cursorPos]}); | ||||
|      // } | ||||
|      try stdout.print("{?s}, {any}, {any}\n", .{parsedJson.value[0].oracle_text, diagnostics.getLine(), diagnostics.getColumn()}); | ||||
| 
 | ||||
|      //======= | ||||
|      // dynamic impl | ||||
|      //====== | ||||
|       | ||||
|     // const parsed = try json.parseFromSlice(json.Value, allocator, oracleString, .{}); | ||||
|     // defer parsed.deinit(); | ||||
|     // try stdout.print("{any}", .{parsed.value.array.items[0].string}); | ||||
|     // parsed.value.dump(); | ||||
| 
 | ||||
|     const listBuf = try allocator.alloc(u8, listFileSize); | ||||
|     var word = std.ArrayList(u8).init(allocator); | ||||
|     //var line = std.ArrayList(u8).init(allocator); | ||||
|     var cardName = std.ArrayList(u8).init(allocator); | ||||
|    for (try fs.cwd().readFile(listFileName, listBuf)) |char| { | ||||
|         if(char != '\n' and char != ' ') { | ||||
|             try word.append(char); | ||||
|         } else { | ||||
|             //try stdout.print("{s}\n", .{word.items}); | ||||
|             if( | ||||
|                 std.mem.indexOfAny(u8, word.items, "()0123456789") == null | ||||
|             ) { | ||||
|                 try cardName.appendSlice(word.items); | ||||
|                 try cardName.appendSlice(" "); | ||||
|             } | ||||
|             word.clearAndFree(); | ||||
|         } | ||||
|         // if (char != '\n' and (char < '0' or char > '9')) { | ||||
|         //     try line.append(char); | ||||
|         // }  | ||||
|         if (char == '\n') { | ||||
|             // try stdout.print("{s}\n", .{cardName.items}); | ||||
|             cardName.clearAndFree(); | ||||
|             //line.clearAndFree(); | ||||
|         } | ||||
|         //try stdout.print("{c}", .{char}); | ||||
|    } | ||||
| } | ||||
| 
 | ||||
| // fn findInTree(parent: anytype, tree: json.Value, query: []u8) json.Value { | ||||
| //     return switch(tree) |val| { | ||||
| //         .null, .bool, .integer, .float, .number_string, .string => { | ||||
| //             if(val == query) { | ||||
| //                 parent | ||||
| //             } else { | ||||
| //                 false | ||||
| //             } | ||||
| //         } else => { | ||||
| //             findInTree(val, val       | ||||
| //         } | ||||
| //     }; | ||||
| // } | ||||
| // // fn testWord(allocator: []u8, check: anytype) !bool { | ||||
|      | ||||
| // // } | ||||
|  | @ -0,0 +1,100 @@ | |||
| 1 Abrupt Decay (GK1) 57 | ||||
| 1 Anointed Peacekeeper (DMU) 2 | ||||
| 1 Archon of Emeria (ZNR) 4 | ||||
| 1 Arid Mesa (MH2) 244 | ||||
| 1 Ayara's Oathsworn (MAT) 11 | ||||
| 1 Badlands (3ED) 282 | ||||
| 1 Bayou (3ED) 283 | ||||
| 1 Birds of Paradise (DMR) 151 | ||||
| 1 Bloodstained Mire (KTK) 230 | ||||
| 1 Boseiju, Who Endures (NEO) 266 | ||||
| 1 Broadside Bombardiers (LCC) 54 | ||||
| 1 Cankerbloom (ONE) 294 | ||||
| 1 Caves of Chaos Adventurer (CLB) 167 | ||||
| 1 Chrome Mox (MRD) 152 | ||||
| 1 City of Brass (CHR) 112 | ||||
| 1 Comet, Stellar Pup (UNF) 275 | ||||
| 1 Dark Confidant (RVR) 323 | ||||
| 1 Deathrite Shaman (RVR) 175 | ||||
| 1 Duress (M20) 97 | ||||
| 1 Eladamri's Call (MH1) 197 | ||||
| 1 Elvish Spirit Guide (DMR) 157 | ||||
| 1 Endurance (MH2) 157 | ||||
| 1 Fatal Push (AER) 57 | ||||
| 1 Flooded Strand (KTK) 233 | ||||
| 1 Forest (ELD) 266 | ||||
| 1 Forth Eorlingas! (LTC) 56 | ||||
| 1 Generous Ent (LTR) 169 | ||||
| 1 Gitaxian Probe (NPH) 35 | ||||
| 1 Godless Shrine (RNA) 248 | ||||
| 1 Green Sun's Zenith (EMA) 169 | ||||
| 1 Grist, the Hunger Tide (MH2) 202 | ||||
| 1 Hexdrinker (MH1) 168 | ||||
| 1 Ignoble Hierarch (MH2) 166 | ||||
| 1 Inquisition of Kozilek (2X2) 80 | ||||
| 1 Inti, Seneschal of the Sun (LCI) 156 | ||||
| 1 Karakas (LTC) 367 | ||||
| 1 Kellan, Daring Traveler // Journey On (LCI) 231 | ||||
| 1 Laelia, the Blade Reforged (C21) 53 | ||||
| 1 Legolas's Quick Reflexes (LTC) 493 | ||||
| 1 Leyline Binding (PDMU) 24p | ||||
| 1 Lord Skitter, Sewer King (WOE) 97 | ||||
| 1 Lotus Petal (PLST) TMP-294 | ||||
| 1 Luminarch Aspirant (ZNR) 24 | ||||
| 1 Mana Confluence (JOU) 163 | ||||
| 1 Marsh Flats (MM3) 239 | ||||
| 1 Mawloc (40K) 133 | ||||
| 1 Mental Misstep (NPH) 38 | ||||
| 1 Minsc & Boo, Timeless Heroes (CLB) 285 | ||||
| 1 Misty Rainforest (MH2) 250 | ||||
| 1 Mosswood Dreadknight // Dread Whispers (WOE) 231 | ||||
| 1 Mother of Runes (PLST) DDO-20 | ||||
| 1 Mox Diamond (STH) 138 | ||||
| 1 Mox Emerald (2ED) 262 | ||||
| 1 Mox Jet (2ED) 263 | ||||
| 1 Mox Pearl (2ED) 264 | ||||
| 1 Noble Hierarch (UMA) 174 | ||||
| 1 Once Upon a Time (ELD) 169 | ||||
| 1 Opposition Agent (CMR) 141 | ||||
| 1 Orcish Bowmasters (LTR) 103 | ||||
| 1 Overgrown Tomb (GRN) 253 | ||||
| 1 Path to Exile (PLST) E02-3 | ||||
| 1 Plains (UND) 87 | ||||
| 1 Plateau (3ED) 284 | ||||
| 1 Polluted Delta (KTK) 239 | ||||
| 1 Prismatic Ending (MH2) 25 | ||||
| 1 Prismatic Vista (MH1) 244 | ||||
| 1 Questing Beast (ELD) 171 | ||||
| 1 Razorverge Thicket (ONE) 257 | ||||
| 1 Reanimate (MKC) 137 | ||||
| 1 Savannah (3ED) 285 | ||||
| 1 Scalding Tarn (MH2) 254 | ||||
| 1 Scavenging Ooze (M21) 204 | ||||
| 1 Scrubland (3ED) 286 | ||||
| 1 Seasoned Dungeoneer (CLB) 660 | ||||
| 1 Sentinel of the Nameless City (LCI) 383 | ||||
| 1 Shadowspear (PTHB) 236p | ||||
| 1 Simian Spirit Guide (TSR) 190 | ||||
| 1 Stomping Ground (SLD) 126 | ||||
| 1 Sungold Sentinel (MID) 332 | ||||
| 1 Swamp (UND) 91 | ||||
| 1 Swords to Plowshares (PLST) C16-78 | ||||
| 1 Taiga (3ED) 287 | ||||
| 1 Tarmogoyf (FUT) 153 | ||||
| 1 Temple Garden (GRN) 258 | ||||
| 1 Tenth District Hero (MKM) 34 | ||||
| 1 Thalia, Guardian of Thraben (VOW) 38 | ||||
| 1 Thalia, Heretic Cathar (EMN) 46 | ||||
| 1 Thoughtseize (THS) 107 | ||||
| 1 Tidehollow Sculler (MMA) 184 | ||||
| 1 Troll of Khazad-dûm (LTR) 111 | ||||
| 1 Underground Mortuary (MKM) 333 | ||||
| 1 Undermountain Adventurer (CLB) 260 | ||||
| 1 Unearth (MH1) 113 | ||||
| 1 Verdant Catacombs (MH2) 260 | ||||
| 1 Wasteland (SLD) 178 | ||||
| 1 White Plume Adventurer (CLB) 49 | ||||
| 1 Windswept Heath (KTK) 248 | ||||
| 1 Witherbloom Command (STX) 248 | ||||
| 1 Wooded Foothills (KTK) 249 | ||||
| 1 Wrenn and Six (MH1) 217 | ||||
		Loading…
	
		Reference in New Issue