|
| 1 | +{$I WaspLib/osrs.simba} |
| 2 | + |
| 3 | +const |
| 4 | + ENDPOINT = 'https://oldschool.runescape.wiki/api.php?'; |
| 5 | + SILENT: Boolean = False; |
| 6 | + TEST_ITEM: String = ''; |
| 7 | + |
| 8 | +function GetEndPoint(name: String): String; |
| 9 | +begin |
| 10 | + Result := ENDPOINT + 'action=parse&page=' + |
| 11 | + name.Replace(' ', '_').Replace('+', '%2B') + |
| 12 | + '&prop=wikitext&redirects=1&format=json'; |
| 13 | +end; |
| 14 | + |
| 15 | +type TSlotArray = array [0..12] of TStringArray; |
| 16 | + |
| 17 | +var |
| 18 | + Slots: TSlotArray; |
| 19 | + |
| 20 | +function TStringArray.GetStr(): String; |
| 21 | +var |
| 22 | + i: Integer; |
| 23 | +begin |
| 24 | + if Self = [] then Exit('[]'); |
| 25 | + Result := '['; |
| 26 | + for i := 0 to High(Self)-1 do |
| 27 | + Result += '"' + Self[i] + '", '; |
| 28 | + Result += '"' + Self[High(Self)] + '"]'; |
| 29 | +end; |
| 30 | + |
| 31 | +function TSlotArray.GetStr(): String; |
| 32 | +var |
| 33 | + i: Integer; |
| 34 | +begin |
| 35 | + for i := 0 to 10 do |
| 36 | + Result += ' "' + ToStr(ERSEquipment(i)).After('ERSEquipment.').ToLower() + '": ' + Self[i].Unique().Sorted().GetStr() + ',' + LINE_SEP; |
| 37 | + |
| 38 | + Result += ' "spec_weapons": ' + Self[11].Unique().Sorted().GetStr() + ',' + LINE_SEP; |
| 39 | + Result += ' "2h": ' + Self[12].Unique().Sorted().GetStr(); |
| 40 | +end; |
| 41 | + |
| 42 | +procedure FillSlot(item, txt: String); |
| 43 | +var |
| 44 | + str: String; |
| 45 | +begin |
| 46 | + if not txt.RegExprExists('equipable(?:[0-9])? = Yes') then |
| 47 | + Exit; |
| 48 | + |
| 49 | + if txt.Contains('slot = weapon') then |
| 50 | + begin |
| 51 | + Slots[ERSEquipment.WEAPON] += item; |
| 52 | + if txt.Contains('Category:Weapons with Special attacks') then |
| 53 | + Slots[11] += item; |
| 54 | + Exit; |
| 55 | + end; |
| 56 | + |
| 57 | + if txt.Contains('slot = 2h') then |
| 58 | + begin |
| 59 | + Slots[12] += item; |
| 60 | + if txt.Contains('Category:Weapons with Special attacks') then |
| 61 | + Slots[11] += item; |
| 62 | + Exit; |
| 63 | + end; |
| 64 | + |
| 65 | + if txt.Contains('slot = head') then |
| 66 | + begin |
| 67 | + Slots[ERSEquipment.HEAD] += item; |
| 68 | + Exit; |
| 69 | + end; |
| 70 | + |
| 71 | + if txt.Contains('slot = cape') then |
| 72 | + begin |
| 73 | + Slots[ERSEquipment.CAPE] += item; |
| 74 | + Exit; |
| 75 | + end; |
| 76 | + |
| 77 | + if txt.Contains('slot = neck') then |
| 78 | + begin |
| 79 | + Slots[ERSEquipment.NECK] += item; |
| 80 | + Exit; |
| 81 | + end; |
| 82 | + |
| 83 | + if txt.Contains('slot = ammo') then |
| 84 | + begin |
| 85 | + Slots[ERSEquipment.AMMO] += item; |
| 86 | + Exit; |
| 87 | + end; |
| 88 | + |
| 89 | + if txt.Contains('slot = body') then |
| 90 | + begin |
| 91 | + Slots[ERSEquipment.BODY] += item; |
| 92 | + Exit; |
| 93 | + end; |
| 94 | + |
| 95 | + if txt.Contains('slot = shield') then |
| 96 | + begin |
| 97 | + Slots[ERSEquipment.SHIELD] += item; |
| 98 | + Exit; |
| 99 | + end; |
| 100 | + |
| 101 | + if txt.Contains('slot = legs') then |
| 102 | + begin |
| 103 | + Slots[ERSEquipment.LEGS] += item; |
| 104 | + Exit; |
| 105 | + end; |
| 106 | + |
| 107 | + if txt.Contains('slot = hands') then |
| 108 | + begin |
| 109 | + Slots[ERSEquipment.HANDS] += item; |
| 110 | + Exit; |
| 111 | + end; |
| 112 | + |
| 113 | + if txt.Contains('slot = feet') then |
| 114 | + begin |
| 115 | + Slots[ERSEquipment.FEET] += item; |
| 116 | + Exit; |
| 117 | + end; |
| 118 | + |
| 119 | + if txt.Contains('slot = ring') then |
| 120 | + Slots[ERSEquipment.RING] += item; |
| 121 | +end; |
| 122 | + |
| 123 | +procedure TStringArray.Filter(); |
| 124 | +var |
| 125 | + i: Integer; |
| 126 | + tmp: String; |
| 127 | +begin |
| 128 | + for i := High(Self) downto 0 do |
| 129 | + begin |
| 130 | + if Self[i].StartsWith('noted ') or Self[i].EndsWith(' 100') or |
| 131 | + Self[i].EndsWith(' 75') or Self[i].EndsWith(' 50') or |
| 132 | + Self[i].EndsWith(' 25') or Self[i].EndsWith(' 0') then |
| 133 | + begin |
| 134 | + Delete(Self, i, 1); |
| 135 | + Continue; |
| 136 | + end; |
| 137 | + |
| 138 | + //get rid of jewellery charges... |
| 139 | + if not Self[i].EndsWith(')') then Continue; |
| 140 | + |
| 141 | + tmp := Self[i].Between('(', ')'); |
| 142 | + if not tmp.IsInteger then Continue; |
| 143 | + |
| 144 | + tmp := Self[i].Replace('(' + tmp + ')', '(' + ToStr(StrToInt(tmp) + 1) + ')'); |
| 145 | + |
| 146 | + if Self.Contains(tmp) then |
| 147 | + Delete(Self, i, 1); |
| 148 | + end; |
| 149 | +end; |
| 150 | + |
| 151 | + |
| 152 | +var |
| 153 | + items: TStringArray; |
| 154 | + i: Integer; |
| 155 | + url: String; |
| 156 | + json, tmp: TJSONItem; |
| 157 | + text: String; |
| 158 | +begin |
| 159 | + ClearSimbaOutput(); |
| 160 | + |
| 161 | + items := FileReadLines(ItemFinder.Database.DataDir + 'item').Unique(); |
| 162 | + items.Filter(); |
| 163 | + |
| 164 | + for i := 0 to High(items) do |
| 165 | + begin |
| 166 | + if (TEST_ITEM <> '') and (i > 0) then Break; |
| 167 | + if i mod 300 = 0 then |
| 168 | + WriteLn('Reading information from the wiki ', i, '/', High(items), ' items done.'); |
| 169 | + |
| 170 | + if TEST_ITEM <> '' then |
| 171 | + url := GetEndPoint(TEST_ITEM) |
| 172 | + else |
| 173 | + url := GetEndPoint(items[i]); |
| 174 | + |
| 175 | + if TEST_ITEM <> '' then |
| 176 | + WriteLn url; |
| 177 | + |
| 178 | + try |
| 179 | + json := HTTPClient.GetJson(url); |
| 180 | + except |
| 181 | + WriteLn('Failed to fetch: ', items[i]); |
| 182 | + end; |
| 183 | + |
| 184 | + if TEST_ITEM <> '' then |
| 185 | + WriteLn json.ToJSON(); |
| 186 | + |
| 187 | + if json = nil then |
| 188 | + begin |
| 189 | + WriteLn('Failed to fetch, json is nil: ', items[i]); |
| 190 | + Continue; |
| 191 | + end; |
| 192 | + |
| 193 | + try |
| 194 | + tmp := json.Item['parse']; |
| 195 | + except |
| 196 | + WriteLn('Failed to find parse: ', items[i]); |
| 197 | + json.Free(); |
| 198 | + Continue; |
| 199 | + end; |
| 200 | + |
| 201 | + try |
| 202 | + text := tmp.Item['wikitext'].ToJSON(); |
| 203 | + except |
| 204 | + if not SILENT then |
| 205 | + WriteLn('Failed to find wikitext: ', items[i]); |
| 206 | + json.Free(); |
| 207 | + Continue; |
| 208 | + end; |
| 209 | + |
| 210 | + if TEST_ITEM <> '' then |
| 211 | + FillSlot(TEST_ITEM, text) |
| 212 | + else |
| 213 | + FillSlot(items[i], text); |
| 214 | + json.Free(); |
| 215 | + end; |
| 216 | + |
| 217 | + text := '{' + LINE_SEP + Slots.GetStr() + LINE_SEP + '}'; |
| 218 | + WriteLn text; |
| 219 | + if TEST_ITEM = '' then |
| 220 | + FileWrite(GearData.PATH, text); |
| 221 | +end; |
0 commit comments