Skip to content

Commit

Permalink
Improve workflow and Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
onizucraft committed Nov 14, 2024
1 parent 59ae85e commit f29ece5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-push-github.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ jobs:
with:
push: true
tags: ${{ env.REGISTRY }}/${{env.IMAGE_NAME}}
build-args: |
BOT_NAME=${{ github.repository_owner }}-${{ github.event.repository.name }}
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM python:3.10-slim
ARG BOT_NAME="intelygenz-codeconz-lighthouses-py-bot"
ARG BOT_NAME
ENV BOT_NAME=${BOT_NAME}

WORKDIR /app
COPY ./ ./
COPY ./randbot.py ./main.py

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 3001
CMD [ "python3", "./main.py", "--bn=${BOT_NAME}", "--la=${BOT_NAME}:3001", "--gs=game:50051"
CMD [ "./entrypoint.sh" ]
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
python3 ./main.py --bn=${BOT_NAME} --la=${BOT_NAME}:3001 --gs=game:50051
59 changes: 30 additions & 29 deletions randbot.py → main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import time
import argparse
import grpc
import random
import time
from concurrent import futures
from grpc import RpcError

import grpc
from google.protobuf import json_format
from grpc import RpcError

from internal.handler.coms import game_pb2
from internal.handler.coms import game_pb2_grpc as game_grpc

timeout_to_response = 1 # 1 second


class BotGameTurn:
def __init__(self, turn, action):
self.turn = turn
Expand Down Expand Up @@ -43,50 +45,50 @@ def new_turn_action(self, turn: game_pb2.NewTurn) -> game_pb2.NewAction:
# No conectar si ya existe la conexión
# No conectar si no controlamos el destino
# Nota: no comprobamos si la conexión se cruza.
if (dest != (cx, cy) and
lighthouses[dest].HaveKey and
[cx, cy] not in lighthouses[dest].Connections and
lighthouses[dest].Owner == self.player_num):
if (
dest != (cx, cy)
and lighthouses[dest].HaveKey
and [cx, cy] not in lighthouses[dest].Connections
and lighthouses[dest].Owner == self.player_num
):
possible_connections.append(dest)

if possible_connections:
possible_connection = random.choice(possible_connections)
action = game_pb2.NewAction(
Action=game_pb2.CONNECT,
Destination=game_pb2.Position(X=possible_connection[0], Y=possible_connection[1])
Destination=game_pb2.Position(
X=possible_connection[0], Y=possible_connection[1]
),
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)

self.countT += 1
return action

# Probabilidad 60%: recargar el faro
if random.randrange(100) < 60:
energy = random.randrange(turn.Energy + 1)
action = game_pb2.NewAction(
Action=game_pb2.ATTACK,
Energy=energy,
Destination=game_pb2.Position(
X=turn.Position.X,
Y=turn.Position.Y
)
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
# Probabilidad 60%: recargar el faro
if random.randrange(100) < 60:
energy = random.randrange(turn.Energy + 1)
action = game_pb2.NewAction(
Action=game_pb2.ATTACK,
Energy=energy,
Destination=game_pb2.Position(X=turn.Position.X, Y=turn.Position.Y),
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)

self.countT += 1
return action
self.countT += 1
return action

# Mover aleatoriamente
moves = ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1))
moves = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
move = random.choice(moves)
action = game_pb2.NewAction(
Action=game_pb2.MOVE,
Destination=game_pb2.Position(
X=turn.Position.X + move[0],
Y=turn.Position.Y + move[1]
)
X=turn.Position.X + move[0], Y=turn.Position.Y + move[1]
),
)

bgt = BotGameTurn(turn, action)
Expand Down Expand Up @@ -122,14 +124,13 @@ def wait_to_join_game(self):
print(f"Could not join game: {e.details()}")
time.sleep(1)


def start_listening(self):
print("Starting to listen on", self.my_address)

# configure gRPC server
grpc_server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
interceptors=(ServerInterceptor(),)
interceptors=(ServerInterceptor(),),
)

# registry of the service
Expand Down

0 comments on commit f29ece5

Please sign in to comment.