forked from TripwireTeam/tripwire
132 lines
2.4 KiB
Go
132 lines
2.4 KiB
Go
|
package main
|
||
|
|
||
|
// todo: make a consistent "player" type and have these functions return it
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"errors"
|
||
|
"io/fs"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func playerExistsByUsername(username string) (bool, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT username FROM users WHERE username = ?;
|
||
|
`
|
||
|
var x string
|
||
|
err := DB.QueryRow(sqlStatement, username).Scan(&x)
|
||
|
if err != nil {
|
||
|
if err == sql.ErrNoRows {
|
||
|
return false, nil
|
||
|
}
|
||
|
return false, err
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func playerExistsByUUID(uuid string) (bool, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT uuid FROM users WHERE uuid = ?;
|
||
|
`
|
||
|
var x string
|
||
|
err := DB.QueryRow(sqlStatement, uuid).Scan(&x)
|
||
|
if err != nil {
|
||
|
if err == sql.ErrNoRows {
|
||
|
return false, nil
|
||
|
}
|
||
|
return false, err
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func getPlayerUUID(username string) (string, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT uuid FROM users WHERE username = ?;
|
||
|
`
|
||
|
row := DB.QueryRow(sqlStatement, username)
|
||
|
|
||
|
var uuid string
|
||
|
err := row.Scan(&uuid)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return uuid, nil
|
||
|
}
|
||
|
|
||
|
func getPlayerUsername(uuid string) (string, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT username FROM users WHERE uuid = ?;
|
||
|
`
|
||
|
row := DB.QueryRow(sqlStatement, uuid)
|
||
|
|
||
|
var username string
|
||
|
err := row.Scan(&username)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return username, nil
|
||
|
}
|
||
|
|
||
|
func getPlayerSkinByUUID(uuid string) ([]byte, error) {
|
||
|
skin, err := os.ReadFile("skins/" + uuid + ".png")
|
||
|
if err != nil {
|
||
|
if errors.Is(err, fs.ErrNotExist) {
|
||
|
return []byte{}, &NotFoundError{}
|
||
|
}
|
||
|
return []byte{}, err
|
||
|
}
|
||
|
|
||
|
return skin, nil
|
||
|
}
|
||
|
|
||
|
func setPlayerSkinByUUID(uuid string, skin []byte) error {
|
||
|
if len(skin) > config.MaxSkinSize {
|
||
|
return &TooLargeError{}
|
||
|
}
|
||
|
exists, err := playerExistsByUUID(uuid)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if !exists {
|
||
|
return &NotFoundError{}
|
||
|
}
|
||
|
|
||
|
err = os.WriteFile("skins/"+uuid+".png", skin, 0644)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func findPlayerUUIDByAuthToken(token string) (string, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT uuid FROM users WHERE auth_token = ?;
|
||
|
`
|
||
|
row := DB.QueryRow(sqlStatement, token)
|
||
|
|
||
|
var uuid string
|
||
|
err := row.Scan(&uuid)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return uuid, nil
|
||
|
}
|
||
|
|
||
|
func checkPlayerPassByUUID(uuid string, password string) (bool, error) {
|
||
|
sqlStatement := `
|
||
|
SELECT password FROM users WHERE auth_token = ?;
|
||
|
`
|
||
|
row := DB.QueryRow(sqlStatement, token)
|
||
|
|
||
|
var pass string
|
||
|
err := row.Scan(&pass)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
if pass != password {
|
||
|
return false, nil
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|