Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

player.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package server
  2. import (
  3. "encoding/gob"
  4. "encoding/json"
  5. "log"
  6. "golang.org/x/net/websocket"
  7. "mcquay.me/bandwidth"
  8. )
  9. const maxMessageSize = 1024
  10. type encoder interface {
  11. Encode(v interface{}) error
  12. }
  13. type decoder interface {
  14. Decode(v interface{}) error
  15. }
  16. // StreamCount is the wrapper we use around reads and writes to keep track of
  17. // bandwidths.
  18. type StreamCount struct {
  19. ws *websocket.Conn
  20. bw *bandwidth.Bandwidth
  21. }
  22. // StreamCount.Read implements io.Reader
  23. func (sc *StreamCount) Read(p []byte) (n int, err error) {
  24. n, err = sc.ws.Read(p)
  25. sc.bw.AddRx <- n
  26. return n, err
  27. }
  28. // StreamCount.Write implements io.Writer
  29. func (sc *StreamCount) Write(p []byte) (n int, err error) {
  30. n, err = sc.ws.Write(p)
  31. sc.bw.AddTx <- n
  32. return n, err
  33. }
  34. // Close is for cleanup
  35. func (sc *StreamCount) Close() error {
  36. return sc.ws.Close()
  37. }
  38. // ProtoTalker is the simplest form of struct that talks to consumers of the
  39. // service. There are two important methods here: Sender and Recv.
  40. type ProtoTalker struct {
  41. enc encoder
  42. dec decoder
  43. counter *StreamCount
  44. send chan Message
  45. Id string
  46. }
  47. func NewProtoTalker(id string, ws *websocket.Conn, bw *bandwidth.Bandwidth, encoding string) *ProtoTalker {
  48. var enc encoder
  49. var dec decoder
  50. comptroller := &StreamCount{
  51. ws: ws,
  52. bw: bw,
  53. }
  54. if encoding == "json" {
  55. enc = json.NewEncoder(comptroller)
  56. dec = json.NewDecoder(comptroller)
  57. } else {
  58. enc = gob.NewEncoder(comptroller)
  59. dec = gob.NewDecoder(comptroller)
  60. }
  61. return &ProtoTalker{
  62. send: make(chan Message, 16),
  63. enc: enc,
  64. dec: dec,
  65. counter: comptroller,
  66. Id: id,
  67. }
  68. }
  69. // Sender is the single implementation for data output to clients, both players
  70. // and spectators.
  71. func (pt *ProtoTalker) Sender() {
  72. log.Printf("%s: %T Sender launched", pt.Id, pt.enc)
  73. for things := range pt.send {
  74. err := pt.enc.Encode(things)
  75. if err != nil {
  76. log.Println(err)
  77. break
  78. }
  79. }
  80. pt.counter.Close()
  81. log.Printf("%s: Sender close", pt.Id)
  82. }
  83. // player uses protoTalker's Sender method, but adds a Recv that knows how to
  84. // deal with game play instructions from the player.
  85. type Player struct {
  86. Robots []*Robot
  87. Instruction Instruction
  88. ProtoTalker
  89. }
  90. func NewPlayer(id string, ws *websocket.Conn, bw *bandwidth.Bandwidth, encoding string) *Player {
  91. return &Player{
  92. Robots: []*Robot{},
  93. ProtoTalker: *NewProtoTalker(id, ws, bw, encoding),
  94. }
  95. }
  96. // Player.Recv is the function responsible for parsing out player instructions
  97. // and sending them to the game.
  98. func (p *Player) Recv() {
  99. log.Println("starting Recv")
  100. for {
  101. var msgs map[string]Instruction
  102. err := p.dec.Decode(&msgs)
  103. if err != nil {
  104. log.Printf("%s: %s", p.Id, err)
  105. break
  106. }
  107. for _, r := range p.Robots {
  108. msg, ok := msgs[r.Id]
  109. if !ok {
  110. continue
  111. }
  112. if msg.Repair != nil && *msg.Repair == true {
  113. r.ActiveScan = false
  114. r.TargetSpeed = 0
  115. r.FireAt = nil
  116. r.MoveTo = nil
  117. if r.RepairCounter <= 0 {
  118. r.RepairCounter = 3.0
  119. }
  120. } else if msg.Scan != nil && *msg.Scan == true {
  121. r.RepairCounter = 0
  122. r.TargetSpeed = 0
  123. r.FireAt = nil
  124. r.MoveTo = nil
  125. r.ActiveScan = true
  126. } else {
  127. r.RepairCounter = 0
  128. r.ActiveScan = false
  129. // Reapiring halts all other activity
  130. if msg.MoveTo != nil {
  131. r.MoveTo = msg.MoveTo
  132. }
  133. if msg.Heading != nil {
  134. r.DesiredHeading = msg.Heading
  135. }
  136. if msg.FireAt != nil {
  137. r.FireAt = msg.FireAt
  138. } else {
  139. r.FireAt = nil
  140. }
  141. if msg.TargetSpeed != nil {
  142. r.TargetSpeed = *msg.TargetSpeed
  143. } else {
  144. r.TargetSpeed = r.Stats.Speed
  145. }
  146. }
  147. if msg.Probe != nil {
  148. r.Probe = msg.Probe
  149. r.ProbeResult = nil
  150. } else {
  151. r.Probe = nil
  152. }
  153. if msg.Message != nil {
  154. r.Message = *msg.Message
  155. }
  156. }
  157. }
  158. log.Printf("%s: Recv close", p.Id)
  159. p.counter.Close()
  160. }
  161. // Spectator merely sends out game state, does not receive meaningful
  162. // instructions from spectators.
  163. type Spectator struct {
  164. ProtoTalker
  165. }
  166. func NewSpectator(id string, ws *websocket.Conn, bw *bandwidth.Bandwidth, encoding string) *Spectator {
  167. return &Spectator{
  168. ProtoTalker: *NewProtoTalker(id, ws, bw, encoding),
  169. }
  170. }
  171. // Spectator.Recv is an interesting beast. We had to add it as for whatever
  172. // reason the server would lock up if we weren't reading the empty responses
  173. // from spectators.
  174. func (s *Spectator) Recv() {
  175. for {
  176. var msgs map[string]Instruction
  177. err := s.dec.Decode(&msgs)
  178. if err != nil {
  179. log.Printf("%s: %s", s.Id, err)
  180. break
  181. }
  182. // After the first bit of handshaking, the rest of the messages should
  183. // only be "{}" for spectators, and the following could hold true:
  184. //
  185. // if string(buff[:n]) != "{}" {
  186. // log.Printf("protocol breach!!")
  187. // break
  188. // }
  189. }
  190. log.Printf("%s: Recv close", s.Id)
  191. s.counter.Close()
  192. }