Aucune description

client.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package funmow
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "net"
  7. "strings"
  8. "unicode"
  9. )
  10. type Client struct {
  11. conn net.Conn
  12. connectionID int
  13. playerID DBRef
  14. authenticated bool
  15. connected bool
  16. reader *bufio.Reader
  17. commandChan chan string
  18. inbound chan PlayerEvent
  19. outbound chan PlayerEvent
  20. eventDistributor *EventDistributor
  21. db *DB
  22. factory *ObjectFactory
  23. }
  24. func NewClient(conn net.Conn, connectionID int, e *EventDistributor, w *DB) *Client {
  25. c := new(Client)
  26. c.conn = conn
  27. c.connectionID = connectionID
  28. c.reader = bufio.NewReader(conn)
  29. c.authenticated = false
  30. c.connected = true
  31. c.commandChan = make(chan string)
  32. c.inbound = make(chan PlayerEvent)
  33. c.eventDistributor = e
  34. c.db = w
  35. c.factory = NewObjectFactory(w)
  36. return c
  37. }
  38. func (c *Client) Run() {
  39. log.Print("Received connection from ", c.conn.RemoteAddr())
  40. go func() {
  41. for c.connected {
  42. message, err := c.reader.ReadString('\n')
  43. if err != nil {
  44. c.connected = false
  45. return
  46. }
  47. message = strings.TrimSpace(message)
  48. if len(message) > 0 {
  49. c.commandChan <- message
  50. }
  51. }
  52. }()
  53. c.splash()
  54. for c.connected {
  55. select {
  56. case m := <-c.inbound:
  57. c.handleInboundEvent(m)
  58. case cmd := <-c.commandChan:
  59. if c.authenticated {
  60. c.sendCommand(cmd)
  61. } else {
  62. c.handleLoginPhase(cmd)
  63. }
  64. }
  65. }
  66. c.conn.Close()
  67. c.outbound <- PlayerEvent{src: c.playerID, dst: c.playerID, messageType: EventTypeTeardown, connectionID: c.connectionID}
  68. log.Print("Lost connection from ", c.conn.RemoteAddr())
  69. }
  70. func (c *Client) splash() {
  71. c.write("Welcome to...\n")
  72. c.write(" ___ __ __ ___ __ __\n")
  73. c.write(" | __| _ _ _ _ | \\/ | / _ \\ \\ \\ / /\n")
  74. c.write(" | _| | || | | ' \\ | |\\/| | | (_) | \\ \\/\\/ /\n")
  75. c.write(" |_| \\_,_| |_||_| |_| |_| \\___/ \\_/\\_/\n\n")
  76. c.write("use connect <username> <password> to login.\n")
  77. }
  78. func (c *Client) handleLoginPhase(message string) {
  79. fields := strings.FieldsFunc(message, func(c rune) bool {
  80. return unicode.IsSpace(c)
  81. })
  82. switch fields[0] {
  83. case "help":
  84. c.write("Commands:\n\tcon[nect] <username>\n\tquit\n")
  85. case "con":
  86. fallthrough
  87. case "connect":
  88. if len(fields) == 3 {
  89. player, found := c.db.GetPlayer(fields[1])
  90. if !found || (found && player.Password != fields[2]) {
  91. c.write("Bad username or password.\n")
  92. break
  93. }
  94. c.playerID = player.ID
  95. c.authenticated = true
  96. // when a player authenticates, the ipc channels get turned on
  97. subReq := EventSubscribeRequest{
  98. connectionID: c.connectionID,
  99. playerID: c.playerID,
  100. inbound: c.inbound,
  101. outbound: make(chan chan PlayerEvent),
  102. }
  103. c.outbound = c.eventDistributor.Subscribe(subReq)
  104. c.sendCommand("look")
  105. } else {
  106. c.write("What?\n")
  107. }
  108. case "create":
  109. if len(fields) == 3 {
  110. _, found := c.db.GetPlayer(fields[1])
  111. if found {
  112. c.write("That name is already taken.\n")
  113. break
  114. }
  115. playerID, _ := c.db.CreatePlayer(strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]), nil)
  116. _, found = c.db.Fetch(playerID)
  117. if !found {
  118. c.write("I can't even.\n")
  119. break
  120. }
  121. c.write("You have been created. Now you can connect!\n")
  122. } else {
  123. c.write("What?\n")
  124. }
  125. case "quit":
  126. c.connected = false
  127. default:
  128. c.write("What?\n")
  129. }
  130. }
  131. func (c *Client) handleInboundEvent(m PlayerEvent) {
  132. switch m.messageType {
  133. case EventTypeOutput:
  134. c.write("%s\n", m.message)
  135. case EventTypeQuit:
  136. c.connected = false
  137. }
  138. }
  139. func (c *Client) write(format string, a ...interface{}) {
  140. fmt.Fprintf(c.conn, format, a...)
  141. }
  142. func (c *Client) sendCommand(message string) {
  143. c.outbound <- PlayerEvent{src: c.playerID, dst: c.playerID, message: message, messageType: EventTypeCommand, connectionID: c.connectionID}
  144. }