2017-10-22 15:29:48 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
package broadcaster
|
2017-10-22 15:29:48 +04:00
|
|
|
|
|
|
|
|
|
import (
|
2018-02-13 22:05:32 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"time"
|
2017-10-22 15:29:48 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (b *Broadcaster) createBroadcastMessage(playerRaw *dbmapping.Player, messageBody string, broadcastType string) (dbmapping.Broadcast, bool) {
|
2017-10-22 15:29:48 +04:00
|
|
|
|
messageRaw := dbmapping.Broadcast{}
|
|
|
|
|
messageRaw.Text = messageBody
|
|
|
|
|
messageRaw.Status = "new"
|
|
|
|
|
messageRaw.BroadcastType = broadcastType
|
|
|
|
|
messageRaw.AuthorID = playerRaw.ID
|
|
|
|
|
messageRaw.CreatedAt = time.Now().UTC()
|
|
|
|
|
_, err := c.Db.NamedExec("INSERT INTO broadcasts VALUES(NULL, :text, :broadcast_type, :status, :author_id, :created_at)", &messageRaw)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
err2 := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE author_id=? AND text=?"), messageRaw.AuthorID, messageRaw.Text)
|
|
|
|
|
if err2 != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err2)
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageRaw, true
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (b *Broadcaster) getBroadcastMessageByID(messageID int) (dbmapping.Broadcast, bool) {
|
2017-10-22 15:29:48 +04:00
|
|
|
|
messageRaw := dbmapping.Broadcast{}
|
|
|
|
|
err := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE id=?"), messageID)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err)
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageRaw, true
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
func (b *Broadcaster) updateBroadcastMessageStatus(messageID int, messageStatus string) (dbmapping.Broadcast, bool) {
|
2017-10-22 15:29:48 +04:00
|
|
|
|
messageRaw := dbmapping.Broadcast{}
|
|
|
|
|
err := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE id=?"), messageID)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
messageRaw.Status = messageStatus
|
|
|
|
|
_, err = c.Db.NamedExec("UPDATE broadcasts SET status=:status WHERE id=:id", &messageRaw)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
err = c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE author_id=? AND text=?"), messageRaw.AuthorID, messageRaw.Text)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
c.Log.Error(err.Error())
|
2017-10-22 15:29:48 +04:00
|
|
|
|
return messageRaw, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageRaw, true
|
|
|
|
|
}
|