Skip to content

Commit

Permalink
🦕
Browse files Browse the repository at this point in the history
  • Loading branch information
riaf committed Oct 11, 2022
0 parents commit bdb954b
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deno 1.26.1
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deno.enable": true,
"deno.lint": true,
"[typescript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Keisuke Sato

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# chaser-client for deno

## Run Example

```
deno run --allow-net example.ts
```
140 changes: 140 additions & 0 deletions client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
export const TURN_START = "@";
export const TURN_END = "#";
export const GAME_FINISHED = "0";

export class Client {
#host;
#port;
#name;
#connection?: Deno.TcpConn;

constructor(host: string, port: number, name = "CHASER CLIENT DENO 0.0") {
this.#host = host;
this.#port = port;
this.#name = name;
}

async connect() {
this.#connection = await Deno.connect({
hostname: this.#host,
port: this.#port,
});
await this.#sendName();
}

async receive() {
const buf = new TextDecoder().decode(await this.#receiveBuffer());

const control = buf[0];
if ([GAME_FINISHED, TURN_START].includes(control)) {
return [control];
}

return buf.trim().split("");
}

async getReady() {
await this.#sendCommand("gr");
return await this.receive();
}

async turnEnd() {
await this.#sendCommand(TURN_END);
}

async walkUp() {
await this.#sendCommand("wu");
return await this.receive();
}

async walkRight() {
await this.#sendCommand("wr");
return await this.receive();
}

async walkDown() {
await this.#sendCommand("wd");
return await this.receive();
}

async walkLeft() {
await this.#sendCommand("wl");
return await this.receive();
}

async lookUp() {
await this.#sendCommand("lu");
return await this.receive();
}

async lookRight() {
await this.#sendCommand("lr");
return await this.receive();
}

async lookDown() {
await this.#sendCommand("ld");
return await this.receive();
}

async lookLeft() {
await this.#sendCommand("ll");
return await this.receive();
}

async searchUp() {
await this.#sendCommand("su");
return await this.receive();
}

async searchRight() {
await this.#sendCommand("sr");
return await this.receive();
}

async searchDown() {
await this.#sendCommand("sd");
return await this.receive();
}

async searchLeft() {
await this.#sendCommand("sl");
return await this.receive();
}

async putUp() {
await this.#sendCommand("pu");
return await this.receive();
}

async putRight() {
await this.#sendCommand("pr");
return await this.receive();
}

async putDown() {
await this.#sendCommand("pd");
return await this.receive();
}

async putLeft() {
await this.#sendCommand("pl");
return await this.receive();
}

async #sendCommand(command: string) {
await this.#connection?.write(new TextEncoder().encode(`${command}\r\n`));
}

async #sendName() {
await this.#sendCommand(this.#name);
}

async #receiveBuffer() {
const buf = new Uint8Array(512);
const n = await this.#connection?.read(buf);
if (n) {
return buf.subarray(0, n);
}
}
}
26 changes: 26 additions & 0 deletions example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Client, GAME_FINISHED } from "./client.ts";

const client = new Client("localhost", 2009, "EXAMPLE BOT");

await client.connect();

while (true) {
const [init] = await client.receive();

if (init === GAME_FINISHED) {
break;
}

const [control, ...info] = await client.getReady();

console.log(control, info);

// TODO: implement your bot here
const [postControl, _postInfo] = await client.lookUp();

if (postControl === GAME_FINISHED) {
break;
}

client.turnEnd();
}

0 comments on commit bdb954b

Please sign in to comment.