Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
597 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from 'react'; | ||
|
||
export const Configurator = ({ | ||
isHost, | ||
setIsHost, | ||
playerID, | ||
setPlayerID, | ||
matchID, | ||
setMatchID, | ||
onSubmit, | ||
}) => ( | ||
<> | ||
<h2>Configure this client</h2> | ||
<form | ||
style={{ | ||
display: 'grid', | ||
gap: '2rem', | ||
maxWidth: '30rem', | ||
}} | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
onSubmit(); | ||
}} | ||
> | ||
<label> | ||
<code>isHost</code>{' '} | ||
<input | ||
type="checkbox" | ||
checked={isHost} | ||
onChange={(e) => setIsHost(e.currentTarget.checked)} | ||
/> | ||
</label> | ||
<label> | ||
<code>playerID</code>{' '} | ||
<select | ||
value={playerID} | ||
onChange={(e) => setPlayerID(e.currentTarget.value)} | ||
> | ||
<option value={''}>spectator</option> | ||
<option value="0">0</option> | ||
<option value="1">1</option> | ||
</select> | ||
</label> | ||
<label> | ||
<code>matchID</code>{' '} | ||
<input | ||
type="text" | ||
value={matchID} | ||
onInput={(e) => setMatchID(e.currentTarget.value)} | ||
/> | ||
</label> | ||
<button type="submit">Connect</button> | ||
</form> | ||
<Instructions /> | ||
</> | ||
); | ||
|
||
function Instructions() { | ||
return ( | ||
<small style={{ display: 'block', marginTop: '4rem' }}> | ||
<h2>Instructions</h2> | ||
<h3>Window/Computer 1</h3> | ||
<ol> | ||
<li> | ||
Click <code>isHost</code> and select your choice of{' '} | ||
<code>playerID</code>. | ||
</li> | ||
<li> | ||
Copy the <code>matchID</code>. | ||
</li> | ||
<li> | ||
Click <strong>Connect</strong>. | ||
</li> | ||
</ol> | ||
<h3>Window/Computer 2</h3> | ||
<ol> | ||
<li> | ||
Select the other <code>playerID</code>. | ||
</li> | ||
<li> | ||
Paste/type the <code>matchID</code> from the other window. | ||
</li> | ||
<li> | ||
Click <strong>Connect</strong>. | ||
</li> | ||
</ol> | ||
</small> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2021 The boardgame.io Authors. | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
import P2P from './p2p'; | ||
|
||
const routes = [ | ||
{ | ||
path: '/p2p/tic-tac-toe', | ||
text: 'Tic-Tac-Toe', | ||
component: P2P, | ||
}, | ||
]; | ||
|
||
export default { routes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2021 The boardgame.io Authors | ||
* | ||
* Use of this source code is governed by a MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import { Client } from 'boardgame.io/react'; | ||
import { P2P } from 'boardgame.io/multiplayer'; | ||
import { nanoid } from 'nanoid'; | ||
import TicTacToe from '../tic-tac-toe/game'; | ||
import Board from '../tic-tac-toe/board'; | ||
import { Configurator } from './configurator'; | ||
|
||
const View = () => { | ||
const [matchID, setMatchID] = useState(() => nanoid()); | ||
const [playerID, setPlayerID] = useState('0'); | ||
const [isHost, setIsHost] = useState(false); | ||
const [showClient, setShowClient] = useState(false); | ||
const [GameView, setGameView] = useState(null); | ||
const onSubmit = () => setShowClient(true); | ||
|
||
useEffect(() => { | ||
if (showClient) { | ||
setGameView(() => | ||
Client({ | ||
game: TicTacToe, | ||
board: Board, | ||
multiplayer: P2P({ isHost }), | ||
}) | ||
); | ||
} else { | ||
setGameView(null); | ||
} | ||
}, [showClient, isHost]); | ||
|
||
if (!GameView) { | ||
return ( | ||
<Configurator | ||
{...{ | ||
matchID, | ||
setMatchID, | ||
playerID, | ||
setPlayerID, | ||
isHost, | ||
setIsHost, | ||
onSubmit, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
<GameView {...{ playerID, matchID }} /> | ||
<p> | ||
<code> | ||
<Client matchID="{matchID}" playerID="{playerID}" /> | ||
</code> | ||
</p> | ||
{isHost && <p>This is the match host.</p>} | ||
<p> | ||
<button onClick={() => setShowClient(false)}>Back</button> | ||
</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<div> | ||
<h1>Peer-to-Peer</h1> | ||
<View /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.