TIL-sparta

스파르타) Ch.5 팀 프로젝트 - 타워 디펜스 온라인 (D-6)

Megadr0ne 2024. 7. 12. 20:57


 > 과제 진행 간 완료한 사항 및 문제점과 해결 과정을 정리해보았다.

 

학습 키워드: Node.js, protobuf

 

1. 완료한 과제 진행 사항

1) proto 파일 생성 및 매핑:

 

11 update 프로토콜 버퍼 by donkim1212 · Pull Request #13 · eliotjang/tower-defense-multiplayer-game-project

관련 Issue close #11 작업 내용 #11 에서 명시한 작업 완료

github.com

 - 작업 내용은 위 PR의 작업 내용 참고

 - 기반 작업이어서 별다른 문제 없이 작업을 완료했다.

 

 

2. 기타 사항

1) protobuf oneof 사용:

// example.proto
syntax = "proto3";

message Foo {
    uint32 id = 1;
    string message = 2;
}

message Bar {
    uint64 timestamp = 1;
    stirng temp = 2;
}

message Temp {
    uint32 packetType = 1;
    oneof payload {
        Foo foo = 2;
        Bar bar = 3;
    }
}
// server.js
import { loadProtoFiles, getProtoMessages } from "./loadProtos.js";

class FooData {
  constructor(id, message) {
    this.id = id;
    this.message = message;
  }
}

const init = async () => {
  await loadProtoFiles().then(() => {
    const protoMessageNames = Object.entries(getProtoMessages()).map((message) => message[1].name);
    console.log(protoMessageNames);
  });
  const Temp = getProtoMessages()["Temp"];

  const temp = Temp.encode({
    packetType: 1,
    foo: new FooData(32, "Hello, World!"),
  }).finish();

  const decoded = Temp.decode(temp);

  console.log("temp = ", temp);
  console.log(decoded);
};

init();

Figure 1. server.js 실행 시 콘솔 결과

 

 protobuf의 oneof 키워드를 사용하여 여러 메세지 타입 중 하나를 payload로 사용하도록 설정하는 방법을 익혔다. 이전 프로젝트에서 사용한 방법보다 편리한 부분이 있는데, 위에서는 세 개의 메세지 타입을 모두 매핑했지만 실제론 큰 범주에 있는 메세지 타입(Temp)만 매핑해두고 encode와 decode를 실행할 수 있어서 코드가 간결해진다는 점이다.

 

 

--


REFERENCES:

 

 

[게임 서버] 타워 디펜스 게임 온라인 프로젝트 | Notion

영상 자료

teamsparta.notion.site

 > 과제 spec

 

GitHub - eliotjang/tower-defense-multiplayer-game-project: Socket.io 사용 타워 디펜스 멀티플레이어 게임 서버

Socket.io 사용 타워 디펜스 멀티플레이어 게임 서버 제작. Contribute to eliotjang/tower-defense-multiplayer-game-project development by creating an account on GitHub.

github.com

 > 과제 repo

 

728x90