-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
117 lines (96 loc) · 3.06 KB
/
db.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
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"log"
)
var (
dbMap *gorp.DbMap
dbType string
dbConnectionString string
)
// Website user in the database
type RealUser struct {
Id int64 `db:"id"`
Username string `db:"username"`
AuthToken string `db:"authtoken"`
Email string `db:"email"`
IsActive bool `db:"is_active"`
}
// Attempt to log in. Should probably rate limit this.
func GetRealUser(username, authtoken string) *RealUser {
if username == "" || authtoken == "" {
return nil
}
var user RealUser
err := dbMap.SelectOne(&user, "SELECT id, username, authtoken, is_active FROM user_user WHERE username=?", username)
if testMode {
// Test mode creates the user if it doesn't exist.
if err != nil || user.Id == 0 {
user.Username = username
user.AuthToken = authtoken
user.Email = username + "@starbowmod.com"
user.IsActive = true
err = dbMap.Insert(&user)
if err != nil {
log.Println(err)
}
}
}
if !user.IsActive {
return nil
}
if err != nil || user.Id == 0 {
return nil
}
if user.AuthToken != authtoken {
return nil
}
return &user
}
func initDb() (err error) {
// connect to db
db, err := sql.Open(dbType, dbConnectionString)
if err != nil {
return
}
// construct a gorp DbMap
switch dbType {
case "sqlite3":
dbMap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
case "mysql":
dbMap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
}
// add tables
table := dbMap.AddTableWithName(Client{}, "clients").SetKeys(false, "Id")
table.ColMap("Division").Transient = true
table.ColMap("DivisionRank").Transient = true
table.ColMap("chatDelayScale").Transient = true
table.ColMap("chatLastMessageTime").Transient = true
table.ColMap("LadderSearchRegions").Transient = true
table = dbMap.AddTableWithName(ClientRegionStats{}, "client_region_stats").SetKeys(true, "Id")
table.ColMap("Division").Transient = true
table.ColMap("DivisionRank").Transient = true
dbMap.AddTableWithName(Division{}, "divisions").SetKeys(true, "Id")
dbMap.AddTableWithName(BattleNetCharacter{}, "battle_net_characters").SetKeys(true, "Id")
dbMap.AddTableWithName(MatchResult{}, "match_results").SetKeys(true, "Id")
dbMap.AddTableWithName(MatchResultPlayer{}, "match_result_players").SetKeys(true, "Id")
dbMap.AddTableWithName(MatchResultSource{}, "match_result_sources").SetKeys(true, "Id")
dbMap.AddTableWithName(MatchmakerMatch{}, "matchmaker_matches").SetKeys(true, "Id")
dbMap.AddTableWithName(MatchmakerMatchParticipant{}, "matchmaker_match_participants").SetKeys(true, "Id")
dbMap.AddTableWithName(Map{}, "maps").SetKeys(true, "Id")
dbMap.AddTableWithName(MapVeto{}, "map_vetoes").SetKeys(true, "Id")
// dbMap.AddTableWithName(ChatMessage{}, "chat_messages").SetKeys(true, "Id")
// if testMode {
dbMap.AddTableWithName(RealUser{}, "user_user").SetKeys(true, "Id")
// }
// create the tables.
err = dbMap.CreateTablesIfNotExists()
if err != nil {
dbMap = nil
return
}
return
}