Example #1
0
func TestMetaAction_PlayerJoined(t *testing.T) {
	code := message.MetaActionCodes["player_joined"]
	tick := message.Tick(42)
	entityId := message.EntityId(69)
	username := "******"

	testMessage(t, message.Types["meta_action"], func(w io.Writer) error {
		return builder.SendPlayerJoined(w, tick, entityId, username)
	}, func(conn *message.Conn, t *testing.T) {
		rt, id, c, u := handler.ReadMetaAction(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if id != entityId {
			t.Fatal("Sent entity id", entityId, "but received", id)
		}
		if c != code {
			t.Fatal("Sent code", code, "but received", c)
		}
		if u != username {
			t.Fatal("Sent username", username, "but received", u)
		}
	})
}
Example #2
0
			err := conn.Write(func(w io.Writer) error {
				return builder.SendEntityCreate(w, ctx.Clock.GetRelativeTick(), e)
			})
			if err != nil {
				return err
			}
		}

		// Send current players
		for _, s := range ctx.Auth.List() {
			if s.Id == session.Id {
				continue
			}

			err := conn.Write(func(w io.Writer) error {
				return builder.SendPlayerJoined(w, ctx.Clock.GetRelativeTick(), s.Entity.Id, s.Username)
			})
			if err != nil {
				return err
			}
		}

		// Register new entity
		req := ctx.Entity.Add(session.Entity, ctx.Clock.GetAbsoluteTick()) // TODO: move this elsewhere?
		err = req.Wait()
		log.Println("Entity registered!")
		if err != nil {
			return err
		}

		// Broadcast new entity to other clients