|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/PuerkitoBio/goquery" |
| 11 | +) |
| 12 | + |
| 13 | +// TibiaCreaturesCreatureV3 func |
| 14 | +func TibiaCreaturesCreatureV3(race string) string { |
| 15 | + |
| 16 | + // Child of JSONData |
| 17 | + type Creature struct { |
| 18 | + Name string `json:"name"` |
| 19 | + Race string `json:"race"` |
| 20 | + ImageURL string `json:"image_url"` |
| 21 | + Description string `json:"description"` |
| 22 | + Behaviour string `json:"behaviour"` |
| 23 | + Hitpoints int `json:"hitpoints"` |
| 24 | + ImmuneTo []string `json:"immune"` |
| 25 | + StrongAgainst []string `json:"strong"` |
| 26 | + WeaknessAgainst []string `json:"weakness"` |
| 27 | + BeParalysed bool `json:"be_paralysed"` |
| 28 | + BeSummoned bool `json:"be_summoned"` |
| 29 | + SummonMana int `json:"summoned_mana"` |
| 30 | + BeConvinced bool `json:"be_convinced"` |
| 31 | + ConvincedMana int `json:"convinced_mana"` |
| 32 | + SeeInvisible bool `json:"see_invisible"` |
| 33 | + ExperiencePoints int `json:"experience_points"` |
| 34 | + IsLootable bool `json:"is_lootable"` |
| 35 | + LootList []string `json:"loot_list"` |
| 36 | + Featured bool `json:"featured"` |
| 37 | + } |
| 38 | + |
| 39 | + // |
| 40 | + // The base includes two levels: Creature and Information |
| 41 | + type JSONData struct { |
| 42 | + Creature Creature `json:"creature"` |
| 43 | + Information Information `json:"information"` |
| 44 | + } |
| 45 | + |
| 46 | + // Getting data with TibiadataHTMLDataCollectorV3 |
| 47 | + BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/library/?subtopic=creatures&race=" + TibiadataQueryEscapeStringV3(race)) |
| 48 | + |
| 49 | + // Loading HTML data into ReaderHTML for goquery with NewReader |
| 50 | + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) |
| 51 | + if err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + // Getting data |
| 56 | + InnerTableContainerTMP1, err := ReaderHTML.Find(".BoxContent div").First().NextAll().Html() |
| 57 | + if err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + // Regex to get data |
| 62 | + regex1 := regexp.MustCompile(`.*;">(.*)<\/h2> <img src="(.*)"\/>.*<p>(.*)<\/p> <p>(.*)<\/p> <p>(.*)<\/p>.*`) |
| 63 | + subma1 := regex1.FindAllStringSubmatch(InnerTableContainerTMP1, -1) |
| 64 | + |
| 65 | + // Preparing vars |
| 66 | + var CreatureDescription, CreatureBehaviour string |
| 67 | + var CreatureLootList, CreatureImmuneTo, CreatureStrongAgainst, CreatureWeaknessAgainst []string |
| 68 | + var CreatureHitpoints, CreatureSummonedMana, CreatureConvincedMana, CreatureExperiencePoints int |
| 69 | + var CreatureBeParalysed, CreatureBeSummoned, CreatureBeConvinced, CreatureSeeInvisible, CreatureIsLootable bool |
| 70 | + |
| 71 | + // Preparing data for JSONData |
| 72 | + if len(subma1) > 0 { |
| 73 | + |
| 74 | + // Description |
| 75 | + CreatureDescription = strings.ReplaceAll(subma1[0][3], "<br/>", "\n") |
| 76 | + |
| 77 | + // Behaviour |
| 78 | + // Regex to get data.. |
| 79 | + regex2 := regexp.MustCompile(`.*have (.*) hitpoints. (.*)`) |
| 80 | + subma2 := regex2.FindAllStringSubmatch(subma1[0][4], -1) |
| 81 | + // Add data to vars |
| 82 | + CreatureHitpoints = TibiadataStringToIntegerV3(subma2[0][1]) |
| 83 | + CreatureBehaviour = subma2[0][2] |
| 84 | + if !strings.Contains(subma1[0][4], "cannot be paralysed") { |
| 85 | + CreatureBeParalysed = true |
| 86 | + } |
| 87 | + if strings.Contains(subma1[0][4], "sense invisible creatures") { |
| 88 | + CreatureSeeInvisible = true |
| 89 | + } |
| 90 | + if strings.Contains(subma1[0][4], " are immune to ") { |
| 91 | + regex21 := regexp.MustCompile(`.*are immune to (.*)`) |
| 92 | + subma21 := regex21.FindAllStringSubmatch(subma1[0][4], -1) |
| 93 | + CreatureImmuneToTmp := strings.Split(subma21[0][1], " damage") |
| 94 | + CreatureImmuneTo = strings.Split(strings.Replace(CreatureImmuneToTmp[0], " and ", ", ", 1), ", ") |
| 95 | + } |
| 96 | + if strings.Contains(subma1[0][4], " are strong against ") { |
| 97 | + regex22 := regexp.MustCompile(`.*are strong against (.*)`) |
| 98 | + subma22 := regex22.FindAllStringSubmatch(subma1[0][4], -1) |
| 99 | + CreatureStrongAgainstTmp := strings.Split(subma22[0][1], " damage") |
| 100 | + CreatureStrongAgainst = strings.Split(strings.Replace(CreatureStrongAgainstTmp[0], " and ", ", ", 1), ", ") |
| 101 | + } |
| 102 | + if strings.Contains(subma1[0][4], " are weak against ") { |
| 103 | + regex23 := regexp.MustCompile(`.*are weak against (.*)`) |
| 104 | + subma23 := regex23.FindAllStringSubmatch(subma1[0][4], -1) |
| 105 | + CreatureWeaknessAgainstTmp := strings.Split(subma23[0][1], " damage") |
| 106 | + CreatureWeaknessAgainst = strings.Split(strings.Replace(CreatureWeaknessAgainstTmp[0], " and ", ", ", 1), ", ") |
| 107 | + } |
| 108 | + if strings.Contains(subma1[0][4], "It takes ") && strings.Contains(subma1[0][4], " mana to ") { |
| 109 | + regex24 := regexp.MustCompile(`.*It takes (.*) mana to (.*)`) |
| 110 | + subma24 := regex24.FindAllStringSubmatch(subma1[0][4], -1) |
| 111 | + if strings.Contains(subma24[0][2], "convince these creatures but they cannot be") { |
| 112 | + CreatureBeConvinced = true |
| 113 | + CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1]) |
| 114 | + } else if strings.Contains(subma24[0][2], "summon or convince these creatures") { |
| 115 | + CreatureBeSummoned = true |
| 116 | + CreatureSummonedMana = TibiadataStringToIntegerV3(subma24[0][1]) |
| 117 | + CreatureBeConvinced = true |
| 118 | + CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1]) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Loot |
| 123 | + // Regex to get loot information |
| 124 | + regex3 := regexp.MustCompile(`.*yield (.*) experience.*carry (.*)with them.`) |
| 125 | + subma3 := regex3.FindAllStringSubmatch(subma1[0][5], -1) |
| 126 | + // Adding data to vars |
| 127 | + CreatureExperiencePoints = TibiadataStringToIntegerV3(subma3[0][1]) |
| 128 | + if subma3[0][2] != "nothing" { |
| 129 | + CreatureIsLootable = true |
| 130 | + CreatureLootListTmp := strings.Split(strings.Replace(strings.Replace(subma3[0][2], "items ", "", 1), " and sometimes other ", "", 1), ", ") |
| 131 | + for _, str := range CreatureLootListTmp { |
| 132 | + if str != "" { |
| 133 | + CreatureLootList = append(CreatureLootList, str) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // |
| 140 | + // Build the data-blob |
| 141 | + jsonData := JSONData{ |
| 142 | + Creature{ |
| 143 | + Name: subma1[0][1], |
| 144 | + Race: race, |
| 145 | + ImageURL: subma1[0][2], |
| 146 | + Description: CreatureDescription, |
| 147 | + Behaviour: CreatureBehaviour, |
| 148 | + Hitpoints: CreatureHitpoints, |
| 149 | + ImmuneTo: CreatureImmuneTo, |
| 150 | + StrongAgainst: CreatureStrongAgainst, |
| 151 | + WeaknessAgainst: CreatureWeaknessAgainst, |
| 152 | + BeParalysed: CreatureBeParalysed, |
| 153 | + BeSummoned: CreatureBeSummoned, |
| 154 | + SummonMana: CreatureSummonedMana, |
| 155 | + BeConvinced: CreatureBeConvinced, |
| 156 | + ConvincedMana: CreatureConvincedMana, |
| 157 | + SeeInvisible: CreatureSeeInvisible, |
| 158 | + ExperiencePoints: CreatureExperiencePoints, |
| 159 | + IsLootable: CreatureIsLootable, |
| 160 | + LootList: CreatureLootList, |
| 161 | + }, |
| 162 | + Information{ |
| 163 | + APIVersion: TibiadataAPIversion, |
| 164 | + Timestamp: TibiadataDatetimeV3(""), |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + js, _ := json.Marshal(jsonData) |
| 169 | + if TibiadataDebug { |
| 170 | + fmt.Printf("%s\n", js) |
| 171 | + } |
| 172 | + return string(js) |
| 173 | +} |
0 commit comments