AI Store에서 AI코딩으로 만들어진 앱을 만나보세요!
지금 바로 방문하기

PySupercell Core로 Supercell 게임 서버 개발 가이드

카테고리

프로그래밍/소프트웨어 개발

서브카테고리

웹 개발

대상자

Python을 사용한 게임 서버 개발자, Supercell 게임 서버 아키텍처 이해자

난이도: 중급 (Python 기초 및 네트워크 프로그래밍 지식 필요)

핵심 요약

  • PySupercell Core(PSC)는 Supercell 게임용 서버 기반 아키텍처를 제공하지만, 완전한 서버가 아니며 개발자가 직접 로직을 구현해야 함
  • 메시지 처리PiranhaMessage 상속 클래스를 통해 구현해야 하며, decode()encode() 메서드가 필수
  • MessageManager를 통해 메시지 유형별 처리 로직을 정의하고 await self.connection.send_message()로 응답 전송

섹션별 세부 요약

1. 환경 설정 및 초기 구성

  • PSC 설치:

```bash

git clone

cd pysupercell-core

pip install -r requirements.txt

```

  • 게임 설정:

- logic_magic_message_factory.py 파일에서 게임 별 코드명(magic, laser 등) 설정

- config.toml 파일에 게임 버전 및 환경(int, prod) 정보 입력

2. 메시지 클래스 구현 (예: `LoginMessage`)

  • 클래스 정의:

```python

class LoginMessage(PiranhaMessage):

def __init__(self):

super().__init__()

self.account_id = LogicLong()

self.pass_token = ""

self.major_version = 0

```

  • 메시지 디코딩:

```python

def decode(self):

super().decode()

self.account_id = self.stream.read_long()

self.pass_token = self.stream.read_string()

self.major_version = self.stream.read_int()

```

  • 메시지 타입 식별:

```python

def get_message_type(self) -> int:

return 10101

```

3. 서버 응답 메시지 처리 (예: `LoginOkMessage`)

  • 응답 메시지 생성:

```python

class LoginOkMessage(PiranhaMessage):

def encode(self):

super().encode()

self.stream.write_long(self.account_id)

self.stream.write_long(self.home_id)

self.stream.write_string(self.pass_token)

```

  • 메시지 유형 식별:

```python

def get_message_type(self) -> int:

return 20104

```

4. 메시지 처리 로직 정의 (MessageManager)

  • 메시지 유형별 분기 처리:

```python

async def receive_message(self, message):

match message.get_message_type():

case 10101:

await self.on_login(message)

```

  • 처리 함수 정의:

```python

async def on_login(self, message: LoginMessage):

response = LoginOkMessage()

response.account_id = ...

response.pass_token = ...

await self.connection.send_message(response)

```

결론

  • PSC는 서버 기반 아키텍처를 제공하지만, 메시지 처리 로직과 응답 생성은 개발자가 직접 구현해야 함
  • PiranhaMessage 상속 및 decode(), encode() 메서드 구현이 필수
  • MessageManager를 통해 메시지 유형별 처리 로직 정의 후 await self.connection.send_message()로 응답 전송
  • Supercell 게임 서버 개발 시 Python 기반 아키텍처 이해가 핵심 요소임