Permalink
Browse files

Initial version of CocosPlayer

  • Loading branch information...
1 parent f040a1b commit 24fc021ac24093cea88303e5c577a447c1f02664 Viktor Lidholt committed Jul 13, 2012
View
@@ -46,4 +46,5 @@ Backup[ ]of[ ]*.numbers/
# Private keys
*priv.pem
-*.ccbresourcelog
+*.ccbresourcelog
+CocosPlayer/CocosPlayer.xcodeproj/xcshareddata/xcschemes/CocosPlayer.xcscheme
@@ -7,7 +7,15 @@
//
#import <Foundation/Foundation.h>
+#import "cocos2d.h"
-@interface PlayerStatusLayer : NSObject
+@interface PlayerStatusLayer : CCLayer
+{
+ CCLabelTTF* lblStatus;
+}
+
++ (PlayerStatusLayer*) sharedInstance;
+
+- (void) setStatus:(NSString*)status;
@end
@@ -8,6 +8,34 @@
#import "PlayerStatusLayer.h"
+static PlayerStatusLayer* sharedPlayerStatusLayer = NULL;
+
@implementation PlayerStatusLayer
++ (PlayerStatusLayer*) sharedInstance
+{
+ return sharedPlayerStatusLayer;
+}
+
+- (id) init
+{
+ self = [super init];
+ if (!self) return NULL;
+
+ sharedPlayerStatusLayer = self;
+
+ CGSize winSize = [[CCDirector sharedDirector] winSize];
+
+ lblStatus = [CCLabelTTF labelWithString:@"Waiting for connections" fontName:@"Helvetica" fontSize:12];
+ lblStatus.position = ccp(winSize.width/2, winSize.height/2);
+ [self addChild:lblStatus];
+
+ return self;
+}
+
+- (void) setStatus:(NSString*)status
+{
+ [lblStatus setString:status];
+}
+
@end
@@ -7,7 +7,16 @@
//
#import <Foundation/Foundation.h>
+#import "ThoMoServerStub.h"
+#import "cocos2d.h"
-@interface ServerController : NSObject
+@interface ServerController : NSObject <ThoMoServerDelegateProtocol>
+{
+ ThoMoServerStub* server;
+
+ NSMutableSet* connectedClients;
+}
+
+- (void) start;
@end
@@ -7,7 +7,157 @@
//
#import "ServerController.h"
+#import "PlayerStatusLayer.h"
+#import "AppController.h"
+
+#import "ScriptingCore.h"
+#import "js_manual_conversions.h"
@implementation ServerController
+- (id) init
+{
+ self = [super init];
+ if (!self) return NULL;
+
+ connectedClients = [[NSMutableSet alloc] init];
+
+ server = [[ThoMoServerStub alloc] initWithProtocolIdentifier:@"CocosPlayer"];
+ [server setDelegate:self];
+
+ return self;
+}
+
+- (void) start
+{
+ if (server)
+ {
+ [server start];
+ NSLog(@"Server started");
+ }
+}
+
+- (void) executeJavaScript:(NSString*)script
+{
+ NSThread *cocos2dThread = [[CCDirector sharedDirector] runningThread];
+
+ [cocos2dThread performBlock:^(void) {
+ NSString * string = @"None\n";
+ jsval out;
+ BOOL success = [[ScriptingCore sharedInstance] evalString:script outVal:&out];
+
+ if(success)
+ {
+ if(JSVAL_IS_BOOLEAN(out))
+ {
+ string = [NSString stringWithFormat:@"Result(bool): %@.\n", (JSVAL_TO_BOOLEAN(out)) ? @"true" : @"false"];
+ }
+ else if(JSVAL_IS_INT(out))
+ {
+ string = [NSString stringWithFormat:@"Result(int): %i.\n", JSVAL_TO_INT(out)];
+ }
+ else if(JSVAL_IS_DOUBLE(out))
+ {
+ string = [NSString stringWithFormat:@"Result(double): %d.\n", JSVAL_TO_DOUBLE(out)];
+ }
+ else if(JSVAL_IS_STRING(out)) {
+ NSString *tmp;
+ jsval_to_nsstring( [[ScriptingCore sharedInstance] globalContext], out, &tmp );
+ string = [NSString stringWithFormat:@"Result(string): %d.\n", tmp];
+ }
+ else if (JSVAL_IS_VOID(out) )
+ string = @"Result(void):\n";
+ else if (JSVAL_IS_OBJECT(out) )
+ string = @"Result(object):\n";
+ }
+ else
+ {
+ string = [NSString stringWithFormat:@"Error evaluating script:\n#############################\n%@\n#############################\n", script];
+ }
+
+ [server sendToAllClients:string];
+
+ }
+ waitUntilDone:NO];
+}
+
+- (void) server:(ThoMoServerStub *)theServer acceptedConnectionFromClient:(NSString *)aClientIdString
+{
+ NSLog(@"New Client: %@", aClientIdString);
+ [connectedClients addObject:aClientIdString];
+
+ NSLog(@"Num connected clients: %d", connectedClients.count);
+
+ if (connectedClients.count == 1)
+ {
+ [[AppController appController] setStatus:@"Connected" forceStop:NO];
+ }
+ else
+ {
+ [[AppController appController] setStatus: @"Connected to more than one client" forceStop:YES];
+ }
+}
+
+- (void)server:(ThoMoServerStub *)theServer lostConnectionToClient:(NSString *)aClientIdString errorMessage:(NSString *)errorMessage
+{
+ NSLog(@"Lost Client: %@", aClientIdString);
+ [connectedClients removeObject:aClientIdString];
+
+ if (connectedClients.count == 0)
+ {
+ [[AppController appController] setStatus:@"Waiting for connections" forceStop:YES];
+ }
+ else if (connectedClients.count == 1)
+ {
+ [[AppController appController] setStatus:@"Connected" forceStop:NO];
+ }
+ else
+ {
+ [[AppController appController] setStatus: @"Connected to more than one client" forceStop:YES];
+ }
+}
+
+- (void)serverDidShutDown:(ThoMoServerStub *)theServer
+{
+ NSLog(@"Server shut down");
+ exit(1);
+}
+
+- (void)netServiceProblemEncountered:(NSString *)errorMessage onServer:(ThoMoServerStub *)theServer
+{
+ NSLog(@"Net service problem: %@", errorMessage);
+ exit(1);
+}
+
+- (void) server:(ThoMoServerStub *)theServer didReceiveData:(id)theData fromClient:(NSString *)aClientIdString
+{
+ if (connectedClients.count != 1) return;
+
+ NSDictionary* msg = theData;
+
+ NSString* cmd = [msg objectForKey:@"cmd"];
+
+ NSLog(@"cmd: %@", cmd);
+
+ if ([cmd isEqualToString:@"script"])
+ {
+ NSString* script = [msg objectForKey:@"script"];
+ [self executeJavaScript:script];
+ }
+ else if ([cmd isEqualToString:@"run"])
+ {
+ }
+ else if ([cmd isEqualToString:@"resource"])
+ {
+ }
+}
+
+- (void) dealloc
+{
+ [server release];
+ [connectedClients release];
+
+ [super dealloc];
+}
+
@end

0 comments on commit 24fc021

Please sign in to comment.