-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
135 lines (123 loc) · 3.05 KB
/
util.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
135
package main
import (
"encoding/json"
"log"
"math"
"math/rand"
"net"
"time"
"github.com/gobwas/ws/wsutil"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
func randSeq(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]rune, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}
func calculateDistance(point1 Location, point2 Location) float64 {
const R = 6371 // Radius of the earth in km
dLat := deg2rad(float64(point1.Lat) - float64(point2.Lat))
dLong := deg2rad(float64(point1.Long) - float64(point2.Long))
a := math.Sin(dLat / 2) * math.Sin(dLat / 2) + math.Cos(
deg2rad(float64(point1.Lat)) * math.Cos(deg2rad(float64(point2.Lat))),
) * math.Sin(dLong / 2) * math.Sin(dLong / 2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1 - a))
d := R * c;
return d
}
func deg2rad(deg float64) float64 {
return deg * (math.Pi / 180)
}
func (s *Server) ParseServerMessage(msg []byte, cl *Client, conn *net.Conn) {
message := Message{}
err := json.Unmarshal(msg, &message)
if err != nil {
log.Println(err.Error(), "sup")
}
if message.Type == BINDMSG {
cl.Range = message.From.Range
cl.Location = message.From.Location
cl.Username = message.From.Username
s.clients[conn] = cl
}
}
func (s *Server) Match() error {
for k, v := range s.clients {
for k2, v2 := range s.clients {
if k == k2 {
continue
}
distance := calculateDistance(v.Location, v2.Location)
if distance <= float64(v.Range) && distance <= float64(v2.Range) {
// match dem
room := new(Room)
roomId := randSeq(5)
c1 := s.clients[k]
c1.conn = *k
c1.room = roomId
room.c1 = *c1
c2 := s.clients[k2]
c2.conn = *k2
c2.room = roomId
room.c2 = *c2
s.rooms[roomId] = *room
// manually send the matched message to both clients
s.SendClientDetails(*c1, *c2, CNNCTMSG)
s.SendClientDetails(*c2, *c1, CNNCTMSG)
delete(s.clients, k)
delete(s.clients, k2)
return nil
}
}
}
return nil
}
func (s *Server) BroadcastMessage(roomId string, msg Message) {
if room, ok := s.rooms[roomId]; ok {
room.messages = append(room.messages, msg)
s.rooms[roomId] = room
jsn, err := json.Marshal(msg)
if err != nil {
log.Println(err.Error())
}
wsutil.WriteServerMessage(
room.c1.conn,
1,
jsn,
)
wsutil.WriteServerMessage(
room.c2.conn,
1,
jsn,
)
}
}
func (s *Server) SendClientDetails(to, of Client, msgType MessageType) {
msg := Message{
Type: msgType,
From: of,
Body: "",
}
msgJson, err := json.Marshal(&msg)
if err != nil {
log.Println(err.Error())
}
wsutil.WriteServerMessage(
to.conn,
1,
msgJson,
)
}
func IsServerMessage(jsn []byte) bool {
msg := Message{}
err := json.Unmarshal(jsn, &msg)
log.Println(msg)
if err != nil {
log.Println(err)
return false
}
return msg.Type != CLNTMSG
}