-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancel_player.go
106 lines (89 loc) · 2.95 KB
/
cancel_player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"database/sql"
"fmt"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func (t *TeleBot) cancelPlayer(u tgbotapi.Update) bool {
var eventTeamPlayers int
eventID := ""
allPlayers, err := t.dbPlayers(u)
db := dbConn("football_bot")
stmt, err := db.Prepare("SELECT id, event_team_players FROM events WHERE bot_id=? AND event_status=1")
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
defer stmt.Close()
err = stmt.QueryRow(botID).Scan(&eventID, &eventTeamPlayers)
if err != nil {
if err == sql.ErrNoRows {
msg := `There are no Games currently created
Try to run command /new_game firstly and then try again`
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 "+msg))
return false
}
t.HandleError(err)
}
var countDBPlayer int
stmt, err = db.Prepare("select count(*) from players where event_id=? and user_id=?")
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
defer stmt.Close()
err = stmt.QueryRow(eventID, u.Message.Chat.ID).Scan(&countDBPlayer)
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
t.HandleError(err)
}
fmt.Printf("Count DB player: %d\n", countDBPlayer)
if countDBPlayer < 1 {
msg := "You are not registered to this game"
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, msg))
return true
}
query := `DELETE FROM players where user_id=? and event_id=?;`
stmt, err = db.Prepare(query)
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
_, err = stmt.Exec(u.Message.Chat.ID, eventID)
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
if allPlayers[u.Message.Chat.ID][4] == "0" {
teamA = nil
teamB = nil
query = `UPDATE players SET reserve_nr=reserve_nr-1 WHERE reserve_nr > 0 ORDER BY id`
} else {
query = `UPDATE players SET reserve_nr=reserve_nr-1 WHERE reserve_nr > 1 ORDER BY id`
}
stmt, err = db.Prepare(query)
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
_, err = stmt.Exec()
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
msg := "You have been removed from the upcoming game"
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26BD "+msg))
db.Close()
err = t.bcastMessage(`Player `+u.Message.From.FirstName+" "+u.Message.From.LastName+` has left the game!
Current Players: `+strconv.Itoa(len(allPlayers)-1)+`/`+strconv.Itoa(eventTeamPlayers*2), u)
if err != nil {
fmt.Printf("Error: %v", err)
return false
}
return true
}