· 8 years ago · Jun 16, 2018, 01:12 PM
1<html>
2<head>
3<style>
4.achievement
5{
6 margin: 0px auto;
7 padding: 5px;
8 border: 1px solid red;
9}
10</style>
11<script type="text/javascript" src="checkpage.js"></script>
12<script type="text/javascript" src="jquery-latest.js"></script>
13<script type="text/ruby" src="rb/gather_xml_classless.rb"></script>
14<script type="text/javascript">
15
16// Global Namespace
17var Achievementor = {
18
19 db: null, // variable for storing the db.
20 dblocked: 0, // variable for telling if the database is locked
21 /**
22 * DATABASE RELATED FUNCTIONS
23 */
24
25 openDB: function(){
26 var character = $('#character')[0].innerHTML;
27 var realm = $('#realm')[0].innerHTML;
28 var server = $('#server')[0].innerHTML;
29
30 try {
31 var shortName = 'wowach' +'_'+ character +'_'+ realm +'_'+ server;
32 var version = '1.0';
33 this.db = openDatabase(shortName, version);
34 } catch(e) {
35 if (e == INVALID_STATE_ERR) {
36 alert("Invalid database version.");
37 } else {
38 alert("Unknown error "+e+".");
39 }
40 return false;
41 }
42
43 //create the table,
44 var query = 'CREATE TABLE IF NOT EXISTS Achievements (id INTEGER PRIMARY KEY, categoryid INTEGER NOT NULL, done TEXT NOT NULL, title TEXT NOT NULL, icon TEXT NOT NULL, desc TEXT NOT NULL, posted INTEGER DEFAULT 0, marked INTEGER DEFAULT 0);';
45 if (this.db) {
46 this.db.transaction(function(transaction){
47 transaction.executeSql(query, [], self.nullDataHandler, self.errorHandler);
48 });
49 }
50 return true;
51 },
52
53 checkAndInsert: function(item, transaction, results) {
54 var self = this;
55 //check if the data is already in the database
56 var row = results.rows.item(0);
57 if (row['count'] > 0)
58 {return false;}
59 //Not already in the database, insert
60 var query = 'Insert into Achievements (id, categoryId, done, title, icon, desc) values("'+item.id+'","'+item.categoryId+'","'+item.done+'","'+item.title+'","'+item.icon+'","'+item.desc+'");';
61 if (this.db) {
62 this.db.transaction(function(transaction){
63 transaction.executeSql(query, [], self.nullDataHandler, self.errorHandler);
64 });
65 }
66 },
67
68 insertAchievement: function(item){
69 var self = this;
70 item = item || { id: '', categoryid: '', done: '', title: '', icon: '', desc: ''};
71 //figure out if this record is in the database yet or not
72 var countquery = 'SELECT COUNT(*) AS "count" FROM Achievements WHERE id ="'+item.id+'";'
73 if (this.db) {
74 this.db.transaction(function(transaction){
75 transaction.executeSql(countquery, [], function(){self.checkAndInsert.call(self, item, arguments[0], arguments[1])}, self.errorHandler);
76 });
77 }
78 },
79
80
81 nullDataHandler: function(transaction, results){
82
83 },
84
85 errorHandler: function(transaction, error){
86 alert('Error: '+error.message+' (Code '+error.code+')');
87 return false;
88 },
89
90 /**
91 * GRABBER AND PARSER
92 */
93
94 grab_data: function(file) {
95 try {
96 var file = Titanium.Filesystem.getFileStream(Titanium.App.appURLToPath('app://' + file));
97 file.open(Titanium.Filesystem.FILESTREAM_MODE_READ);
98 var data = file.read();
99 file.close();
100 } catch (e) {
101 alert("Error: Unable to open xml files.");
102 }
103
104 this.parse_data(data);
105 },
106
107 parse_data: function(data){
108 var self = this;
109 var dom = new DOMParser().parseFromString(data,"text/xml");
110 var entries = dom.getElementsByTagName('achievement');
111 entries.forEach = Array.prototype.forEach
112 var toreturn = '';
113
114 var status = document.getElementById('status');
115
116 //lock value for database
117 this.dblocked = 1;
118 //Open the database corresponding to this character
119 this.openDB();
120 entries.forEach(function(entry){
121 var data = {
122 id: entry.getAttribute('id'),
123 done: entry.getAttribute('dateCompleted'),
124 title: entry.getAttribute('title'),
125 icon: entry.getAttribute('icon'),
126 desc: entry.getAttribute('desc'),
127 categoryId : entry.getAttribute('categoryId')
128 }
129
130 if (data.done.length > 0) {
131 data.done = data.done.substr(0,10);
132 toreturn += '<div class="achievement"><br>ID: ' + data.id + ' Completed on:' + data.done + ' Title:' + data.title +'<br>Description: ' + data.desc +"</div>";
133 self.insertAchievement(data);
134 }
135 });
136
137 if (entries.length == 0) {
138 status.innerHTML = 'Error: Armory returned 0 achievements.';
139 } else {
140 status.innerHTML += toreturn;
141 }
142 this.dblocked = 0;
143 }
144
145};
146
147
148$(document).ready(function(){
149 $('#check').click(function () {
150 while(Achievementor.dblocked == 1)
151 { $('#status').innerHTML = "Waiting";}
152 $('#status').innerHTML = "Nominal";
153 Achievementor.grab_data('/rb/temp81.xml');
154
155 while(Achievementor.dblocked == 1)
156 { $('#status').innerHTML = "Waiting";}
157 $('#status').innerHTML = "nominal";
158 Achievementor.grab_data('/rb/temp0.xml');
159
160
161
162 });
163 $('#populate').click(function () {
164 getxml.set_charinfo();
165 });
166 $('#getcharinfo').click(function () {
167 getxml.print_charinfo();
168 });
169 $('#gatherdata').click(function () {
170 gatherdata();
171 });
172
173
174});
175</script>
176</head>
177<body>
178<button id="populate">Populate XML</button>
179<button id="getcharinfo">Get Char Info</button>
180<button id="gatherdata">Gather Data</button>
181<button id="check">Parse</button>
182<div id="status">Default</div>
183<div id="character">Tesandra</div>
184<div id="realm">Ghostlands</div>
185<div id="server">us</div>
186</body>
187</html>