Add infos into target window
[ryzomcore.git] / ryzom / server / www / login / libs / join_shard.php
blobeacc5e97f930dd251a6328c7da0c1a493d71683e
1 <?php
3 include_once('validate_cookie.php');
4 include_once('domain_info.php');
5 include_once('ring_session_manager_itf.php');
7 class JoinShardCb extends CRingSessionManagerWeb
9 /**
10 * Join the specified shard
12 function joinSessionResult($userId, $sessionId, $result, $shardAddr, $participantStatus)
14 global $JoinSessionResultCode, $JoinSessionResultMsg;
15 $JoinSessionResultCode = $result;
16 $JoinSessionResultMsg = $shardAddr;
17 if ($result != 0)
19 global $FSHostLuaMode, $FSHostResult, $FSHostResultStr;
20 $FSHostResult = 0;
21 $FSHostResultStr = "Error ".$result." : '".$shardAddr."' while trying to join a shard";
22 if ($FSHostLuaMode)
24 echo $FSHostResultStr;
25 echo '<p><p><a href="web_start.php">Back to menu</a>';
28 else
30 // ok, we have the info to connect !
31 // generate the lua script
32 $cookie=convertCookieForActionHandler($_COOKIE["ryzomId"]);
33 global $FSHostLuaMode, $FSHostResult, $FSHostResultStr;
34 $FSHostResult = 1;
35 $FSHostResultStr = $shardAddr;
36 if ($FSHostLuaMode)
38 $luaScript='runAH(nil, "on_connect_to_shard", "cookie='.$cookie.'|fsAddr='.$shardAddr.'")';
39 //echo 'luaScript : '.$luaScript.'<br>';
40 echo '<lua>'.$luaScript.'</lua>';
41 global $verbose;
42 if ($verbose)
43 echo '<br>Teleporting to shard<br>';
48 /**
49 * Receive the result of the shard list, then call the global $getShardListCallback with the associative array as argument
51 function getShardsResult($userId, $resultStr)
53 global $getShardListCallback;
55 $onlineShardsBySessionId = array();
56 $resultArray = split(';', $resultStr);
57 foreach ($resultArray as $shardInfo)
59 $shardAttr = split(',', $shardInfo);
60 if (isset($shardAttr[1]))
61 $onlineShardsBySessionId[$shardAttr[0]] = $shardAttr[1];
63 $getShardListCallback($onlineShardsBySessionId);
67 // External use
68 $FSHostLuaMode = false;
69 $FSHostResultStr = 0;
71 // Internal use
72 $verbose = true;
73 $FSHostResult = 0;
75 // THE ABOVE DECLARATIONS ARE NEEDED FOR ALL CODE BELOW THIS LINE
76 if (isset($_POST["ml"]))
77 joinMainlandPost();
78 if (isset($_POST["destSessionId"]))
79 joinShardFromIdPost($_POST["destSessionId"]);
81 /**
82 * Authenticate and request to join a mainland shard
84 function joinMainlandPost()
86 global $FSHostLuaMode;
87 $FSHostLuaMode = true;
88 $domainId = -1;
89 if (!validateCookie($userId, $domainId, $charId))
91 echo "Invalid cookie !";
92 die();
94 else
96 joinMainland($userId, $domainId);
101 * Authenticate and request to join the specified shard
103 function joinShardFromIdPost( $destSessionId )
105 $domainId = -1;
106 if (!validateCookie($userId, $domainId, $charId))
108 echo "Invalid cookie !";
109 die();
111 else
113 joinShardFromId($userId, $domainId, $destSessionId);
118 * Request to join the specified shard
120 function joinShardFromId( $userId, $domainId, $destSessionId )
122 $domainInfo = getDomainInfo($domainId);
123 $addr = split(":", $domainInfo["session_manager_address"]);
124 $RSMHost = $addr[0];
125 $RSMPort = $addr[1];
127 // request join to the session manager
128 $joinShard = new JoinShardCb;
129 $res = "";
130 $joinShard->connect($RSMHost, $RSMPort, $res);
131 $charSlot = getCharSlot(); // if ingame (!=15), the RSM can check if this character has the right to connect to the specified shard
132 $charId = ($userId<<4) + $charSlot;
133 echo "Requesting teleportation of $charId/$userId to shard session ".$destSessionId."...<br>";
134 $joinShard->joinSession($charId, $destSessionId, $domainInfo["domain_name"]);
136 // wait the the return message
137 if ($joinShard->waitCallback() == false)
139 // Note: the answer is a joinSessionResult message
140 echo "No response from server, joinShard failed<br>";
142 exit;
146 * Request to get the shard list
148 function getShardList($userId, $domainId)
150 $domainInfo = getDomainInfo($domainId);
151 $addr = split(":", $domainInfo["session_manager_address"]);
152 $RSMHost = $addr[0];
153 $RSMPort = $addr[1];
155 // request get to the session manager
156 $joinShard = new JoinShardCb;
157 $res = "";
158 $joinShard->connect($RSMHost, $RSMPort, $res);
160 $charId = ($userId<<4)+15;
161 echo "Retrieving online shards for $charId...<br>";
162 $joinShard->getShards($charId);
164 // wait the the return message
165 if ($joinShard->waitCallback() == false)
167 echo "No response from server, getShards failed<br>";
169 exit;
173 * Display the list of mainland shards
174 * This is a candidate to the callback $getShardListCallback
176 function displayAllShards(&$onlineShardsBySessionId)
178 // Get the userId and domainId back
179 $domainId = -1;
180 if (!validateCookie($userId, $domainId, $charId))
182 echo "Invalid cookie !";
183 die();
186 // List all shards of the domain, including offline ones
187 global $DBName;
188 mysql_select_db ($DBName) or die ("Can't access to the db dbname:$DBName");
189 $query = "select * from shard where domain_id = $domainId";
190 $resShards = mysql_query ($query) or die ("Can't execute the query: ".$query." ".mysql_error());
191 echo "Select a shard to join:<br>";
192 //echo "<form name='far_tp' action='join_shard.php' method='post'>";
193 while ($rowShard = mysql_fetch_array($resShards))
195 $mainlandSessionId = $rowShard['FixedSessionId'];
196 $isOnline = isset($onlineShardsBySessionId[$mainlandSessionId]);
197 // Radio button not supported by Client's html component. Instead: one form (button) per shard.
198 //echo "<input type='radio' name='destSessionId' value='".$mainlandSessionId."' ".($isOnline?"":"disabled ")."/>".$rowShard['Name']." (".($isOnline?"online with $nbOnlinePlayers players":"offline").", version ".$rowShard['Version'].")<br>";
199 echo "<form name='far_tp_".$rowShard['ShardId']."' action='join_shard.php' method='post'>";
200 echo "<input type='hidden' name='destSessionId' value='".$mainlandSessionId."' />";
201 echo "<input type='hidden' name='charSlot' value='".getCharSlot()."'>";
202 echo " ".$rowShard['Name']." ".$rowShard['ShardId']." (".($isOnline ? $onlineShardsBySessionId[$mainlandSessionId]." online)" : "offline)");
203 if ($isOnline)
204 echo "<input type='submit' name='button' value='Teleport' />";
205 echo "</form><br>";
208 //echo "<input type='submit' name='button' value='Teleport' />";
209 //echo "</form></font>";
210 exit;
215 * Auto-join a shard.
216 * Returns true in case of success, and the FSHost address in $FSHostResultStr.
217 * Returns false in case of error, and an explanatory string in $FSHostResultStr.
219 function joinMainland($userId, $domainId)
221 $domainInfo = getDomainInfo($domainId);
222 // $addr = split(":", $domainInfo["session_manager_address"]);
223 $addr = explode(":", $domainInfo["session_manager_address"]);
224 $RSMHost = $addr[0];
225 $RSMPort = $addr[1];
227 // request get to the session manager
228 $joinsShard = new JoinShardCb;
229 $res = "";
230 $joinsShard->connect($RSMHost, $RSMPort, $res);
232 // set the shard Id to allow any character (by using the special value '15')
233 $charId = ($userId<<4)+15;
234 global $FSHostLuaMode, $verbose;
235 if ($FSHostLuaMode && $verbose)
236 echo "Joining a mainland shard for $charId...<br>";
237 $joinsShard->joinMainland($charId, $domainInfo["domain_name"]);
239 // wait the the return message
240 if ($joinsShard->waitCallback() == false)
242 global $FSHostResultStr;
243 $FSHostResultStr = "No response from server for joinMainland<br>";
244 return false;
246 global $FSHostResult;
247 return $FSHostResult;