Browse Source

Shuffled event buffer around, look after go.

Keelan Lightfoot 8 years ago
parent
commit
c9c88ac5a3
4 changed files with 51 additions and 16 deletions
  1. 4
    4
      client.go
  2. 38
    0
      event_buffer.go
  3. 7
    7
      event_distributor.go
  4. 2
    5
      execution_context.go

+ 4
- 4
client.go View File

3
 import (
3
 import (
4
 	"bufio"
4
 	"bufio"
5
 	"fmt"
5
 	"fmt"
6
+	"github.com/mgutz/ansi"
6
 	"log"
7
 	"log"
7
 	"net"
8
 	"net"
8
 	"strings"
9
 	"strings"
9
 	"unicode"
10
 	"unicode"
10
-	"github.com/mgutz/ansi"
11
 )
11
 )
12
 
12
 
13
 type Client struct {
13
 type Client struct {
88
 
88
 
89
 func (c *Client) splash() {
89
 func (c *Client) splash() {
90
 
90
 
91
-	c.write(ansi.Color("Welcome to...","red+b")+"\n")
91
+	c.write(ansi.Color("Welcome to...", "red+b") + "\n")
92
 	c.write("  ___                __  __    ___  __      __\n")
92
 	c.write("  ___                __  __    ___  __      __\n")
93
 	c.write(" | __| _  _   _ _   |  \\/  |  / _ \\ \\ \\    / /\n")
93
 	c.write(" | __| _  _   _ _   |  \\/  |  / _ \\ \\ \\    / /\n")
94
 	c.write(" | _| | || | | ' \\  | |\\/| | | (_) | \\ \\/\\/ /\n")
94
 	c.write(" | _| | || | | ' \\  | |\\/| | | (_) | \\ \\/\\/ /\n")
95
 	c.write(" |_|   \\_,_| |_||_| |_|  |_|  \\___/   \\_/\\_/\n\n")
95
 	c.write(" |_|   \\_,_| |_||_| |_|  |_|  \\___/   \\_/\\_/\n\n")
96
 	c.write("Commands:\n")
96
 	c.write("Commands:\n")
97
-	c.write("To create a player: "+ansi.Color("create <username> <password>","blue+b")+"\n")
98
-	c.write("To connect: "+ansi.Color("connect <username> <password>","blue+b")+"\n")
97
+	c.write("To create a player: " + ansi.Color("create <username> <password>", "blue+b") + "\n")
98
+	c.write("To connect: " + ansi.Color("connect <username> <password>", "blue+b") + "\n")
99
 
99
 
100
 }
100
 }
101
 
101
 

+ 38
- 0
event_buffer.go View File

1
+package funmow
2
+
3
+func EventBuffer(inbound chan PlayerEvent) chan PlayerEvent {
4
+
5
+	queue := make([]*PlayerEvent, 0)
6
+	inboundBuffer := make(chan PlayerEvent)
7
+
8
+	go func() {
9
+		running := true
10
+	bufferLoop:
11
+		for running {
12
+			if len(queue) == 0 {
13
+				select {
14
+				case newEvent, ok := <-inbound:
15
+					if !ok {
16
+						break bufferLoop
17
+					}
18
+					queue = append(queue, &newEvent)
19
+				}
20
+			} else {
21
+				event := queue[0]
22
+				select {
23
+				case newEvent, ok := <-inbound:
24
+					if !ok {
25
+						break bufferLoop
26
+					}
27
+					queue = append(queue, &newEvent)
28
+				case inboundBuffer <- *event:
29
+					queue = queue[1:]
30
+				}
31
+			}
32
+		}
33
+		close(inboundBuffer)
34
+	}()
35
+
36
+	return inboundBuffer
37
+
38
+}

+ 7
- 7
event_distributor.go View File

65
 	e.whoChan <- replyChan
65
 	e.whoChan <- replyChan
66
 	onlinePlayers := <-replyChan
66
 	onlinePlayers := <-replyChan
67
 	return onlinePlayers
67
 	return onlinePlayers
68
-	return DBRefList{}
69
 }
68
 }
70
 
69
 
71
 //[client runs quit command]
70
 //[client runs quit command]
90
 	e.players = make(map[DBRef]*playerRegistration)
89
 	e.players = make(map[DBRef]*playerRegistration)
91
 
90
 
92
 	outbound := make(chan PlayerEvent)
91
 	outbound := make(chan PlayerEvent)
92
+	outboundBuffered := EventBuffer(outbound)
93
 
93
 
94
 	forceContext := NewForceContext(e, e.db, outbound)
94
 	forceContext := NewForceContext(e, e.db, outbound)
95
 	forceInbound := forceContext.StartInboundChannel()
95
 	forceInbound := forceContext.StartInboundChannel()
98
 
98
 
99
 		select {
99
 		select {
100
 		case replyChan := <-e.whoChan:
100
 		case replyChan := <-e.whoChan:
101
- 			onlinePlayers := make(DBRefList, 0)
102
- 			for playerID, _ := range e.players {
103
- 				onlinePlayers = append(onlinePlayers, playerID)
104
- 			}
105
- 			replyChan <- onlinePlayers
101
+			onlinePlayers := make(DBRefList, 0)
102
+			for playerID, _ := range e.players {
103
+				onlinePlayers = append(onlinePlayers, playerID)
104
+			}
105
+			replyChan <- onlinePlayers
106
 		case sub := <-e.subscribeChan:
106
 		case sub := <-e.subscribeChan:
107
 
107
 
108
 			if _, exists := e.players[sub.playerID]; !exists {
108
 			if _, exists := e.players[sub.playerID]; !exists {
118
 			reg := e.players[sub.playerID]
118
 			reg := e.players[sub.playerID]
119
 			reg.connInbound[sub.connectionID] = sub.inbound
119
 			reg.connInbound[sub.connectionID] = sub.inbound
120
 			sub.outbound <- outbound
120
 			sub.outbound <- outbound
121
-		case event := <-outbound: // message
121
+		case event := <-outboundBuffered: // message
122
 			//fmt.Printf("received message src:%d dest:%d type: %d\n", event.src, event.dst, event.messageType)
122
 			//fmt.Printf("received message src:%d dest:%d type: %d\n", event.src, event.dst, event.messageType)
123
 
123
 
124
 			destPlayer, validDest := e.players[event.dst]
124
 			destPlayer, validDest := e.players[event.dst]

+ 2
- 5
execution_context.go View File

66
 func (c *ExecutionContext) StartInboundChannel() chan PlayerEvent {
66
 func (c *ExecutionContext) StartInboundChannel() chan PlayerEvent {
67
 	c.inbound = make(chan PlayerEvent)
67
 	c.inbound = make(chan PlayerEvent)
68
 	inboundBuffer := make(chan PlayerEvent)
68
 	inboundBuffer := make(chan PlayerEvent)
69
-	c.outbound = c.outbound
70
 
69
 
71
 	go func() {
70
 	go func() {
72
 
71
 
145
 		c.evaluateCommand(m)
144
 		c.evaluateCommand(m)
146
 		return
145
 		return
147
 	}
146
 	}
148
-	
147
+
149
 	c.actor.Refresh()
148
 	c.actor.Refresh()
150
 	contextID, found := c.db.GetParent(c.actor.ID)
149
 	contextID, found := c.db.GetParent(c.actor.ID)
151
 	if found {
150
 	if found {
1068
 	c.output("You head towards %s.", newRoom.DetailedName())
1067
 	c.output("You head towards %s.", newRoom.DetailedName())
1069
 	c.oemit(c.context.ID, "%s leaves the room.", c.actor.ColorName())
1068
 	c.oemit(c.context.ID, "%s leaves the room.", c.actor.ColorName())
1070
 	c.oemit(newRoom.ID, "%s enters the room.", c.actor.ColorName())
1069
 	c.oemit(newRoom.ID, "%s enters the room.", c.actor.ColorName())
1070
+	c.command("look")
1071
 	return true
1071
 	return true
1072
-
1073
-	return false
1074
-
1075
 }
1072
 }
1076
 
1073
 
1077
 func (c *ExecutionContext) command(format string, a ...interface{}) {
1074
 func (c *ExecutionContext) command(format string, a ...interface{}) {