Skip to content

Commit

Permalink
It now at least loads the client, but interactions with the server ar…
Browse files Browse the repository at this point in the history
…e not registered.
  • Loading branch information
crazy2be committed Mar 24, 2013
1 parent 70530bd commit 692c23f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
18 changes: 11 additions & 7 deletions client/js/chunks/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ function initConn(payload) {
var manager = new ChunkManager();

function processChunk(payload) {
var size = payload.size;
if (size.w != CHUNK_WIDTH ||
size.h != CHUNK_HEIGHT ||
size.d != CHUNK_DEPTH
var size = payload.Size;
if (size.X != CHUNK_WIDTH ||
size.Y != CHUNK_HEIGHT ||
size.Z != CHUNK_DEPTH
) {
throw "Got chunk of size which does not match our expected chunk size!";
}

var cc = payload.ccpos;
var data = payload.data;
var cc = {
x: payload.CCPos.X,
y: payload.CCPos.Y,
z: payload.CCPos.Z,
};
var data = payload.Data;

var blocks = new Uint8Array(data.length);
for (var i = 0; i < blocks.length; i++) {
Expand All @@ -67,7 +71,7 @@ function processChunk(payload) {
}

var chunk = manager.get(cc);
if (chunk) throw "Got chunk data twice! Server bug! Ignoring message...";
if (chunk) throw "Got chunk data twice! Server bug! Ignoring message..." + JSON.stringify(cc);

chunk = new ChunkGeometry(cc, blocks, manager);
manager.set(cc, chunk);
Expand Down
17 changes: 9 additions & 8 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"log"
"time"
"reflect"
)

type Client struct {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (c *Client) RunChunks(conn *Conn) {

chunk := c.world.RequestChunk(cc)

m := MsgChunk{
m := &MsgChunk{
CCPos: cc,
Size: Vec3{
X: CHUNK_WIDTH,
Expand All @@ -78,12 +79,12 @@ func (c *Client) RunChunks(conn *Conn) {

func (c *Client) handleMessage(m Message) {
switch m.(type) {
case MsgBlock:
c.handleBlock(m.(MsgBlock))
case MsgPlayerPosition:
c.handleClientPosition(m.(MsgPlayerPosition))
case *MsgBlock:
c.handleBlock(m.(*MsgBlock))
case *MsgPlayerPosition:
c.handleClientPosition(m.(*MsgPlayerPosition))
default:
log.Print("Unknown message recieved from client:", m)
log.Print("Unknown message recieved from client:", reflect.TypeOf(m))
return
}
}
Expand All @@ -95,12 +96,12 @@ func (c *Client) sendClientPos(wc WorldCoords) {
c.conn.Send(m)
}

func (c *Client) handleBlock(m MsgBlock) {
func (c *Client) handleBlock(m *MsgBlock) {
c.world.ChangeBlock(m.Pos, m.Type)
// c.world.Broadcast <- m
}

func (c *Client) handleClientPosition(m MsgPlayerPosition) {
func (c *Client) handleClientPosition(m *MsgPlayerPosition) {
wc := m.Pos
// wc := readWorldCoords(pl["pos"].(map[string]interface{}))
//
Expand Down
32 changes: 19 additions & 13 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io"
"log"
"reflect"
"encoding/json"
"code.google.com/p/go.net/websocket"
)
Expand Down Expand Up @@ -43,38 +44,43 @@ func (c *Conn) Recv() Message {
return nil
}
m := kindToType(cm.Kind)
json.Unmarshal(cm.Payload, &m)
err = json.Unmarshal(cm.Payload, &m)
if err != nil {
log.Println("Unmarshalling websocket message: ", err)
}
return m
}

func kindToType(kind MessageKind) interface{} {
func kindToType(kind MessageKind) Message {
switch kind {
case MSG_ENTITY_CREATE:
return MsgEntityCreate{}
return &MsgEntityCreate{}
case MSG_ENTITY_POSITION:
return MsgEntityPosition{}
return &MsgEntityPosition{}
case MSG_ENTITY_REMOVE:
return MsgEntityRemove{}
return &MsgEntityRemove{}
case MSG_BLOCK:
return MsgBlock{}
return &MsgBlock{}
case MSG_PLAYER_POSITION:
return MsgPlayerPosition{}
return &MsgPlayerPosition{}
}
panic("Unknown message recieved from client!")
}

func typeToKind(m Message) MessageKind {
switch m.(type) {
case MsgEntityCreate:
case *MsgEntityCreate:
return MSG_ENTITY_CREATE
case MsgEntityPosition:
case *MsgEntityPosition:
return MSG_ENTITY_POSITION
case MsgEntityRemove:
case *MsgEntityRemove:
return MSG_ENTITY_REMOVE
case MsgBlock:
case *MsgChunk:
return MSG_CHUNK
case *MsgBlock:
return MSG_BLOCK
case MsgPlayerPosition:
case *MsgPlayerPosition:
return MSG_PLAYER_POSITION
}
panic("Attempted to send unknown message to client!")
panic("Attempted to send unknown message to client!" + reflect.TypeOf(m).String())
}
17 changes: 0 additions & 17 deletions server/messages.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
// "fmt"
"encoding/json"
)

Expand Down Expand Up @@ -54,19 +53,3 @@ type ClientMessage struct {
}

type Message interface{}

// struct {
// Kind MessageKind
// Payload interface{}
// }

// func NewMessage(kind MessageKind) *Message {
// ms := new(Message)
// ms.Kind = kind
// // ms.Payload = make(map[string]interface{})
// return ms
// }
//
// func (m *Message) String() string {
// return fmt.Sprintf("{kind: %s, payload: %v}", m.Kind, m.Payload)
// }

0 comments on commit 692c23f

Please sign in to comment.