about summary refs log tree commit diff homepage
path: root/server.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-23 15:33:56 -0400
committerJune McEnroe <june@causal.agency>2018-08-23 15:33:56 -0400
commit97216e7486e887e640f3c3a1b713278d7e97e15c (patch)
tree8263d5eb85152c1ae9f37398344fa3bc27e06e03 /server.c
parentImplement I, directional insert (diff)
downloadtorus-97216e7486e887e640f3c3a1b713278d7e97e15c.tar.gz
torus-97216e7486e887e640f3c3a1b713278d7e97e15c.zip
Add g for flip
Diffstat (limited to '')
-rw-r--r--server.c96
1 files changed, 55 insertions, 41 deletions
diff --git a/server.c b/server.c
index 9486fcd..73cd9f1 100644
--- a/server.c
+++ b/server.c
@@ -172,6 +172,51 @@ static bool clientCursors(const struct Client *client) {
 	return true;
 }
 
+static bool clientUpdate(struct Client *client, const struct Client *old) {
+	struct ServerMessage msg = {
+		.type = SERVER_MOVE,
+		.move = { .cellX = client->cellX, .cellY = client->cellY },
+	};
+	if (!clientSend(client, msg)) return false;
+
+	if (client->tileX != old->tileX || client->tileY != old->tileY) {
+		msg.type = SERVER_TILE;
+		if (!clientSend(client, msg)) return false;
+
+		if (!clientCursors(client)) return false;
+
+		msg = (struct ServerMessage) {
+			.type = SERVER_CURSOR,
+			.cursor = {
+				.oldCellX = old->cellX,  .oldCellY = old->cellY,
+				.newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE,
+			},
+		};
+		clientCast(old, msg);
+
+		msg = (struct ServerMessage) {
+			.type = SERVER_CURSOR,
+			.cursor = {
+				.oldCellX = CURSOR_NONE,   .oldCellY = CURSOR_NONE,
+				.newCellX = client->cellX, .newCellY = client->cellY,
+			},
+		};
+		clientCast(client, msg);
+
+	} else {
+		msg = (struct ServerMessage) {
+			.type = SERVER_CURSOR,
+			.cursor = {
+				.oldCellX = old->cellX,    .oldCellY = old->cellY,
+				.newCellX = client->cellX, .newCellY = client->cellY,
+			},
+		};
+		clientCast(client, msg);
+	}
+
+	return true;
+}
+
 static bool clientMove(struct Client *client, int8_t dx, int8_t dy) {
 	struct Client old = *client;
 
@@ -210,48 +255,14 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) {
 	assert(client->tileX < TILE_COLS);
 	assert(client->tileY < TILE_ROWS);
 
-	struct ServerMessage msg = {
-		.type = SERVER_MOVE,
-		.move = { .cellX = client->cellX, .cellY = client->cellY },
-	};
-	if (!clientSend(client, msg)) return false;
-
-	if (client->tileX != old.tileX || client->tileY != old.tileY) {
-		msg.type = SERVER_TILE;
-		if (!clientSend(client, msg)) return false;
-
-		if (!clientCursors(client)) return false;
-
-		msg = (struct ServerMessage) {
-			.type = SERVER_CURSOR,
-			.cursor = {
-				.oldCellX = old.cellX,  .oldCellY = old.cellY,
-				.newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE,
-			},
-		};
-		clientCast(&old, msg);
-
-		msg = (struct ServerMessage) {
-			.type = SERVER_CURSOR,
-			.cursor = {
-				.oldCellX = CURSOR_NONE,   .oldCellY = CURSOR_NONE,
-				.newCellX = client->cellX, .newCellY = client->cellY,
-			},
-		};
-		clientCast(client, msg);
-
-	} else {
-		msg = (struct ServerMessage) {
-			.type = SERVER_CURSOR,
-			.cursor = {
-				.oldCellX = old.cellX,    .oldCellY = old.cellY,
-				.newCellX = client->cellX, .newCellY = client->cellY,
-			},
-		};
-		clientCast(client, msg);
-	}
+	return clientUpdate(client, &old);
+}
 
-	return true;
+static bool clientFlip(struct Client *client) {
+	struct Client old = *client;
+	client->tileX = (client->tileX + TILE_COLS / 2) % TILE_COLS;
+	client->tileY = (client->tileY + TILE_ROWS / 2) % TILE_ROWS;
+	return clientUpdate(client, &old);
 }
 
 static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell) {
@@ -371,6 +382,9 @@ int main() {
 			break; case CLIENT_MOVE: {
 				success = clientMove(client, msg.move.dx, msg.move.dy);
 			}
+			break; case CLIENT_FLIP: {
+				success = clientFlip(client);
+			}
 			break; case CLIENT_PUT: {
 				success = clientPut(client, msg.put.color, msg.put.cell);
 			}