Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/getters/player.go

50 lines
1.3 KiB
Go
Raw Normal View History

// 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:52:04 +04:00
func (g *Getters) GetPlayerByID(player_id int) (dbmapping.Player, bool) {
2017-10-18 07:03:34 +04:00
player_raw := dbmapping.Player{}
err := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE id=?"), player_id)
if err != nil {
log.Println(err)
return player_raw, false
}
2017-10-13 03:52:04 +04:00
2017-10-18 07:03:34 +04:00
return player_raw, true
2017-10-13 03:52:04 +04:00
}
func (g *Getters) GetOrCreatePlayer(telegram_id int) (dbmapping.Player, bool) {
2017-10-18 07:03:34 +04:00
player_raw := dbmapping.Player{}
err := c.Db.Get(&player_raw, c.Db.Rebind("SELECT * FROM players WHERE telegram_id=?"), telegram_id)
if err != nil {
log.Printf("Message user not found in database.")
log.Printf(err.Error())
2017-10-18 07:03:34 +04:00
// Create "nobody" user
player_raw.Telegram_id = telegram_id
player_raw.League_id = 0
player_raw.Squad_id = 0
player_raw.Status = "nobody"
player_raw.Created_at = time.Now().UTC()
player_raw.Updated_at = time.Now().UTC()
_, err = c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :squad_id, :status, :created_at, :updated_at)", &player_raw)
if err != nil {
log.Printf(err.Error())
return player_raw, false
}
} else {
log.Printf("Message user found in database.")
}
2017-10-18 07:03:34 +04:00
return player_raw, true
}