· 8 years ago · Jun 13, 2018, 04:44 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
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 //var displayName = 'Database for ' + character + " on " + realm + "_" + server;
34 //var maxSize = 512000; // in bytes: 5 megs
35 alert(character +'_'+ realm +'_'+ server);
36 //this.db = openDatabase(shortName, version, displayName, maxSize);
37 this.db = openDatabase(shortName, version);
38 } catch(e) {
39 if (e == INVALID_STATE_ERR) {
40 alert("Invalid database version.");
41 } else {
42 alert("Unknown error "+e+".");
43 }
44 return false;
45 }
46
47 //create the table,
48 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);';
49 //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);';
50 alert(query);
51 alert(this.db);
52 if (this.db) {
53 this.db.transaction(function(transaction){
54 alert("HERE");
55 alert(transaction);
56 transaction.executeSql(query, [], self.nullDataHandler, self.errorHandler);
57 });
58 }
59 return true;
60 },
61
62 checkAndInsert: function(transaction, results) {
63 var self = this;
64 alert("Existing data returned, analyzing");
65 //check if the data is already in the database
66 var row = results.rows.item(0);
67 if (row['count'] == 0)
68 {return false;}
69 //Not already in the database, insert
70 var query = 'Insert into Acheivements (id, categoryid, dateCompleted, title, icon, desc) values("'+item.id+'","'+item.categoryid+'","'+item.done+'","'+item.title+'","'+item.icon+'","'+item.desc+'");';
71 if (this.db) {
72 alert("DB Valid, Inserting");
73 this.db.transaction(function(transaction){
74 transaction.executeSql(query, [], self.nullDataHandler, self.errorHandler);
75 });
76 }
77 alert("An insert was completed");
78 },
79
80 insertAchievement: function(item){
81 alert("Checking for existing data");
82 var self = this;
83 item = item || { id: '', categoryid: '', done: '', title: '', icon: '', desc: ''};
84 //figure out if this record is in the database yet or not
85 var countquery = 'SELECT COUNT(*) FROM Achievements WHERE id ="'+item.id+'";'
86 if (this.db) {
87 this.db.transaction(function(transaction){
88 transaction.executeSql(countquery, [], self.checkAndInsert, self.errorHandler);
89 });
90 } alert("Exiting Insert");
91 },
92
93
94 nullDataHandler: function(transaction, results){
95
96 },
97
98 errorHandler: function(transaction, error){
99 alert('Error: '+error.message+' (Code '+error.code+')');
100 return false;
101 },
102
103 /**
104 * GRABBER AND PARSER
105 */
106
107 grab_data: function() {
108 alert("starting file grab");
109 try {
110 var file = Titanium.Filesystem.getFileStream(Titanium.App.appURLToPath('app://temp81.xml'));
111 file.open(Titanium.Filesystem.FILESTREAM_MODE_READ);
112 var data = file.read();
113 file.close();
114 } catch (e) {
115 alert("Error");
116 }
117
118 alert(data);
119 this.parse_data(data);
120 },
121
122 parse_data: function(data){
123 alert("Starting Data Parse via Dom");
124 var self = this;
125 var dom = new DOMParser().parseFromString(data,"text/xml");
126 var entries = dom.getElementsByTagName('achievement');
127 entries.forEach = Array.prototype.forEach
128 var toreturn = '';
129
130 var status = document.getElementById('status');
131
132 //Open the database corresponding to this character
133 this.openDB();
134 alert("Database open");
135 alert(entries.length);
136 entries.forEach(function(entry){
137 alert("Placing data into hash");
138 var data = {
139 id: entry.getAttribute('id'),
140 done: entry.getAttribute('dateCompleted'),
141 title: entry.getAttribute('title'),
142 icon: entry.getAttribute('icon'),
143 desc: entry.getAttribute('desc'),
144 categoryId : entry.getAttribute('categoryId')
145 }
146 alert(data.done);
147 if (data.done.length > 0) {
148 data.done = data.done.substr(0,10);
149 //toreturn += '<a id="' + data.id + '" done="' + data.done + '"/>';
150 toreturn += '<div class="achievement"><br>ID: ' + data.id + ' Completed on:' + data.done + ' Title:' + data.title +'<br>Description: ' + data.desc +"</div>";
151 alert("Inserting into database");
152 self.insertAchievement(data);
153 alert("Inserted into database");
154 }
155 });
156
157 alert("Done Parsing Entries");
158
159 if (entries.length == 0) {
160 status.innerHTML = 'Error: Armory returned 0 achievements.';
161 } else {
162 alert("Returning");
163 status.innerHTML = toreturn;
164 }
165 }
166
167};
168
169
170$(document).ready(function(){
171 $('#check').click(function () {
172 Achievementor.grab_data();
173 });
174 $('#populate').click(function () {
175 getxml.set_charinfo();
176 });
177 $('#getcharinfo').click(function () {
178 getxml.print_charinfo();
179 });
180 $('#gatherdata').click(function () {
181 gatherdata();
182 });
183
184
185});
186</script>
187</head>
188<body>
189<button id="populate">Populate XML</button>
190<button id="getcharinfo">Get Char Info</button>
191<button id="gatherdata">Gather Data</button>
192<button id="check">Parse</button>
193<div id="status">Default</div>
194<div id="character">Tesandra</div>
195<div id="realm">Ghostlands</div>
196<div id="server">us</div>
197</body>
198</html>