2017-10-13 03:05:26 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
package users
|
2017-10-13 03:05:26 +04:00
|
|
|
|
|
|
|
|
|
import (
|
2017-12-20 21:45:32 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2018-01-21 23:28:53 +04:00
|
|
|
|
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"time"
|
2017-10-13 03:05:26 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// GetPlayerByID returns dbmapping.Player instance with given ID.
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (u *Users) GetPlayerByID(playerID int) (dbmapping.Player, bool) {
|
2017-10-18 09:39:50 +04:00
|
|
|
|
playerRaw := dbmapping.Player{}
|
|
|
|
|
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE id=?"), playerID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-18 09:39:50 +04:00
|
|
|
|
return playerRaw, false
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-13 03:52:04 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
return playerRaw, true
|
2017-10-13 03:52:04 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// GetOrCreatePlayer seeks for player in database via Telegram ID.
|
|
|
|
|
// In case, when there is no player with such ID, new player will be created.
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (u *Users) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) {
|
2017-10-18 09:39:50 +04:00
|
|
|
|
playerRaw := dbmapping.Player{}
|
2017-12-23 17:03:26 +04:00
|
|
|
|
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=? ORDER BY created_at desc LIMIT 1"), telegramID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error("Message user not found in database.")
|
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-13 03:05:26 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// Create "nobody" user
|
2017-10-18 09:39:50 +04:00
|
|
|
|
playerRaw.TelegramID = telegramID
|
|
|
|
|
playerRaw.LeagueID = 0
|
|
|
|
|
playerRaw.Status = "nobody"
|
|
|
|
|
playerRaw.CreatedAt = time.Now().UTC()
|
|
|
|
|
playerRaw.UpdatedAt = time.Now().UTC()
|
2017-11-13 22:51:50 +04:00
|
|
|
|
_, err = c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :status, :created_at, :updated_at)", &playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-18 09:39:50 +04:00
|
|
|
|
return playerRaw, false
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Debug("Message user found in database.")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-13 03:05:26 +04:00
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
return playerRaw, true
|
2017-10-13 03:05:26 +04:00
|
|
|
|
}
|
2017-10-22 14:04:14 +04:00
|
|
|
|
|
2017-12-20 21:45:32 +04:00
|
|
|
|
// GetPrettyName returns "pretty" name of user (first_name + last name or username)
|
|
|
|
|
func (u *Users) GetPrettyName(user *tgbotapi.User) string {
|
|
|
|
|
userName := user.FirstName
|
|
|
|
|
if user.LastName != "" {
|
|
|
|
|
userName += " " + user.LastName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if user.UserName != "" {
|
|
|
|
|
userName += " (@" + user.UserName + ")"
|
|
|
|
|
}
|
|
|
|
|
return c.Users.FormatUsername(userName)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 14:04:14 +04:00
|
|
|
|
// PlayerBetterThan return true, if profile is more or equal powerful than
|
|
|
|
|
// provided power level
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (u *Users) PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool {
|
2017-10-22 14:04:14 +04:00
|
|
|
|
var isBetter = false
|
|
|
|
|
switch playerRaw.Status {
|
2017-11-26 15:28:55 +04:00
|
|
|
|
case "special":
|
|
|
|
|
isBetter = true
|
2017-10-22 14:04:14 +04:00
|
|
|
|
case "owner":
|
|
|
|
|
isBetter = true
|
|
|
|
|
case "admin":
|
|
|
|
|
if powerLevel != "owner" {
|
|
|
|
|
isBetter = true
|
|
|
|
|
}
|
2017-12-01 10:55:18 +04:00
|
|
|
|
case "academic":
|
|
|
|
|
if powerLevel != "ownder" && powerLevel != "admin" {
|
|
|
|
|
isBetter = true
|
|
|
|
|
}
|
2017-10-22 14:04:14 +04:00
|
|
|
|
default:
|
|
|
|
|
isBetter = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isBetter
|
|
|
|
|
}
|