-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighscoreServer.go
More file actions
executable file
·153 lines (132 loc) · 4.29 KB
/
highscoreServer.go
File metadata and controls
executable file
·153 lines (132 loc) · 4.29 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"net"
"os"
"fmt"
"strings"
"github.com/thoj/go-mysqlpure"
"time"
"bytes"
)
func main() {
logIt("SETUP", "Starting...")
addr, err := net.ResolveTCPAddr("ip4", ":4848")
errorCheck(err, "Problem resolving TCP address")
listen, err := net.ListenTCP("tcp", addr)
errorCheck(err, "TCP listening error")
logIt("SETUP", "Ready.")
for{
connection, err := listen.Accept()
if(err != nil){
continue
}
logIt("CONNECTION", "Got new connection")
go newClient(connection)
}
os.Exit(0)
}
func newClient(connect net.Conn){
logIt("CONNECTION", "Handling new client")
var buffer [512]byte
_, err := connect.Read(buffer[0:])
if err != nil {
logError("ERROR", "Error reading from client", err)
connect.Close()
return
}
commm := parseCommand(string(buffer[0:]))
_, err2 := connect.Write([]byte(commm))
if err2 != nil {
logError("ERROR", "Error writing to client", err2)
connect.Close()
return
}
connect.Close()
logIt("CONNECTION", "Closing connection to client")
}
func logError(ertype string, message string, err error){
fmt.Printf("%s\t%s: %s: %s\n", time.Now().String(), ertype, message, err)
}
func logIt(ertype string, message string){
fmt.Printf("%s\t%s: %s\n", time.Now().String(), ertype, message)
}
func errorCheck(err error, message string){
if(err != nil){
logError("ERROR", message, err)
}
}
func parseCommand(com string) (string){
com = string(bytes.TrimRight([]byte(com), string(byte(0))))
com = strings.Replace(com, "\n", "", -1)
var response string
fmt.Println("COMMAND:"+com)
dataCon, err := mysql.Connect("tcp", "127.0.0.1:3306", "hhss", "highscores", "hhss")
errorCheck(err, "Could not connect to MySQL database.")
scores := new(mysql.MySQLResponse)
checker := new(mysql.MySQLResponse)
parts := strings.Split(com, ":")
switch parts[0]{
case "user":
switch parts[1]{
case "new":
checker, err = dataCon.Query("SELECT username FROM users WHERE username='" + parts[2] + "';")
if len(checker.FetchRowMap()) == 0{
logIt("QUERY", "Inserting new user " + parts[2] + " " + parts[4] + ", " + parts[3])
_, err = dataCon.Query("INSERT INTO users (firstName,lastName,username) VALUES('" + parts[3] + "', '" + parts[4] + "', '" + parts[2] + "')")
if(err != nil){
errorCheck(err, "Could not enter new user into database. USER: " + parts[2])
response = "user:new:failure (reason unknown)\n"
} else{
response = "user:new:success\n"
}
} else {
response = "user:exists\n";
}
}
case "score":
switch parts[1]{
case "new":
checker, err = dataCon.Query("SELECT username FROM users WHERE username='" + parts[2] + "';")
if len(checker.FetchRowMap()) > 0 {
logIt("QUERY", "Inserting new score of " + parts[3] + " for " + parts[2])
_, err = dataCon.Query("INSERT INTO scores (username,score) VALUES('" + parts[2] + "', " + parts[3] + ")")
errorCheck(err, "Could not enter score into database. USER: " + parts[2] + "SCORE:" + parts[3])
response = "score:added\n"
} else {
response = "score:user does not exist\n";
}
case "get":
parts[2] = string(bytes.TrimRight([]byte(parts[2]), string(byte(0))))
if parts[2] == "all" {
logIt("QUERY", "Reading scores for " + parts[2])
scores, err = dataCon.Query("SELECT * FROM scores ORDER BY score DESC")
errorCheck(err, "Could not get scores from database. USER: " + parts[2])
response = "score:all:"
i := 0
for row := scores.FetchRowMap(); row != nil && i < 10; row = scores.FetchRowMap() {
response += row["username"] + "," + row["score"] + ";"
i += 1
}
response += "\n"
fmt.Println(response)
} else {
logIt("QUERY", "Reading scores for " + parts[2])
scores, err = dataCon.Query("SELECT * FROM scores ORDER BY score DESC")
errorCheck(err, "Could not get scores from database. USER: " + parts[2])
//fmt.Println(len("ll"))
response = "score:" + parts[2] + ":"
i := 0
for row := scores.FetchRowMap(); row != nil && i < 10; row = scores.FetchRowMap() {
if row["username"] == parts[2] {
response += row["username"] + "," + row["score"] + ";"
i += 1
}
}
response += "\n"
fmt.Println(response)
}
}
}
dataCon.Quit();
return response
}