Improvements - untested
[vchess.git] / client / src / views / MyGames.vue
1 <template lang="pug">
2 main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
6 button.tabbtn#liveGames(@click="setDisplay('live',$event)")
7 | {{ st.tr["Live games"] }}
8 button.tabbtn#corrGames(@click="setDisplay('corr',$event)")
9 | {{ st.tr["Correspondance games"] }}
10 GameList(
11 v-show="display=='live'"
12 :games="liveGames"
13 @show-game="showGame"
14 )
15 GameList(
16 v-show="display=='corr'"
17 :games="corrGames"
18 @show-game="showGame"
19 )
20 </template>
21
22 <script>
23 import { store } from "@/store";
24 import { GameStorage } from "@/utils/gameStorage";
25 import { ajax } from "@/utils/ajax";
26 import GameList from "@/components/GameList.vue";
27 export default {
28 name: "my-my-games",
29 components: {
30 GameList
31 },
32 data: function() {
33 return {
34 st: store.state,
35 display: "live",
36 liveGames: [],
37 corrGames: [],
38 conn: null,
39 connexionString: ""
40 };
41 },
42 created: function() {
43 GameStorage.getAll(true, localGames => {
44 localGames.forEach(g => (g.type = this.classifyObject(g)));
45 this.liveGames = localGames;
46 });
47 if (this.st.user.id > 0) {
48 ajax("/games", "GET", { uid: this.st.user.id }, res => {
49 res.games.forEach(g => (g.type = this.classifyObject(g)));
50 this.corrGames = res.games;
51 });
52 }
53 // Initialize connection
54 this.connexionString =
55 params.socketUrl +
56 "/?sid=" +
57 this.st.user.sid +
58 "&tmpId=" +
59 getRandString() +
60 "&page=" +
61 encodeURIComponent(this.$route.path);
62 this.conn = new WebSocket(this.connexionString);
63 this.conn.onmessage = this.socketMessageListener;
64 this.conn.onclose = this.socketCloseListener;
65 },
66 mounted: function() {
67 const showType = localStorage.getItem("type-myGames") || "live";
68 this.setDisplay(showType);
69 },
70 methods: {
71 setDisplay: function(type, e) {
72 this.display = type;
73 localStorage.setItem("type-myGames", type);
74 let elt = e ? e.target : document.getElementById(type + "Games");
75 elt.classList.add("active");
76 if (elt.previousElementSibling)
77 elt.previousElementSibling.classList.remove("active");
78 else elt.nextElementSibling.classList.remove("active");
79 },
80 // TODO: classifyObject is redundant (see Hall.vue)
81 classifyObject: function(o) {
82 return o.cadence.indexOf("d") === -1 ? "live" : "corr";
83 },
84 showGame: function(g) {
85 this.$router.push("/game/" + g.id);
86 },
87 socketMessageListener: function(msg) {
88 const data = JSON.parse(msg.data);
89 // Only event is newmove, and received only:
90 if (data.code == "newmove") {
91 let games = !!parseInt(data.gid)
92 ? this.corrGames
93 : this.liveGames;
94 // NOTE: new move itself is not received, because it wouldn't be used.
95 let g = games.find(g => g.id == data.gid);
96 this.$set(g, "movesCount", g.movesCount + 1);
97 }
98 },
99 socketCloseListener: function() {
100 this.conn = new WebSocket(this.connexionString);
101 this.conn.addEventListener("message", this.socketMessageListener);
102 this.conn.addEventListener("close", this.socketCloseListener);
103 }
104 }
105 };
106 </script>
107
108 <style lang="sass">
109 .active
110 color: #42a983
111
112 .tabbtn
113 background-color: #f9faee
114
115 table.game-list
116 max-height: 100%
117 </style>