-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·507 lines (371 loc) · 10.1 KB
/
main.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package main
import (
"code.google.com/p/go.net/websocket"
"github.com/zenazn/goji"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"encoding/json"
"github.com/zenazn/goji/web"
"strconv"
)
const (
GS_EMPTY = 0
GS_READY = 2
GS_ACTION_A_REQ = 3
GS_KICKOFF = 4
GS_PLAYING = 5
GS_DONE = 100
GS_DONE_P1 = 110
GS_DONE_P2 = 120
)
const (
PC_UP = 1
PC_DOWN = 2
PC_LEFT = 3
PC_RIGHT = 4
PC_ACTION_A = 10
PC_ACTION_B = 12
PC_GAME_GET = 20
)
const (
PDEF_P1_XPOS = 10
PDEF_P1_YPOS = 140
PDEF_P2_XPOS = 1130
PDEF_P2_YPOS = 140
)
type PeePlayer struct {
XPos int
YPos int
ZPos int
Name string
Points int
Damage int
PowerUp int
EpocTime int64
Ip string
Score int
RKey string
Height int
Width int
PMsg int
PMsgId int
}
type PeeItem struct {
XPos int
YPos int
ZPos int
Name string
VX int
VY int
AX int
AY int
Src string
Visible int
DefaultStepSize int
Height int
Width int
}
type PeeeGame struct {
Id int
State int
PlayerCount int
Players [2]PeePlayer
PCount int
WorldWidth int
WorldHeight int
Ball PeeItem
PowerUp PeeItem
TextMsg string;
}
type PCmd struct {
GameId int
Cmd int
Cmd2 int
Timestamp int64
RKey string
}
var pgames = make([]PeeeGame, 3)
func updateWorlds() {
for {
for i, _ := range pgames {
if pgames[i].State >= GS_KICKOFF && pgames[i].State <= GS_DONE {
pgames[i].Ball.XPos = pgames[i].Ball.XPos + pgames[i].Ball.VX
pgames[i].Ball.YPos = pgames[i].Ball.YPos + pgames[i].Ball.VY
if pgames[i].Ball.YPos < 0 { // hitting the top wall
pgames[i].Ball.YPos = 0
pgames[i].Ball.VY = -pgames[i].Ball.VY
// pgames[i].Ball.VX = -pgames[i].Ball.VX;
} else if pgames[i].Ball.YPos > (600 - 50) { // hitting the bottom wall
pgames[i].Ball.YPos = 600 - 50
pgames[i].Ball.VY = -pgames[i].Ball.VY
// pgames[i].Ball.VX = -pgames[i].Ball.VX;
}
// a point was scored
if pgames[i].Ball.XPos < 0 || pgames[i].Ball.XPos > 1250 {
pgames[i].TextMsg="Press BUTTON A to START";
if pgames[i].Ball.XPos < 5 {
pgames[i].Players[1].Score++
pgames[i].Players[1].PMsg=100;
pgames[i].Players[1].PMsgId = rand.Intn(1000000) + 1;
} else if pgames[i].Ball.XPos > 1250 {
pgames[i].Players[0].Score++
pgames[i].Players[0].PMsg=100;
pgames[i].Players[0].PMsgId = rand.Intn(1000000) + 1;
}
pgames[i].Ball.XPos = pgames[i].WorldWidth/2 - (pgames[i].Ball.Width / 2)
pgames[i].Ball.YPos = pgames[i].WorldHeight/2 - (pgames[i].Ball.Height / 2)
pgames[i].Ball.VY = 0
pgames[i].Ball.VX = -pgames[i].Ball.VX
pgames[i].State = GS_ACTION_A_REQ
}
//hit left paddle
if pgames[i].Ball.XPos < pgames[i].Players[0].XPos+pgames[i].Players[0].Width && pgames[i].Ball.XPos+50 > pgames[i].Players[0].XPos &&
pgames[i].Ball.YPos < pgames[i].Players[0].YPos+pgames[i].Players[0].Height && pgames[i].Ball.YPos+50 > pgames[i].Players[0].YPos {
pgames[i].Ball.VX = -pgames[i].Ball.VX
pgames[i].Ball.VY += (pgames[i].Ball.YPos - pgames[i].Players[0].YPos)
if pgames[i].Ball.VY > 3 {
pgames[i].Ball.VY = 2
} else if pgames[i].Ball.VY < -3 {
pgames[i].Ball.VY = -2
}
pgames[i].Ball.XPos += 130
pgames[i].Players[0].PMsg=101;
pgames[i].Players[0].PMsgId = rand.Intn(1000000) + 1;
}
//hit right paddle
if pgames[i].Ball.XPos < pgames[i].Players[1].XPos+pgames[i].Players[1].Width && pgames[i].Ball.XPos+50 > pgames[i].Players[1].XPos &&
pgames[i].Ball.YPos < pgames[i].Players[1].YPos+pgames[i].Players[1].Height && pgames[i].Ball.YPos+50 > pgames[i].Players[1].YPos {
// The objects are touching
pgames[i].Ball.VX = -pgames[i].Ball.VX
pgames[i].Ball.VY += (pgames[i].Ball.YPos - pgames[i].Players[1].YPos)
if pgames[i].Ball.VY > 3 {
pgames[i].Ball.VY = 2
} else if pgames[i].Ball.VY < -3 {
pgames[i].Ball.VY = -2
}
pgames[i].Ball.XPos -= 130
pgames[i].Players[1].PMsg=101;
pgames[i].Players[1].PMsgId = rand.Intn(1000000) + 1;
}
} //end if game state
} //end foreach games
time.Sleep(12000000)
} //end for-infinite
} //end updatewords
func findGameById(gameId int) *PeeeGame {
for i, agame := range pgames {
if agame.Id == gameId {
return &pgames[i]
} //end if
}
return nil
} //end find game
func processCmd(cmd *PCmd) *PeeeGame {
stepSize := 30
game := findGameById(cmd.GameId)
if game == nil {
log.Println("\n\nBAD NEWS CANT FIND\n\n")
return nil
}
pindex := 0
if cmd.RKey == game.Players[1].RKey {
pindex = 1
}
_ = pindex
switch cmd.Cmd2 {
case PC_LEFT:
{
game.Players[pindex].XPos = game.Players[pindex].XPos - stepSize
if pindex == 0 && game.Players[pindex].XPos < 10 {
game.Players[pindex].XPos = 10
}
if pindex == 1 && game.Players[pindex].XPos < (game.WorldWidth/2)+(game.Players[pindex].Width+100) {
game.Players[pindex].XPos = (game.WorldWidth / 2) + (game.Players[pindex].Width + 100)
}
log.Println("CMD LEFR")
break
}
case PC_RIGHT:
{
game.Players[pindex].XPos = game.Players[pindex].XPos + stepSize
if pindex == 0 && game.Players[pindex].XPos > (game.WorldWidth/2)-(game.Players[pindex].Width+100) {
game.Players[pindex].XPos = (game.WorldWidth / 2) - (game.Players[pindex].Width + 100)
}
if pindex == 1 && game.Players[pindex].XPos > 1130 {
game.Players[pindex].XPos = 1130
}
log.Println("CMD RIGHT")
break
}
} //end first switch
switch cmd.Cmd {
case PC_UP:
{
//log.Println("CMD UP")
game.Players[pindex].YPos = game.Players[pindex].YPos - stepSize
if game.Players[pindex].YPos < 1 {
game.Players[pindex].YPos = 0
}
return game
}
case PC_DOWN:
{
//log.Println("CMD DOWN")
game.Players[pindex].YPos = game.Players[pindex].YPos + stepSize
if game.Players[pindex].YPos > (game.WorldHeight - game.Players[pindex].Height) {
game.Players[pindex].YPos = (game.WorldHeight - game.Players[pindex].Height)
}
return game
}
case PC_ACTION_A:
{
log.Println("CMD PC_ACTION_A")
if game.State == GS_ACTION_A_REQ {
game.State = GS_KICKOFF
game.TextMsg = ""
}
return nil
}
case PC_ACTION_B:
{
log.Println("CMD PC_ACTION_B")
return nil
}
case PC_GAME_GET:
{
return game
}
} //end switch
return game
} //end process
func webHandlerCmd(ws *websocket.Conn) {
for {
//msg :=""
//fmt.Println("DEBUG:ip=" + ws.Request().RemoteAddr)
var reply string
err := websocket.Message.Receive(ws, &reply)
if err != nil {
fmt.Println("Can't receive")
break
}
rxCmd := &PCmd{}
json.Unmarshal([]byte(reply), &rxCmd)
if rxCmd.Cmd != 20 {
log.Println(fmt.Sprintf("RX-CMD:GameId(%d) , CMD(%d) ", rxCmd.GameId, rxCmd.Cmd))
}
///// fmt.Println("RX:" + reply + ",ip=" + ws.Request().RemoteAddr)
ws_resp := processCmd(rxCmd)
if ws_resp != nil {
str, _ := json.Marshal(ws_resp)
websocket.Message.Send(ws, string(str))
//log.Println(string(str))
} //end processCmd
//time.Sleep()
}
}
func RESTGameList(w http.ResponseWriter, r *http.Request) {
str, _ := json.Marshal(pgames)
fmt.Fprintf(w, string(str))
}
func RESTGameNew(w http.ResponseWriter, r *http.Request) {
for i, agame := range pgames {
if agame.State == GS_EMPTY {
setupGame(&pgames[i])
log.Println("AFTER", pgames[i].Id, "xxxx", i)
str, _ := json.Marshal(pgames[i])
fmt.Fprintf(w, string(str))
break
} //end if
}
}
func RESTGameGet(c web.C, w http.ResponseWriter, r *http.Request) {
gameidStr := c.URLParams["gameid"]
gameId, err := strconv.Atoi(gameidStr)
if err != nil {
fmt.Println(err)
}
for i, agame := range pgames {
if agame.Id == gameId {
str, _ := json.Marshal(pgames[i])
fmt.Fprintf(w, string(str))
return
} //end if
}
}
func RESTGameJoin(w http.ResponseWriter, r *http.Request) {
gameIdstr := r.URL.Query()["gameid"][0]
rkeyIdstr := r.URL.Query()["rkey"][0]
log.Println("RESTGAMEJOIN:gi=" + gameIdstr + ",rkey:" + rkeyIdstr)
gameId, err := strconv.Atoi(gameIdstr)
if err != nil {
fmt.Println(err)
fmt.Fprintf(w, err.Error())
}
for i := range pgames {
if gameId == pgames[i].Id && pgames[i].State >= GS_READY {
if pgames[i].Players[0].RKey == "" {
pgames[i].Players[0].RKey = rkeyIdstr
pgames[i].PCount++
//pgames[i].State = GS_KICKOFF
} else if pgames[i].Players[1].RKey == "" {
pgames[i].Players[1].RKey = rkeyIdstr
pgames[i].PCount++
} else if pgames[i].Players[1].RKey == rkeyIdstr {
//
} else if pgames[i].Players[0].RKey == rkeyIdstr {
} else {
///http.Error(w, "Too Many Players", 429)
}
if pgames[i].PCount > 1 {
pgames[i].State = GS_KICKOFF
}
str, _ := json.Marshal(pgames[i])
fmt.Fprintf(w, string(str))
fmt.Println("%#v", pgames[i])
return
} //end if
}
}
func setupGame(agame *PeeeGame) {
rand.Seed(time.Now().UTC().UnixNano())
agame.Id = rand.Intn(1000000) + 1
agame.State = GS_READY
agame.WorldWidth = 1300
agame.WorldHeight = 600
agame.Ball.Width = 50
agame.Ball.Height = 50
agame.Ball.XPos = agame.WorldWidth/2 - (agame.Ball.Width / 2)
agame.Ball.YPos = agame.WorldHeight/2 - (agame.Ball.Height / 2)
agame.Ball.Visible = 1
agame.Ball.VX = 4
agame.Ball.VY = 2
agame.Ball.Src = "http://peeepong.host/ball.png"
agame.Players[0].XPos = PDEF_P1_XPOS
agame.Players[0].YPos = PDEF_P1_YPOS
agame.Players[0].RKey = ""
agame.Players[0].Height = 160
agame.Players[0].Width = 160
agame.Players[0].PMsg = -1;
agame.Players[1].XPos = PDEF_P2_XPOS
agame.Players[1].YPos = PDEF_P2_YPOS
agame.Players[1].RKey = ""
agame.Players[1].PMsg = -1;
agame.Players[1].Height = 160
agame.Players[1].Width = 160
}
func main() {
http.Handle("/rest/cmd", websocket.Handler(webHandlerCmd))
goji.Post("/rest/game", RESTGameNew)
goji.Get("/rest/game", RESTGameList)
goji.Get("/rest/game/:gameid", RESTGameGet)
goji.Post("/rest/gamejoin", RESTGameJoin)
log.Println("PeeeServer")
setupGame(&pgames[0])
flag.Set("bind", ":8080")
go updateWorlds()
goji.Serve()
}