Archived
1

All code formatted with gofmt

This commit is contained in:
2017-10-18 07:03:34 +04:00
parent df58e25adc
commit 6f374e560e
58 changed files with 1957 additions and 1962 deletions

View File

@@ -4,19 +4,19 @@
package parsers
import (
// local
"../appcontext"
"../parsers/parsersinterface"
// local
"../appcontext"
"../parsers/parsersinterface"
)
var (
c *appcontext.Context
c *appcontext.Context
)
type Parsers struct {}
type Parsers struct{}
func New(ac *appcontext.Context) {
c = ac
p := &Parsers{}
c.RegisterParsersInterface(parsersinterface.ParsersInterface(p))
c = ac
p := &Parsers{}
c.RegisterParsersInterface(parsersinterface.ParsersInterface(p))
}

View File

@@ -4,15 +4,14 @@
package parsersinterface
import (
// 3rd party
"github.com/go-telegram-bot-api/telegram-bot-api"
// local
"../../dbmapping"
// 3rd party
"github.com/go-telegram-bot-api/telegram-bot-api"
// local
"../../dbmapping"
)
type ParsersInterface interface {
ParsePokememe(text string, player_raw dbmapping.Player) string
ParseProfile(update tgbotapi.Update, player_raw dbmapping.Player) string
ReturnPoints(points int) string
ParsePokememe(text string, player_raw dbmapping.Player) string
ParseProfile(update tgbotapi.Update, player_raw dbmapping.Player) string
ReturnPoints(points int) string
}

View File

@@ -4,265 +4,263 @@
package parsers
import (
// stdlib
"log"
"regexp"
"strings"
"strconv"
"time"
// local
"../dbmapping"
// stdlib
"log"
"regexp"
"strconv"
"strings"
"time"
// local
"../dbmapping"
)
// Internal functions
func (p *Parsers) getPoints(points_str string) int {
value := 0
if strings.HasSuffix(points_str, "K") {
value_num := strings.Replace(points_str, "K", "", 1)
value_float, _ := strconv.ParseFloat(value_num, 64)
value = int(value_float * 1000)
} else if strings.HasSuffix(points_str, "M") {
value_num := strings.Replace(points_str, "M", "", 1)
value_float, _ := strconv.ParseFloat(value_num, 64)
value = int(value_float * 1000000)
} else {
value, _ = strconv.Atoi(points_str)
}
return value
value := 0
if strings.HasSuffix(points_str, "K") {
value_num := strings.Replace(points_str, "K", "", 1)
value_float, _ := strconv.ParseFloat(value_num, 64)
value = int(value_float * 1000)
} else if strings.HasSuffix(points_str, "M") {
value_num := strings.Replace(points_str, "M", "", 1)
value_float, _ := strconv.ParseFloat(value_num, 64)
value = int(value_float * 1000000)
} else {
value, _ = strconv.Atoi(points_str)
}
return value
}
// External functions
func (p *Parsers) ParsePokememe(text string, player_raw dbmapping.Player) string {
var defendable_pokememe bool = false
pokememe_info_strings := strings.Split(text, "\n")
pokememe_info_runed_strings := make([][]rune, 0)
for i := range(pokememe_info_strings) {
pokememe_info_runed_strings = append(pokememe_info_runed_strings, []rune(pokememe_info_strings[i]))
}
var defendable_pokememe bool = false
pokememe_info_strings := strings.Split(text, "\n")
pokememe_info_runed_strings := make([][]rune, 0)
for i := range pokememe_info_strings {
pokememe_info_runed_strings = append(pokememe_info_runed_strings, []rune(pokememe_info_strings[i]))
}
if len(pokememe_info_runed_strings) == 13 {
defendable_pokememe = true
}
if len(pokememe_info_runed_strings) == 13 {
defendable_pokememe = true
}
// Getting elements
elements := []dbmapping.Element{}
element_emojis := make([]string, 0)
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][11]))
if len(pokememe_info_runed_strings[4]) > 12 {
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][13]))
}
if len(pokememe_info_runed_strings[4]) > 14 {
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][15]))
}
// Getting elements
elements := []dbmapping.Element{}
element_emojis := make([]string, 0)
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][11]))
if len(pokememe_info_runed_strings[4]) > 12 {
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][13]))
}
if len(pokememe_info_runed_strings[4]) > 14 {
element_emojis = append(element_emojis, string(pokememe_info_runed_strings[4][15]))
}
err := c.Db.Select(&elements, "SELECT * FROM elements WHERE symbol IN ('" + strings.Join(element_emojis, "', '") + "')")
if err != nil {
log.Printf(err.Error())
return "fail"
}
err := c.Db.Select(&elements, "SELECT * FROM elements WHERE symbol IN ('"+strings.Join(element_emojis, "', '")+"')")
if err != nil {
log.Printf(err.Error())
return "fail"
}
// Getting hit-points
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
hitPoints := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[5]), -1)
if len(hitPoints) != 3 {
log.Printf("Can't parse hitpoints!")
log.Println(pokememe_info_runed_strings[5])
return "fail"
}
// Getting hit-points
hitPointsRx := regexp.MustCompile("(\\d|\\.)+(K|M)?")
hitPoints := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[5]), -1)
if len(hitPoints) != 3 {
log.Printf("Can't parse hitpoints!")
log.Println(pokememe_info_runed_strings[5])
return "fail"
}
defence := "0"
price := "0"
defence := "0"
price := "0"
locations := []dbmapping.Location{}
locations := []dbmapping.Location{}
purchaseable := false
image := ""
purchaseable := false
image := ""
if defendable_pokememe {
// Actions for high-grade pokememes
defenceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[6]), -1)
if len(defenceMatch) < 1 {
log.Printf("Can't parse defence!")
log.Println(pokememe_info_runed_strings[6])
return "fail"
}
defence = defenceMatch[0]
priceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[7]), -1)
if len(priceMatch) < 1 {
log.Printf("Can't parse price!")
log.Println(pokememe_info_runed_strings[7])
return "fail"
}
price = priceMatch[0]
locationsPrepare := strings.Split(string(pokememe_info_runed_strings[8]), ": ")
if len(locationsPrepare) < 2 {
log.Printf("Can't parse locations!")
log.Println(pokememe_info_runed_strings[8])
return "fail"
}
locationsNames := strings.Split(locationsPrepare[1], ", ")
if len(locationsNames) < 1 {
log.Printf("Can't parse locations!")
log.Println(locationsPrepare)
return "fail"
}
if defendable_pokememe {
// Actions for high-grade pokememes
defenceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[6]), -1)
if len(defenceMatch) < 1 {
log.Printf("Can't parse defence!")
log.Println(pokememe_info_runed_strings[6])
return "fail"
}
defence = defenceMatch[0]
priceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[7]), -1)
if len(priceMatch) < 1 {
log.Printf("Can't parse price!")
log.Println(pokememe_info_runed_strings[7])
return "fail"
}
price = priceMatch[0]
locationsPrepare := strings.Split(string(pokememe_info_runed_strings[8]), ": ")
if len(locationsPrepare) < 2 {
log.Printf("Can't parse locations!")
log.Println(pokememe_info_runed_strings[8])
return "fail"
}
locationsNames := strings.Split(locationsPrepare[1], ", ")
if len(locationsNames) < 1 {
log.Printf("Can't parse locations!")
log.Println(locationsPrepare)
return "fail"
}
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('" + strings.Join(locationsNames, "', '") + "')")
if err2 != nil {
log.Printf(err2.Error())
return "fail"
}
if strings.HasSuffix(string(pokememe_info_runed_strings[9]), "Можно") {
purchaseable = true
}
image = strings.Replace(string(pokememe_info_runed_strings[12]), " ", "", -1)
} else {
// Actions for low-grade pokememes
defence = hitPoints[0]
priceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[6]), -1)
if len(priceMatch) < 1 {
log.Printf("Can't parse price!")
log.Println(pokememe_info_runed_strings[6])
return "fail"
}
price = priceMatch[0]
locationsPrepare := strings.Split(string(pokememe_info_runed_strings[7]), ": ")
if len(locationsPrepare) < 2 {
log.Printf("Can't parse locations!")
log.Println(pokememe_info_runed_strings[7])
return "fail"
}
locationsNames := strings.Split(locationsPrepare[1], ", ")
if len(locationsNames) < 1 {
log.Printf("Can't parse locations!")
log.Println(locationsPrepare)
return "fail"
}
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
if err2 != nil {
log.Printf(err2.Error())
return "fail"
}
if strings.HasSuffix(string(pokememe_info_runed_strings[9]), "Можно") {
purchaseable = true
}
image = strings.Replace(string(pokememe_info_runed_strings[12]), " ", "", -1)
} else {
// Actions for low-grade pokememes
defence = hitPoints[0]
priceMatch := hitPointsRx.FindAllString(string(pokememe_info_runed_strings[6]), -1)
if len(priceMatch) < 1 {
log.Printf("Can't parse price!")
log.Println(pokememe_info_runed_strings[6])
return "fail"
}
price = priceMatch[0]
locationsPrepare := strings.Split(string(pokememe_info_runed_strings[7]), ": ")
if len(locationsPrepare) < 2 {
log.Printf("Can't parse locations!")
log.Println(pokememe_info_runed_strings[7])
return "fail"
}
locationsNames := strings.Split(locationsPrepare[1], ", ")
if len(locationsNames) < 1 {
log.Printf("Can't parse locations!")
log.Println(locationsPrepare)
return "fail"
}
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('" + strings.Join(locationsNames, "', '") + "')")
if err2 != nil {
log.Printf(err2.Error())
return "fail"
}
if strings.HasSuffix(string(pokememe_info_runed_strings[8]), "Можно") {
purchaseable = true
}
image = strings.Replace(string(pokememe_info_runed_strings[11]), " ", "", -1)
}
err2 := c.Db.Select(&locations, "SELECT * FROM locations WHERE name IN ('"+strings.Join(locationsNames, "', '")+"')")
if err2 != nil {
log.Printf(err2.Error())
return "fail"
}
if strings.HasSuffix(string(pokememe_info_runed_strings[8]), "Можно") {
purchaseable = true
}
image = strings.Replace(string(pokememe_info_runed_strings[11]), " ", "", -1)
}
grade := string(pokememe_info_runed_strings[0][0])
name := string(pokememe_info_runed_strings[0][3:])
description := string(pokememe_info_runed_strings[1])
log.Printf("Pokememe grade: " + grade)
log.Printf("Pokememe name: " + name)
log.Printf("Pokememe description: " + description)
log.Printf("Elements:")
for i := range elements {
log.Printf(elements[i].Symbol + " " + elements[i].Name)
}
log.Printf("Attack: " + hitPoints[0])
log.Printf("HP: " + hitPoints[1])
log.Printf("MP: " + hitPoints[2])
log.Printf("Defence: " + defence)
log.Printf("Price: " + price)
log.Printf("Locations:")
for i := range locations {
log.Printf(locations[i].Symbol + " " + locations[i].Name)
}
if purchaseable {
log.Printf("Purchaseable")
} else {
log.Printf("Non-purchaseable")
}
log.Printf("Image: " + image)
grade := string(pokememe_info_runed_strings[0][0])
name := string(pokememe_info_runed_strings[0][3:])
description := string(pokememe_info_runed_strings[1])
log.Printf("Pokememe grade: " + grade)
log.Printf("Pokememe name: " + name)
log.Printf("Pokememe description: " + description)
log.Printf("Elements:")
for i := range(elements) {
log.Printf(elements[i].Symbol + " " + elements[i].Name)
}
log.Printf("Attack: " + hitPoints[0])
log.Printf("HP: " + hitPoints[1])
log.Printf("MP: " + hitPoints[2])
log.Printf("Defence: " + defence)
log.Printf("Price: " + price)
log.Printf("Locations:")
for i := range(locations) {
log.Printf(locations[i].Symbol + " " + locations[i].Name)
}
if purchaseable {
log.Printf("Purchaseable")
} else {
log.Printf("Non-purchaseable")
}
log.Printf("Image: " + image)
// Building pokememe
pokememe := dbmapping.Pokememe{}
// Checking if pokememe exists in database
err3 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='"+grade+"' AND name='"+name+"';"))
if err3 != nil {
log.Printf("Adding new pokememe...")
} else {
log.Printf("This pokememe already exist. Return specific error.")
return "dup"
}
// Building pokememe
pokememe := dbmapping.Pokememe{}
// Checking if pokememe exists in database
err3 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='" + grade + "' AND name='" + name + "';"))
if err3 != nil {
log.Printf("Adding new pokememe...")
} else {
log.Printf("This pokememe already exist. Return specific error.")
return "dup"
}
grade_int, _ := strconv.Atoi(grade)
attack_int := p.getPoints(hitPoints[0])
hp_int := p.getPoints(hitPoints[1])
mp_int := p.getPoints(hitPoints[2])
defence_int := p.getPoints(defence)
price_int := p.getPoints(price)
grade_int, _ := strconv.Atoi(grade)
attack_int := p.getPoints(hitPoints[0])
hp_int := p.getPoints(hitPoints[1])
mp_int := p.getPoints(hitPoints[2])
defence_int := p.getPoints(defence)
price_int := p.getPoints(price)
pokememe.Grade = grade_int
pokememe.Name = name
pokememe.Description = description
pokememe.Attack = attack_int
pokememe.HP = hp_int
pokememe.MP = mp_int
pokememe.Defence = defence_int
pokememe.Price = price_int
if purchaseable {
pokememe.Purchaseable = true
} else {
pokememe.Purchaseable = false
}
pokememe.Image_url = image
pokememe.Player_id = player_raw.Id
pokememe.Created_at = time.Now().UTC()
pokememe.Grade = grade_int
pokememe.Name = name
pokememe.Description = description
pokememe.Attack = attack_int
pokememe.HP = hp_int
pokememe.MP = mp_int
pokememe.Defence = defence_int
pokememe.Price = price_int
if purchaseable {
pokememe.Purchaseable = true
} else {
pokememe.Purchaseable = false
}
pokememe.Image_url = image
pokememe.Player_id = player_raw.Id
pokememe.Created_at = time.Now().UTC()
_, err4 := c.Db.NamedExec("INSERT INTO pokememes VALUES(NULL, :grade, :name, :description, :attack, :hp, :mp, :defence, :price, :purchaseable, :image_url, :player_id, :created_at)", &pokememe)
if err4 != nil {
log.Printf(err4.Error())
return "fail"
}
_, err4 := c.Db.NamedExec("INSERT INTO pokememes VALUES(NULL, :grade, :name, :description, :attack, :hp, :mp, :defence, :price, :purchaseable, :image_url, :player_id, :created_at)", &pokememe)
if err4 != nil {
log.Printf(err4.Error())
return "fail"
}
// Getting new pokememe
err5 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='"+grade+"' AND name='"+name+"';"))
if err5 != nil {
log.Printf("Pokememe isn't added!")
return "fail"
}
for i := range elements {
link := dbmapping.PokememeElement{}
link.Pokememe_id = pokememe.Id
link.Element_id = elements[i].Id
link.Created_at = time.Now().UTC()
// Getting new pokememe
err5 := c.Db.Get(&pokememe, c.Db.Rebind("SELECT * FROM pokememes WHERE grade='" + grade + "' AND name='" + name + "';"))
if err5 != nil {
log.Printf("Pokememe isn't added!")
return "fail"
}
for i := range(elements) {
link := dbmapping.PokememeElement{}
link.Pokememe_id = pokememe.Id
link.Element_id = elements[i].Id
link.Created_at = time.Now().UTC()
_, err6 := c.Db.NamedExec("INSERT INTO pokememes_elements VALUES(NULL, :pokememe_id, :element_id, :created_at)", &link)
if err6 != nil {
log.Printf(err6.Error())
return "fail"
}
}
for i := range locations {
link := dbmapping.PokememeLocation{}
link.Pokememe_id = pokememe.Id
link.Location_id = locations[i].Id
link.Created_at = time.Now().UTC()
_, err6 := c.Db.NamedExec("INSERT INTO pokememes_elements VALUES(NULL, :pokememe_id, :element_id, :created_at)", &link)
if err6 != nil {
log.Printf(err6.Error())
return "fail"
}
}
for i := range(locations) {
link := dbmapping.PokememeLocation{}
link.Pokememe_id = pokememe.Id
link.Location_id = locations[i].Id
link.Created_at = time.Now().UTC()
_, err7 := c.Db.NamedExec("INSERT INTO pokememes_locations VALUES(NULL, :pokememe_id, :location_id, :created_at)", &link)
if err7 != nil {
log.Printf(err7.Error())
return "fail"
}
}
_, err7 := c.Db.NamedExec("INSERT INTO pokememes_locations VALUES(NULL, :pokememe_id, :location_id, :created_at)", &link)
if err7 != nil {
log.Printf(err7.Error())
return "fail"
}
}
return "ok"
return "ok"
}
func (p *Parsers) ReturnPoints(points int) string {
if points < 1000 {
return strconv.Itoa(points)
} else if points < 1000000 {
float_num := float64(points) / 1000.0
return strconv.FormatFloat(float_num, 'f', -1, 64) + "K"
} else {
float_num := float64(points) / 1000000.0
return strconv.FormatFloat(float_num, 'f', -1, 64) + "M"
}
if points < 1000 {
return strconv.Itoa(points)
} else if points < 1000000 {
float_num := float64(points) / 1000.0
return strconv.FormatFloat(float_num, 'f', -1, 64) + "K"
} else {
float_num := float64(points) / 1000000.0
return strconv.FormatFloat(float_num, 'f', -1, 64) + "M"
}
}

View File

@@ -4,279 +4,278 @@
package parsers
import (
// stdlib
"log"
"regexp"
"strings"
"strconv"
"time"
// 3rd party
// stdlib
"log"
"regexp"
"strconv"
"strings"
"time"
// 3rd party
"github.com/go-telegram-bot-api/telegram-bot-api"
// local
"../dbmapping"
// local
"../dbmapping"
)
// Internal functions
func (p *Parsers) fillProfilePokememe(profile_id int, meme string, attack string, rarity string) {
spk_raw := dbmapping.Pokememe{}
err := c.Db.Get(&spk_raw, c.Db.Rebind("SELECT * FROM pokememes WHERE name='" + meme + "';"))
if err != nil {
log.Println(err)
} else {
attack_int := p.getPoints(attack)
// Improve it. Game's precision is unstable
orig_attack := float64(spk_raw.Attack)
if rarity == "rare" {
orig_attack = orig_attack * 1.1
}
level := int(float64(attack_int) / orig_attack)
spk_raw := dbmapping.Pokememe{}
err := c.Db.Get(&spk_raw, c.Db.Rebind("SELECT * FROM pokememes WHERE name='"+meme+"';"))
if err != nil {
log.Println(err)
} else {
attack_int := p.getPoints(attack)
// Improve it. Game's precision is unstable
orig_attack := float64(spk_raw.Attack)
if rarity == "rare" {
orig_attack = orig_attack * 1.1
}
level := int(float64(attack_int) / orig_attack)
ppk := dbmapping.ProfilePokememe{}
ppk.Profile_id = profile_id
ppk.Pokememe_id = spk_raw.Id
ppk.Pokememe_lvl = level
ppk.Pokememe_rarity = rarity
ppk.Created_at = time.Now().UTC()
_, err2 := c.Db.NamedExec("INSERT INTO `profiles_pokememes` VALUES(NULL, :profile_id, :pokememe_id, :pokememe_lvl, :pokememe_rarity, :created_at)", &ppk)
if err2 != nil {
log.Println(err2)
}
}
ppk := dbmapping.ProfilePokememe{}
ppk.Profile_id = profile_id
ppk.Pokememe_id = spk_raw.Id
ppk.Pokememe_lvl = level
ppk.Pokememe_rarity = rarity
ppk.Created_at = time.Now().UTC()
_, err2 := c.Db.NamedExec("INSERT INTO `profiles_pokememes` VALUES(NULL, :profile_id, :pokememe_id, :pokememe_lvl, :pokememe_rarity, :created_at)", &ppk)
if err2 != nil {
log.Println(err2)
}
}
}
// External functions
func (p *Parsers) ParseProfile(update tgbotapi.Update, player_raw dbmapping.Player) string {
text := update.Message.Text
log.Println(text)
text := update.Message.Text
log.Println(text)
profile_info_strings := strings.Split(text, "\n")
profile_info_runed_strings := make([][]rune, 0)
for i := range(profile_info_strings) {
profile_info_runed_strings = append(profile_info_runed_strings, []rune(profile_info_strings[i]))
}
profile_info_strings := strings.Split(text, "\n")
profile_info_runed_strings := make([][]rune, 0)
for i := range profile_info_strings {
profile_info_runed_strings = append(profile_info_runed_strings, []rune(profile_info_strings[i]))
}
league := dbmapping.League{}
league := dbmapping.League{}
telegram_nickname := update.Message.From.UserName
nickname := ""
level := ""
level_int := 0
exp := ""
exp_int := 0
egg_exp := ""
egg_exp_int := 0
pokeballs := ""
pokeballs_int := 0
wealth := ""
wealth_int := 0
crystalls := ""
crystalls_int := 0
weapon := ""
pokememes := make(map[string]string)
power_int := 1
telegram_nickname := update.Message.From.UserName
nickname := ""
level := ""
level_int := 0
exp := ""
exp_int := 0
egg_exp := ""
egg_exp_int := 0
pokeballs := ""
pokeballs_int := 0
wealth := ""
wealth_int := 0
crystalls := ""
crystalls_int := 0
weapon := ""
pokememes := make(map[string]string)
power_int := 1
// Filling information
// We don't know how many strings we got, so we iterating each other
for i := range(profile_info_runed_strings) {
current_string := string(profile_info_runed_strings[i])
current_runes := profile_info_runed_strings[i]
if strings.HasPrefix(current_string, "🈸") || strings.HasPrefix(current_string, "🈳 ") || strings.HasPrefix(current_string, "🈵") {
err1 := c.Db.Get(&league, c.Db.Rebind("SELECT * FROM leagues WHERE symbol='" + string(current_runes[0]) + "'"))
if err1 != nil {
log.Println(err1)
return "fail"
}
for j := range(current_runes) {
if j > 1 {
nickname += string(current_runes[j])
}
}
}
if strings.HasPrefix(current_string, "👤Уровень:") {
levelRx := regexp.MustCompile("\\d+")
level_array := levelRx.FindAllString(current_string, -1)
if len(level_array) < 1 {
log.Println("Level string broken")
return "fail"
}
level = level_array[0]
level_int, _ = strconv.Atoi(level)
}
// Filling information
// We don't know how many strings we got, so we iterating each other
for i := range profile_info_runed_strings {
current_string := string(profile_info_runed_strings[i])
current_runes := profile_info_runed_strings[i]
if strings.HasPrefix(current_string, "🈸") || strings.HasPrefix(current_string, "🈳 ") || strings.HasPrefix(current_string, "🈵") {
err1 := c.Db.Get(&league, c.Db.Rebind("SELECT * FROM leagues WHERE symbol='"+string(current_runes[0])+"'"))
if err1 != nil {
log.Println(err1)
return "fail"
}
for j := range current_runes {
if j > 1 {
nickname += string(current_runes[j])
}
}
}
if strings.HasPrefix(current_string, "👤Уровень:") {
levelRx := regexp.MustCompile("\\d+")
level_array := levelRx.FindAllString(current_string, -1)
if len(level_array) < 1 {
log.Println("Level string broken")
return "fail"
}
level = level_array[0]
level_int, _ = strconv.Atoi(level)
}
if strings.HasPrefix(current_string, "🎓Опыт") {
expRx := regexp.MustCompile("\\d+")
exp_array := expRx.FindAllString(current_string, -1)
if len(exp_array) < 4 {
log.Println("Exp string broken")
return "fail"
}
exp = exp_array[0]
exp_int, _ = strconv.Atoi(exp)
egg_exp = exp_array[2]
egg_exp_int, _ = strconv.Atoi(egg_exp)
}
if strings.HasPrefix(current_string, "🎓Опыт") {
expRx := regexp.MustCompile("\\d+")
exp_array := expRx.FindAllString(current_string, -1)
if len(exp_array) < 4 {
log.Println("Exp string broken")
return "fail"
}
exp = exp_array[0]
exp_int, _ = strconv.Atoi(exp)
egg_exp = exp_array[2]
egg_exp_int, _ = strconv.Atoi(egg_exp)
}
if strings.HasPrefix(current_string, "⭕Покеболы") {
pkbRx := regexp.MustCompile("\\d+")
pkb_array := pkbRx.FindAllString(current_string, -1)
if len(pkb_array) < 2 {
log.Println("Pokeballs string broken")
return "fail"
}
pokeballs = pkb_array[1]
pokeballs_int, _ = strconv.Atoi(pokeballs)
}
if strings.HasPrefix(current_string, "⭕Покеболы") {
pkbRx := regexp.MustCompile("\\d+")
pkb_array := pkbRx.FindAllString(current_string, -1)
if len(pkb_array) < 2 {
log.Println("Pokeballs string broken")
return "fail"
}
pokeballs = pkb_array[1]
pokeballs_int, _ = strconv.Atoi(pokeballs)
}
if strings.HasPrefix(current_string, "💲") {
wealthRx := regexp.MustCompile("(\\d|\\.|K|M)+")
wealth_array := wealthRx.FindAllString(current_string, -1)
if len(wealth_array) < 2 {
log.Println("Wealth string broken")
return "fail"
}
wealth = wealth_array[0]
wealth_int = p.getPoints(wealth)
crystalls =wealth_array[1]
crystalls_int = p.getPoints(crystalls)
}
if strings.HasPrefix(current_string, "💲") {
wealthRx := regexp.MustCompile("(\\d|\\.|K|M)+")
wealth_array := wealthRx.FindAllString(current_string, -1)
if len(wealth_array) < 2 {
log.Println("Wealth string broken")
return "fail"
}
wealth = wealth_array[0]
wealth_int = p.getPoints(wealth)
crystalls = wealth_array[1]
crystalls_int = p.getPoints(crystalls)
}
if strings.HasPrefix(current_string, "🔫") {
// We need NEXT string!
weapon_type_string := strings.Replace(current_string, "🔫 ", "", 1)
wnRx := regexp.MustCompile("(.+)(ита)")
weapon = wnRx.FindString(weapon_type_string)
}
if strings.HasPrefix(current_string, "🔫") {
// We need NEXT string!
weapon_type_string := strings.Replace(current_string, "🔫 ", "", 1)
wnRx := regexp.MustCompile("(.+)(ита)")
weapon = wnRx.FindString(weapon_type_string)
}
if strings.HasPrefix(current_string, "🐱Покемемы: ") {
pkmnumRx := regexp.MustCompile("\\d+")
pk_num_array := pkmnumRx.FindAllString(current_string, -1)
if len(pk_num_array) < 2 {
log.Println("Pokememes count broken")
return "fail"
}
pokememes_count, _ := strconv.Atoi(pk_num_array[0])
if pokememes_count > 0 {
for pi := 0; pi < pokememes_count; pi++ {
pokememe_string := string(profile_info_runed_strings[i + 1 + pi])
attackRx := regexp.MustCompile("(\\d|\\.|K|M)+")
pk_points_array := attackRx.FindAllString(pokememe_string, -1)
pk_attack := pk_points_array[1]
pk_name := strings.Split(pokememe_string, "+")[0]
pk_name = strings.Replace(pk_name, " ⭐", "", 1)
pk_name = strings.TrimSuffix(pk_name, " ")
pk_name = strings.Split(pk_name, "⃣ ")[1]
pokememes[pk_name] = pk_attack
power_int += p.getPoints(pk_attack)
}
}
}
}
if strings.HasPrefix(current_string, "🐱Покемемы: ") {
pkmnumRx := regexp.MustCompile("\\d+")
pk_num_array := pkmnumRx.FindAllString(current_string, -1)
if len(pk_num_array) < 2 {
log.Println("Pokememes count broken")
return "fail"
}
pokememes_count, _ := strconv.Atoi(pk_num_array[0])
if pokememes_count > 0 {
for pi := 0; pi < pokememes_count; pi++ {
pokememe_string := string(profile_info_runed_strings[i+1+pi])
attackRx := regexp.MustCompile("(\\d|\\.|K|M)+")
pk_points_array := attackRx.FindAllString(pokememe_string, -1)
pk_attack := pk_points_array[1]
pk_name := strings.Split(pokememe_string, "+")[0]
pk_name = strings.Replace(pk_name, " ⭐", "", 1)
pk_name = strings.TrimSuffix(pk_name, " ")
pk_name = strings.Split(pk_name, "⃣ ")[1]
pokememes[pk_name] = pk_attack
power_int += p.getPoints(pk_attack)
}
}
}
}
log.Printf("Telegram nickname: " + telegram_nickname)
log.Printf("Nickname: " + nickname)
log.Printf("League: " + league.Name)
log.Printf("Level: " + level)
log.Println(level_int)
log.Printf("Exp: " + exp)
log.Println(exp_int)
log.Printf("Egg exp: " + egg_exp)
log.Println(egg_exp_int)
log.Printf("Pokeballs: " + pokeballs)
log.Println(pokeballs_int)
log.Printf("Wealth: " + wealth)
log.Println(wealth_int)
log.Printf("Crystalls: " + crystalls)
log.Println(crystalls_int)
log.Printf("Weapon: " + weapon)
if len(pokememes) > 0 {
for meme, attack := range(pokememes) {
log.Printf(meme + ": " + attack)
}
} else {
log.Printf("Hand is empty.")
}
log.Printf("Telegram nickname: " + telegram_nickname)
log.Printf("Nickname: " + nickname)
log.Printf("League: " + league.Name)
log.Printf("Level: " + level)
log.Println(level_int)
log.Printf("Exp: " + exp)
log.Println(exp_int)
log.Printf("Egg exp: " + egg_exp)
log.Println(egg_exp_int)
log.Printf("Pokeballs: " + pokeballs)
log.Println(pokeballs_int)
log.Printf("Wealth: " + wealth)
log.Println(wealth_int)
log.Printf("Crystalls: " + crystalls)
log.Println(crystalls_int)
log.Printf("Weapon: " + weapon)
if len(pokememes) > 0 {
for meme, attack := range pokememes {
log.Printf(meme + ": " + attack)
}
} else {
log.Printf("Hand is empty.")
}
// Information is gathered, let's create profile in database!
weapon_raw := dbmapping.Weapon{}
err2 := c.Db.Get(&weapon_raw, c.Db.Rebind("SELECT * FROM weapons WHERE name='" + weapon + "'"))
if err2 != nil {
log.Println(err2)
}
// Information is gathered, let's create profile in database!
weapon_raw := dbmapping.Weapon{}
err2 := c.Db.Get(&weapon_raw, c.Db.Rebind("SELECT * FROM weapons WHERE name='"+weapon+"'"))
if err2 != nil {
log.Println(err2)
}
if player_raw.League_id == 0 {
// Updating player with league
player_raw.League_id = league.Id
if player_raw.Status == "nobody" {
player_raw.Status = "common"
}
_, err4 := c.Db.NamedExec("UPDATE `players` SET league_id=:league_id, status=:status WHERE id=:id", &player_raw)
if err4 != nil {
log.Println(err4)
return "fail"
}
} else if player_raw.League_id != league.Id {
// Duplicate profile: user changed league, beware!
player_raw.League_id = league.Id
player_raw.Squad_id = 0
player_raw.Status = "league_changed"
player_raw.Created_at = time.Now().UTC()
_, err5 := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &player_raw)
if err5 != nil {
log.Println(err5)
return "fail"
}
err6 := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id='" + strconv.Itoa(player_raw.Telegram_id) + "' AND league_id='" + strconv.Itoa(league.Id) + "';"))
if err6 != nil {
log.Println(err6)
return "fail"
}
}
if player_raw.League_id == 0 {
// Updating player with league
player_raw.League_id = league.Id
if player_raw.Status == "nobody" {
player_raw.Status = "common"
}
_, err4 := c.Db.NamedExec("UPDATE `players` SET league_id=:league_id, status=:status WHERE id=:id", &player_raw)
if err4 != nil {
log.Println(err4)
return "fail"
}
} else if player_raw.League_id != league.Id {
// Duplicate profile: user changed league, beware!
player_raw.League_id = league.Id
player_raw.Squad_id = 0
player_raw.Status = "league_changed"
player_raw.Created_at = time.Now().UTC()
_, err5 := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &player_raw)
if err5 != nil {
log.Println(err5)
return "fail"
}
err6 := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id='"+strconv.Itoa(player_raw.Telegram_id)+"' AND league_id='"+strconv.Itoa(league.Id)+"';"))
if err6 != nil {
log.Println(err6)
return "fail"
}
}
profile_raw := dbmapping.Profile{}
profile_raw.Player_id = player_raw.Id
profile_raw.Nickname = nickname
profile_raw.TelegramNickname = telegram_nickname
profile_raw.Level_id = level_int
profile_raw.Pokeballs = pokeballs_int
profile_raw.Wealth = wealth_int
profile_raw.Exp = exp_int
profile_raw.Egg_exp = egg_exp_int
profile_raw.Power = power_int
profile_raw.Weapon_id = weapon_raw.Id
profile_raw.Crystalls = crystalls_int
profile_raw.Created_at = time.Now().UTC()
profile_raw := dbmapping.Profile{}
profile_raw.Player_id = player_raw.Id
profile_raw.Nickname = nickname
profile_raw.TelegramNickname = telegram_nickname
profile_raw.Level_id = level_int
profile_raw.Pokeballs = pokeballs_int
profile_raw.Wealth = wealth_int
profile_raw.Exp = exp_int
profile_raw.Egg_exp = egg_exp_int
profile_raw.Power = power_int
profile_raw.Weapon_id = weapon_raw.Id
profile_raw.Crystalls = crystalls_int
profile_raw.Created_at = time.Now().UTC()
_, err3 := c.Db.NamedExec("INSERT INTO `profiles` VALUES(NULL, :player_id, :nickname, :telegram_nickname, :level_id, :pokeballs, :wealth, :exp, :egg_exp, :power, :weapon_id, :crystalls, :created_at)", &profile_raw)
if err3 != nil {
log.Println(err3)
return "fail"
}
_, err3 := c.Db.NamedExec("INSERT INTO `profiles` VALUES(NULL, :player_id, :nickname, :telegram_nickname, :level_id, :pokeballs, :wealth, :exp, :egg_exp, :power, :weapon_id, :crystalls, :created_at)", &profile_raw)
if err3 != nil {
log.Println(err3)
return "fail"
}
err8 := c.Db.Get(&profile_raw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? AND created_at=?"), profile_raw.Player_id, profile_raw.Created_at)
if err8 != nil {
log.Println(err8)
log.Printf("Profile isn't added!")
return "fail"
}
err8 := c.Db.Get(&profile_raw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? AND created_at=?"), profile_raw.Player_id, profile_raw.Created_at)
if err8 != nil {
log.Println(err8)
log.Printf("Profile isn't added!")
return "fail"
}
player_raw.Updated_at = time.Now().UTC()
_, err7 := c.Db.NamedExec("UPDATE `players` SET updated_at=:updated_at WHERE id=:id", &player_raw)
if err7 != nil {
log.Println(err7)
return "fail"
}
player_raw.Updated_at = time.Now().UTC()
_, err7 := c.Db.NamedExec("UPDATE `players` SET updated_at=:updated_at WHERE id=:id", &player_raw)
if err7 != nil {
log.Println(err7)
return "fail"
}
for meme, attack := range pokememes {
rarity := "common"
if strings.HasPrefix(meme, "🔸") {
rarity = "rare"
meme = strings.Replace(meme, "🔸", "", 1)
}
p.fillProfilePokememe(profile_raw.Id, meme, attack, rarity)
}
for meme, attack := range(pokememes) {
rarity := "common"
if strings.HasPrefix(meme, "🔸") {
rarity = "rare"
meme = strings.Replace(meme, "🔸", "", 1)
}
p.fillProfilePokememe(profile_raw.Id, meme, attack, rarity)
}
return "ok"
return "ok"
}