Archived
1

Massive refactoring for future development

This commit is contained in:
2017-11-21 06:06:32 +04:00
parent f5d801b768
commit dfe0d08ecc
45 changed files with 742 additions and 640 deletions

28
lib/users/exported.go Normal file
View File

@@ -0,0 +1,28 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package users
import (
"lab.pztrn.name/fat0troll/i2_bot/lib/appcontext"
"lab.pztrn.name/fat0troll/i2_bot/lib/users/usersinterface"
)
var (
c *appcontext.Context
)
// Users is a function-handling struct for users
type Users struct{}
// New is a appcontext initialization function
func New(ac *appcontext.Context) {
c = ac
u := &Users{}
c.RegisterUsersInterface(usersinterface.UsersInterface(u))
}
// Init is an initialization function for users
func (u *Users) Init() {
c.Log.Info("Initializing Users...")
}

78
lib/users/getters.go Normal file
View File

@@ -0,0 +1,78 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package users
import (
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
"time"
)
// GetProfile returns last saved profile of player
func (u *Users) GetProfile(playerID int) (dbmapping.Profile, bool) {
profileRaw := dbmapping.Profile{}
err := c.Db.Get(&profileRaw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? ORDER BY created_at DESC LIMIT 1"), playerID)
if err != nil {
c.Log.Error(err)
return profileRaw, false
}
return profileRaw, true
}
// GetPlayerByID returns dbmapping.Player instance with given ID.
func (u *Users) GetPlayerByID(playerID int) (dbmapping.Player, bool) {
playerRaw := dbmapping.Player{}
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE id=?"), playerID)
if err != nil {
c.Log.Error(err.Error())
return playerRaw, false
}
return playerRaw, true
}
// GetOrCreatePlayer seeks for player in database via Telegram ID.
// In case, when there is no player with such ID, new player will be created.
func (u *Users) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) {
playerRaw := dbmapping.Player{}
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), telegramID)
if err != nil {
c.Log.Error("Message user not found in database.")
c.Log.Error(err.Error())
// Create "nobody" user
playerRaw.TelegramID = telegramID
playerRaw.LeagueID = 0
playerRaw.Status = "nobody"
playerRaw.CreatedAt = time.Now().UTC()
playerRaw.UpdatedAt = time.Now().UTC()
_, err = c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :status, :created_at, :updated_at)", &playerRaw)
if err != nil {
c.Log.Error(err.Error())
return playerRaw, false
}
} else {
c.Log.Debug("Message user found in database.")
}
return playerRaw, true
}
// PlayerBetterThan return true, if profile is more or equal powerful than
// provided power level
func (u *Users) PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool {
var isBetter = false
switch playerRaw.Status {
case "owner":
isBetter = true
case "admin":
if powerLevel != "owner" {
isBetter = true
}
default:
isBetter = false
}
return isBetter
}

302
lib/users/parsers.go Normal file
View File

@@ -0,0 +1,302 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package users
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
"regexp"
"strconv"
"strings"
"time"
)
// Internal functions
func (u *Users) fillProfilePokememe(profileID int, meme string, attack string, rarity string) {
spkRaw := dbmapping.Pokememe{}
err := c.Db.Get(&spkRaw, c.Db.Rebind("SELECT * FROM pokememes WHERE name='"+meme+"';"))
if err != nil {
c.Log.Error(err.Error())
} else {
attackInt := c.Statistics.GetPoints(attack)
ppk := dbmapping.ProfilePokememe{}
ppk.ProfileID = profileID
ppk.PokememeID = spkRaw.ID
ppk.PokememeAttack = attackInt
ppk.PokememeRarity = rarity
ppk.CreatedAt = time.Now().UTC()
_, err2 := c.Db.NamedExec("INSERT INTO `profiles_pokememes` VALUES(NULL, :profile_id, :pokememe_id, :pokememe_attack, :pokememe_rarity, :created_at)", &ppk)
if err2 != nil {
c.Log.Error(err2.Error())
}
}
}
// External functions
// ParseProfile parses user profile, forwarded from PokememBroBot, to database
func (u *Users) ParseProfile(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
text := update.Message.Text
c.Log.Info(text)
profileStringsArray := strings.Split(text, "\n")
profileRunesArray := make([][]rune, 0)
for i := range profileStringsArray {
profileRunesArray = append(profileRunesArray, []rune(profileStringsArray[i]))
}
league := dbmapping.League{}
telegramNickname := update.Message.From.UserName
nickname := ""
level := ""
levelInt := 0
exp := ""
expInt := 0
eggexp := ""
eggexpInt := 0
pokeballs := ""
pokeballsInt := 0
wealth := ""
wealthInt := 0
pokememesWealth := ""
pokememesWealthInt := 0
crystalls := ""
crystallsInt := 0
weapon := ""
pokememes := make(map[string]string)
powerInt := 1
// Filling information
// We don't know how many strings we got, so we iterating each other
for i := range profileRunesArray {
currentString := string(profileRunesArray[i])
currentRunes := profileRunesArray[i]
if strings.HasPrefix(currentString, "🈸") || strings.HasPrefix(currentString, "🈳 ") || strings.HasPrefix(currentString, "🈵") {
err1 := c.Db.Get(&league, c.Db.Rebind("SELECT * FROM leagues WHERE symbol='"+string(currentRunes[0])+"'"))
if err1 != nil {
c.Log.Error(err1.Error())
u.profileAddFailureMessage(update)
return "fail"
}
for j := range currentRunes {
if j > 1 {
nickname += string(currentRunes[j])
}
}
}
if strings.HasPrefix(currentString, "👤Уровень:") {
levelRx := regexp.MustCompile("\\d+")
levelArray := levelRx.FindAllString(currentString, -1)
if len(levelArray) < 1 {
c.Log.Error("Level string broken")
u.profileAddFailureMessage(update)
return "fail"
}
level = levelArray[0]
levelInt, _ = strconv.Atoi(level)
}
if strings.HasPrefix(currentString, "🎓Опыт") {
expRx := regexp.MustCompile("\\d+")
expArray := expRx.FindAllString(currentString, -1)
if len(expArray) < 4 {
c.Log.Error("Exp string broken")
u.profileAddFailureMessage(update)
return "fail"
}
exp = expArray[0]
expInt, _ = strconv.Atoi(exp)
eggexp = expArray[2]
eggexpInt, _ = strconv.Atoi(eggexp)
}
if strings.HasPrefix(currentString, "⭕Покеболы") {
pkbRx := regexp.MustCompile("\\d+")
pkbArray := pkbRx.FindAllString(currentString, -1)
if len(pkbArray) < 2 {
c.Log.Error("Pokeballs string broken")
u.profileAddFailureMessage(update)
return "fail"
}
pokeballs = pkbArray[1]
pokeballsInt, _ = strconv.Atoi(pokeballs)
}
if strings.HasPrefix(currentString, "💲") {
wealthRx := regexp.MustCompile("(\\d|\\.|K|M)+")
wealthArray := wealthRx.FindAllString(currentString, -1)
if len(wealthArray) < 2 {
c.Log.Error("Wealth string broken")
u.profileAddFailureMessage(update)
return "fail"
}
wealth = wealthArray[0]
wealthInt = c.Statistics.GetPoints(wealth)
crystalls = wealthArray[1]
crystallsInt = c.Statistics.GetPoints(crystalls)
}
if strings.HasPrefix(currentString, "🔫") {
// We need NEXT string!
weaponType := strings.Replace(currentString, "🔫 ", "", 1)
wnRx := regexp.MustCompile("(.+)(ита)")
weapon = wnRx.FindString(weaponType)
}
if strings.HasPrefix(currentString, "🐱Покемемы: ") {
pkmnumRx := regexp.MustCompile(`(\d+)(\d|K|M|)`)
pkNumArray := pkmnumRx.FindAllString(currentString, -1)
if len(pkNumArray) < 3 {
c.Log.Error("Pokememes count broken")
u.profileAddFailureMessage(update)
return "fail"
}
pokememesCount, _ := strconv.Atoi(pkNumArray[0])
pokememesWealth = pkNumArray[2]
pokememesWealthInt = c.Statistics.GetPoints(pokememesWealth)
if pokememesCount > 0 {
for pi := 0; pi < pokememesCount; pi++ {
pokememeString := string(profileRunesArray[i+1+pi])
attackRx := regexp.MustCompile("(\\d|\\.|K|M)+")
pkPointsArray := attackRx.FindAllString(pokememeString, -1)
pkAttack := pkPointsArray[1]
pkName := strings.Split(pokememeString, "+")[0]
pkName = strings.Replace(pkName, " ⭐", "", 1)
pkName = strings.TrimSuffix(pkName, " ")
pkName = strings.Split(pkName, "⃣ ")[1]
pokememes[pkName] = pkAttack
powerInt += c.Statistics.GetPoints(pkAttack)
}
}
}
}
c.Log.Debug("Telegram nickname: " + telegramNickname)
c.Log.Debug("Nickname: " + nickname)
c.Log.Debug("League: " + league.Name)
c.Log.Debug("Level: " + level)
c.Log.Debugln(levelInt)
c.Log.Debug("Exp: " + exp)
c.Log.Debugln(expInt)
c.Log.Debug("Egg exp: " + eggexp)
c.Log.Debugln(eggexpInt)
c.Log.Debug("Pokeballs: " + pokeballs)
c.Log.Debugln(pokeballsInt)
c.Log.Debug("Wealth: " + wealth)
c.Log.Debugln(wealthInt)
c.Log.Debug("Crystalls: " + crystalls)
c.Log.Debugln(crystallsInt)
c.Log.Debug("Weapon: " + weapon)
if len(pokememes) > 0 {
c.Log.Debug("Hand cost: " + pokememesWealth)
c.Log.Debugln(pokememesWealthInt)
for meme, attack := range pokememes {
c.Log.Debug(meme + ": " + attack)
}
} else {
c.Log.Debug("Hand is empty.")
}
// Information is gathered, let's create profile in database!
weaponRaw := dbmapping.Weapon{}
err2 := c.Db.Get(&weaponRaw, c.Db.Rebind("SELECT * FROM weapons WHERE name='"+weapon+"'"))
if err2 != nil {
c.Log.Error(err2.Error())
}
if playerRaw.LeagueID == 0 {
// Updating player with league
playerRaw.LeagueID = league.ID
if playerRaw.Status == "nobody" {
playerRaw.Status = "common"
}
_, err4 := c.Db.NamedExec("UPDATE `players` SET league_id=:league_id, status=:status WHERE id=:id", &playerRaw)
if err4 != nil {
c.Log.Error(err4.Error())
u.profileAddFailureMessage(update)
return "fail"
}
} else if playerRaw.LeagueID != league.ID {
// Duplicate profile: user changed league, beware!
playerRaw.LeagueID = league.ID
playerRaw.Status = "league_changed"
playerRaw.CreatedAt = time.Now().UTC()
_, err5 := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :status, :created_at, :updated_at)", &playerRaw)
if err5 != nil {
c.Log.Error(err5.Error())
u.profileAddFailureMessage(update)
return "fail"
}
err6 := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id='"+strconv.Itoa(playerRaw.TelegramID)+"' AND league_id='"+strconv.Itoa(league.ID)+"';"))
if err6 != nil {
c.Log.Error(err6.Error())
u.profileAddFailureMessage(update)
return "fail"
}
}
profileRaw := dbmapping.Profile{}
profileRaw.PlayerID = playerRaw.ID
profileRaw.Nickname = nickname
profileRaw.TelegramNickname = telegramNickname
profileRaw.LevelID = levelInt
profileRaw.Pokeballs = pokeballsInt
profileRaw.Wealth = wealthInt
profileRaw.PokememesWealth = pokememesWealthInt
profileRaw.Exp = expInt
profileRaw.EggExp = eggexpInt
profileRaw.Power = powerInt
profileRaw.WeaponID = weaponRaw.ID
profileRaw.Crystalls = crystallsInt
profileRaw.CreatedAt = time.Now().UTC()
_, err3 := c.Db.NamedExec("INSERT INTO `profiles` VALUES(NULL, :player_id, :nickname, :telegram_nickname, :level_id, :pokeballs, :wealth, :pokememes_wealth, :exp, :egg_exp, :power, :weapon_id, :crystalls, :created_at)", &profileRaw)
if err3 != nil {
c.Log.Error(err3.Error())
u.profileAddFailureMessage(update)
return "fail"
}
err8 := c.Db.Get(&profileRaw, c.Db.Rebind("SELECT * FROM profiles WHERE player_id=? AND created_at=?"), profileRaw.PlayerID, profileRaw.CreatedAt)
if err8 != nil {
c.Log.Error(err8.Error())
c.Log.Error("Profile isn't added!")
u.profileAddFailureMessage(update)
return "fail"
}
playerRaw.UpdatedAt = time.Now().UTC()
_, err7 := c.Db.NamedExec("UPDATE `players` SET updated_at=:updated_at WHERE id=:id", &playerRaw)
if err7 != nil {
c.Log.Error(err7.Error())
u.profileAddFailureMessage(update)
return "fail"
}
for meme, attack := range pokememes {
rarity := "common"
if strings.HasPrefix(meme, "🔸") {
rarity = "rare"
meme = strings.Replace(meme, "🔸", "", 1)
}
if strings.HasPrefix(meme, "🔶") {
rarity = "super rare"
meme = strings.Replace(meme, "🔶", "", 1)
}
if strings.HasPrefix(meme, "🔹") {
rarity = "liber"
meme = strings.Replace(meme, "🔹", "", 1)
}
if strings.HasPrefix(meme, "🔷") {
rarity = "super liber"
meme = strings.Replace(meme, "🔷", "", 1)
}
u.fillProfilePokememe(profileRaw.ID, meme, attack, rarity)
}
u.profileAddSuccessMessage(update)
return "ok"
}

94
lib/users/responders.go Normal file
View File

@@ -0,0 +1,94 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package users
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
"strconv"
)
// ProfileMessage shows current player's profile
func (u *Users) ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
profileRaw, ok := u.GetProfile(playerRaw.ID)
if !ok {
c.Talkers.AnyMessageUnauthorized(update)
return "fail"
}
league := dbmapping.League{}
err := c.Db.Get(&league, c.Db.Rebind("SELECT * FROM leagues WHERE id=?"), playerRaw.LeagueID)
if err != nil {
c.Log.Error(err)
}
level := dbmapping.Level{}
err = c.Db.Get(&level, c.Db.Rebind("SELECT * FROM levels WHERE id=?"), profileRaw.LevelID)
if err != nil {
c.Log.Error(err)
}
weapon := dbmapping.Weapon{}
if profileRaw.WeaponID != 0 {
err = c.Db.Get(&weapon, c.Db.Rebind("SELECT * FROM weapons WHERE id=?"), profileRaw.WeaponID)
if err != nil {
c.Log.Error(err)
}
}
profilePokememes := []dbmapping.ProfilePokememe{}
err = c.Db.Select(&profilePokememes, c.Db.Rebind("SELECT * FROM profiles_pokememes WHERE profile_id=?"), profileRaw.ID)
if err != nil {
c.Log.Error(err)
}
pokememes := []dbmapping.Pokememe{}
err = c.Db.Select(&pokememes, c.Db.Rebind("SELECT * FROM pokememes"))
if err != nil {
c.Log.Error(err)
}
attackPokememes := 0
for i := range profilePokememes {
for j := range pokememes {
if profilePokememes[i].PokememeID == pokememes[j].ID {
singleAttack := profilePokememes[i].PokememeAttack
attackPokememes += singleAttack
}
}
}
message := "*Профиль игрока "
message += profileRaw.Nickname + "* (@" + profileRaw.TelegramNickname + ")\n"
message += "\nЛига: " + league.Symbol + league.Name
message += "\n👤 " + strconv.Itoa(profileRaw.LevelID)
message += " | 🎓 " + strconv.Itoa(profileRaw.Exp) + "/" + strconv.Itoa(level.MaxExp)
message += " | 🥚 " + strconv.Itoa(profileRaw.EggExp) + "/" + strconv.Itoa(level.MaxEgg)
message += "\n💲" + c.Statistics.GetPrintablePoints(profileRaw.Wealth)
message += " |💎" + strconv.Itoa(profileRaw.Crystalls)
message += " |⭕" + strconv.Itoa(profileRaw.Pokeballs)
message += "\n⚔Атака: 1 + " + c.Statistics.GetPrintablePoints(weapon.Power) + " + " + c.Statistics.GetPrintablePoints(attackPokememes) + "\n"
if profileRaw.WeaponID != 0 {
message += "\n🔫Оружие: " + weapon.Name + " " + c.Statistics.GetPrintablePoints(weapon.Power) + "⚔"
}
message += "\n🐱Покемемы:"
for i := range profilePokememes {
for j := range pokememes {
if profilePokememes[i].PokememeID == pokememes[j].ID {
message += "\n" + strconv.Itoa(pokememes[j].Grade)
message += "⃣ " + pokememes[j].Name
message += " +" + c.Statistics.GetPrintablePoints(profilePokememes[i].PokememeAttack) + "⚔"
}
}
}
message += "\nСтоимость покемемов на руках: " + c.Statistics.GetPrintablePoints(profileRaw.PokememesWealth) + "$"
message += "\n\n💳" + strconv.Itoa(playerRaw.TelegramID)
message += "\n⏰Последнее обновление профиля: " + profileRaw.CreatedAt.Format("02.01.2006 15:04:05")
message += "\n\nНе забывай обновляться, это важно для получения актуальной информации.\n\n"
message += "/best посмотреть лучших покемемов для поимки"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
return "ok"
}

34
lib/users/users.go Normal file
View File

@@ -0,0 +1,34 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package users
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// Internal functions for Users package
// profileAddSuccessMessage shows profile addition success message
func (u *Users) profileAddSuccessMessage(update *tgbotapi.Update) {
message := "*Профиль успешно обновлен.*\n\n"
message += "Функциональность бота держится на актуальности профилей. Обновляйся почаще, и да пребудет с тобой Рандом!\n"
message += "Сохраненный профиль ты можешь просмотреть командой /me.\n\n"
message += "/best посмотреть лучших покемемов для поимки"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
}
// profileAddFailureMessage shows profile addition failure message
func (u *Users) profileAddFailureMessage(update *tgbotapi.Update) {
message := "*Неудачно получилось :(*\n\n"
message += "Случилась жуткая ошибка, и мы не смогли записать профиль в базу. Напиши @fat0troll, он разберется."
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
}

View File

@@ -0,0 +1,23 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package usersinterface
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
)
// UsersInterface implements Users for importing via appcontex
type UsersInterface interface {
Init()
ParseProfile(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
GetProfile(playerID int) (dbmapping.Profile, bool)
GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool)
GetPlayerByID(playerID int) (dbmapping.Player, bool)
PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool
ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
}