mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-03 19:47:14 +08:00
using uuid instead of usernames in friend table
This commit is contained in:
parent
b5d4118e96
commit
ac059b0a2e
@ -265,9 +265,19 @@ func FetchUsernameFromUUID(uuid []byte) (string, error) {
|
||||
return username, nil
|
||||
}
|
||||
|
||||
func isFriendWith(sourceUsername string, friendUsername string) (bool, error) {
|
||||
func FetchUUIDFromUsername(username string) ([]byte, error) {
|
||||
var uuid []byte
|
||||
err := handle.QueryRow("SELECT uuid FROM accounts WHERE username = ?", username).Scan(&uuid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return uuid, nil
|
||||
}
|
||||
|
||||
func IsFriendWith(sourceUuid []byte, friendUuid []byte) (bool, error) {
|
||||
var result int
|
||||
err := handle.QueryRow("SELECT COUNT(*) FROM friends WHERE user = ? AND friend = ?", sourceUsername, friendUsername).Scan(&result)
|
||||
err := handle.QueryRow("SELECT COUNT(*) FROM friends WHERE user = ? AND friend = ?", sourceUuid, friendUuid).Scan(&result)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -277,27 +287,21 @@ func isFriendWith(sourceUsername string, friendUsername string) (bool, error) {
|
||||
|
||||
func AddFriend(uuid []byte, friendUsername string) (bool, error) {
|
||||
// We are making db errors more generic as error is used in the response data to the client.
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("An error occured, are you connected ?")
|
||||
}
|
||||
|
||||
var doesUserExist int
|
||||
err = handle.QueryRow("SELECT COUNT(*) FROM accounts WHERE username = ?", friendUsername).Scan(&doesUserExist)
|
||||
friendUuid, err := FetchUUIDFromUsername(friendUsername)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("An error occured, if this persist, please contact an administrator.")
|
||||
}
|
||||
|
||||
if doesUserExist == 0 {
|
||||
if len(friendUuid) == 0 {
|
||||
return false, fmt.Errorf("User does not exist")
|
||||
}
|
||||
|
||||
alreadyFriends, _ := isFriendWith(username, friendUsername)
|
||||
alreadyFriends, _ := IsFriendWith(uuid, friendUuid)
|
||||
if alreadyFriends {
|
||||
return false, fmt.Errorf("Already friend with this user")
|
||||
}
|
||||
|
||||
_, err = handle.Exec("INSERT INTO friends (user, friend, since) VALUES (?, ?, UTC_TIMESTAMP())", username, friendUsername)
|
||||
_, err = handle.Exec("INSERT INTO friends (user, friend, since) VALUES (?, ?, UTC_TIMESTAMP())", uuid, friendUuid)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("An error occured, if this persist, please contact an administrator.")
|
||||
}
|
||||
@ -307,17 +311,21 @@ func AddFriend(uuid []byte, friendUsername string) (bool, error) {
|
||||
|
||||
func RemoveFriend(uuid []byte, friendUsername string) (bool, error) {
|
||||
// We are making db errors more generic as error is used in the response data to the client.
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
friendUuid, err := FetchUUIDFromUsername(friendUsername)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("An error occured, are you connected ?")
|
||||
return false, fmt.Errorf("An error occured, if this persist, please contact an administrator.")
|
||||
}
|
||||
|
||||
alreadyFriends, _ := isFriendWith(username, friendUsername)
|
||||
if len(friendUuid) == 0 {
|
||||
return false, fmt.Errorf("User does not exist")
|
||||
}
|
||||
|
||||
alreadyFriends, _ := IsFriendWith(uuid, friendUuid)
|
||||
if !alreadyFriends {
|
||||
return false, fmt.Errorf("You are not friend with this user")
|
||||
}
|
||||
|
||||
_, err = handle.Exec("DELETE FROM friends WHERE user = ? AND friend = ?", username, friendUsername)
|
||||
_, err = handle.Exec("DELETE FROM friends WHERE user = ? AND friend = ?", uuid, friendUuid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -326,13 +334,8 @@ func RemoveFriend(uuid []byte, friendUsername string) (bool, error) {
|
||||
}
|
||||
|
||||
func FriendCount(uuid []byte) (int, error) {
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
var count int
|
||||
err = handle.QueryRow("SELECT COUNT(*) FROM friends WHERE user = ?", username).Scan(&count)
|
||||
err := handle.QueryRow("SELECT COUNT(*) FROM friends WHERE user = ?", uuid).Scan(&count)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
@ -341,19 +344,14 @@ func FriendCount(uuid []byte) (int, error) {
|
||||
}
|
||||
|
||||
func FriendOnlineCount(uuid []byte) (int, error) {
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
query := `SELECT COUNT(*) FROM accounts a
|
||||
JOIN friends f
|
||||
ON a.username = f.friend
|
||||
ON a.uuid = f.friend
|
||||
WHERE a.lastActivity > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 5 MINUTE)
|
||||
AND f.user = ?;`
|
||||
|
||||
var count int
|
||||
err = handle.QueryRow(query, username).Scan(&count)
|
||||
err := handle.QueryRow(query, uuid).Scan(&count)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
29
db/daily.go
29
db/daily.go
@ -57,11 +57,6 @@ func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {
|
||||
func FetchRankings(category int, page int, uuid []byte) ([]defs.DailyRanking, error) {
|
||||
var rankings []defs.DailyRanking
|
||||
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return rankings, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * 10
|
||||
|
||||
var query string
|
||||
@ -78,7 +73,7 @@ func FetchRankings(category int, page int, uuid []byte) ([]defs.DailyRanking, er
|
||||
FROM accountDailyRuns adr
|
||||
JOIN dailyRuns dr ON dr.date = adr.date
|
||||
JOIN accounts a ON adr.uuid = a.uuid
|
||||
JOIN friends f ON a.username = f.friend
|
||||
JOIN friends f ON a.uuid = f.friend
|
||||
WHERE dr.date = UTC_DATE()
|
||||
AND a.banned = 0
|
||||
AND f.user = ?
|
||||
@ -89,13 +84,14 @@ func FetchRankings(category int, page int, uuid []byte) ([]defs.DailyRanking, er
|
||||
JOIN accounts a ON adr.uuid = a.uuid
|
||||
WHERE dr.date = UTC_DATE()
|
||||
AND a.banned = 0
|
||||
AND a.username = ?
|
||||
AND a.uuid = ?
|
||||
) AS combined LIMIT 10 OFFSET ?;`
|
||||
}
|
||||
|
||||
var results *sql.Rows
|
||||
var err error
|
||||
if category == 2 {
|
||||
results, err = handle.Query(query, username, username, offset)
|
||||
results, err = handle.Query(query, uuid, uuid, offset)
|
||||
} else {
|
||||
results, err = handle.Query(query, offset)
|
||||
}
|
||||
@ -120,11 +116,6 @@ func FetchRankings(category int, page int, uuid []byte) ([]defs.DailyRanking, er
|
||||
}
|
||||
|
||||
func FetchRankingPageCount(category int, uuid []byte) (int, error) {
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var query string
|
||||
switch category {
|
||||
case 0:
|
||||
@ -136,15 +127,15 @@ func FetchRankingPageCount(category int, uuid []byte) (int, error) {
|
||||
FROM accountDailyRuns adr
|
||||
JOIN dailyRuns dr ON dr.date = adr.date
|
||||
JOIN accounts a ON adr.uuid = a.uuid
|
||||
JOIN friends f ON a.username = f.friend
|
||||
JOIN friends f ON a.uuid = f.friend
|
||||
WHERE dr.date = UTC_DATE()
|
||||
AND f.user = ?
|
||||
OR a.username = ?`
|
||||
AND f.user = ?`
|
||||
}
|
||||
|
||||
var recordCount int
|
||||
var err error
|
||||
if category == 2 {
|
||||
err = handle.QueryRow(query, username, username).Scan(&recordCount)
|
||||
err = handle.QueryRow(query, uuid).Scan(&recordCount)
|
||||
// We only fetch friends of the account, not the account itself so adding +1 here.
|
||||
// this way, we don't have to do the big union query like in FetchRankings
|
||||
recordCount += 1
|
||||
@ -152,5 +143,9 @@ func FetchRankingPageCount(category int, uuid []byte) (int, error) {
|
||||
err = handle.QueryRow(query).Scan(&recordCount)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(math.Ceil(float64(recordCount) / 10)), nil
|
||||
}
|
||||
|
8
db/db.go
8
db/db.go
@ -179,11 +179,11 @@ func setupDb(tx *sql.Tx) error {
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS friends (
|
||||
relationId INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
user VARCHAR(16) NOT NULL,
|
||||
friend VARCHAR(16) NOT NULL,
|
||||
user BINARY(16) NOT NULL,
|
||||
friend BINARY(16) NOT NULL,
|
||||
since DATE NOT NULL,
|
||||
FOREIGN KEY (user) REFERENCES accounts (username) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
FOREIGN KEY (friend) REFERENCES accounts (username) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
FOREIGN KEY (user) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
FOREIGN KEY (friend) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
)`,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user