summaryrefslogtreecommitdiff
path: root/client/src/client.ts
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2021-07-15 11:52:28 +0200
committerMike Vink <mike1994vink@gmail.com>2021-07-15 11:52:28 +0200
commit0f8edbe0b2c0bd09cd40872f06993c08e67f26cb (patch)
tree93a9943eaf7baeb5bdbeb489f867dc1d0bb13cac /client/src/client.ts
parent9d54842c2f5b084ab1b0de4c4b8978cc43297546 (diff)
refactor(client/api): need to debug!
Diffstat (limited to 'client/src/client.ts')
-rw-r--r--client/src/client.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/client/src/client.ts b/client/src/client.ts
new file mode 100644
index 0000000..ecd15fc
--- /dev/null
+++ b/client/src/client.ts
@@ -0,0 +1,58 @@
+import type {
+ Event,
+} from './events';
+import type AkkamonSession from './session';
+import GameState from './GameState';
+
+
+export default class Client
+{
+ static instance: Client
+ session: AkkamonSession | undefined;
+
+ static getInstance() {
+ if (Client.instance) return Client.instance;
+ else {
+ Client.instance = new Client();
+ return Client.instance;
+ }
+ }
+
+ setSession(akkamonSession: AkkamonSession) {
+ this.session = akkamonSession;
+ }
+
+ in(eventString: string) {
+ let event: Event = JSON.parse(eventString);
+ console.log("-> client is handling incoming event:");
+ console.log(event);
+ switch (event.type) {
+ case 'updatePos':
+ GameState.getInstance().posUpdate(event.gameState!);
+ break;
+ }
+ }
+
+ out(event: Event) {
+ console.log("-> client is now sending out message:");
+ console.log(event)
+ if (this.session) {
+ this.session.send(JSON.stringify(event));
+ }
+ }
+
+ login(user: {name:string, password: string}) {
+ console.log("Sending the login message");
+ if (this.session) {
+ this.session.send(JSON.stringify(
+ {
+ type: 'login',
+ user: {
+ name: user.name,
+ password: user.password
+ }
+ }
+ ));
+ }
+ }
+}