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,11 +3,11 @@ package funmow
3 3
 import (
4 4
 	"bufio"
5 5
 	"fmt"
6
+	"github.com/mgutz/ansi"
6 7
 	"log"
7 8
 	"net"
8 9
 	"strings"
9 10
 	"unicode"
10
-	"github.com/mgutz/ansi"
11 11
 )
12 12
 
13 13
 type Client struct {
@@ -88,14 +88,14 @@ func (c *Client) Run() {
88 88
 
89 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 92
 	c.write("  ___                __  __    ___  __      __\n")
93 93
 	c.write(" | __| _  _   _ _   |  \\/  |  / _ \\ \\ \\    / /\n")
94 94
 	c.write(" | _| | || | | ' \\  | |\\/| | | (_) | \\ \\/\\/ /\n")
95 95
 	c.write(" |_|   \\_,_| |_||_| |_|  |_|  \\___/   \\_/\\_/\n\n")
96 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

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

+ 2
- 5
execution_context.go View File

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