Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. var PokerBoy = (function () {
  2. var socket,
  3. game_uuid,
  4. password,
  5. game;
  6.  
  7. return {
  8. init: init,
  9. create_game: create_game
  10. };
  11.  
  12. function init(){
  13. socket = new Phoenix.Socket('/socket');
  14. socket.connect();
  15. }
  16.  
  17. function create_game(game_name, user_name){
  18. return new Promise(function(resolve, reject){
  19. var channel = socket.channel("game:lobby")
  20. channel.join()
  21. .receive("ok", messages => console.log("joined lobby", messages) )
  22. .receive("error", reason => reject(reason) );
  23.  
  24. channel.push('create', {name: name});
  25. channel.on('created', game => {
  26. game_uuid = game.uuid;
  27. password = game.password;
  28. channel.leave();
  29. resolve();
  30. });
  31. })
  32. .then(function(){
  33. return new Game(game_uuid, user_name);
  34. });
  35. }
  36.  
  37. function Game(game_uuid, username){
  38. var self = this;
  39. var game;
  40.  
  41. this.become_admin = become_admin;
  42. this.vote = vote;
  43.  
  44. //runs on new to return promise which resovles with game object
  45. return new Promise(function(resolve, reject){
  46. game = socket.channel("game:"+game_uuid, {name: username});
  47.  
  48. game.join()
  49. .receive("ok", message => console.log("joined game", message) )
  50. .receive("error", reason => reject(reason) );
  51.  
  52. game.on("game_update", state => update(state.state) );
  53.  
  54. var intervalCount = 0;
  55. var interval = setInterval(function(){
  56. if(game.state == "joined"){
  57. clearInterval(interval);
  58. resolve(self);
  59. }
  60. else if(intervalCount++ > 100){
  61. clearInterval(interval);
  62. reject("failed to connect to game");
  63. }
  64. }, 10);
  65. });
  66.  
  67. function vote(vote){
  68. game.push('user_vote', {vote: vote});
  69. }
  70.  
  71. function become_admin(password){
  72. game.push('become_admin', {password: password});
  73. }
  74.  
  75. function user_promote(user_name){
  76. game.push('user_promote', {user: user_name});
  77. }
  78.  
  79. function toggle_playing(user_name){
  80. game.push('toggle_playing', {user: user_name});
  81. }
  82.  
  83. function update(state){
  84. debugger;
  85. }
  86. }
  87. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement