Archived
1

Add karma field to players

Karma will be used for inactive users filtering
This commit is contained in:
Vladimir Hodakov 2018-05-05 09:50:07 +04:00
parent b8af1740fb
commit 0a5361ee09
6 changed files with 36 additions and 2 deletions

View File

@ -38,7 +38,7 @@ func (dc *DataCache) loadPlayers() {
// AddPlayer creates new player in database // AddPlayer creates new player in database
func (dc *DataCache) AddPlayer(player *dbmapping.Player) (int, error) { func (dc *DataCache) AddPlayer(player *dbmapping.Player) (int, error) {
c.Log.Info("DataCache: Creating new user...") c.Log.Info("DataCache: Creating new user...")
_, err := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :status, :created_at, :updated_at)", &player) _, err := c.Db.NamedExec("INSERT INTO players VALUES(NULL, :telegram_id, :league_id, :status, :karma, :created_at, :updated_at)", &player)
if err != nil { if err != nil {
c.Log.Error(err.Error()) c.Log.Error(err.Error())
return 0, err return 0, err
@ -76,6 +76,7 @@ func (dc *DataCache) GetOrCreatePlayerByTelegramID(telegramID int) (*dbmapping.P
newPlayer := dbmapping.Player{} newPlayer := dbmapping.Player{}
newPlayer.TelegramID = telegramID newPlayer.TelegramID = telegramID
newPlayer.LeagueID = 0 newPlayer.LeagueID = 0
newPlayer.Karma = 250
newPlayer.Status = "nobody" newPlayer.Status = "nobody"
newPlayer.CreatedAt = time.Now().UTC() newPlayer.CreatedAt = time.Now().UTC()
newPlayer.UpdatedAt = time.Now().UTC() newPlayer.UpdatedAt = time.Now().UTC()
@ -125,7 +126,7 @@ func (dc *DataCache) GetPlayerByTelegramID(telegramID int) (*dbmapping.Player, e
// UpdatePlayerFields writes new fields to player // UpdatePlayerFields writes new fields to player
func (dc *DataCache) UpdatePlayerFields(player *dbmapping.Player) (*dbmapping.Player, error) { func (dc *DataCache) UpdatePlayerFields(player *dbmapping.Player) (*dbmapping.Player, error) {
if dc.players[player.ID] != nil { if dc.players[player.ID] != nil {
_, err := c.Db.NamedExec("UPDATE `players` SET league_id=:league_id, status=:status WHERE id=:id", player) _, err := c.Db.NamedExec("UPDATE `players` SET league_id=:league_id, status=:status, karma=:karma WHERE id=:id", player)
if err != nil { if err != nil {
c.Log.Error(err.Error()) c.Log.Error(err.Error())
return dc.players[player.ID], err return dc.players[player.ID], err

View File

@ -15,6 +15,7 @@ type Player struct {
TelegramID int `db:"telegram_id"` TelegramID int `db:"telegram_id"`
LeagueID int `db:"league_id"` LeagueID int `db:"league_id"`
Status string `db:"status"` Status string `db:"status"`
Karma int `db:"karma"`
CreatedAt time.Time `db:"created_at"` CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"` UpdatedAt time.Time `db:"updated_at"`
} }

View File

@ -0,0 +1,29 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017-2018 Vladimir "fat0troll" Hodakov
package migrations
import (
// stdlib
"database/sql"
)
// AddKarmaToPlayersUp creates `karma` column in `players` table
func AddKarmaToPlayersUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `players` ADD COLUMN `karma` INT(11) NOT NULL DEFAULT 250 COMMENT 'Карма игрока' AFTER `status`")
if err != nil {
return err
}
return nil
}
// AddKarmaToPlayersDown destroys `karma` column
func AddKarmaToPlayersDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `players` DROP COLUMN `karma`")
if err != nil {
return err
}
return nil
}

View File

@ -45,6 +45,7 @@ func (m *Migrations) Init() {
goose.AddNamedMigration("32_add_is_active_to_pokememes.go", AddIsActiveToPokememesUp, AddIsActiveToPokememesDown) goose.AddNamedMigration("32_add_is_active_to_pokememes.go", AddIsActiveToPokememesUp, AddIsActiveToPokememesDown)
goose.AddNamedMigration("33_delete_datamapped_tables.go", DeleteDataMappedTablesUp, DeleteDataMappedTablesDown) goose.AddNamedMigration("33_delete_datamapped_tables.go", DeleteDataMappedTablesUp, DeleteDataMappedTablesDown)
goose.AddNamedMigration("34_delete_pokememes_tables.go", DeletePokememesTablesUp, DeletePokememesTablesDown) goose.AddNamedMigration("34_delete_pokememes_tables.go", DeletePokememesTablesUp, DeletePokememesTablesDown)
goose.AddNamedMigration("35_add_karma_to_players.go", AddKarmaToPlayersUp, AddKarmaToPlayersDown)
} }
// Migrate migrates database to current version // Migrate migrates database to current version

View File

@ -192,6 +192,8 @@ func (u *Users) ProfileMessage(update *tgbotapi.Update, playerRaw *dbmapping.Pla
} }
} }
message += "\nКарма: " + strconv.Itoa(playerRaw.Karma)
message += "\n\n⏰Последнее обновление профиля: " + profileRaw.CreatedAt.Format("02.01.2006 15:04:05") message += "\n\n⏰Последнее обновление профиля: " + profileRaw.CreatedAt.Format("02.01.2006 15:04:05")
message += "\nНе забывай обновляться, это важно для получения актуальной информации.\n\n" message += "\nНе забывай обновляться, это важно для получения актуальной информации.\n\n"
message += "/best посмотреть лучших покемемов для поимки\n" message += "/best посмотреть лучших покемемов для поимки\n"