-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2852-2.go
More file actions
160 lines (136 loc) · 3.14 KB
/
2852-2.go
File metadata and controls
160 lines (136 loc) · 3.14 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
154
155
156
157
158
159
160
package main
import (
"fmt"
"math/rand"
"time"
"net"
"log"
"encoding/json"
)
type pokemon struct {
nombre string
latitud int
longitud int
}
type jugador struct {
nombre string
latitud int
longitud int
}
type tMsg struct {
Code int
Msg string
}
var localAddr string
var otherAddrs []string
var pokemons []pokemon
func main(){
fmt.Print("Dirección local: ")
fmt.Scanln(&localAddr)
otherPlayerAddr := ""
fmt.Print("Dirección de otro jugador: ")
fmt.Scanln(&otherPlayerAddr)
if otherPlayerAddr == "" { //Primera conexion
} else {
otherAddrs = append(otherAddrs, otherPlayerAddr)
//Update en el nuevo
sendMsg(otherPlayerAddr, tMsg {0, localAddr})
}
go poKreate()
server()
}
func randomPokeSpawn() pokemon{
rand.Seed(time.Now().UTC().UnixNano())
randX := rand.Intn(100)
randY := rand.Intn(100)
newPokemon := pokemon {"Chu", randX, randY}
return newPokemon
}
func server() {
if ln, err := net.Listen("tcp", localAddr); err != nil {
log.Panicln(err.Error())
} else {
defer ln.Close()
fmt.Println(localAddr, "listening")
for {
if conn, err := ln.Accept(); err != nil {
log.Panicln(err.Error())
} else {
go handle(conn)
}
}
}
}
func sendMsg(addr string, msg tMsg) {
if conn, err := net.Dial("tcp", addr); err != nil {
log.Println(err.Error())
} else {
defer conn.Close()
enc := json.NewEncoder(conn)
fmt.Println("Sending", msg, "to", addr)
enc.Encode(&msg)
}
}
func addToNet(newAddr string) {
for _, addr := range otherAddrs {
sendMsg(addr, tMsg {1, newAddr})
}
}
func poKreate(){
for {
var tPassed int64;
initTime := time.Now().UTC().UnixNano();
for {
tPassed = - initTime + time.Now().UTC().UnixNano();
if tPassed > 60000000000 { //Cada 1 minuto
fmt.Println("Creating pokemon!")
newPokemon := poKreate(); //Creacion de pokemon
//Buscar los 2 mas cercanos
for i, addrs := range otherAddrs{
//Mandar solicitud de confirmacion
sendMsg(addrs, tMsg {2, newPokemon}){}
if i > 2 {
break;
}
}
break;
}
}
}
}
func handle(conn net.Conn) {
defer conn.Close()
//fmt.Println(conn.RemoteAddr(), "accepted")
var msg tMsg
dec := json.NewDecoder(conn)
if err := dec.Decode(&msg); err != nil {
log.Println(err.Error())
} else {
fmt.Println("Got", msg)
switch msg.Code {
case 0: //Agregar en existente
accepted := ""
fmt.Print("El jugador ", msg.Msg, " se quiere conectar mediante ti. Aceptar? (y/n): ")
fmt.Scanln(&accepted)
if accepted == "y" {
addToNet(msg.Msg)
fmt.Println(msg.Msg, " se ha conectado mediante ti!")
otherAddrs = append(otherAddrs, msg.Msg)
//fmt.Println(otherAddrs)
}
case 1: //Agregado en red
otherAddrs = append(otherAddrs, msg.Msg)
fmt.Print("What´s going on folks? Ha llegado un nuevo entrenador! Los entrenadores en la red ahora son los siguientes: ")
fmt.Println(otherAddrs)
case 2: //Agregar nuevo pokemon
accepted := ""
fmt.Print("Se desea agregar el pokemon ", msg.Msg, ". Aceptar? (y/n): ")
fmt.Scanln(&accepted)
if accepted == "y" {
addToNet(msg.Msg)
fmt.Println(msg.Msg, " se creo el Pokemon")
pokemon = append(pokemon, msg.Msg)
}
}
}
}