Skip to content

Instantly share code, notes, and snippets.

@Databean
Last active October 21, 2019 01:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Databean/45c8564bd52c5ba0fb6e4f9948b3961f to your computer and use it in GitHub Desktop.
Save Databean/45c8564bd52c5ba0fb6e4f9948b3961f to your computer and use it in GitHub Desktop.
"""
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import asyncio
import websockets
from s2clientprotocol import sc2api_pb2 as sc_pb
def makeGameRequest():
req = sc_pb.Request()
req.create_game.battlenet_map_name = "Ohana LE"
req.create_game.disable_fog = True
me = req.create_game.player_setup.add()
me.type = sc_pb.Participant
me.race = sc_pb.Protoss
opponent = req.create_game.player_setup.add()
opponent.type = sc_pb.Computer
opponent.race = sc_pb.Terran
opponent.difficulty = sc_pb.CheatInsane
print(req)
return req
def makeJoinGameRequest():
req = sc_pb.Request()
req.join_game.race = sc_pb.Protoss
req.join_game.options.raw = True
print(req)
return req
def makeStepRequest():
req = sc_pb.Request()
req.step.count = 8
print(req)
return req
def makeObservationRequest():
req = sc_pb.Request()
req.observation.SetInParent()
print(req)
return req
def makeLeaveRequest():
req = sc_pb.Request()
req.leave_game.SetInParent()
print(req)
return req
def makeDataRequest():
req = sc_pb.Request()
req.data.SetInParent()
print(req)
return req
async def hello():
async with websockets.connect('ws://127.0.0.1:5000/sc2api') as websocket:
await websocket.send(makeGameRequest().SerializeToString())
response = sc_pb.Response()
response_bytes = await websocket.recv()
response.ParseFromString(response_bytes)
print("< {}".format(response))
await websocket.send(makeJoinGameRequest().SerializeToString())
response = sc_pb.Response()
response_bytes = await websocket.recv()
response.ParseFromString(response_bytes)
print("< {}".format(response))
still_going = True
while still_going:
await websocket.send(makeObservationRequest().SerializeToString())
response = sc_pb.Response()
response_bytes = await websocket.recv()
response.ParseFromString(response_bytes)
print("< {}".format(response))
if len(response.observation.player_result) > 0:
still_going = False
await websocket.send(makeStepRequest().SerializeToString())
response = sc_pb.Response()
response_bytes = await websocket.recv()
response.ParseFromString(response_bytes)
print("< {}".format(response))
await websocket.send(makeLeaveRequest().SerializeToString())
response = sc_pb.Response()
response_bytes = await websocket.recv()
response.ParseFromString(response_bytes)
print("< {}".format(response))
asyncio.get_event_loop().run_until_complete(hello())
@derpypony
Copy link

derpypony commented Oct 21, 2019

Hi, it seems that part of your code are no longer working. In line 28, 32, 41, me.race = sc_pb.Protoss opponent.race = sc_pb.Terran, req.join_game.race = sc_pb.Protoss. Information about race can no longer be found in s2clientprotocol.sc2api_pb2 any more, instead, it resides in s2clientprotocol.common_pb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment