2017-10-13 03:05:26 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package getters
|
|
|
|
|
|
|
|
|
|
import (
|
2017-10-18 07:03:34 +04:00
|
|
|
|
// stdlib
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
// local
|
|
|
|
|
"../dbmapping"
|
2017-10-13 03:05:26 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// GetPlayerByID returns dbmapping.Player instance with given ID.
|
|
|
|
|
func (g *Getters) GetPlayerByID(playerID int) (dbmapping.Player, bool) {
|
|
|
|
|
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 {
|
|
|
|
|
log.Println(err)
|
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.
|
|
|
|
|
func (g *Getters) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) {
|
|
|
|
|
playerRaw := dbmapping.Player{}
|
|
|
|
|
err := c.Db.Get(&playerRaw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), telegramID)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Message user not found in database.")
|
|
|
|
|
log.Printf(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.SquadID = 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, :squad_id, :status, :created_at, :updated_at)", &playerRaw)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf(err.Error())
|
2017-10-18 09:39:50 +04:00
|
|
|
|
return playerRaw, false
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Message user found in database.")
|
|
|
|
|
}
|
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
|
|
|
|
}
|