Skip to content
Snippets Groups Projects
Commit 9910ce9f authored by Alex Constantin-Gomez's avatar Alex Constantin-Gomez
Browse files

database code

parent 84e843b5
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,11 @@ import ( ...@@ -7,10 +7,11 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"sync" "sync"
_ "github.com/mattn/go-sqlite3" _ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
...@@ -54,19 +55,19 @@ func main() { ...@@ -54,19 +55,19 @@ func main() {
// Initialise global variables // Initialise global variables
gameData = make(map[int]*Game) gameData = make(map[int]*Game)
connStr := "dbname=bigtactoe sslmode=require" connStr := "user=btt password='" + os.Getenv("BTT_PASSWORD") + "' dbname=bigtactoe sslmode=require"
database, err := sql.Open("postgres", connStr) database, err := sql.Open("postgres", connStr)
if err != nil { if err != nil {
log.Fatal("could not connect to database") log.Fatal("could not connect to database")
} }
defer database.Close() defer database.Close()
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT, wins INTEGER)") // statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT, wins INTEGER)")
statement.Exec() // statement.Exec()
// test query // // test query
database.QueryRow("INSERT INTO users(username, password, wins) VALUES($1, $2, $3);", database.QueryRow("INSERT INTO users(username, password, wins) VALUES($1, $2, $3);",
"alex123", "qwerty", 0) "user123", "qwerty", 0)
http.HandleFunc("/", hello) http.HandleFunc("/", hello)
...@@ -77,9 +78,9 @@ func main() { ...@@ -77,9 +78,9 @@ func main() {
http.HandleFunc("/wait", checkJoinGame) http.HandleFunc("/wait", checkJoinGame)
// User authentication endpoints // User authentication endpoints
// http.HandleFunc("/create-account", createUser) http.HandleFunc("/create-account", createUser)
// http.HandleFunc("/login", login) http.HandleFunc("/login", login)
// http.HandleFunc("/leaderboard", leaderboard) http.HandleFunc("/leaderboard", leaderboard)
err = http.ListenAndServe(":8080", nil) err = http.ListenAndServe(":8080", nil)
if err != nil { if err != nil {
...@@ -114,6 +115,7 @@ func move(w http.ResponseWriter, r *http.Request) { ...@@ -114,6 +115,7 @@ func move(w http.ResponseWriter, r *http.Request) {
recChan := game.Channels[1-move.Player] recChan := game.Channels[1-move.Player]
sendChan <- move.MoveData sendChan <- move.MoveData
// If game over or disconnect occurs
if move.GameOver { if move.GameOver {
fmt.Fprintf(w, "Game over") fmt.Fprintf(w, "Game over")
return return
...@@ -138,7 +140,8 @@ func createGame(w http.ResponseWriter, r *http.Request) { ...@@ -138,7 +140,8 @@ func createGame(w http.ResponseWriter, r *http.Request) {
} }
err := json.Unmarshal(jsonData, &playerData) err := json.Unmarshal(jsonData, &playerData)
if err != nil { if err != nil {
log.Println("could not decode json data into Move struct") fmt.Fprintf(w, "could not decode json data into Move struct")
return
} }
newGameID := generateID() newGameID := generateID()
...@@ -170,7 +173,7 @@ func joinGame(w http.ResponseWriter, r *http.Request) { ...@@ -170,7 +173,7 @@ func joinGame(w http.ResponseWriter, r *http.Request) {
} }
err := json.Unmarshal(jsonData, &join) err := json.Unmarshal(jsonData, &join)
if err != nil { if err != nil {
log.Println("could not decode json data") fmt.Fprintf(w, "could not decode json data")
} }
gameDataMutex.RLock() gameDataMutex.RLock()
...@@ -201,7 +204,8 @@ func checkJoinGame(w http.ResponseWriter, r *http.Request) { ...@@ -201,7 +204,8 @@ func checkJoinGame(w http.ResponseWriter, r *http.Request) {
} }
err := json.Unmarshal(jsonData, &gameID) err := json.Unmarshal(jsonData, &gameID)
if err != nil { if err != nil {
log.Println("could not decode json data") fmt.Fprintf(w, "could not decode json data")
return
} }
// check if player 2 joined // check if player 2 joined
...@@ -209,7 +213,7 @@ func checkJoinGame(w http.ResponseWriter, r *http.Request) { ...@@ -209,7 +213,7 @@ func checkJoinGame(w http.ResponseWriter, r *http.Request) {
game, found := gameData[gameID.GameID] game, found := gameData[gameID.GameID]
gameDataMutex.RUnlock() gameDataMutex.RUnlock()
if !found { if !found {
log.Println("error: could not find game ID in checkJoinGame endpoint") fmt.Fprintf(w, "error: could not find game ID in checkJoinGame endpoint")
return return
} }
p2 := <-game.Channels[0] p2 := <-game.Channels[0]
...@@ -227,7 +231,8 @@ func createUser(w http.ResponseWriter, r *http.Request) { ...@@ -227,7 +231,8 @@ func createUser(w http.ResponseWriter, r *http.Request) {
} }
err := json.Unmarshal(jsonData, &account) err := json.Unmarshal(jsonData, &account)
if err != nil { if err != nil {
log.Println("could not decode json data") fmt.Fprintf(w, "could not decode json data")
return
} }
// response will be an error message and success boolean // response will be an error message and success boolean
...@@ -248,7 +253,8 @@ func login(w http.ResponseWriter, r *http.Request) { ...@@ -248,7 +253,8 @@ func login(w http.ResponseWriter, r *http.Request) {
} }
err := json.Unmarshal(jsonData, &account) err := json.Unmarshal(jsonData, &account)
if err != nil { if err != nil {
log.Println("could not decode json data") fmt.Fprintf(w, "could not decode json data")
return
} }
// response will be the new user's ID if successful, otherwise, will be other message // response will be the new user's ID if successful, otherwise, will be other message
...@@ -270,7 +276,8 @@ func leaderboard(w http.ResponseWriter, r *http.Request) { ...@@ -270,7 +276,8 @@ func leaderboard(w http.ResponseWriter, r *http.Request) {
jsonString, err := json.Marshal(usernames) jsonString, err := json.Marshal(usernames)
if err != nil { if err != nil {
log.Println("could not marshal leaderboard usernames into json") fmt.Fprintf(w, "could not marshal leaderboard usernames into json")
return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
...@@ -324,6 +331,16 @@ func getLeaderboard() (map[string]int, bool) { ...@@ -324,6 +331,16 @@ func getLeaderboard() (map[string]int, bool) {
return usernames, true return usernames, true
} }
func incrementWins(username string) (int, bool) {
var wins int
err := database.QueryRow("UPDATE users SET wins=wins+1 WHERE username=$1 RETURNING wins;",
username).Scan(&wins)
if err != nil {
return -1, false
}
return wins, true
}
/************** Helper functions *******************/ /************** Helper functions *******************/
func generateID() int { func generateID() int {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment