Skip to content

Instantly share code, notes, and snippets.

@juaxix
Last active December 19, 2015 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juaxix/5906458 to your computer and use it in GitHub Desktop.
Save juaxix/5906458 to your computer and use it in GitHub Desktop.
Photon Cloud SDK with ShiVa 3D : C++
//-----------------------------------------------------------------------------
#include "PrecompiledHeader.h"
#include "myPhotonCloud.h"
//-----------------------------------------------------------------------------
#ifdef S3DX_DLL
myPhotonCloudAPI myPhotonCloud ;
#endif
//-----------------------------------------------------------------------------
// Callbacks
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_getPlayersNumberList ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.getPlayersNumberList" ) ;
// Output Parameters
S3DX::AIVariable sPlayersNumberList ;
sPlayersNumberList = myPhotonCloud.MyPhotonCloudConfig.getCurrentPlayersNumbers();
// Return output Parameters
int iReturnCount = 0 ;
_pOut[iReturnCount++] = sPlayersNumberList ;
S3DX_API_PROFILING_STOP( ) ;
return iReturnCount;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_sendUserData ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.sendUserData" ) ;
// Input Parameters
int iInputCount = 0 ;
S3DX::AIVariable x = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
S3DX::AIVariable y = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
S3DX::AIVariable z = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
S3DX::AIVariable rx = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
S3DX::AIVariable ry = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
S3DX::AIVariable rz = ( iInputCount < _iInCount ) ? _pIn[iInputCount++] : S3DX::AIVariable ( ) ;
myPhotonCloud.MyPhotonCloudConfig.sendUserDataToPhotonCloud( x, y, z, rx, ry, rz );
S3DX_API_PROFILING_STOP( ) ;
return 0;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_disconnect ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.disconnect" ) ;
myPhotonCloud.MyPhotonCloudConfig.disconnectPhotonCloud();
S3DX_API_PROFILING_STOP( ) ;
return 0;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_createRoom ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.createRoom" ) ;
myPhotonCloud.MyPhotonCloudConfig.createPhotonCloudRoom();
S3DX_API_PROFILING_STOP( ) ;
return 0;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_joinRandomRoom ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.joinRandomRoom" ) ;
myPhotonCloud.MyPhotonCloudConfig.joinPhotonCloudRandomRoom();
S3DX_API_PROFILING_STOP( ) ;
return 0;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_getStatus ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.getStatus" ) ;
// Output Parameters
S3DX::AIVariable nStatus ;
nStatus = myPhotonCloud.MyPhotonCloudConfig.getPhotonCloudStatus ();
// Return output Parameters
int iReturnCount = 0 ;
_pOut[iReturnCount++] = nStatus ;
S3DX_API_PROFILING_STOP( ) ;
return iReturnCount;
}
//-----------------------------------------------------------------------------
int Callback_myPhotonCloud_connect ( int _iInCount, const S3DX::AIVariable *_pIn, S3DX::AIVariable *_pOut )
{
S3DX_API_PROFILING_START( "myPhotonCloud.connect" ) ;
myPhotonCloud.MyPhotonCloudConfig.connect();
S3DX_API_PROFILING_STOP( ) ;
return 0;
}
//-----------------------------------------------------------------------------
// Constructor / Destructor
//-----------------------------------------------------------------------------
myPhotonCloudPackage::myPhotonCloudPackage ( )
{
#ifdef S3DX_DLL
myPhotonCloud.pfn_myPhotonCloud_getPlayersNumberList = Callback_myPhotonCloud_getPlayersNumberList ;
myPhotonCloud.pfn_myPhotonCloud_sendUserData = Callback_myPhotonCloud_sendUserData ;
myPhotonCloud.pfn_myPhotonCloud_disconnect = Callback_myPhotonCloud_disconnect ;
myPhotonCloud.pfn_myPhotonCloud_createRoom = Callback_myPhotonCloud_createRoom ;
myPhotonCloud.pfn_myPhotonCloud_joinRandomRoom = Callback_myPhotonCloud_joinRandomRoom ;
myPhotonCloud.pfn_myPhotonCloud_getStatus = Callback_myPhotonCloud_getStatus ;
myPhotonCloud.pfn_myPhotonCloud_connect = Callback_myPhotonCloud_connect ;
#endif
}
//-----------------------------------------------------------------------------
myPhotonCloudPackage::~myPhotonCloudPackage ( )
{
}
//-----------------------------------------------------------------------------
// Functions table
//-----------------------------------------------------------------------------
static S3DX::AIFunction aMyFunctions [ ] =
{
{ "getPlayersNumberList", Callback_myPhotonCloud_getPlayersNumberList, "sPlayersNumberList", "" , "Get a string with the list of number (ID) of players in the cloud", 0 },
{ "sendUserData" , Callback_myPhotonCloud_sendUserData , "" , "x,y,z, rx,ry,rz", "Send basic user data to the cloud" , 0 },
{ "disconnect" , Callback_myPhotonCloud_disconnect , "" , "", "Disconnect from cloud" , 0 },
{ "createRoom" , Callback_myPhotonCloud_createRoom , "" , "", "Create a Room without params" , 0 },
{ "joinRandomRoom", Callback_myPhotonCloud_joinRandomRoom, "" , "", "Join a random room of my Photon Cloud", 0 },
{ "getStatus", Callback_myPhotonCloud_getStatus, "nStatus", "", "Get My Photon Cloud Connection Status", 0 },
{ "connect", Callback_myPhotonCloud_connect, "", "", "Connect with default params", 0 }
//{ NULL, NULL, NULL, NULL, NULL, 0}
} ;
//-----------------------------------------------------------------------------
// Constants table
//-----------------------------------------------------------------------------
static S3DX::AIConstant aMyConstants [ ] =
{
{ NULL, 0, NULL, 0}
} ;
//-----------------------------------------------------------------------------
// Accessors
//-----------------------------------------------------------------------------
const char *myPhotonCloudPackage::GetName ( ) const
{
return "myPhotonCloud" ;
}
//-----------------------------------------------------------------------------
S3DX::uint32 myPhotonCloudPackage::GetFunctionCount ( ) const
{
if ( aMyFunctions[0].pName == NULL )
{
return 0 ;
}
else
{
return sizeof( aMyFunctions ) / sizeof( S3DX::AIFunction ) ;
}
}
//-----------------------------------------------------------------------------
S3DX::uint32 myPhotonCloudPackage::GetConstantCount ( ) const
{
if ( aMyConstants[0].pName == NULL )
{
return 0 ;
}
else
{
return sizeof( aMyConstants ) / sizeof( S3DX::AIConstant ) ;
}
}
//-----------------------------------------------------------------------------
const S3DX::AIFunction *myPhotonCloudPackage::GetFunctionAt ( S3DX::uint32 _iIndex ) const
{
return &aMyFunctions[ _iIndex ] ;
}
//-----------------------------------------------------------------------------
const S3DX::AIConstant *myPhotonCloudPackage::GetConstantAt ( S3DX::uint32 _iIndex ) const
{
return &aMyConstants[ _iIndex ] ;
}
//-----------------------------------------------------------------------------
#ifndef __myPhotonCloud_h__
#define __myPhotonCloud_h__
//-----------------------------------------------------------------------------
#include "S3DXAIPackage.h"
#include "myPhotonCloudConfig.h"
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Package declaration
//-----------------------------------------------------------------------------
class myPhotonCloudPackage : public S3DX::AIPackage
{
public :
//-------------------------------------------------------------------------
// Constructor / Destructor
//-------------------------------------------------------------------------
myPhotonCloudPackage ( ) ;
~myPhotonCloudPackage ( ) ;
//-------------------------------------------------------------------------
// Accessors
//-------------------------------------------------------------------------
const char *GetName ( ) const ;
S3DX::uint32 GetFunctionCount ( ) const ;
S3DX::uint32 GetConstantCount ( ) const ;
const S3DX::AIFunction *GetFunctionAt ( S3DX::uint32 _iIndex ) const ;
const S3DX::AIConstant *GetConstantAt ( S3DX::uint32 _iIndex ) const ;
} ;
//-----------------------------------------------------------------------------
// Package API declaration
//-----------------------------------------------------------------------------
class myPhotonCloudAPI
{
public :
//Config Server Here
myPhotonCloudConfig MyPhotonCloudConfig;
//-------------------------------------------------------------------------
// API Constructor
//-------------------------------------------------------------------------
myPhotonCloudAPI ( )
{
pfn_myPhotonCloud_getPlayersNumberList = NULL ;
pfn_myPhotonCloud_sendUserData = NULL ;
pfn_myPhotonCloud_disconnect = NULL ;
pfn_myPhotonCloud_createRoom = NULL ;
pfn_myPhotonCloud_joinRandomRoom = NULL ;
pfn_myPhotonCloud_getStatus = NULL ;
pfn_myPhotonCloud_connect = NULL ;
}
//-------------------------------------------------------------------------
// API Callbacks
//-------------------------------------------------------------------------
S3DX::AICallback pfn_myPhotonCloud_getPlayersNumberList ;
S3DX::AICallback pfn_myPhotonCloud_sendUserData ;
S3DX::AICallback pfn_myPhotonCloud_disconnect ;
S3DX::AICallback pfn_myPhotonCloud_createRoom ;
S3DX::AICallback pfn_myPhotonCloud_joinRandomRoom ;
S3DX::AICallback pfn_myPhotonCloud_getStatus ;
S3DX::AICallback pfn_myPhotonCloud_connect ;
//-------------------------------------------------------------------------
// API Functions
//-------------------------------------------------------------------------
inline S3DX::AIVariable getPlayersNumberList ( ) { S3DX::AIVariable vOut ; if ( pfn_myPhotonCloud_getPlayersNumberList ) pfn_myPhotonCloud_getPlayersNumberList ( 0, NULL, &vOut ); return vOut ; }
inline void sendUserData ( const S3DX::AIVariable& x, const S3DX::AIVariable& y, const S3DX::AIVariable& z, const S3DX::AIVariable& rx, const S3DX::AIVariable& ry, const S3DX::AIVariable& rz ) { S3DX_DECLARE_VIN_06( x, y, z, rx, ry, rz ) ; if ( pfn_myPhotonCloud_sendUserData ) pfn_myPhotonCloud_sendUserData ( 6, vIn, NULL ); }
inline void disconnect ( ) { if ( pfn_myPhotonCloud_disconnect ) pfn_myPhotonCloud_disconnect ( 0, NULL, NULL ); }
inline void createRoom ( ) { if ( pfn_myPhotonCloud_createRoom ) pfn_myPhotonCloud_createRoom ( 0, NULL, NULL ); }
inline void joinRandomRoom ( ) { if ( pfn_myPhotonCloud_joinRandomRoom ) pfn_myPhotonCloud_joinRandomRoom ( 0, NULL, NULL ); }
inline S3DX::AIVariable getStatus ( ) { S3DX::AIVariable vOut ; if ( pfn_myPhotonCloud_getStatus ) pfn_myPhotonCloud_getStatus ( 0, NULL, &vOut ); return vOut ; }
inline void connect ( ) { if ( pfn_myPhotonCloud_connect ) pfn_myPhotonCloud_connect ( 0, NULL, NULL ); }
//-------------------------------------------------------------------------
// API Constants
//-------------------------------------------------------------------------
} ;
extern myPhotonCloudAPI myPhotonCloud;
//-----------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
#include "PrecompiledHeader.h"
#include "myPhotonCloudConfig.h"
void StdOutputListener::write(const ExitGames::Common::JString& str)
{
std::wcout << str;
_bstr_t b(str);
const char* c = b;
S3DX::log.message("myPhotonCloud Plugin:",
c
);
}
void StdOutputListener::writeLine(const ExitGames::Common::JString& str)
{
write(str);
write(L"\n");
}
int StdUI::serverConnection(int option)
{
switch (option){
case 0: //initialize and connect
networkLogic.registerForStateUpdates(this, &mOutputListener);
networkLogic.connectNow();
break;
case 1: //each frame must synch with server using: networkLogic.mLoadBalancingClient.service()
networkLogic.run();
return networkLogic.getState();
break;
case 2: //once connected you can join a random room
networkLogic.joinRandomRoom();
break;
case 3: //connect again
networkLogic.connectNow();
break;
case 4: // create room without params
networkLogic.opCreateRoom();
break;
case 5: //disconnect
networkLogic.disconnect();
break;
default:
return -1;
break;
}
return 0;
/*mOutputListener.writeLine(L"always:");
mOutputListener.writeLine(L"0 - exit");
mOutputListener.writeLine(L"--------------------");
mOutputListener.writeLine(L"outside a game room:");
mOutputListener.writeLine(L"1 - create game");
mOutputListener.writeLine(L"2 - join random game");
mOutputListener.writeLine(L"--------------------");
mOutputListener.writeLine(L"inside a game room:");
mOutputListener.writeLine(L"1 - leave game");
mOutputListener.writeLine(L"--------------------");
while(networkLogic.getLastInput() != INPUT_EXIT && networkLogic.getState() != STATE_DISCONNECTED)
{
int ch = '\0';// getcharIfKbhit();
switch(networkLogic.getState())
{
case STATE_CONNECTED:
switch(ch)
{
case '0':
networkLogic.setLastInput(INPUT_EXIT);
break;
case '1':
networkLogic.setLastInput(INPUT_CREATE_GAME);
break;
case '2':
networkLogic.setLastInput(INPUT_JOIN_RANDOM_GAME);
break;
default:
networkLogic.setLastInput(INPUT_NON);
break;
}
break;
case STATE_JOINED:
switch(ch)
{
case '0':
networkLogic.setLastInput(INPUT_EXIT);
break;
case '1':
networkLogic.setLastInput(INPUT_LEAVE_GAME);
break;
default:
networkLogic.setLastInput(INPUT_NON);
break;
}
break;
default:
switch(ch)
{
case '0':
networkLogic.setLastInput(INPUT_EXIT);
break;
default:
networkLogic.setLastInput(INPUT_NON);
break;
}
break;
}
networkLogic.run();
SLEEP(100);
}*/
}
void StdUI::sendUserDataToServer(float x,float y,float z, float rx,float ry,float rz) {
//Create hashtable
ExitGames::Common::Hashtable userData = ExitGames::Common::Hashtable();
//Add user data
userData.put<const char*,float>("x",x);
userData.put<const char*,float>("y",y);
userData.put<const char*,float>("z",z);
userData.put<const char*,float>("rx",rx);
userData.put<const char*,float>("ry",ry);
userData.put<const char*,float>("rz",rz);
networkLogic.sendUserData(userData);
}
ExitGames::Common::JString StdUI::getCurrentRoomPlayersNumbers() {
//Get list
ExitGames::Common::JVector<int> playersIDs = networkLogic.getCurrentRoomPlayerNumbers();
ExitGames::Common::JString playerString;
for (unsigned int i=0; i<playersIDs.getSize(); i++){
playerString += playersIDs[i] + ",";
}
return playerString;
}
myPhotonCloudConfig::myPhotonCloudConfig(void)
{
initialized = false;
}
void myPhotonCloudConfig::connect(){
if (!initialized){
S3DX::log.warning("Server initialization");
ui.serverConnection(0);
initialized = true;
} else {
ui.serverConnection(0);
}
}
int myPhotonCloudConfig::getPhotonCloudStatus(){
return ui.serverConnection(1);
}
void myPhotonCloudConfig::joinPhotonCloudRandomRoom () {
ui.serverConnection(2);
}
void myPhotonCloudConfig::createPhotonCloudRoom () {
ui.serverConnection(4);
}
void myPhotonCloudConfig::disconnectPhotonCloud(){
ui.serverConnection(5);
}
void myPhotonCloudConfig::sendUserDataToPhotonCloud(float x, float y, float z, float rx, float ry, float rz){
ui.sendUserDataToServer(x,y,z,rx,ry,rz);
}
S3DX::AIVariable myPhotonCloudConfig::getCurrentPlayersNumbers() {
_bstr_t b(ui.getCurrentRoomPlayersNumbers());
const char* c = b;
return S3DX::AIVariable(c);
}
myPhotonCloudConfig::~myPhotonCloudConfig(void)
{
}
#pragma once
//Now we can load the Network logic class:
#include "NetworkLogic.h"
class StdOutputListener: public OutputListener
{
public:
virtual void write(const ExitGames::Common::JString& str);
virtual void writeLine(const ExitGames::Common::JString& str);
};
class StdUI: public NetworkLogicListener
{
public:
int serverConnection(int option);
void sendUserDataToServer(float x,float y,float z, float rx,float ry,float rz);
ExitGames::Common::JString getCurrentRoomPlayersNumbers() ;
private:
virtual void stateUpdate(State newState)
{}
private:
StdOutputListener mOutputListener;
NetworkLogic networkLogic;
};
class myPhotonCloudConfig
{
private:
StdUI ui;
bool initialized;
NetworkLogic networklogic();
public:
myPhotonCloudConfig(void);
void connect();
int getPhotonCloudStatus();
void joinPhotonCloudRandomRoom();
void createPhotonCloudRoom();
void disconnectPhotonCloud();
void sendUserDataToPhotonCloud(float x, float y, float z, float rx, float ry, float rz);
S3DX::AIVariable getCurrentPlayersNumbers() ;
~myPhotonCloudConfig(void);
};
#include "PrecompiledHeader.h"
#include "NetworkLogic.h"
#if defined _EG_MARMALADE_PLATFORM
# if defined I3D_ARCH_X86
# if(defined _MSC_VER && !defined __clang__ && !defined __gcc__)
# define PLAYER_NAME L"Marmalade X86 Windows"
# else
# define PLAYER_NAME L"Marmalade X86 OS X"
# endif
# elif defined I3D_ARCH_ARM
# define PLAYER_NAME L"Marmalade ARM"
# else
# define PLAYER_NAME L"unknown Marmalade platform"
# endif
#elif defined _EG_WINDOWS_PLATFORM
# define PLAYER_NAME L"Win32"
#elif defined _EG_IPHONE_PLATFORM
# define PLAYER_NAME L"iOS"
#elif defined _EG_IMAC_PLATFORM
# define PLAYER_NAME L"OS X"
#elif defined _EG_ANDROID_PLATFORM
# define PLAYER_NAME L"Android"
#elif defined _EG_BLACKBERRY_PLATFORM
# define PLAYER_NAME L"Blackberry"
#elif defined _EG_PS3_PLATFORM
# define PLAYER_NAME L"PS3"
#elif defined _EG_LINUX_PLATFORM
# define PLAYER_NAME L"Linux"
#else
# define PLAYER_NAME L"unknown platform"
#endif
#define CODE_SEND_USER_DATA 100
ExitGames::Common::JString& NetworkLogicListener::toString(ExitGames::Common::JString& retStr, bool /*withTypes*/) const
{
return retStr;
}
State StateAccessor::getState(void) const
{
return mState;
}
void StateAccessor::setState(State newState)
{
mState = newState;
for(unsigned int i=0; i<mStateUpdateListeners.getSize(); i++)
mStateUpdateListeners[i]->stateUpdate(newState);
}
void StateAccessor::registerForStateUpdates(NetworkLogicListener* listener)
{
mStateUpdateListeners.addElement(listener);
}
Input NetworkLogic::getLastInput(void) const
{
return mLastInput;
}
void NetworkLogic::setLastInput(Input newInput)
{
mLastInput = newInput;
}
void NetworkLogic::joinRandomRoom() {
if (mStateAccessor.getState()==STATE_CONNECTED){
opJoinRandomRoom();
mStateAccessor.setState(STATE_JOINING);
mOutputListener->writeLine(L"joining random game");
} else {
mOutputListener->writeLine(L"You need to connect first");
}
}
State NetworkLogic::getState(void) const
{
return mStateAccessor.getState();
}
ExitGames::Common::JVector<int> NetworkLogic::getCurrentRoomPlayerNumbers(){
ExitGames::Common::JVector<int> players;
ExitGames::Common::JVector<ExitGames::LoadBalancing::Player> mPlayers = mLoadBalancingClient.getCurrentlyJoinedRoom().getPlayers();
for (unsigned int i=0; i<mPlayers.getSize(); i++){
players.addElement(mPlayers[i].getNumber());
}
return players;
}
NetworkLogic::NetworkLogic()
#ifdef _EG_MS_COMPILER
# pragma warning(push)
# pragma warning(disable:4355)
#endif
: mLoadBalancingClient(*this, MY_PHOTON_CLOUD_APP_ID, MY_PHOTON_CLOUD_APP_VERSION, PLAYER_NAME)
, mLastInput(INPUT_NON)
, mOutputListener()
#ifdef _EG_MS_COMPILER
# pragma warning(pop)
#endif
{
mStateAccessor.setState(STATE_INITIALIZED);
mLoadBalancingClient.setDebugOutputLevel(ExitGames::Common::DebugLevel::OFF);
mLogger.setListener(*this);
mLogger.setDebugOutputLevel(ExitGames::Common::DebugLevel::OFF);
}
NetworkLogic::NetworkLogic(OutputListener* listener, const wchar_t* appVersion)
#ifdef _EG_MS_COMPILER
# pragma warning(push)
# pragma warning(disable:4355)
#endif
: mLoadBalancingClient(*this, MY_PHOTON_CLOUD_APP_ID, appVersion, PLAYER_NAME)
, mLastInput(INPUT_NON)
, mOutputListener(listener)
#ifdef _EG_MS_COMPILER
# pragma warning(pop)
#endif
{
mStateAccessor.setState(STATE_INITIALIZED);
mLoadBalancingClient.setDebugOutputLevel(ExitGames::Common::DebugLevel::INFO);
mLogger.setListener(*this);
mLogger.setDebugOutputLevel(ExitGames::Common::DebugLevel::INFO);
}
void NetworkLogic::registerForStateUpdates(NetworkLogicListener* listener)
{
mStateAccessor.registerForStateUpdates(listener);
}
void NetworkLogic::registerForStateUpdates(NetworkLogicListener* nlistener,OutputListener* slistener )
{
mOutputListener = slistener;
mStateAccessor.registerForStateUpdates(nlistener);
}
void NetworkLogic::connect(void)
{
mLoadBalancingClient.connect(L"app.exitgamescloud.com:5055"); // specify the ip and port of your local masterserver here; call the parameterless overload instead, if you want to connect to the exitgamescloud
mStateAccessor.setState(STATE_CONNECTING);
}
void NetworkLogic::disconnect(void)
{
mLoadBalancingClient.disconnect();
}
void NetworkLogic::opCreateRoom(void)
{
// if last digits are always nearly the same, this is because of the timer calling this function being triggered every x ms with x being a factor of 10
ExitGames::Common::JString tmp;
mLoadBalancingClient.opCreateRoom(tmp=(int)GETTIMEMS());
mStateAccessor.setState(STATE_JOINING);
mOutputListener->writeLine(ExitGames::Common::JString(L"creating game \"") + tmp + L"\"");
}
void NetworkLogic::opJoinRandomRoom(void)
{
mLoadBalancingClient.opJoinRandomRoom();
}
void NetworkLogic::connectNow (void){
this->connect();
mStateAccessor.setState(STATE_CONNECTING);
mOutputListener->writeLine(L"connecting");
}
void NetworkLogic::run(void)
{
if(mLastInput == INPUT_EXIT && mStateAccessor.getState() != STATE_DISCONNECTING && mStateAccessor.getState() != STATE_DISCONNECTED)
{
disconnect();
mStateAccessor.setState(STATE_DISCONNECTING);
mOutputListener->writeLine(L"terminating application");
}
else
{
State state = mStateAccessor.getState();
switch(state)
{
case STATE_INITIALIZED:
connect();
mStateAccessor.setState(STATE_CONNECTING);
mOutputListener->writeLine(L"connecting");
break;
case STATE_CONNECTING:
break; // wait for callback
case STATE_CONNECTED:
switch(mLastInput)
{
case INPUT_CREATE_GAME: // create Game
opCreateRoom();
break;
case INPUT_JOIN_RANDOM_GAME: // join Game
opJoinRandomRoom();
mStateAccessor.setState(STATE_JOINING);
mOutputListener->writeLine(L"joining random game");
break;
default: // no or illegal input -> stay waiting for legal input
break;
}
break;
case STATE_JOINING:
break; // wait for callback
case STATE_JOINED:
//sendEvent();
switch(mLastInput)
{
case INPUT_LEAVE_GAME: // leave Game
mLoadBalancingClient.opLeaveRoom();
mStateAccessor.setState(STATE_LEAVING);
mOutputListener->writeLine(L"");
mOutputListener->writeLine(L"leaving game room");
break;
default: // no or illegal input -> stay waiting for legal input
break;
}
break;
case STATE_LEAVING:
break; // wait for callback
case STATE_LEFT:
mStateAccessor.setState(STATE_CONNECTED);
break;
case STATE_DISCONNECTING:
break; // wait for callback
default:
break;
}
}
mLastInput = INPUT_NON;
mLoadBalancingClient.service();
}
void NetworkLogic::sendEvent(void)
{
#ifdef _EG_PS3_PLATFORM
static int count = 0;
ExitGames::Common::Hashtable data;
data.put<int, int>(0, count++);
mLoadBalancingClient.opRaiseEvent(false, data, 0);
mOutputListener->write(ExitGames::Common::JString(L"s:") + count + ExitGames::Common::JString(L" "));
#else
mLoadBalancingClient.opRaiseEvent(false, ExitGames::Common::Hashtable(), 0);
//mOutputListener->write(L"s");
#endif
}
void NetworkLogic::sendUserData(ExitGames::Common::Hashtable uData)
{
#ifdef _EG_PS3_PLATFORM
static int count = 0;
ExitGames::Common::Hashtable data;
data.put<int, int>(0, count++);
mLoadBalancingClient.opRaiseEvent(false, data, 0);
mOutputListener->write(ExitGames::Common::JString(L"s:") + count + ExitGames::Common::JString(L" "));
#else
mLoadBalancingClient.opRaiseEvent(false, uData, CODE_SEND_USER_DATA);
//mOutputListener->write(L"s");
#endif
}
// protocol implementations
void NetworkLogic::debugReturn(const ExitGames::Common::JString& string)
{
mOutputListener->debugReturn(string);
}
void NetworkLogic::debugReturn(ExitGames::Common::DebugLevel::DebugLevel /*debugLevel*/, const ExitGames::Common::JString& string)
{
debugReturn(string);
}
void NetworkLogic::connectionErrorReturn(int errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"code: %d", errorCode);
mOutputListener->writeLine(ExitGames::Common::JString(L"connection failed with error ") + errorCode);
mStateAccessor.setState(STATE_DISCONNECTED);
}
void NetworkLogic::clientErrorReturn(int errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"code: %d", errorCode);
mOutputListener->writeLine(ExitGames::Common::JString(L"received error ") + errorCode + L" from client");
}
void NetworkLogic::warningReturn(int warningCode)
{
EGLOG(ExitGames::Common::DebugLevel::WARNINGS, L"code: %d", warningCode);
mOutputListener->writeLine(ExitGames::Common::JString(L"received warning ") + warningCode + L" from client");
}
void NetworkLogic::serverErrorReturn(int errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"code: %d", errorCode);
mOutputListener->writeLine(ExitGames::Common::JString(L"received error ") + errorCode + " from server");
}
void NetworkLogic::joinRoomEventAction(int playerNr, const ExitGames::Common::JVector<int>& /*playernrs*/, const ExitGames::LoadBalancing::Player& player)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"%ls joined the game", player.getName().cstr());
mOutputListener->writeLine(L"");
mOutputListener->writeLine(ExitGames::Common::JString(L"player ") + playerNr + L" " + player.getName() + L" has joined the game");
if (playerNr!=this->getCurrentPlayerNumber()){
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onUserEnterRoom"), S3DX::AIVariable(playerNr));
}
}
void NetworkLogic::leaveRoomEventAction(int playerNr)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
mOutputListener->writeLine(L"");
mOutputListener->writeLine(ExitGames::Common::JString(L"player ") + playerNr + " has left the game");
//send event to the AI to destroy object
if (playerNr!=this->getCurrentPlayerNumber()){
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onUserExitRoom"), S3DX::AIVariable(playerNr));
}
}
void NetworkLogic::customEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Hashtable& eventContent)
{
// you do not receive your own events,
//unless you specify yourself as one of the receivers explicitly,
//so you must start 2 clients, to receive the events,
//which you have sent, as sendEvent() uses the default receivers of opRaiseEvent()
//(all players in same room like the sender, except the sender itself)
//EGLOG(ExitGames::Common::DebugLevel::ALL, L"r");
#ifdef _EG_PS3_PLATFORM
int count = ExitGames::Common::ValueObject<int>(eventContent.getValue<int>(0)).getDataCopy();
mOutputListener->write(ExitGames::Common::JString(L"r:") + count + ExitGames::Common::JString(L"\n"));
#else
if (eventCode==CODE_SEND_USER_DATA){
/*ExitGames::Common::JVector<ExitGames::Common::Object> keys = eventContent.getKeys();
for(int i=0; i<keys.getSize(); i++){
}*/
S3DX::user.sendEvent(
S3DX::application.getCurrentUser(),
S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onGetUserData"),
S3DX::AIVariable(playerNr), //player number
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("x")).getDataCopy()),
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("y")).getDataCopy()),
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("z")).getDataCopy()),
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("rx")).getDataCopy()),
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("ry")).getDataCopy()),
S3DX::AIVariable(ExitGames::Common::ValueObject<float>(eventContent.getValue<const char*>("rz")).getDataCopy())
);
}
//send to - > Shiva : onGetUserData
#endif
}
void NetworkLogic::connectReturn(int errorCode, const ExitGames::Common::JString& errorString)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
if(errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"%ls", errorString.cstr());
mStateAccessor.setState(STATE_DISCONNECTING);
return;
}
mOutputListener->writeLine(L"connected");
mStateAccessor.setState(STATE_CONNECTED);
}
void NetworkLogic::disconnectReturn(void)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
mOutputListener->writeLine(L"disconnected");
mStateAccessor.setState(STATE_DISCONNECTED);
}
void NetworkLogic::createRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& /*gameProperties*/, const ExitGames::Common::Hashtable& /*playerProperties*/, int errorCode, const ExitGames::Common::JString& errorString)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
if(errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"%ls", errorString.cstr());
mOutputListener->writeLine(L"opCreateRoom() failed: " + errorString);
mStateAccessor.setState(STATE_CONNECTED);
return;
}
EGLOG(ExitGames::Common::DebugLevel::INFO, L"localPlayerNr: %d", localPlayerNr);
mOutputListener->writeLine(L"game room \"" + mLoadBalancingClient.getCurrentlyJoinedRoom().getName() + "\" has been created");
mOutputListener->writeLine(L"regularly sending dummy events now");
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onCurrentUserEnterRoom"), S3DX::AIVariable(localPlayerNr));
mStateAccessor.setState(STATE_JOINED);
}
void NetworkLogic::joinRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& /*gameProperties*/, const ExitGames::Common::Hashtable& /*playerProperties*/, int errorCode, const ExitGames::Common::JString& errorString)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
if(errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"%ls", errorString.cstr());
mOutputListener->writeLine(L"opJoinRoom() failed: " + errorString);
mStateAccessor.setState(STATE_CONNECTED);
return;
}
EGLOG(ExitGames::Common::DebugLevel::INFO, L"localPlayerNr: %d", localPlayerNr);
mOutputListener->writeLine(L"game room \"" + mLoadBalancingClient.getCurrentlyJoinedRoom().getName() + "\" has been successfully joined");
mOutputListener->writeLine(L"regularly sending dummy events now");
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onCurrentUserEnterRoom"), S3DX::AIVariable(localPlayerNr));
//Add other players to the session right now:
ExitGames::Common::JVector<ExitGames::LoadBalancing::Player> mPlayers = mLoadBalancingClient.getCurrentlyJoinedRoom().getPlayers();
for (unsigned int i=0; i<mPlayers.getSize(); i++){
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onUserEnterRoom"), S3DX::AIVariable(mPlayers[i].getNumber()));
}
mStateAccessor.setState(STATE_JOINED);
}
void NetworkLogic::joinRandomRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& /*gameProperties*/, const ExitGames::Common::Hashtable& /*playerProperties*/, int errorCode, const ExitGames::Common::JString& errorString)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
if(errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"%ls", errorString.cstr());
mOutputListener->writeLine(L"opJoinRandomRoom() failed: " + errorString);
mStateAccessor.setState(STATE_CONNECTED);
return;
}
EGLOG(ExitGames::Common::DebugLevel::INFO, L"localPlayerNr: %d", localPlayerNr);
mOutputListener->writeLine(L"game room \"" + mLoadBalancingClient.getCurrentlyJoinedRoom().getName() + "\" has been successfully joined");
mOutputListener->writeLine(L"regularly sending dummy events now");
S3DX::user.sendEvent(S3DX::application.getCurrentUser() ,S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onCurrentUserEnterRoom"), S3DX::AIVariable(localPlayerNr));
//Add other players to the session right now:
ExitGames::Common::JVector<ExitGames::LoadBalancing::Player> mPlayers = mLoadBalancingClient.getCurrentlyJoinedRoom().getPlayers();
for (unsigned int i=0; i<mPlayers.getSize(); i++){
S3DX::user.sendEvent(S3DX::application.getCurrentUser(), S3DX::AIVariable("myPhotonCloud_AI"), S3DX::AIVariable("onUserEnterRoom"), S3DX::AIVariable(mPlayers[i].getNumber()));
}
mStateAccessor.setState(STATE_JOINED);
}
void NetworkLogic::leaveRoomReturn(int errorCode, const ExitGames::Common::JString& errorString)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
if(errorCode)
{
EGLOG(ExitGames::Common::DebugLevel::ERRORS, L"%ls", errorString.cstr());
mOutputListener->writeLine(L"opLeaveRoom() failed: " + errorString);
mStateAccessor.setState(STATE_DISCONNECTING);
return;
}
mOutputListener->writeLine(L"game room has been successfully left");
mStateAccessor.setState(STATE_LEFT);
}
void NetworkLogic::gotQueuedReturn(void)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
}
void NetworkLogic::joinLobbyReturn(void)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
mOutputListener->writeLine(L"joined lobby");
}
void NetworkLogic::leaveLobbyReturn(void)
{
EGLOG(ExitGames::Common::DebugLevel::INFO, L"");
mOutputListener->writeLine(L"left lobby");
}
#pragma once
#ifndef __NETWORK_LOGIC_H
#define __NETWORK_LOGIC_H
//Get these values from the PC Dashboard: https://cloud.exitgames.com/Dashboard
#define MY_PHOTON_CLOUD_APP_ID L"ENTER YOUR APP ID HERE"
#define MY_PHOTON_CLOUD_APP_VERSION L"1.0"
#ifndef __OUTPUT_LISTENER_H
#define __OUTPUT_LISTENER_H
#ifndef _EG_ANDROID_PLATFORM
#include <iostream>
#endif
#include "LoadBalancing-cpp/inc/LoadBalancingClient.h"
#include <comdef.h> // you will need this
#if defined _EG_WIN32_PLATFORM
# define SLEEP(ms) Sleep(ms)
#else
# define SLEEP(ms) usleep(ms*1000)
#endif
class OutputListener
{
public:
virtual ~OutputListener(void){};
virtual void write(const ExitGames::Common::JString& str) = 0;
virtual void writeLine(const ExitGames::Common::JString& str) = 0;
virtual void debugReturn(const ExitGames::Common::JString& str)
{
#ifndef _EG_ANDROID_PLATFORM
std::wcerr << str << std::endl;
#endif
_bstr_t b(str);
const char* c = b;
S3DX::log.message("myPhotonCloud Plugin:",
c
);
}
};
#endif
typedef enum _State
{
STATE_INITIALIZED = 0,
STATE_CONNECTING,
STATE_CONNECTED,
STATE_JOINING,
STATE_JOINED,
STATE_LEAVING,
STATE_LEFT,
STATE_DISCONNECTING,
STATE_DISCONNECTED
} State;
typedef enum _Input
{
INPUT_NON = 0,
INPUT_CREATE_GAME,
INPUT_JOIN_RANDOM_GAME,
INPUT_LEAVE_GAME,
INPUT_EXIT
} Input;
class NetworkLogicListener : public ExitGames::Common::ToString
{
public:
using ExitGames::Common::ToString::toString;
virtual void stateUpdate(State newState) = 0;
virtual ExitGames::Common::JString& toString(ExitGames::Common::JString& retStr, bool withTypes=false) const;
};
class StateAccessor
{
public:
State getState(void) const;
void setState(State newState);
void registerForStateUpdates(NetworkLogicListener* listener);
private:
State mState;
ExitGames::Common::JVector<NetworkLogicListener*> mStateUpdateListeners;
};
class NetworkLogic : private ExitGames::LoadBalancing::Listener
{
public:
NetworkLogic(); //empty constructor
NetworkLogic(OutputListener* listener, const wchar_t* appVersion);
void registerForStateUpdates(NetworkLogicListener* listener);
void registerForStateUpdates(NetworkLogicListener* nlistener,OutputListener* slistener );
void run(void);
void connectNow(void); //invoke by juaxix
void connect(void);
void opCreateRoom(void);
void opJoinRandomRoom(void);
void disconnect(void);
void sendEvent(void);
void sendUserData(ExitGames::Common::Hashtable uData);
void joinRandomRoom();
Input getLastInput(void) const;
void setLastInput(Input newInput);
State getState(void) const;
int getCurrentPlayerNumber(void) {return this->mLoadBalancingClient.getLocalPlayer().getNumber();};
ExitGames::Common::JVector<int> getCurrentRoomPlayerNumbers(void);
private:
//From Common::BaseListener
// receive and print out Photon datatypes debug out here
virtual void debugReturn(const ExitGames::Common::JString& string);
//From LoadBalancing::LoadBalancingListener
// receive and print out Photon LoadBalancing debug out here
virtual void debugReturn(ExitGames::Common::DebugLevel::DebugLevel debugLevel, const ExitGames::Common::JString& string);
// implement your error-handling here
virtual void connectionErrorReturn(int errorCode);
virtual void clientErrorReturn(int errorCode);
virtual void warningReturn(int warningCode);
virtual void serverErrorReturn(int errorCode);
// events, triggered by certain operations of all players in the same room
virtual void joinRoomEventAction(int playerNr, const ExitGames::Common::JVector<int>& playernrs, const ExitGames::LoadBalancing::Player& player);
virtual void leaveRoomEventAction(int playerNr);
virtual void customEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Hashtable& eventContent);
// callbacks for operations on PhotonLoadBalancing server
virtual void connectReturn(int errorCode, const ExitGames::Common::JString& errorString);
virtual void disconnectReturn(void);
virtual void createRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& gameProperties, const ExitGames::Common::Hashtable& playerProperties, int errorCode, const ExitGames::Common::JString& errorString);
virtual void joinRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& gameProperties, const ExitGames::Common::Hashtable& playerProperties, int errorCode, const ExitGames::Common::JString& errorString);
virtual void joinRandomRoomReturn(int localPlayerNr, const ExitGames::Common::Hashtable& gameProperties, const ExitGames::Common::Hashtable& playerProperties, int errorCode, const ExitGames::Common::JString& errorString);
virtual void leaveRoomReturn(int errorCode, const ExitGames::Common::JString& errorString);
virtual void gotQueuedReturn(void);
virtual void joinLobbyReturn(void);
virtual void leaveLobbyReturn(void);
ExitGames::LoadBalancing::Client mLoadBalancingClient;
ExitGames::Common::JString mGameID;
ExitGames::Common::Logger mLogger;
StateAccessor mStateAccessor;
Input mLastInput;
OutputListener* mOutputListener;
};
#endif
//-----------------------------------------------------------------------------
#include "PrecompiledHeader.h"
//-----------------------------------------------------------------------------
#include <string.h>
//-----------------------------------------------------------------------------
S3DX_IMPLEMENT_AIVARIABLE_STRING_POOL ( 524288 ) ;
S3DX_IMPLEMENT_AIENGINEAPI ( MyPhotonCloud ) ;
S3DX_IMPLEMENT_PLUGIN ( MyPhotonCloud ) ;
//-----------------------------------------------------------------------------
// AI Packages
//-----------------------------------------------------------------------------
#include "myPhotonCloud.h"
//-----------------------------------------------------------------------------
// Constructor / Destructor
//-----------------------------------------------------------------------------
MyPhotonCloud::MyPhotonCloud ( )
{
S3DX_REGISTER_PLUGIN ( "com.xixgames.myphotoncloud" ) ;
aContentsDirectory[0] = '\0' ;
//Instanciate AI Packages
S3DX::uint32 iAIPackageIndex = 0 ;
if ( iAIPackageIndex < PLUGIN_AIPACKAGES_COUNT ) aAIPackages [iAIPackageIndex++] = new myPhotonCloudPackage ( ) ;
for ( ; iAIPackageIndex < PLUGIN_AIPACKAGES_COUNT; iAIPackageIndex ++ )
{
aAIPackages[iAIPackageIndex] = NULL ;
}
}
//-----------------------------------------------------------------------------
MyPhotonCloud::~MyPhotonCloud ( )
{
for ( S3DX::uint32 iAIPackageIndex = 0 ; iAIPackageIndex < PLUGIN_AIPACKAGES_COUNT; iAIPackageIndex ++ )
{
if ( aAIPackages [iAIPackageIndex] )
{
delete aAIPackages [iAIPackageIndex] ;
aAIPackages[iAIPackageIndex] = NULL ;
}
}
}
//-----------------------------------------------------------------------------
// Plugin content directory
//-----------------------------------------------------------------------------
void MyPhotonCloud::SetContentsDirectory ( const char *_pDirectory ) { strcpy ( aContentsDirectory, _pDirectory ) ; }
//-----------------------------------------------------------------------------
// AI packages
//-----------------------------------------------------------------------------
S3DX::uint32 MyPhotonCloud::GetAIPackageCount ( ) const { return PLUGIN_AIPACKAGES_COUNT ; }
const S3DX::AIPackage *MyPhotonCloud::GetAIPackageAt ( S3DX::uint32 _iIndex ) const { return (_iIndex < PLUGIN_AIPACKAGES_COUNT) ? aAIPackages[_iIndex] : NULL ; }
//-----------------------------------------------------------------------------
#ifndef __MyPhotonCloud_Plugin_h__
#define __MyPhotonCloud_Plugin_h__
//-----------------------------------------------------------------------------
#include "S3DXPlugin.h"
//-----------------------------------------------------------------------------
#define PLUGIN_AIPACKAGES_COUNT 1
//-----------------------------------------------------------------------------
// Class declaration
//-----------------------------------------------------------------------------
S3DX_DECLARE_PLUGIN ( MyPhotonCloud )
{
S3DX_DECLARE_AIENGINEAPI ( ) ;
S3DX_DECLARE_PLUGIN_INSTANCE ( MyPhotonCloud ) ;
public :
//-------------------------------------------------------------------------
// Constructor / Destructor
//-------------------------------------------------------------------------
MyPhotonCloud ( ) ;
~MyPhotonCloud ( ) ;
//-------------------------------------------------------------------------
// Plugin content directory
//-------------------------------------------------------------------------
void SetContentsDirectory ( const char *_pDirectory ) ;
//-------------------------------------------------------------------------
// AI packages
//-------------------------------------------------------------------------
S3DX::uint32 GetAIPackageCount ( ) const ;
const S3DX::AIPackage *GetAIPackageAt ( S3DX::uint32 _iIndex ) const ;
//-------------------------------------------------------------------------
// User functions
//-------------------------------------------------------------------------
inline const char *GetContentsDirectory ( ) const { return aContentsDirectory ; }
private :
//-------------------------------------------------------------------------
// Variables
//-------------------------------------------------------------------------
S3DX::AIPackage * aAIPackages [PLUGIN_AIPACKAGES_COUNT] ;
char aContentsDirectory [ 1024 ] ;
} ;
//-----------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment