@ -1,19 +1,21 @@
package main
package db
import (
import (
"errors"
"errors"
"time"
"time"
"TerraOceanBot/common"
)
)
const (
const (
f orceRejectionVote = - 1
F orceRejectionVote = - 1
n uclearOptionVote = 10
N uclearOptionVote = 10
)
)
var e rrForceRejectionVoteReuse = errors . New ( "db: the user has used force rejection vote in the last month" )
var E rrForceRejectionVoteReuse = errors . New ( "db: the user has used force rejection vote in the last month" )
var e rrVoteIsOver = errors . New ( "db: the vote cannot be changed once it's over" )
var E rrVoteIsOver = errors . New ( "db: the vote cannot be changed once it's over" )
func c reateCustomVote( messageID , text string ) ( int , error ) {
func C reateCustomVote( messageID , text string ) ( int , error ) {
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , text , "custom" , false )
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , text , "custom" , false )
if err != nil {
if err != nil {
return 0 , err
return 0 , err
@ -22,7 +24,7 @@ func createCustomVote(messageID, text string) (int, error) {
return int ( lastID ) , nil
return int ( lastID ) , nil
}
}
func c reateInviteVote( messageID , username , reason string ) ( int , error ) {
func C reateInviteVote( messageID , username , reason string ) ( int , error ) {
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , username + ":" + reason , "invite" , false )
result , err := db . Exec ( "INSERT INTO vote(messageId, name, type, finished) VALUES(?, ?, ?, ?)" , messageID , username + ":" + reason , "invite" , false )
if err != nil {
if err != nil {
return 0 , err
return 0 , err
@ -31,7 +33,7 @@ func createInviteVote(messageID, username, reason string) (int, error) {
return int ( lastID ) , nil
return int ( lastID ) , nil
}
}
func g etVoteName( id int ) ( string , error ) {
func G etVoteName( id int ) ( string , error ) {
rows , err := db . Query ( "SELECT name FROM vote WHERE id=?" , id )
rows , err := db . Query ( "SELECT name FROM vote WHERE id=?" , id )
if err != nil {
if err != nil {
return "" , err
return "" , err
@ -45,10 +47,10 @@ func getVoteName(id int) (string, error) {
}
}
return name , nil
return name , nil
}
}
return "" , e rrNotFound
return "" , E rrNotFound
}
}
func g etVoteFromMessageID( msgID string ) ( int , error ) {
func G etVoteFromMessageID( msgID string ) ( int , error ) {
rows , err := db . Query ( "SELECT id FROM vote WHERE messageId=?" , msgID )
rows , err := db . Query ( "SELECT id FROM vote WHERE messageId=?" , msgID )
if err != nil {
if err != nil {
return 0 , err
return 0 , err
@ -62,10 +64,10 @@ func getVoteFromMessageID(msgID string) (int, error) {
}
}
return id , nil
return id , nil
}
}
return 0 , e rrNotFound
return 0 , E rrNotFound
}
}
func g etMessageIDFromVote( voteID int ) ( string , error ) {
func G etMessageIDFromVote( voteID int ) ( string , error ) {
rows , err := db . Query ( "SELECT messageId FROM vote WHERE id=?" , voteID )
rows , err := db . Query ( "SELECT messageId FROM vote WHERE id=?" , voteID )
if err != nil {
if err != nil {
return "" , err
return "" , err
@ -79,10 +81,10 @@ func getMessageIDFromVote(voteID int) (string, error) {
}
}
return id , nil
return id , nil
}
}
return "" , e rrNotFound
return "" , E rrNotFound
}
}
func g etVoteType( voteID int ) ( string , error ) {
func G etVoteType( voteID int ) ( string , error ) {
rows , err := db . Query ( "SELECT type FROM vote WHERE id=?" , voteID )
rows , err := db . Query ( "SELECT type FROM vote WHERE id=?" , voteID )
if err != nil {
if err != nil {
return "" , err
return "" , err
@ -96,18 +98,18 @@ func getVoteType(voteID int) (string, error) {
}
}
return messageType , nil
return messageType , nil
}
}
return "" , e rrNotFound
return "" , E rrNotFound
}
}
func u pdateVote( voteID int , userID string , voteValue int ) error {
func U pdateVote( voteID int , userID string , voteValue int ) error {
if voteValue == f orceRejectionVote {
if voteValue == F orceRejectionVote {
//Check if they used it within a month.
//Check if they used it within a month.
rows , err := db . Query ( "SELECT voteId FROM choices WHERE date >= ? AND value=?" , time . Now ( ) . AddDate ( 0 , - 1 , 0 ) )
rows , err := db . Query ( "SELECT voteId FROM choices WHERE date >= ? AND value=?" , time . Now ( ) . AddDate ( 0 , - 1 , 0 ) )
if err != nil {
if err != nil {
return err
return err
}
}
if rows . Next ( ) {
if rows . Next ( ) {
return e rrForceRejectionVoteReuse
return E rrForceRejectionVoteReuse
}
}
rows . Close ( )
rows . Close ( )
}
}
@ -116,30 +118,30 @@ func updateVote(voteID int, userID string, voteValue int) error {
rows , err := db . Query ( "SELECT finished FROM vote WHERE id=? AND finished=?" , voteID , true )
rows , err := db . Query ( "SELECT finished FROM vote WHERE id=? AND finished=?" , voteID , true )
defer rows . Close ( )
defer rows . Close ( )
if rows . Next ( ) {
if rows . Next ( ) {
return e rrVoteIsOver
return E rrVoteIsOver
}
}
_ , err = db . Exec ( "REPLACE INTO choices (voteId, userId, date, value) VALUES (?, ?, ?, ?)" , voteID , userID , time . Now ( ) , voteValue )
_ , err = db . Exec ( "REPLACE INTO choices (voteId, userId, date, value) VALUES (?, ?, ?, ?)" , voteID , userID , time . Now ( ) , voteValue )
return err
return err
}
}
func f inishVote( voteID int ) error {
func F inishVote( voteID int ) error {
_ , err := db . Exec ( "UPDATE vote SET finished=true WHERE id=?" , voteID )
_ , err := db . Exec ( "UPDATE vote SET finished=true WHERE id=?" , voteID )
return err
return err
}
}
func getAllVoteChoices( voteID int ) ( [ ] v oteChoice, error ) {
func GetAllVoteChoices( voteID int ) ( [ ] common . V oteChoice, error ) {
rows , err := db . Query ( "SELECT userId, value FROM choices WHERE voteId=?" , voteID )
rows , err := db . Query ( "SELECT userId, value FROM choices WHERE voteId=?" , voteID )
if err != nil {
if err != nil {
return [ ] v oteChoice{ } , err
return [ ] common. V oteChoice{ } , err
}
}
defer rows . Close ( )
defer rows . Close ( )
array := make ( [ ] v oteChoice, 0 )
array := make ( [ ] common. V oteChoice, 0 )
for rows . Next ( ) {
for rows . Next ( ) {
var userID string
var userID string
var value int
var value int
rows . Scan ( & userID , & value )
rows . Scan ( & userID , & value )
array = append ( array , v oteChoice{
array = append ( array , common. V oteChoice{
UserID : userID ,
UserID : userID ,
Value : value ,
Value : value ,
} )
} )