// 自身のID及びMap
const myId = gameSpace.id;
const myMap = gameSpace.mapId;
const nearPlayerId = getNearPlayerId();
// じゃんけん開始
await play();
// じゃんけん処理
async function play(){
// じゃんけん開始
const hands = ["✊","✌","🖐"];
const judge = ["あいこ","負け","勝ち"];
const pr = Math.floor(Math.random()*3);
const cr = Math.floor(Math.random()*3);
game.setEmote(hands[pr],myId);
game.setEmote(hands[cr],nearPlayerId);
// 指定秒間スリープ
const seconds = 2000;
await new Promise(r => setTimeout(r,seconds));
// 判定
const result = (3 + pr - cr) % 3;
if(result === 0){ // あいこ
// 再実行
play();
return;
}else if(result === 1){ // 負け
game.setEmote("",myId);
game.setEmote("🎉",nearPlayerId);
}else { // 勝ち
game.setEmote("🎉",myId);
game.setEmote("",nearPlayerId);
}
// 指定秒後に初期化して終了
await new Promise(r => setTimeout(r,seconds));
game.setEmote("",myId);
game.setEmote("",nearPlayerId);
}
// 自身と一番近い人のプレイヤーID
function getNearPlayerId() {
// 自身の現在位置の座標
const myCoordinate = (({ x, y }) => ({ x, y }))(gameSpace.gameState[myId]);
// 自身以外の参加者の情報(ID, 名前, x座標, y座標)
const otherInfo = Object.values(gameSpace.gameState).filter(obj => obj.map === myMap && obj.id !== myId).map(({ id, name, x, y }) => ({ id, name, x, y }));
// 近い座標の取得
function getClosestCoordinate(targetCoordinate, coordinates) {
return coordinates.reduce((closest, current) => {
const closestDistance = getDistance(targetCoordinate, closest);
const currentDistance = getDistance(targetCoordinate, current);
return currentDistance < closestDistance ? current : closest;
});
}
// 2点間の座標距離の計算
function getDistance(firstCoordinate, secondCoordinate) {
return Math.hypot(firstCoordinate.x - secondCoordinate.x, firstCoordinate.y - secondCoordinate.y);
}
// 近い人の情報
const closestOther = otherInfo.find(({ x, y }) => {
const otherCoordinates = otherInfo.map(({ x, y }) => ({ x, y }));
const closestCoordinate = getClosestCoordinate(myCoordinate, otherCoordinates)
return x === closestCoordinate.x && y === closestCoordinate.y
});
return closestOther.id
}