Skip to content

Commit

Permalink
Merge pull request #20 from lugobots/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
rubens21 authored Feb 27, 2024
2 parents 72d5294 + 1c7859a commit f83c3db
Show file tree
Hide file tree
Showing 46 changed files with 1,294 additions and 1,054 deletions.
17 changes: 11 additions & 6 deletions dist/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Point } from "./pb/physics_pb.js";
import { OrderSet } from "./pb/server_pb.js";
import { Bot } from './stub.js';
import { Order, OrderSet } from "./pb/server_pb.js";
import { EnvVarLoader } from './configurator.js';
import GameSnapshotInspector from "./game-snapshot-inspector.js";
import { Bot } from './stub.js';
export declare const PROTOCOL_VERSION = "1.0.0";
export type RawTurnProcessor = (OrderSet: any, GameSnapshot: any) => Promise<OrderSet>;
export type RawTurnProcessorReturn = Order[] | {
orders: Order[];
debug_message: string;
} | null;
export type RawTurnProcessor = (inspector: GameSnapshotInspector) => Promise<RawTurnProcessorReturn>;
/**
*
* @param {EnvVarLoader} config
Expand All @@ -23,7 +28,7 @@ export declare class Client {
private readonly init_position;
private client;
/**
* @type {function(GameSnapshot)}
* @type {function(GameSnapshotInspector)}
*/
private gettingReadyHandler;
/**
Expand Down Expand Up @@ -52,11 +57,11 @@ export declare class Client {
play(raw_processor: RawTurnProcessor, onJoin?: () => void): Promise<void>;
/**
*
* @param {function(GameSnapshot)} handler
* @param {function(GameSnapshotInspector)} handler
*
* @returns {Client}
*/
setGettingReadyHandler(handler: any): this;
setGettingReadyHandler(handler: (s: GameSnapshotInspector) => void): this;
/**
*
* @param {function(OrderSet, GameSnapshot):OrderSet} processor
Expand Down
44 changes: 28 additions & 16 deletions dist/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
exports.__esModule = true;
exports.Client = exports.NewClientFromConfig = exports.PROTOCOL_VERSION = void 0;
var server_pb_js_1 = require("./pb/server_pb.js");
var server_grpc_pb_1 = require("./pb/server_grpc_pb");
var grpc_js_1 = require("@grpc/grpc-js");
var server_grpc_pb_1 = require("./pb/server_grpc_pb");
var server_pb_js_1 = require("./pb/server_pb.js");
var game_snapshot_inspector_js_1 = require("./game-snapshot-inspector.js");
var index_1 = require("./index");
var stub_js_1 = require("./stub.js");
var index_js_1 = require("./index.js");
exports.PROTOCOL_VERSION = "1.0.0";
/**
*
Expand All @@ -66,7 +67,7 @@ var Client = /** @class */ (function () {
*/
function Client(server_add, grpc_insecure, token, teamSide, number, init_position) {
/**
* @type {function(GameSnapshot)}
* @type {function(GameSnapshotInspector)}
*/
this.gettingReadyHandler = function (gs) {
};
Expand All @@ -91,25 +92,25 @@ var Client = /** @class */ (function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.setGettingReadyHandler(function (s) {
bot.gettingReady(s);
})._start(function (ordersSet, snapshot) {
})._start(function (inspector) {
return new Promise(function (resolve, reject) {
var playerState = (0, index_js_1.defineState)(snapshot, _this.number, _this.teamSide);
var playerState = (0, index_1.defineState)(inspector, _this.number, _this.teamSide);
if (_this.number === 1) {
resolve(bot.asGoalkeeper(ordersSet, snapshot, playerState));
resolve(bot.asGoalkeeper(inspector, playerState));
return;
}
switch (playerState) {
case stub_js_1.PLAYER_STATE.DISPUTING_THE_BALL:
resolve(bot.onDisputing(ordersSet, snapshot));
resolve(bot.onDisputing(inspector));
break;
case stub_js_1.PLAYER_STATE.DEFENDING:
resolve(bot.onDefending(ordersSet, snapshot));
resolve(bot.onDefending(inspector));
break;
case stub_js_1.PLAYER_STATE.SUPPORTING:
resolve(bot.onSupporting(ordersSet, snapshot));
resolve(bot.onSupporting(inspector));
break;
case stub_js_1.PLAYER_STATE.HOLDING_THE_BALL:
resolve(bot.onHolding(ordersSet, snapshot));
resolve(bot.onHolding(inspector));
break;
}
});
Expand All @@ -133,7 +134,7 @@ var Client = /** @class */ (function () {
};
/**
*
* @param {function(GameSnapshot)} handler
* @param {function(GameSnapshotInspector)} handler
*
* @returns {Client}
*/
Expand Down Expand Up @@ -173,11 +174,12 @@ var Client = /** @class */ (function () {
var running = _this.client.joinATeam(req);
onJoin();
running.on('data', function (snapshot) { return __awaiter(_this, void 0, void 0, function () {
var _a, orderSet, e_1, e_2;
var inspector, _a, orderSet, botReturn, e_1, e_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 11, , 12]);
inspector = new game_snapshot_inspector_js_1["default"](this.teamSide, this.number, snapshot);
_a = snapshot.getState();
switch (_a) {
case server_pb_js_1.GameSnapshot.State.LISTENING: return [3 /*break*/, 1];
Expand All @@ -190,9 +192,19 @@ var Client = /** @class */ (function () {
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, processor(orderSet, snapshot)];
return [4 /*yield*/, processor(inspector)];
case 3:
orderSet = _b.sent();
botReturn = _b.sent();
if (!botReturn) {
orderSet.setOrdersList([]);
}
if (Array.isArray(botReturn)) {
orderSet.setOrdersList(botReturn);
}
else {
orderSet.setOrdersList(botReturn.orders);
orderSet.setDebugMessage(botReturn.debug_message);
}
return [3 /*break*/, 5];
case 4:
e_1 = _b.sent();
Expand All @@ -209,7 +221,7 @@ var Client = /** @class */ (function () {
_b.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
this.gettingReadyHandler(snapshot);
this.gettingReadyHandler(inspector);
return [3 /*break*/, 10];
case 10: return [3 /*break*/, 12];
case 11:
Expand Down
36 changes: 36 additions & 0 deletions dist/game-snapshot-inspector.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DIRECTION } from '.';
import * as Lugo from './proto_exported';
export default class GameSnapshotInspector {
mySide: Lugo.Team.Side;
myNumber: number;
me: Lugo.Player;
snapshot: Lugo.GameSnapshot;
constructor(botSide: Lugo.Team.Side, playerNumber: number, gameSnapshot: Lugo.GameSnapshot);
getSnapshot(): Lugo.GameSnapshot | null;
getTurn(): number;
getMe(): Lugo.Player;
getBall(): Lugo.Ball | null;
getPlayer(side: Lugo.Team.Side, number: number): Lugo.Player | null;
getBallHolder(): Lugo.Player | null;
isBallHolder(player: Lugo.Player): boolean;
getTeam(side: Lugo.Team.Side): Lugo.Team | null;
getMyTeam(): Lugo.Team | null;
getOpponentTeam(): Lugo.Team | null;
getMyTeamSide(): Lugo.Team.Side;
getOpponentSide(): Lugo.Team.Side;
getMyTeamPlayers(): Lugo.Player[];
getOpponentPlayers(): Lugo.Player[];
getMyTeamGoalkeeper(): Lugo.Player | null;
getOpponentGoalkeeper(): Lugo.Player | null;
makeOrderMove(target: Lugo.Point, speed: number): Lugo.Order;
makeOrderMoveMaxSpeed(target: Lugo.Point): Lugo.Order;
makeOrderMoveFromPoint(origin: Lugo.Point, target: Lugo.Point, speed: number): Lugo.Order;
makeOrderMoveFromVector(direction: Lugo.Vector, speed: number): Lugo.Order;
makeOrderMoveByDirection(direction: DIRECTION, speed?: number): Lugo.Order;
makeOrderMoveToStop(): Lugo.Order;
makeOrderJump(target: Lugo.Point, speed: number): Lugo.Order;
makeOrderKick(target: Lugo.Point, speed: number): Lugo.Order;
makeOrderKickMaxSpeed(target: Lugo.Point): Lugo.Order;
makeOrderCatch(): Lugo.Order;
private getOrientationByDirection;
}
184 changes: 184 additions & 0 deletions dist/game-snapshot-inspector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"use strict";
exports.__esModule = true;
var _1 = require(".");
var Geo = require("./geo");
var Helpers = require("./helpers");
var ORIENTATION = require("./orentation");
var Lugo = require("./proto_exported");
var specs_1 = require("./specs");
var GameSnapshotInspector = /** @class */ (function () {
function GameSnapshotInspector(botSide, playerNumber, gameSnapshot) {
this.mySide = botSide;
this.myNumber = playerNumber;
this.snapshot = gameSnapshot;
this.me = this.getPlayer(botSide, playerNumber);
if (!this.me) {
throw new Error("Could not find the player ".concat(botSide, "-").concat(playerNumber));
}
}
GameSnapshotInspector.prototype.getSnapshot = function () {
return this.snapshot;
};
GameSnapshotInspector.prototype.getTurn = function () {
return this.snapshot.getTurn();
};
GameSnapshotInspector.prototype.getMe = function () {
return this.me;
};
GameSnapshotInspector.prototype.getBall = function () {
var _a, _b;
return (_b = (_a = this.snapshot) === null || _a === void 0 ? void 0 : _a.getBall()) !== null && _b !== void 0 ? _b : null;
};
GameSnapshotInspector.prototype.getPlayer = function (side, number) {
return Helpers.getPlayer(this.snapshot, side, number);
};
GameSnapshotInspector.prototype.getBallHolder = function () {
return Helpers.getBallHolder(this.snapshot);
};
GameSnapshotInspector.prototype.isBallHolder = function (player) {
return Helpers.isBallHolder(this.snapshot, player);
};
GameSnapshotInspector.prototype.getTeam = function (side) {
return Helpers.getTeam(this.snapshot, side);
};
GameSnapshotInspector.prototype.getMyTeam = function () {
return this.getTeam(this.mySide);
};
GameSnapshotInspector.prototype.getOpponentTeam = function () {
return this.getTeam(this.getOpponentSide());
};
GameSnapshotInspector.prototype.getMyTeamSide = function () {
return this.mySide;
};
GameSnapshotInspector.prototype.getOpponentSide = function () {
return Helpers.getOpponentSide(this.mySide);
};
GameSnapshotInspector.prototype.getMyTeamPlayers = function () {
var myTeam = this.getMyTeam();
return myTeam ? myTeam.getPlayersList() : [];
};
GameSnapshotInspector.prototype.getOpponentPlayers = function () {
var opponentTeam = this.getOpponentTeam();
return opponentTeam ? opponentTeam.getPlayersList() : [];
};
GameSnapshotInspector.prototype.getMyTeamGoalkeeper = function () {
return this.getPlayer(this.getMyTeamSide(), specs_1.SPECS.GOALKEEPER_NUMBER);
};
GameSnapshotInspector.prototype.getOpponentGoalkeeper = function () {
return this.getPlayer(this.getOpponentSide(), specs_1.SPECS.GOALKEEPER_NUMBER);
};
GameSnapshotInspector.prototype.makeOrderMove = function (target, speed) {
var _a, _b;
return this.makeOrderMoveFromPoint((_b = (_a = this.me) === null || _a === void 0 ? void 0 : _a.getPosition()) !== null && _b !== void 0 ? _b : Geo.newZeroedPoint(), target, speed);
};
GameSnapshotInspector.prototype.makeOrderMoveMaxSpeed = function (target) {
var _a, _b;
return this.makeOrderMoveFromPoint((_b = (_a = this.me) === null || _a === void 0 ? void 0 : _a.getPosition()) !== null && _b !== void 0 ? _b : Geo.newZeroedPoint(), target, specs_1.SPECS.PLAYER_MAX_SPEED);
};
GameSnapshotInspector.prototype.makeOrderMoveFromPoint = function (origin, target, speed) {
var vec = Geo.NewVector(origin, target);
var vel = Geo.NewZeroedVelocity(Geo.normalize(vec));
vel.setSpeed(speed);
var moveOrder = new Lugo.Move();
moveOrder.setVelocity(vel);
return new Lugo.Order().setMove(moveOrder);
};
GameSnapshotInspector.prototype.makeOrderMoveFromVector = function (direction, speed) {
var _a, _b, _c, _d;
var targetPoint = Geo.TargetFrom(direction, (_b = (_a = this.me) === null || _a === void 0 ? void 0 : _a.getPosition()) !== null && _b !== void 0 ? _b : Geo.newZeroedPoint());
return this.makeOrderMoveFromPoint((_d = (_c = this.me) === null || _c === void 0 ? void 0 : _c.getPosition()) !== null && _d !== void 0 ? _d : Geo.newZeroedPoint(), targetPoint, speed);
};
GameSnapshotInspector.prototype.makeOrderMoveByDirection = function (direction, speed) {
var directionTarget = this.getOrientationByDirection(direction);
return this.makeOrderMoveFromVector(directionTarget, speed !== null && speed !== void 0 ? speed : specs_1.SPECS.PLAYER_MAX_SPEED);
};
GameSnapshotInspector.prototype.makeOrderMoveToStop = function () {
var _a, _b, _c;
var myDirection = (_c = (_b = (_a = this.getMe()) === null || _a === void 0 ? void 0 : _a.getVelocity()) === null || _b === void 0 ? void 0 : _b.getDirection()) !== null && _c !== void 0 ? _c : this.getOrientationByDirection(_1.DIRECTION.FORWARD);
return this.makeOrderMoveFromVector(myDirection, 0);
};
GameSnapshotInspector.prototype.makeOrderJump = function (target, speed) {
var _a, _b;
var vec = Geo.NewVector((_b = (_a = this.me) === null || _a === void 0 ? void 0 : _a.getPosition()) !== null && _b !== void 0 ? _b : Geo.newZeroedPoint(), target);
var vel = Geo.NewZeroedVelocity(Geo.normalize(vec));
vel.setSpeed(speed);
var jump = new Lugo.Jump();
jump.setVelocity(vel);
return new Lugo.Order().setJump(jump);
};
GameSnapshotInspector.prototype.makeOrderKick = function (target, speed) {
var _a, _b, _c, _d, _e, _f, _g;
var ballExpectedDirection = Geo.NewVector((_c = (_b = (_a = this.snapshot) === null || _a === void 0 ? void 0 : _a.getBall()) === null || _b === void 0 ? void 0 : _b.getPosition()) !== null && _c !== void 0 ? _c : Geo.newZeroedPoint(), target);
var diffVector = Geo.subVector(ballExpectedDirection, (_g = (_f = (_e = (_d = this.snapshot) === null || _d === void 0 ? void 0 : _d.getBall()) === null || _e === void 0 ? void 0 : _e.getVelocity()) === null || _f === void 0 ? void 0 : _f.getDirection()) !== null && _g !== void 0 ? _g : Geo.newZeroedPoint());
var vel = Geo.NewZeroedVelocity(Geo.normalize(diffVector));
vel.setSpeed(speed);
var kick = new Lugo.Kick();
kick.setVelocity(vel);
return new Lugo.Order().setKick(kick);
};
GameSnapshotInspector.prototype.makeOrderKickMaxSpeed = function (target) {
return this.makeOrderKick(target, specs_1.SPECS.BALL_MAX_SPEED);
};
GameSnapshotInspector.prototype.makeOrderCatch = function () {
var catchOrder = new Lugo.Catch();
return new Lugo.Order().setCatch(catchOrder);
};
GameSnapshotInspector.prototype.getOrientationByDirection = function (direction) {
var directionTarget;
switch (direction) {
case _1.DIRECTION.FORWARD:
directionTarget = ORIENTATION.EAST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.WEST;
}
break;
case _1.DIRECTION.BACKWARD:
directionTarget = ORIENTATION.WEST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.EAST;
}
break;
case _1.DIRECTION.LEFT:
directionTarget = ORIENTATION.NORTH;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.SOUTH;
}
break;
case _1.DIRECTION.RIGHT:
directionTarget = ORIENTATION.SOUTH;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.NORTH;
}
break;
case _1.DIRECTION.BACKWARD_LEFT:
directionTarget = ORIENTATION.NORTH_WEST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.SOUTH_EAST;
}
break;
case _1.DIRECTION.BACKWARD_RIGHT:
directionTarget = ORIENTATION.SOUTH_WEST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.NORTH_EAST;
}
break;
case _1.DIRECTION.FORWARD_LEFT:
directionTarget = ORIENTATION.NORTH_EAST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.SOUTH_WEST;
}
break;
case _1.DIRECTION.FORWARD_RIGHT:
directionTarget = ORIENTATION.SOUTH_EAST;
if (this.mySide === Lugo.Team.Side.AWAY) {
directionTarget = ORIENTATION.NORTH_WEST;
}
break;
default:
throw new Error("unknown direction ".concat(direction));
}
return directionTarget;
};
return GameSnapshotInspector;
}());
exports["default"] = GameSnapshotInspector;
5 changes: 4 additions & 1 deletion dist/geo.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Point, Vector } from './pb/physics_pb.js';
import { Point, Vector, Velocity } from './pb/physics_pb.js';
/**
*
* @param {Point} from
Expand Down Expand Up @@ -33,3 +33,6 @@ export declare function getScaledVector(v: Vector, scale: number): Vector;
*/
export declare function subVector(originalV: Vector, subV: Vector): Vector;
export declare function distanceBetweenPoints(a: Point, b: Point): number;
export declare function NewZeroedVelocity(direction: Vector): Velocity;
export declare function TargetFrom(v: Vector, point: Point): Point;
export declare function newZeroedPoint(): Point;
Loading

0 comments on commit f83c3db

Please sign in to comment.