Skip to content

Commit

Permalink
init, partykit init
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua committed Dec 25, 2023
0 parents commit b52665a
Show file tree
Hide file tree
Showing 15 changed files with 1,823 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```bash
# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "typer99-app",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"svelte": "^5.0.0-next.1",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^5.0.3",
"partykit": "0.0.53"
},
"type": "module",
"dependencies": {
"partysocket": "0.0.18"
}
}
65 changes: 65 additions & 0 deletions party/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type * as Party from "partykit/server";

type Player = {
conn_id: string,
name: string,
score: number
};

export default class Server implements Party.Server {

constructor(readonly party: Party.Party) {}

// each party's local data;
party_state : "lobby" | "running" | "ending" = "lobby";
target_string = "";
players : Player[] = [];

onConnect(conn: Party.Connection, ctx: Party.ConnectionContext) {
// A websocket just connected!
console.log(
`Connected:
id: ${conn.id}
room: ${this.party.id}
url: ${new URL(ctx.request.url).pathname}`
);
}

onMessage(message: string, sender: Party.Connection) {
// check the party state (in lobby, in game, or game ended)
if (this.party_state === "lobby") {
switch (message) {
case "ready": {
this.party_state = "running"
break;
}
}
}

else if (this.party_state === "running") {
switch (message) {

// sent by a player who won the game first
case "winner": {
this.party_state = "ending";
this.players.map((p) => {
// give player a score
if (p.conn_id === sender.id) { p.score += 1; }
});
break;
}
}
}

else if (this.party_state === "ending") {

}
}

// when a connection leaves, remove a Player associated
onClose(connection: Party.Connection<unknown>): void | Promise<void> {
this.players = this.players.filter((p) => p.conn_id !== connection.id);
}
}

Server satisfies Party.Worker;
5 changes: 5 additions & 0 deletions partykit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "typer99-app-party",
"main": "party/index.ts",
"compatibilityDate": "2023-12-24"
}
Loading

0 comments on commit b52665a

Please sign in to comment.