PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/cdk/apps/tuxbox/plugins/fx2/tetris/somain.c

https://github.com/nx111/OpenPLi.cn
C | 688 lines | 591 code | 82 blank | 15 comment | 103 complexity | 94b9d279d91ce7e853c0bdae3a02095f MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. ** initial coding by fx2
  3. */
  4. #ifdef HAVE_DREAMBOX_HARDWARE
  5. #undef HAVE_CURL
  6. #else
  7. #define HAVE_CURL
  8. #endif
  9. #include <fcntl.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include <board.h>
  19. #include <colors.h>
  20. #include <draw.h>
  21. #include <fx2math.h>
  22. #include <pig.h>
  23. #include <plugin.h>
  24. #include <rcinput.h>
  25. #ifdef HAVE_CURL
  26. #include <curl/curl.h>
  27. #include <curl/types.h>
  28. #include <curl/easy.h>
  29. #endif
  30. #include <config.h>
  31. extern int debug;
  32. extern int doexit;
  33. extern unsigned short actcode;
  34. extern unsigned short realcode;
  35. extern long score;
  36. #ifdef HAVE_CURL
  37. static char *proxy_addr=0;
  38. static char *proxy_user=0;
  39. #endif
  40. static char *hscore=0;
  41. static char isalloc=0;
  42. static int localuser=-1;
  43. #ifdef HAVE_DBOX_HARDWARE
  44. #ifndef TRUE
  45. #define TRUE (!0)
  46. #endif
  47. #ifndef FALSE
  48. #define FALSE (0)
  49. #endif
  50. #endif
  51. typedef struct _HScore
  52. {
  53. char name[12];
  54. long points;
  55. } HScore;
  56. static HScore hsc[8];
  57. #ifdef HAVE_CURL
  58. static HScore ihsc[8];
  59. static int use_ihsc=0;
  60. #endif
  61. unsigned long BuildCheck( char *user, long score )
  62. {
  63. unsigned long ret = 22;
  64. unsigned long temp = 55;
  65. while ( * user )
  66. {
  67. ret = ret << 1 ^ 90 ^ ( * user++ + temp );
  68. temp += 2;
  69. }
  70. return ret ^ score;
  71. }
  72. #ifdef HAVE_CURL
  73. static void LoadHScore( void )
  74. {
  75. CURL *curl;
  76. CURLcode res;
  77. FILE *fp;
  78. char url[ 512 ];
  79. char *p;
  80. int i;
  81. FBDrawString( 150,32,32,"try load high score from",GRAY,0);
  82. FBDrawString( 150,64,32,hscore,GRAY,0);
  83. #ifdef USEX
  84. FBFlushGrafic();
  85. #endif
  86. sprintf(url,"%s/games/tetris.php?action=get",hscore);
  87. curl = curl_easy_init();
  88. if ( !curl )
  89. return;
  90. fp = fopen( "/var/tmp/trash", "w");
  91. if ( !fp )
  92. {
  93. curl_easy_cleanup(curl);
  94. return;
  95. }
  96. curl_easy_setopt( curl, CURLOPT_URL, url );
  97. curl_easy_setopt( curl, CURLOPT_FILE, fp );
  98. curl_easy_setopt( curl, CURLOPT_NOPROGRESS, TRUE );
  99. if ( proxy_addr )
  100. {
  101. curl_easy_setopt( curl, CURLOPT_PROXY, proxy_addr );
  102. if ( proxy_user )
  103. curl_easy_setopt( curl, CURLOPT_PROXYUSERPWD, proxy_user );
  104. }
  105. res = curl_easy_perform(curl);
  106. curl_easy_cleanup(curl);
  107. fclose( fp );
  108. if ( res )
  109. return;
  110. fp=fopen( "/var/tmp/trash", "r" );
  111. if ( !fp )
  112. return;
  113. for( i=0; i<8; i++ )
  114. {
  115. if ( !fgets(url,512,fp) )
  116. break;
  117. p=strchr(url,'\n');
  118. if ( p )
  119. *p=0;
  120. p=strchr(url,'&');
  121. if ( !p )
  122. break;
  123. *p=0;
  124. p++;
  125. strncpy(ihsc[i].name,url,10);
  126. ihsc[i].name[9]=0;
  127. ihsc[i].points = atoi(p);
  128. }
  129. if ( i==8 )
  130. use_ihsc=1;
  131. fclose(fp);
  132. unlink("/var/tmp/trash");
  133. }
  134. #endif // HAVE_CURL
  135. static void LocalSave( void )
  136. {
  137. int x;
  138. char *user;
  139. int i;
  140. localuser=-1;
  141. for( i=0; i < 8; i++ )
  142. if ( score > hsc[i].points )
  143. break;
  144. if ( i==8 )
  145. return;
  146. Fx2PigPause();
  147. FBFillRect( 500,32,3*52,4*52+4,BLACK );
  148. FBFillRect( 150,420,470,64,BLACK );
  149. FBDrawRect( 149,419,472,66,WHITE );
  150. FBDrawRect( 148,418,474,68,WHITE );
  151. x=FBDrawString( 154,420,64,"name : ",WHITE,0);
  152. user=FBEnterWord(154+x,420,64,9,WHITE);
  153. Fx2PigResume();
  154. if ( i < 7 )
  155. memmove( hsc+i+1,hsc+i,sizeof(HScore)*(7-i) );
  156. strcpy(hsc[i].name,user);
  157. hsc[i].points=score;
  158. localuser=i;
  159. }
  160. static void SaveGame( void )
  161. {
  162. #ifdef HAVE_CURL
  163. CURL *curl;
  164. CURLcode res;
  165. FILE *fp;
  166. char url[ 512 ];
  167. char *user=0;
  168. char luser[ 32 ];
  169. int x;
  170. int n;
  171. char *p;
  172. struct timeval tv;
  173. unsigned long chk=0;
  174. doexit=0;
  175. #endif // HAVE_CURL
  176. if ( score < 31 )
  177. return;
  178. LocalSave();
  179. #ifdef HAVE_CURL
  180. LocalSave();
  181. if ( !use_ihsc )
  182. return;
  183. for( x=0; x < 8; x++ )
  184. {
  185. if ( score > ihsc[x].points )
  186. break;
  187. }
  188. if ( x == 8 )
  189. return;
  190. FBDrawString( 100,230,64,"Inet-Send Highscore ? (OK/BLUE)",GREEN,0);
  191. #ifdef USEX
  192. FBFlushGrafic();
  193. #endif
  194. while( realcode != 0xee )
  195. RcGetActCode();
  196. actcode=0xee;
  197. while( !doexit )
  198. {
  199. tv.tv_sec = 0;
  200. tv.tv_usec = 100000;
  201. select( 0,0,0,0, &tv );
  202. RcGetActCode();
  203. if ( actcode == RC_BLUE )
  204. return;
  205. if ( actcode == RC_OK )
  206. break;
  207. }
  208. if ( doexit )
  209. return;
  210. if ( localuser != -1 )
  211. {
  212. strcpy(luser,hsc[localuser].name);
  213. user=luser;
  214. }
  215. else
  216. {
  217. Fx2PigPause();
  218. FBFillRect( 500,32,3*52,4*52+4,BLACK );
  219. FBFillRect( 150,420,470,64,BLACK );
  220. FBDrawRect( 149,419,472,66,WHITE );
  221. FBDrawRect( 148,418,474,68,WHITE );
  222. x=FBDrawString( 154,420,64,"name : ",WHITE,0);
  223. user=FBEnterWord(154+x,420,64,9,WHITE);
  224. Fx2PigResume();
  225. }
  226. n=FBDrawString( 210,360,48,"sending",BLACK,WHITE);
  227. /* clean name */
  228. x = strlen(user);
  229. p=user;
  230. for( p=user; *p; x--, p++ )
  231. {
  232. if (( *p == ' ' ) || ( *p == '&' ) || ( *p == '/' ))
  233. memcpy(p,p+1,x);
  234. }
  235. chk=BuildCheck( user, score );
  236. sprintf(url,"%s/games/tetris.php?action=put&user=%s&score=%ld&chk=%lu",
  237. hscore,user,score,chk);
  238. curl = curl_easy_init();
  239. if ( !curl )
  240. return;
  241. fp = fopen( "/var/tmp/trash", "w");
  242. if ( !fp )
  243. {
  244. curl_easy_cleanup(curl);
  245. return;
  246. }
  247. curl_easy_setopt( curl, CURLOPT_URL, url );
  248. curl_easy_setopt( curl, CURLOPT_FILE, fp );
  249. curl_easy_setopt( curl, CURLOPT_NOPROGRESS, TRUE );
  250. if ( proxy_addr )
  251. {
  252. curl_easy_setopt( curl, CURLOPT_PROXY, proxy_addr );
  253. if ( proxy_user )
  254. curl_easy_setopt( curl, CURLOPT_PROXYUSERPWD, proxy_user );
  255. }
  256. res = curl_easy_perform(curl);
  257. FBFillRect( 210,360,n,48,GRAY);
  258. if ( !res )
  259. FBDrawString( 210,360,48,"success",GREEN,GRAY);
  260. else
  261. FBDrawString( 210,360,48,"failed",RED,GRAY);
  262. curl_easy_cleanup(curl);
  263. fclose( fp );
  264. unlink( "/var/tmp/trash" );
  265. LoadHScore();
  266. return;
  267. #endif // HAVE_CURL
  268. }
  269. static void ShowHScore( HScore *g )
  270. {
  271. int i;
  272. int x;
  273. char pp[64];
  274. FBFillRect( 0, 0, 720, 576, BLACK );
  275. #ifdef HAVE_CURL
  276. if ( g==ihsc )
  277. FBDrawString( 190, 32, 64, "Internet HighScore", RED, BLACK );
  278. else
  279. #endif
  280. FBDrawString( 220, 32, 64, "HighScore", RED, BLACK );
  281. for( i=0; i < 8; i++ )
  282. {
  283. FBDrawString( 100, 100+i*48, 48, g[i].name, WHITE, 0 );
  284. sprintf(pp,"%ld",g[i].points);
  285. x = FBDrawString( 400, 100+i*48, 48, pp, BLACK, BLACK );
  286. FBDrawString( 500-x, 100+i*48, 48, pp, WHITE, BLACK );
  287. }
  288. #ifdef USEX
  289. FBFlushGrafic();
  290. #endif
  291. while( realcode != 0xee )
  292. RcGetActCode();
  293. }
  294. #ifdef HAVE_CURL
  295. static void ShowIHScore( void )
  296. {
  297. int i = 50;
  298. struct timeval tv;
  299. ShowHScore( ihsc );
  300. while( !doexit && ( realcode == 0xee ) && ( i>0 ))
  301. {
  302. tv.tv_sec=0;
  303. tv.tv_usec=200000;
  304. select( 0,0,0,0,&tv);
  305. RcGetActCode();
  306. i--;
  307. }
  308. }
  309. #endif // HAVE_CURL
  310. static void setup_colors(void)
  311. {
  312. FBSetColor( YELLOW, 235, 235, 30 );
  313. FBSetColor( GREEN, 30, 235, 30 );
  314. FBSetColor( STEELBLUE, 80, 80, 200 );
  315. FBSetColor( BLUE, 80, 80, 230 );
  316. FBSetColor( GRAY, 130, 130, 130 );
  317. FBSetColor( DARK, 60, 60, 60 );
  318. FBSetColor( RED1, 198, 131, 131 );
  319. FBSetColor( RED2, 216, 34, 49 );
  320. /* magenta */
  321. FBSetColor( 30, 216, 175, 216);
  322. FBSetColor( 31, 205, 160, 207);
  323. FBSetColor( 32, 183, 131, 188);
  324. FBSetColor( 33, 230, 196, 231);
  325. FBSetColor( 34, 159, 56, 171);
  326. FBSetColor( 35, 178, 107, 182);
  327. FBSetColor( 36, 172, 85, 180);
  328. FBSetColor( 37, 180, 117, 184);
  329. FBSetColor( 38, 120, 1, 127);
  330. FBSetColor( 39, 89, 1, 98);
  331. /* blue */
  332. FBSetColor( 40, 165, 172, 226);
  333. FBSetColor( 41, 148, 156, 219);
  334. FBSetColor( 42, 119, 130, 200);
  335. FBSetColor( 43, 189, 196, 238);
  336. FBSetColor( 44, 81, 90, 146);
  337. FBSetColor( 45, 104, 114, 185);
  338. FBSetColor( 46, 91, 103, 174);
  339. FBSetColor( 47, 109, 119, 192);
  340. FBSetColor( 48, 46, 50, 81);
  341. FBSetColor( 49, 34, 38, 63);
  342. /* cyan */
  343. FBSetColor( 50, 157, 218, 234);
  344. FBSetColor( 51, 140, 208, 227);
  345. FBSetColor( 52, 108, 186, 211);
  346. FBSetColor( 53, 184, 233, 243);
  347. FBSetColor( 54, 55, 143, 172);
  348. FBSetColor( 55, 92, 171, 197);
  349. FBSetColor( 56, 78, 160, 187);
  350. FBSetColor( 57, 98, 177, 203);
  351. FBSetColor( 58, 7, 98, 120);
  352. FBSetColor( 59, 1, 78, 98);
  353. /* green */
  354. FBSetColor( 60, 173, 218, 177);
  355. FBSetColor( 61, 158, 209, 165);
  356. FBSetColor( 62, 130, 189, 140);
  357. FBSetColor( 63, 195, 232, 199);
  358. FBSetColor( 64, 89, 138, 98);
  359. FBSetColor( 65, 115, 174, 122);
  360. FBSetColor( 66, 102, 163, 112);
  361. FBSetColor( 67, 121, 180, 129);
  362. FBSetColor( 68, 50, 77, 55);
  363. FBSetColor( 69, 38, 59, 41);
  364. /* red */
  365. FBSetColor( 70, 239, 157, 152);
  366. FBSetColor( 71, 231, 141, 136);
  367. FBSetColor( 72, 210, 112, 109);
  368. FBSetColor( 73, 246, 184, 181);
  369. FBSetColor( 74, 153, 76, 74);
  370. FBSetColor( 75, 197, 97, 92);
  371. FBSetColor( 76, 184, 86, 81);
  372. FBSetColor( 77, 202, 101, 99);
  373. FBSetColor( 78, 95, 33, 32);
  374. FBSetColor( 79, 78, 20, 19);
  375. /* yellow */
  376. FBSetColor( 80, 238, 239, 152);
  377. FBSetColor( 81, 230, 231, 136);
  378. FBSetColor( 82, 207, 214, 105);
  379. FBSetColor( 83, 246, 246, 181);
  380. FBSetColor( 84, 148, 157, 70);
  381. FBSetColor( 85, 194, 200, 89);
  382. FBSetColor( 86, 180, 189, 76);
  383. FBSetColor( 87, 199, 206, 95);
  384. FBSetColor( 88, 88, 93, 34);
  385. FBSetColor( 89, 69, 75, 22);
  386. /* orange */
  387. FBSetColor( 90, 243, 199, 148);
  388. FBSetColor( 91, 237, 185, 130);
  389. FBSetColor( 92, 220, 159, 99);
  390. FBSetColor( 93, 249, 220, 178);
  391. FBSetColor( 94, 184, 113, 43);
  392. FBSetColor( 95, 208, 144, 81);
  393. FBSetColor( 96, 198, 132, 67);
  394. FBSetColor( 97, 213, 150, 88);
  395. FBSetColor( 98, 127, 63, 1);
  396. FBSetColor( 99, 98, 46, 1);
  397. FBSetupColors();
  398. }
  399. int tetris_exec( int fdfb, int fdrc, int fdlcd, char *cfgfile )
  400. {
  401. struct timeval tv;
  402. int x;
  403. int i;
  404. int fd;
  405. FILE *fp;
  406. char *line;
  407. char *p;
  408. if ( FBInitialize( 720, 576, 8, fdfb ) < 0 )
  409. return -1;
  410. setup_colors();
  411. if ( RcInitialize( fdrc ) < 0 )
  412. return -1;
  413. /* load setup */
  414. fp = fopen( CONFIGDIR "/games.cfg", "r" );
  415. if ( fp )
  416. {
  417. line=malloc(128);
  418. isalloc=1;
  419. #ifdef HAVE_CURL
  420. proxy_addr=0;
  421. proxy_user=0;
  422. #endif
  423. hscore=0;
  424. while( fgets( line, 128, fp ) )
  425. {
  426. if ( *line == '#' )
  427. continue;
  428. if ( *line == ';' )
  429. continue;
  430. p=strchr(line,'\n');
  431. if ( p )
  432. *p=0;
  433. p=strchr(line,'=');
  434. if ( !p )
  435. continue;
  436. *p=0;
  437. p++;
  438. #ifdef HAVE_CURL
  439. if ( !strcmp(line,"proxy") )
  440. proxy_addr=strdup(p);
  441. else if ( !strcmp(line,"proxy_user") )
  442. proxy_user=strdup(p);
  443. else if ( !strcmp(line,"hscore") )
  444. hscore=strdup(p);
  445. #endif
  446. }
  447. fclose(fp);
  448. free(line);
  449. }
  450. fd = open( GAMESDIR "/tetris.hscore", O_RDONLY );
  451. if ( fd == -1 )
  452. {
  453. mkdir( GAMESDIR, 567 );
  454. for( i=0; i < 8; i++ )
  455. {
  456. strcpy(hsc[i].name,"nobody");
  457. hsc[i].points=30;
  458. }
  459. }
  460. else
  461. {
  462. read( fd, hsc, sizeof(hsc) );
  463. close(fd);
  464. }
  465. #ifdef HAVE_CURL
  466. if ( hscore )
  467. {
  468. LoadHScore();
  469. }
  470. #endif
  471. #ifdef HAVE_DBOX_HARDWARE
  472. Fx2ShowPig( 480, 400, 176, 144 );
  473. #endif
  474. while( doexit != 3 )
  475. {
  476. BoardInitialize();
  477. DrawBoard( ); /* 0 = all */
  478. NextItem();
  479. #ifdef HAVE_DREAMBOX_HARDWARE
  480. Fx2ShowPig(480, 400, 176, 144 );
  481. #endif
  482. doexit=0;
  483. while( !doexit )
  484. {
  485. tv.tv_sec = 0;
  486. tv.tv_usec = 10000;
  487. x = select( 0, 0, 0, 0, &tv ); /* 10ms pause */
  488. RcGetActCode( );
  489. if ( doexit )
  490. break;
  491. tv.tv_sec = 0;
  492. tv.tv_usec = 10000;
  493. x = select( 0, 0, 0, 0, &tv ); /* 10ms pause */
  494. RcGetActCode( );
  495. if ( doexit )
  496. break;
  497. MoveSide();
  498. if ( !FallDown() )
  499. {
  500. RemoveCompl();
  501. if ( !NextItem() )
  502. doexit=1;
  503. }
  504. #ifdef USEX
  505. FBFlushGrafic();
  506. #endif
  507. RcGetActCode( );
  508. }
  509. if ( doexit != 3 )
  510. {
  511. actcode=0xee;
  512. DrawGameOver();
  513. #ifdef USEX
  514. FBFlushGrafic();
  515. #endif
  516. doexit=0;
  517. SaveGame();
  518. #ifdef HAVE_CURL
  519. if ( use_ihsc )
  520. ShowIHScore();
  521. #endif
  522. ShowHScore(hsc);
  523. Fx2PigPause();
  524. #ifdef USEX
  525. FBFlushGrafic();
  526. #endif
  527. i=0;
  528. actcode=0xee;
  529. while(( actcode != RC_OK ) && !doexit )
  530. {
  531. tv.tv_sec = 0;
  532. tv.tv_usec = 100000;
  533. x = select( 0, 0, 0, 0, &tv ); /* 100ms pause */
  534. RcGetActCode( );
  535. i++;
  536. if ( i == 50 )
  537. {
  538. FBDrawString( 190, 480, 48, "press OK for new game",GRAY,0);
  539. #ifdef USEX
  540. FBFlushGrafic();
  541. #endif
  542. }
  543. }
  544. Fx2PigResume();
  545. }
  546. }
  547. Fx2StopPig();
  548. #ifdef HAVE_DBOX_HARDWARE
  549. /* fx2 */
  550. /* buffer leeren, damit neutrino nicht rumspinnt */
  551. realcode = RC_0;
  552. while( realcode != 0xee )
  553. {
  554. tv.tv_sec = 0;
  555. tv.tv_usec = 300000;
  556. x = select( 0, 0, 0, 0, &tv ); /* 300ms pause */
  557. RcGetActCode( );
  558. }
  559. #endif
  560. RcClose();
  561. FBClose();
  562. /* save hscore */
  563. fd = open( GAMESDIR "/tetris.hscore", O_CREAT|O_WRONLY, 438 );
  564. if ( fd != -1 )
  565. {
  566. write( fd, hsc, sizeof(hsc) );
  567. close(fd);
  568. }
  569. if ( isalloc )
  570. {
  571. #ifdef HAVE_CURL
  572. if ( proxy_addr )
  573. free ( proxy_addr );
  574. if ( proxy_user )
  575. free ( proxy_user );
  576. #endif
  577. if ( hscore )
  578. free ( hscore );
  579. }
  580. return 0;
  581. }
  582. int plugin_exec( PluginParam *par )
  583. {
  584. int fd_fb=-1;
  585. int fd_rc=-1;
  586. for( ; par; par=par->next )
  587. {
  588. if ( !strcmp(par->id,P_ID_FBUFFER) )
  589. fd_fb=_atoi(par->val);
  590. else if ( !strcmp(par->id,P_ID_RCINPUT) )
  591. fd_rc=_atoi(par->val);
  592. else if ( !strcmp(par->id,P_ID_NOPIG) )
  593. fx2_use_pig=!_atoi(par->val);
  594. #ifdef HAVE_CURL
  595. else if ( !strcmp(par->id,P_ID_PROXY) && par->val && *par->val )
  596. proxy_addr=par->val;
  597. #endif
  598. else if ( !strcmp(par->id,P_ID_HSCORE) && par->val && *par->val )
  599. hscore=par->val;
  600. #ifdef HAVE_CURL
  601. else if ( !strcmp(par->id,P_ID_PROXY_USER) && par->val && *par->val )
  602. proxy_user=par->val;
  603. #endif
  604. }
  605. return tetris_exec( fd_fb, fd_rc, -1, 0 );
  606. }