-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhotonWebRpcHandler.cs
105 lines (88 loc) · 3.09 KB
/
PhotonWebRpcHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using ExitGames.Client.Photon;
using ExitGames.Client.Photon.LoadBalancing;
using System.Collections.Generic;
using System.Collections;
namespace TurnbasedConsole
{
class PhotonWebRpcHandler
{
PhotonClientCachedData cache = null;
// delegates, events
const string RPC_GET_GAMELIST = "GetGameList";
public delegate void OnGameListDelegate(List<string> aGameList);
public event OnGameListDelegate OnGameList;
const string RPC_GET_GAMES = "GetGames";
public delegate void OnGamesDelegate(List<string> aGames);
public event OnGamesDelegate OnGames;
const string RPC_GET_PLAYER = "PlayerLoad";
public delegate void OnPlayerLoadDelegate(string aPlayer);
public event OnPlayerLoadDelegate OnPlayerLoad;
const string RPC_POST_PLAYER = "PlayerSave";
public delegate void OnPlayerSaveDelegate();
public event OnPlayerSaveDelegate OnPlayerSave;
public PhotonWebRpcHandler(PhotonClientCachedData cache)
{
this.cache = cache;
}
public void OnWebRpcResponse(WebRpcResponse webResponse)
{
switch (webResponse.Name)
{
case RPC_GET_GAMELIST:
OnGetGameListResponse(webResponse.Parameters);
break;
case RPC_GET_GAMES:
OnGetGamesResponse(webResponse.Parameters);
break;
case RPC_GET_PLAYER:
OnPlayerLoadResponse(webResponse.Parameters);
break;
case RPC_POST_PLAYER:
OnPlayerSaveResponse();
break;
}
}
private void OnGetGameListResponse(Dictionary<string, object> data)
{
cache.Rooms = new List<string>();
cache.Actors = new List<int>();
if (data != null)
{
foreach (var item in data)
{
cache.Rooms.Add(item.Key);
cache.Actors.Add(int.Parse((string)item.Value));
}
}
OnGameList(cache.Rooms);
}
private void OnGetGamesResponse(Dictionary<string, object> game)
{
cache.Games = new List<string>();
if (game != null)
{
foreach (var item in game)
{
cache.Games.Add(item.Key);
}
}
OnGames(cache.Games);
}
private void OnPlayerSaveResponse()
{
OnPlayerSave();
}
private void OnPlayerLoadResponse(Dictionary<string, object> playerState)
{
string playerJson = string.Empty;
if (playerState != null)
{
foreach (var item in playerState)
{
playerJson = (string)item.Value;
}
}
OnPlayerLoad(playerJson);
}
}
}