add socket message for toggling territory
This commit is contained in:
parent
b1d43b5b02
commit
0db13d2913
4 changed files with 41 additions and 0 deletions
|
@ -40,6 +40,14 @@ const Point = (props) => {
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
const clickHandle = (e) => {
|
const clickHandle = (e) => {
|
||||||
|
if (meta?.turn === 0 && !meta?.winner) {
|
||||||
|
const action = {
|
||||||
|
type: "SOCKET",
|
||||||
|
message: "TOGGLE_TERRITORY",
|
||||||
|
body: { user, point: `${posX}-${posY}`, game, room: game.room },
|
||||||
|
};
|
||||||
|
return dispatch(action);
|
||||||
|
}
|
||||||
const action = {
|
const action = {
|
||||||
type: "SOCKET",
|
type: "SOCKET",
|
||||||
message: "MAKE_MOVE",
|
message: "MAKE_MOVE",
|
||||||
|
|
|
@ -42,6 +42,10 @@ export const socketReducer = (state, action) => {
|
||||||
return pass(state, action);
|
return pass(state, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "TOGGLE_TERRITORY": {
|
||||||
|
return toggleTerritory(state, action);
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -82,3 +86,9 @@ function pass(state, action) {
|
||||||
socket.emit("pass", { ...action.body });
|
socket.emit("pass", { ...action.body });
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleTerritory(state, action) {
|
||||||
|
const socket = state.socket;
|
||||||
|
socket.emit("toggle_territory", { ...action.body });
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
|
@ -100,6 +100,11 @@ const GameService = (moveQueries) => {
|
||||||
return this.getDataForUI(id);
|
return this.getDataForUI(id);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
toggleTerritory({ id, point }) {
|
||||||
|
gamesInProgress[id] = gamesInProgress[id].toggleTerritory(point);
|
||||||
|
return this.getDataForUI(id);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@ io.on("connection", async (socket) => {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// PASS
|
// PASS
|
||||||
socket.on("pass", async ({ game, player }) => {
|
socket.on("pass", async ({ game, player }) => {
|
||||||
const { id, room } = game;
|
const { id, room } = game;
|
||||||
|
@ -92,6 +93,23 @@ io.on("connection", async (socket) => {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TOGGLE TERRITORY
|
||||||
|
socket.on("toggle_territory", async ({ user, point, board, game }) => {
|
||||||
|
const { id, room } = game;
|
||||||
|
const gameNsp = `game${id}`;
|
||||||
|
try {
|
||||||
|
const { board, ...meta } = await gameServices.toggleTerritory({
|
||||||
|
id,
|
||||||
|
point,
|
||||||
|
});
|
||||||
|
socket.join(gameNsp, () => {
|
||||||
|
io.of(room).to(gameNsp).emit("update_board", { board, meta });
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue