https://github.com/zients/unity-online-tic-tac-toe-client
https://github.com/zients/unity-online-tic-tac-toe-client
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zients/unity-online-tic-tac-toe-client
- Owner: zients
- Created: 2019-12-01T11:57:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-24T17:51:40.000Z (almost 6 years ago)
- Last Synced: 2026-03-23T18:30:57.117Z (3 months ago)
- Language: C#
- Size: 21.1 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Online TicTacToe Client
This is Online TicTacToe client made with Unity and Nakama-Unity-Client.
TicTacToe Nakama Server : https://github.com/tsen1220/LuaOnlineTicTacToeServer
We need to import Nakama and LitJson to build this app.
And we need to select 1024 x 768 (standalone).
## Nakama Client Connect
We need to get session and client to let the websocket connected.
```
...
using Nakama;
...
...
```
First, we get the client information and create a new socket with client API : client.NewSocket().
Then, try to get the session with deviceID and Nakama Client API : client.AuthenticateDeviceAsync(deviceID).
After receiving the session, Take the session to build the socket with socket API : socket.ConnectAsync(session) .
```
private readonly IClient client = new Client("defaultkey");
public ISocket socket;
private ISession session;
private async void Awake()
{
if (socket != null)
{
await socket.CloseAsync();
}
socket = client.NewSocket();
socket.Closed += () => Debug.Log("Socket closed.");
socket.Connected += () => Debug.Log("Socket connected.");
socket.ReceivedError += e => Debug.LogErrorFormat("Socket error: {0}", e.Message);
var deviceId = SystemInfo.deviceUniqueIdentifier;
session = await client.AuthenticateDeviceAsync(deviceId);
await socket.ConnectAsync(session);
}
```
## Add And Remove Nakama Matchmaker Process
When our socket has built, we can add to Nakama matchmaker pool and then get a matchmakerticket with socket API :
socket.AddMatchmakerAsync(query, minCount, maxCount, stringProperties, numericProperties) .
```
IMatchmakerTicket matchTicket= await socket.AddMatchmakerAsync(query, minCount, maxCount, stringProperties, numericProperties);
query: The match which you search contains requirements or rules. "*" means regardless of requirements.
minCount: A match can start with minimum number of people.
minCount: A match contains maximum number of people.
stringProperties: Dictionary structure. The match extra information which value contains string.
numericProperties: Dictionary structure. The match extra information which value contains number.
```
If we wish to leave the matchmaker, we can use socket API : socket.RemoveMatchmakerAsync(matchTicket).
This action will remove your matchTicket and leave the matchmaker pool.
```
{
await socket.RemoveMatchmakerAsync(matchTicket);
matchTicket: Get after adding to matchmaker pool.
}
```
## Received Matchmaker Matched And Join Match
Client has the event listener to handle the information When users in Nakama matchmaker pool are matched.
This part is associated with Nakama Server match_create with lua.
With socket API : socket.ReceivedMatchmakerMatched += matchedInformation => callback() .
```
socket.ReceivedMatchmakerMatched += matched =>
{
IMatchmakerMatched MatchInfo = matched;
};
matched : Match information which contains MatchID.
```
And players can join the match after receiving the match information from socket API : socket.ReceivedMatchmakerMatched.
This part is associated with Nakama Server match_join_attempt and match_join with lua.
```
await socket.JoinMatchAsync(matchInfo.MatchId);
```
## Match Running
This part is associated with Nakama Server match_loop with lua.
We can send json_encode_data about match to Nakama Server with socket API : socket.SendMatchStateAsync( MatchId, opCode, json_encode_data );
```
await socket.SendMatchStateAsync(matchInfo.MatchId, 1, drawPostion.ToJson());
```
And Receive the Nakama server data with socket API : socket.ReceivedMatchState += data => callback().
```
socket.ReceivedMatchState += data =>
{
if(data.OpCode == 3)
{
...
...
...
...
...
}
if (data.OpCode == 5)
{
...
...
...
...
...
}
};
data : data from Nakama Server.
```
## Match Leave
When game is over, player can click leave button to leave the match.
Use socket API : socket.LeaveMatchAsync(MatchId);
```
await socket.LeaveMatchAsync(matchInfo.MatchId);
```