not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.0 KiB

  1. /* Start of the app, we require everything that is needed */
  2. define(function (require) {
  3. requirejs(['domReady!', "sugar-web/activity/activity", "sugar-web/graphics/presencepalette", 'activity/memorize-app'], function (doc, activity, presencePalette, memorizeApp) {
  4. window.memorizeApp = memorizeApp;
  5. memorizeApp.activity = activity;
  6. memorizeApp.activity.setup();
  7. if (window.top.sugar.environment.sharedId) {
  8. memorizeApp.initUI(function () {
  9. initPresence(memorizeApp.activity, memorizeApp, presencePalette);
  10. })
  11. } else {
  12. memorizeApp.initUI(function () {
  13. initPresence(memorizeApp.activity, memorizeApp, presencePalette);
  14. memorizeApp.computeCards(true);
  15. memorizeApp.drawGame();
  16. loadData(memorizeApp.activity, memorizeApp, function () {
  17. memorizeApp.drawGame();
  18. });
  19. })
  20. }
  21. });
  22. });
  23. function loadData(activity, memorizeApp, callback) {
  24. var timeout = 0;
  25. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  26. chrome.storage.local.get('sugar_settings', function (values) {
  27. timeout = 500;
  28. });
  29. } else {
  30. timeout = 0;
  31. }
  32. setTimeout(function () {
  33. activity.getDatastoreObject().loadAsText(function (error, metadata, jsonData) {
  34. if (jsonData == null) {
  35. return;
  36. }
  37. var data = JSON.parse(jsonData);
  38. if (data == null) {
  39. return;
  40. }
  41. if (data.game) {
  42. memorizeApp.game = data.game;
  43. memorizeApp.game.multiplayer = false;
  44. memorizeApp.game.selectedCards = [];
  45. memorizeApp.game.currentPlayer = "";
  46. memorizeApp.game.players = []
  47. }
  48. if (callback) {
  49. callback();
  50. }
  51. });
  52. }, timeout);
  53. }
  54. function initPresence(activity, memorizeApp, presencepalette, callback) {
  55. activity.getPresenceObject(function (error, presence) {
  56. memorizeApp.presence = presence;
  57. var networkButton = document.getElementById("network-button");
  58. var presencePalette = new presencepalette.PresencePalette(networkButton, undefined, presence);
  59. presence.onSharedActivityUserChanged(function (msg) {
  60. presencePalette.onSharedActivityUserChanged(msg);
  61. });
  62. //We use one of the palette feature that allows us to get the full list of current users everytime the list changes
  63. presencePalette.onUsersListChanged(function (users) {
  64. memorizeApp.onUsersListChanged(users);
  65. });
  66. // Launched with a shared id, activity is already shared
  67. if (window.top && window.top.sugar && window.top.sugar.environment && window.top.sugar.environment.sharedId) {
  68. shareActivity(activity, presence, memorizeApp, false);
  69. presencePalette.setShared(true);
  70. } else {
  71. presencePalette.addEventListener('shared', function () {
  72. shareActivity(activity, presence, memorizeApp, true);
  73. });
  74. }
  75. if (callback) {
  76. callback();
  77. }
  78. });
  79. }
  80. function shareActivity(activity, presence, memorizeApp, isHost) {
  81. memorizeApp.shareActivity(isHost);
  82. var userSettings = presence.getUserInfo();
  83. // Not found, create a new shared activity
  84. if (!window.top.sugar.environment.sharedId) {
  85. presence.createSharedActivity('org.olpcfrance.MemorizeActivity', function (groupId) {
  86. //console.log(groupId)
  87. });
  88. }
  89. // Show a disconnected message when the WebSocket is closed.
  90. presence.onConnectionClosed(function (event) {
  91. console.log(event);
  92. console.log("Connection closed");
  93. });
  94. presence.onDataReceived(function (data) {
  95. memorizeApp.onDataReceived(data);
  96. });
  97. presence.listUsers(function (users) {
  98. console.log(users)
  99. });
  100. }