Skip to content

Commit

Permalink
Rename Player to Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy2be committed Mar 24, 2013
1 parent 49f77ec commit 4a097d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 190 deletions.
165 changes: 3 additions & 162 deletions server/player.go
Original file line number Diff line number Diff line change
@@ -1,169 +1,10 @@
package main

import (
"log"
"time"
"math/rand"
"code.google.com/p/go.net/websocket"

)
// http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go
func randString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
bytes := make([]byte, n)
for i := 0; i < n; i++ {
bytes[i] = alphanum[rand.Int() % len(alphanum)]
}
return string(bytes)
}

type Player struct {
w *World
c *Conn

name string

Broadcast chan *Message
cm *ChunkManager
}

func NewPlayer(world *World, name string) *Player {
p := new(Player)
p.w = world
p.name = name

p.Broadcast = make(chan *Message, 10)
p.cm = newChunkManager()

return p
}

func (p *Player) Run(ws *websocket.Conn) {
p.c = NewConn(ws)
p.w.Join <- p

for {
select {
case m := <-p.Broadcast:
// A bit of a gross hack, but we don't want the player
// to recieve broadcast messages for the position of
// their own entity.
if m.Kind == MSG_ENTITY_POSITION && p.name == m.Payload["id"] {
continue
}
p.c.Send(m)
default:
m := p.c.Recv()
if m != nil {
p.handleMessage(m)
} else {
p.w.Leave <- p
return
}
}
}
}

func (p *Player) RunChunks(ws *websocket.Conn) {
c := NewConn(ws)

for {
cc, valid := p.cm.top()
if !valid {
<-time.After(time.Second / 10)
continue
}

m := NewMessage(MSG_CHUNK)
m.Payload["ccpos"] = cc.toMap()
m.Payload["size"] = map[string]interface{}{
"w": CHUNK_WIDTH,
"h": CHUNK_HEIGHT,
"d": CHUNK_DEPTH,
}

chunk := p.w.RequestChunk(cc)
m.Payload["data"] = chunk.Flatten()

c.Send(m)
}
}

func (p *Player) handleMessage(m *Message) {
switch m.Kind {
case "block":
p.handleBlock(m)
case "player-position":
p.handlePlayerPosition(m)
default:
log.Print("Unknown message recieved from client of kind ", m.Kind)
return
}
}

func (p *Player) sendPlayerPos(wc WorldCoords) {
m := NewMessage(MSG_PLAYER_POSITION)
m.Payload["pos"] = wc.toMap()
p.c.Send(m)
}

func (p *Player) handleBlock(m *Message) {
pl := m.Payload
wc := readWorldCoords(pl)
typ := Block(pl["type"].(float64))

p.w.ChangeBlock(wc, typ)
p.w.Broadcast <- m
}

func (p *Player) handlePlayerPosition(m *Message) {
pl := m.Payload
// TODO: Verify position is valid
// (they didn't move too much in the last
// couple frames, and they are not currently
// in the ground).
wc := readWorldCoords(pl["pos"].(map[string]interface{}))

pl["id"] = p.name
m.Kind = MSG_ENTITY_POSITION
p.w.Broadcast <- m

occ := func (cc ChunkCoords, x, y, z int) ChunkCoords {
return ChunkCoords{
x: cc.x + x,
y: cc.y + y,
z: cc.z + z,
}
}

eachWithin := func (cc ChunkCoords, xdist, ydist, zdist int, cb func (newCC ChunkCoords, dist int)) {
abs := func (n int) int {
if n < 0 {
return -n
}
return n
}
dist := func (x, y, z int) int {
return abs(x) + abs(y) + abs(z)
}
cb(cc, 0)
for x := -xdist; x <= xdist; x++ {
for y := -ydist; y <= ydist; y++ {
for z := -zdist; z <= zdist; z++ {
cb(occ(cc, x, y, z), dist(x, y, z))
}
}
}
}

cc := wc.Chunk()
eachWithin(cc, 2, 0, 2, func (newCC ChunkCoords, dist int) {
p.cm.display(newCC, -dist)
});

oc := wc.Offset();
if oc.y <= 4 {
p.cm.display(occ(cc, 0, -1, 0), 1);
} else if oc.y >= 28 {
p.cm.display(occ(cc, 0, 1, 0), 1);
}
Position WorldCoords
Name string
}
14 changes: 7 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "." + r.URL.Path)
}

func getPlayerName(config *websocket.Config) string {
func getClientName(config *websocket.Config) string {
path := config.Location.Path
bits := strings.Split(path, "/")
if len(bits) < 4 {
Expand All @@ -27,15 +27,15 @@ func getPlayerName(config *websocket.Config) string {
}

func mainSocketHandler(ws *websocket.Conn) {
name := getPlayerName(ws.Config())
p := NewPlayer(globalWorld, name)
p.Run(ws)
name := getClientName(ws.Config())
p := NewClient(globalWorld, name)
p.Run(NewConn(ws))
}

func chunkSocketHandler(ws *websocket.Conn) {
name := getPlayerName(ws.Config())
p := globalWorld.FindPlayer(name)
p.RunChunks(ws)
name := getClientName(ws.Config())
p := globalWorld.FindClient(name)
p.RunChunks(NewConn(ws))
}

func doProfile() {
Expand Down
42 changes: 21 additions & 21 deletions server/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ type World struct {
chunks map[ChunkCoords]Chunk
generator ChunkGenerator
chunkLock sync.Mutex
players []*Player
find chan FindPlayerRequest
players []*Client
find chan FindClientRequest

Join chan *Player
Leave chan *Player
Join chan *Client
Leave chan *Client
Broadcast chan *Message
}

type FindPlayerRequest struct {
type FindClientRequest struct {
name string
resp chan *Player
resp chan *Client
}

func NewWorld(seed float64) *World {
w := new(World)
w.seed = seed;
w.chunks = make(map[ChunkCoords]Chunk)
w.generator = NewMazeArenaGenerator(seed)
w.players = make([]*Player, 0)
w.find = make(chan FindPlayerRequest)
w.players = make([]*Client, 0)
w.find = make(chan FindClientRequest)

w.Join = make(chan *Player)
w.Leave = make(chan *Player)
w.Join = make(chan *Client)
w.Leave = make(chan *Client)
w.Broadcast = make(chan *Message, 10)

return w
Expand All @@ -56,10 +56,10 @@ func (w *World) ChangeBlock(wc WorldCoords, newBlock Block) {
chunk.SetBlock(wc.Offset(), newBlock)
}

func (w *World) FindPlayer(name string) *Player {
req := FindPlayerRequest {
func (w *World) FindClient(name string) *Client {
req := FindClientRequest {
name: name,
resp: make(chan *Player),
resp: make(chan *Client),
}
w.find <- req

Expand All @@ -77,34 +77,34 @@ func (w *World) broadcast(m *Message) {
}
}

func (w *World) join(p *Player) {
func (w *World) join(p *Client) {
m := NewMessage(MSG_ENTITY_CREATE)
m.Payload["id"] = p.name
w.broadcast(m)

for _, otherPlayer := range w.players {
for _, otherClient := range w.players {
m := NewMessage(MSG_ENTITY_CREATE)
m.Payload["id"] = otherPlayer.name
m.Payload["id"] = otherClient.name
p.Broadcast <- m
}

w.players = append(w.players, p)
log.Println("New player connected! Name: ", p.name)
}

func (w *World) leave(p *Player) {
i := w.findPlayer(p.name)
func (w *World) leave(p *Client) {
i := w.findClient(p.name)
w.players[i] = w.players[len(w.players)-1]
w.players = w.players[0:len(w.players)-1]

log.Println("Player disconnected...", p.name)
log.Println("Client disconnected...", p.name)

m := NewMessage(MSG_ENTITY_REMOVE)
m.Payload["id"] = p.name
w.broadcast(m)
}

func (w *World) findPlayer(name string) int {
func (w *World) findClient(name string) int {
for i, p := range w.players {
if p.name == name {
return i
Expand All @@ -123,7 +123,7 @@ func (w *World) Run() {
case m := <-w.Broadcast:
w.broadcast(m)
case req := <-w.find:
i := w.findPlayer(req.name)
i := w.findClient(req.name)
req.resp <- w.players[i]
}
}
Expand Down

0 comments on commit 4a097d7

Please sign in to comment.