/
server.go
135 lines (122 loc) · 3.7 KB
/
server.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) 2010 Alex Gontmakher (gsasha@gmail.com)
// This code is licensed under GPLv3
package main
import (
"flag"
"fmt"
"http"
"log"
"strings"
"game/game"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var room = game.NewGameRoom()
func main() {
flag.Parse()
http.HandleFunc("/connect/", Connect)
http.HandleFunc("/list/", List)
http.HandleFunc("/wait/", Wait)
http.HandleFunc("/create/", Create)
http.HandleFunc("/send/", Send)
http.HandleFunc("/receive/", Receive)
fmt.Println("Server starting")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Exit("ListenAndServe:", err)
}
}
func Connect(c *http.Conn, req *http.Request) {
query_params, _ := http.ParseQuery(req.URL.RawQuery)
arg_owner, ok_owner := query_params["owner"]
if ok_owner {
fmt.Println("Connect owner=", arg_owner)
owner := arg_owner[0]
owner_id := room.Connect(owner)
fmt.Println("Connected as ", owner_id)
c.Write([]byte(owner_id))
} else {
c.Write([]byte("Parameters not specified correctly"))
}
}
func List(c *http.Conn, req *http.Request) {
players := room.List()
c.Write([]byte(strings.Join(players, ",")))
}
func Create(c *http.Conn, req *http.Request) {
query_params, _ := http.ParseQuery(req.URL.RawQuery)
arg_owner_id, ok_owner_id := query_params["owner_id"]
arg_guest, ok_guest := query_params["guest"]
if ok_owner_id && ok_guest {
fmt.Println("Create owner_id=", arg_owner_id, ", guest=", arg_guest)
owner_id := arg_owner_id[0]
guest := arg_guest[0]
game_id := room.Create(owner_id, guest)
fmt.Println("Create ", game_id)
c.Write([]byte(game_id))
} else {
c.Write([]byte("Parameters not specified correctly"))
}
}
func Wait(c *http.Conn, req *http.Request) {
query_params, _ := http.ParseQuery(req.URL.RawQuery)
arg_owner_id, ok_owner_id := query_params["owner_id"]
if ok_owner_id {
fmt.Println("Create owner_id=", arg_owner_id)
owner_id := arg_owner_id[0]
game_id := room.Wait(owner_id)
fmt.Println("Create ", game_id)
c.Write([]byte(game_id))
} else {
c.Write([]byte("Parameters not specified correctly"))
}
}
func Send(c *http.Conn, req *http.Request) {
query_params, _ := http.ParseQuery(req.URL.RawQuery)
arg_game, ok_game := query_params["game"]
arg_msg, ok_msg := query_params["msg"]
if !ok_game {
c.Write([]byte("Game parameter not set"))
return
}
if !ok_msg {
c.Write([]byte("Msg parameter not set"))
return
}
game := arg_game[0]
msg := arg_msg[0]
params := strings.Split(game, ",", 2)
if len(params) != 2 {
c.Write([]byte("Malformed game parameter"))
return
}
game_id, player_id := params[0], params[1]
game_instance := room.Find(game_id)
if room == nil {
c.Write([]byte("Game not found"))
return
}
result := game_instance.Send(player_id, msg)
c.Write([]byte(result))
}
func Receive(c *http.Conn, req *http.Request) {
query_params, _ := http.ParseQuery(req.URL.RawQuery)
arg_game, ok_game := query_params["game"]
if !ok_game {
c.Write([]byte("Game parameter not set"))
return
}
game := arg_game[0]
params := strings.Split(game, ",", 2)
if len(params) != 2 {
c.Write([]byte("Malformed game parameter"))
return
}
game_id, player_id := params[0], params[1]
game_instance := room.Find(game_id)
if room == nil {
c.Write([]byte("Game not found"))
return
}
result := game_instance.Receive(player_id)
c.Write([]byte(result))
}