Programming Thread

General off-topic stuffs goes here.
User avatar
Nixitur
Posts: 25
Joined: December 23rd, 2015, 3:19 pm
Location: The internet
Contact:

Re: Programming Thread

Postby Nixitur » December 29th, 2015, 4:22 am

Alice wrote:Seems to crash Firefox. I've tried 100,000 twice now and it crashed both times for some reason.

Yeah, sorry, I didn't exactly understand how JSFiddle worked, so that was still your script. ^^ Mine is here.

Alice wrote:I think I might see where I messed up. It looks like I'm only ever checking loopincr one time when what I was intending to do, if I recall correctly, was to check if we're on an even number and if so then we increment by 1 then we later check if we're on an odd number and if so we increment by 2 instead to skip evens.

That sort of works and is what I tried, but what messes that up is that you explicitly test whether input is the value given by the user and only set the factors if that is true. I threw that away, so that it now correctly sets the factors for whatever input you give the function, regardless of whether it's actually the one that was originally given by the user.
I changed it up a bit more, so that it actually only goes up until i < input always counting up by 2 and after that, I explicitly call check(input) again. That way, it only ever checks odd numbers (since the last element in primes is guaranteed to be odd) up until the largest odd number smaller than input and input itself is always checked last, guaranteeing that the correct factors are found.
Image

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » December 29th, 2015, 4:36 am

Nixitur wrote:Yeah, sorry, I didn't exactly understand how JSFiddle worked, so that was still your script. ^^ Mine is here.

Oh, alright. That one's definitely far quicker than mine. Only took about 10 seconds to go straight a million.
That sort of works and is what I tried, but what messes that up is that you explicitly test whether input is the value given by the user and only set the factors if that is true. I threw that away, so that it now correctly sets the factors for whatever input you give the function, regardless of whether it's actually the one that was originally given by the user.

I probably failed somewhere in the logic of what I was trying to do. At some point fairly early on I completely screwed up somewhere and broke the script altogether then rather than starting over I tried to fix everything instead. Somewhere along the line I probably messed up even though my end result was working.
I changed it up a bit more, so that it actually only goes up until i < input always counting up by 2 and after that, I explicitly call check(input) again. That way, it only ever checks odd numbers (since the last element in primes is guaranteed to be odd) up until the largest odd number smaller than input and input itself is always checked last, guaranteeing that the correct factors are found.

That's probably better than my method actually.
💙💙💙
Image
Image

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 4th, 2016, 7:09 am

It'd sure be nice to ever get answers on my main forum. They have a thread specifically for asking questions about web development stuff yet I never get any help there. It's kinda frustrating because by the time I bother posting there I've already looked up info on my problems so I don't have much I can do.

Specifically in this instance I'm running into an issue because strings in javascript are treated as UTF-16. This is an issue because what I'm loading into the string needs to be converted to hexadecimal so I'm not getting the right values. I've looked around and the only thing I've really found that could help here is using an array buffer but that requires using the filereader api which I can't find ANY information on if what you're trying to do involves specific static files rather than drag and drop operations or a file dialog window.
💙💙💙
Image
Image


User avatar
chridd
Posts: 204
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » January 4th, 2016, 6:05 pm

Alice wrote:Specifically in this instance I'm running into an issue because strings in javascript are treated as UTF-16. This is an issue because what I'm loading into the string needs to be converted to hexadecimal so I'm not getting the right values. I've looked around and the only thing I've really found that could help here is using an array buffer but that requires using the filereader api which I can't find ANY information on if what you're trying to do involves specific static files rather than drag and drop operations or a file dialog window.
It's not clear to me from this what exactly the problem is. What are you actually trying to do? What kind of data are you dealing with? Is it actually strings/text/a text-based format (like XML or JSON), or are you trying to treat binary data as a string? Where is the data coming from, and what do you want to do with it?

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 4th, 2016, 6:39 pm

chridd wrote:It's not clear to me from this what exactly the problem is. What are you actually trying to do? What kind of data are you dealing with? Is it actually strings/text/a text-based format (like XML or JSON), or are you trying to treat binary data as a string? Where is the data coming from, and what do you want to do with it?

It's binary data from a file. Specifically it's rom data for a simple emulator. (A chip8 emulator in particular.) Someone convinced me it would be an interesting idea to try making one but I seem to have really gotten stuck on this bit for actually loading the rom.
💙💙💙
Image
Image


User avatar
chridd
Posts: 204
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » January 5th, 2016, 2:17 am

Alice wrote:
chridd wrote:It's not clear to me from this what exactly the problem is. What are you actually trying to do? What kind of data are you dealing with? Is it actually strings/text/a text-based format (like XML or JSON), or are you trying to treat binary data as a string? Where is the data coming from, and what do you want to do with it?

It's binary data from a file. Specifically it's rom data for a simple emulator. (A chip8 emulator in particular.) Someone convinced me it would be an interesting idea to try making one but I seem to have really gotten stuck on this bit for actually loading the rom.
Some things that might possibly be helpful or maybe not...
• There's intentionally no way in JavaScript (at least in webpages) to read a file from the computer given the path to the file—that is, there's no way a script can, for example, say "open the file C:\My Documents\file.txt". Otherwise, any webpage you visit could read (and send to the server) the user's private data (e.g., what sites they've been to previously, their emails and address book, anything in any document they've written, etc.). If you want a file from a disk on a webpage, you have to use the open dialog or drag/drop; that way, the user can choose what files the webpage can see.
• If you just want to not have to show the dialog every time you open the page, you can store the contents of the file in localStorage. If you do this, it has to be a string, but the string can be any format you want (although some characters are not allowed). One way to do this would be to represent each byte as one character, but adding some specific number (say, 32) to each character to get the character code (so that the character will be in the proper range). Not the most space-efficient way, but it should work.
• If you want a specific file on the server, you can use XMLHttpRequest; see here

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 5th, 2016, 6:04 am

chridd wrote:Some things that might possibly be helpful or maybe not...
• There's intentionally no way in JavaScript (at least in webpages) to read a file from the computer given the path to the file—that is, there's no way a script can, for example, say "open the file C:\My Documents\file.txt". Otherwise, any webpage you visit could read (and send to the server) the user's private data (e.g., what sites they've been to previously, their emails and address book, anything in any document they've written, etc.). If you want a file from a disk on a webpage, you have to use the open dialog or drag/drop; that way, the user can choose what files the webpage can see.
• If you just want to not have to show the dialog every time you open the page, you can store the contents of the file in localStorage. If you do this, it has to be a string, but the string can be any format you want (although some characters are not allowed). One way to do this would be to represent each byte as one character, but adding some specific number (say, 32) to each character to get the character code (so that the character will be in the proper range). Not the most space-efficient way, but it should work.
• If you want a specific file on the server, you can use XMLHttpRequest; see here

The files it's meant to be loading are going to be in the same place as the page itself. So that first bit should be a moot point.

That might work, though it sounds like it wouldn't work without me having to modify rom files before they can be used.

That third method is probably what I'll go with since what I've got is already an XMLHttpRequest. Weirdly though I'd been made to understand that an array buffer is incompatible with it though. That should hopefully solve this issue so I can focus on finishing the other parts.

If you're curious on my current code it's this: (Note that this is a huge mess currently due to everything I've tried to get it to read right)

Code: Select all

(function() {
   var loadedRom;
   var memory = [];
   var maze = '¢Â2¢Ðp0@ ` q1  €@  @€';
   var maze2 = toHex(maze);
   var maze3;// = new Blob();
   var path = 'GAMES/MAZE';
   var maze4 = load_binary_resource(path);
   loadedRom = new XMLHttpRequest();
   loadedRom.responseType = "text";
   loadedRom.filepath = path;
   loadedRom.open('GET', path);
   loadedRom.onloadend = function() {
      maze3 = loadedRom.response;
      console.log("Hex3: " + toHex(maze3.toString()));
      console.log("Req3: " + maze3.toString());
   }
   loadedRom.send();
   
   console.log("1: " + maze);
   console.log("2: " + maze2);
   console.log('X: a21ec2013201a21ad014700430401200600071043120120012188040201020408010');//'a21e c201 3201 a21a d014 7004 3040 1200 6000 7104 3120 1200 1218 8040 2010 2040 8010'
   console.log("3: " + maze3);
   var mazetmp = "";
   for (var i = 0; i < maze4.length; i++) {
      mazetmp += maze4[i];
   }
   console.log("4: " + mazetmp);
})();
  function toHex(str) {
    var result = '';
    for (var i=0; i<str.length; i++) {
      result += str.charCodeAt(i).toString(16);
    }
    return result;
  }

The "a21ec2013201a21ad014700430401200600071043120120012188040201020408010" bit is what I'm supposed to be getting. I know this because coincidentally right after I started this project someone on my main forum posted their desktop version of the same project and I asked them if they had any helpful resources as well as for their exact version of the rom I'm using for getting my load function working plus the values they're storing to memory. The value I'm storing into the variable maze2 ("a21ec21321a21ad01470430401220602071431201220121820ac402010204020ac10") is the closest result but it's not right and as far as I can tell it's actually because of javascript treating strings as UTF-16.
💙💙💙
Image
Image


User avatar
chridd
Posts: 204
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » January 5th, 2016, 6:06 pm

Alice wrote:That might work, though it sounds like it wouldn't work without me having to modify rom files before they can be used.
No... I was thinking more like this (as a solution to the case where the files are stored on the user's computer, rather than on the server):
1. The first time the user runs a program, they choose a file using a file dialog
2. The program then puts a copy of the program into localStorage
3. When the user runs the program again, it gets the copy from localStorage rather than having to use a file dialog.
The original file doesn't have to be changed, and the copy in memory that it runs from doesn't have to be changed, but the copy in localStorage has to be a string. So step 2 has to somehow convert the file into a string, and step 3 has to convert it back into binary. What I suggested was one way of doing that, though there are others (e.g., store the program as a hexadecimal string).
Of course, this isn't necessary if the file is on the server, where the program can access it.

The "a21ec2013201a21ad014700430401200600071043120120012188040201020408010" bit is what I'm supposed to be getting. I know this because coincidentally right after I started this project someone on my main forum posted their desktop version of the same project and I asked them if they had any helpful resources as well as for their exact version of the rom I'm using for getting my load function working plus the values they're storing to memory. The value I'm storing into the variable maze2 ("a21ec21321a21ad01470430401220602071431201220121820ac402010204020ac10") is the closest result but it's not right and as far as I can tell it's actually because of javascript treating strings as UTF-16.
If you're curious, here's where the differences come from:
• There's a bug in toHex: if a byte has one hexadecimal digit (i.e., it's less than 0x10), then it'll only output one digit, whereas it should output 2 digits for each byte. (You can fix this with an if statement that adds "0" in the right place.)
• Somewhere along the line (which could be when copying the string into the program), null bytes (00) got changed into spaces (20). This doesn't have anything to do with UTF-16; many programs that handle text, regardless of encoding, don't like dealing with null characters. Firefox seems to allow null characters in strings if you write them as \0, but I wouldn't count on every browser dealing with them correctly.
• Somewhere along the line, characters were converted from Windows-1252 encoding to Unicode (character values are the same for the two encodings except for 0x80-0x9f (the actual representation in bytes is different, but that's not important here)), so 0x80 (€ in Windows-1252) was changed to 0x20ac (€ in Unicode).

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 5th, 2016, 6:25 pm

chridd wrote:No... I was thinking more like this (as a solution to the case where the files are stored on the user's computer, rather than on the server):
1. The first time the user runs a program, they choose a file using a file dialog
2. The program then puts a copy of the program into localStorage
3. When the user runs the program again, it gets the copy from localStorage rather than having to use a file dialog.
The original file doesn't have to be changed, and the copy in memory that it runs from doesn't have to be changed, but the copy in localStorage has to be a string. So step 2 has to somehow convert the file into a string, and step 3 has to convert it back into binary. What I suggested was one way of doing that, though there are others (e.g., store the program as a hexadecimal string).
Of course, this isn't necessary if the file is on the server, where the program can access it.

Oh, alright. I think I misunderstood how you meant it the first time then. I think the third method will still be easiest though since what I currently have is just about the same as what you suggested.
If you're curious, here's where the differences come from:
• There's a bug in toHex: if a byte has one hexadecimal digit (i.e., it's less than 0x10), then it'll only output one digit, whereas it should output 2 digits for each byte. (You can fix this with an if statement that adds "0" in the right place.)
• Somewhere along the line (which could be when copying the string into the program), null bytes (00) got changed into spaces (20). This doesn't have anything to do with UTF-16; many programs that handle text, regardless of encoding, don't like dealing with null characters. Firefox seems to allow null characters in strings if you write them as \0, but I wouldn't count on every browser dealing with them correctly.
• Somewhere along the line, characters were converted from Windows-1252 encoding to Unicode (character values are the same for the two encodings except for 0x80-0x9f (the actual representation in bytes is different, but that's not important here)), so 0x80 (€ in Windows-1252) was changed to 0x20ac (€ in Unicode).

Okay, this sounds like it'll help quite a lot actually. The last two bulletpoints are probably my fault since the string I'm manually logging into the variables in my code was just me opening the rom in Notepad++ and copy+pasting the data into my file. The third one in particular could be an issue either from Notepad++ opening the rom as ANSI (I never checked before but that appears to be what it's going for here) or it could be because my script is UTF-8. So once I get the first issue fixed and load it through the XMLHttpRequest then it should hopefully turn out correctly.

This has really been a huge help so thanks a lot.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 24th, 2016, 10:49 pm

I really love overcomplicating things only to realize something simpler would work just as well and with fewer issues. My userscript for that other forum adds buttons that take you to the last post on a given thread. At first I implemented this in a fairly hacky way that exploits the way the unread posts link works in threads you've already read all the posts in. This only took you to the last page (unless that page had enough posts that the next one would make a new page, in which case it would take you to a completely blank page instead) but I offset this by setting a Greasemonkey local storage variable then checking for that variable when you load the page. This worked but it would only work on one thread at a time. If you opened a second thread before the first fully loaded then only the first one would take you to the last post.

So yesterday I got rid of the bit where it utilized the unread posts link which let me add the link to all threads even if they were unread. I also changed it so it kept track of what thread you clicked. This still has the side effect of having the same issue as earlier but only if you click the same thread multiple times so it's really not an issue. I was laying in bed this morning though and a thought popped into my head. Why don't I just do something like add "#lastpost" to the freaking last post links and check for that instead of some variable manipulation crap? That would not only be simpler but it would even fix that complete non-issue of it sometimes failing to take you to the last post under the right circumstances.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » January 31st, 2016, 12:08 pm

Turns out that the new spoilers I made for IsoNation that don't have that weird linebreak issue helped benefit my userscript too. The last post link I posted about has always had an issue (at least on my end since my internet is often really slow because our ISP is trash) where if the page is loading slowly, whether it's from your internet being slow or the server being slow, it won't quite take you to the last post because the script will activate before images and such load thus putting you a bit up the page from it. So I utilized the code that waits until the page is fully loaded then stuffs the spoilers on the page to delay the script taking you to the last post until the page is fully loaded.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 21st, 2016, 11:56 pm

Regular expressions are stupid and I hope they die a painful death
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » February 22nd, 2016, 12:06 am

Ashan wrote:Regular expressions are stupid and I hope they die a painful death

You should consider using something like this when trying to deal with regular expressions. Makes a world of difference.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 22nd, 2016, 12:36 am

Alice wrote:
Ashan wrote:Regular expressions are stupid and I hope they die a painful death

You should consider using something like this when trying to deal with regular expressions. Makes a world of difference.

I was actually using this one, which did help a tonne.

Although I did run into an issue where one wasn't working cause it doesn't take into account escape slashes necessary in Javascript. I see the one you linked does have a version specifically for Javascript though so that should help. I'll check it out.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » February 22nd, 2016, 12:38 am

Ashan wrote:I was actually using this one, which did help a tonne.

Although I did run into an issue where one wasn't working cause it doesn't take into account escape slashes necessary in Javascript.

Regexr is alright, I used to use it myself. The one I linked is a lot better though, especially since it's not limited to only javascript. Unlike Regexr it explains exactly what your current regex is trying to do too making it easier to find errors.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 22nd, 2016, 2:36 am

Just spent another like half hour trying to figure out why my validateSignup() function wasn't working.

It's because when I made the function I called it validateSingup(). Today has not been a good day for me.
Image
► Show Spoiler

User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 27th, 2016, 5:08 am

I need to make an upvote/downvote type thing for an assignment and I have no idea where to start. They never really taught us this stuff and I've been trying to figure it out for a few days.

I need to figure out how to like... Set functions for a bunch of divs, and modify individual nodes in each of those divs, specifically to swap out the upvote/downvote images and whatnot. I have no idea how to do this though.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » February 27th, 2016, 5:27 am

Ashan wrote:I need to make an upvote/downvote type thing for an assignment and I have no idea where to start. They never really taught us this stuff and I've been trying to figure it out for a few days.

I need to figure out how to like... Set functions for a bunch of divs, and modify individual nodes in each of those divs, specifically to swap out the upvote/downvote images and whatnot. I have no idea how to do this though.

Fuck sake. I went to submit a response here and Firefox decided that was a GREAT time to randomly log me out of the site for no god damned reason.

Rough paraphrase of what I was trying to say: I'm assuming you're using javascript since your last post here mentioned it and it sounds like you're messing with webpages. You might try looking into javascript's getElementsByTagName command. I think that does what you're looking for.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 28th, 2016, 4:29 am

Alice wrote:
Ashan wrote:I need to make an upvote/downvote type thing for an assignment and I have no idea where to start. They never really taught us this stuff and I've been trying to figure it out for a few days.

I need to figure out how to like... Set functions for a bunch of divs, and modify individual nodes in each of those divs, specifically to swap out the upvote/downvote images and whatnot. I have no idea how to do this though.

Fuck sake. I went to submit a response here and Firefox decided that was a GREAT time to randomly log me out of the site for no god damned reason.

Rough paraphrase of what I was trying to say: I'm assuming you're using javascript since your last post here mentioned it and it sounds like you're messing with webpages. You might try looking into javascript's getElementsByTagName command. I think that does what you're looking for.

I'll give that a look. Thanks!
Image
► Show Spoiler

User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 29th, 2016, 7:22 am

It's always funny when you want to make one quick addition to your code before bed and then you look up to see this
Image
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » February 29th, 2016, 7:41 am

Ashan wrote:It's always funny when you want to make one quick addition to your code before bed and then you look up to see this
Image

I have this issue quite often too, lol. It's why I'll often open an entirely new browser window just for coding references and toss it on my second monitor. Even with TabMix Plus' ability to have multiple rows of tabs it ends up a huge mess at times.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » February 29th, 2016, 7:44 am

Hah, yeah that was just my second monitor. My other one had another set of tabs, albeit not as full
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 3rd, 2016, 7:52 pm

Messing with things through Javascript without using jQuery is a lot weirder in general than using jQuery. Patreon's private message system annoys me. The textbox for them is set to four rows of text and this cannot be resized even through CSS because it relies on the textarea html element's rows attribute to decide what to use.

Code: Select all

"use strict";
var messagebox = document.getElementsByTagName("textarea")[0];
var typing = setInterval(function() {
   var charcount = Math.floor(messagebox.value.length / 150);
  if (charcount > 4) {
    if (charcount < 24) {
      messagebox.setAttribute("rows", charcount + 1);
    } else {
      messagebox.setAttribute("rows", 24);
    }
  } else {
    messagebox.setAttribute("rows", 4);
  }
}, 250);

Took awhile to actually work out what I needed but this will make the textarea dynamically resize according to how much you've typed in it similarly to how the post reply page on these forums works with a minimum size of four rows and a max size of twenty four. Only issue with it is that the 150 should actually be some dynamic number according to the element's width so that it can accommodate any screen size but since this is a personal use script and I basically always browse with Firefox maximized oh well.

Also that 150 number seems really off to me. One row at my resolution contains 156 characters. But because of the way it works a scrollbar will appear before it actually resizes for the first time and that scrollbar will push a few characters onto the next line and end up permanently staying there. Over large amounts of text 150 is inaccurate but not too terribly much so I guess it's not too big a deal. (144 characters at maximum textarea size, so almost one line.)

I could also probably setup an event handler watching deal instead of a timer to deal with resizing it but I haven't really messed with those much before and didn't really want to spend much time on the script. It was just annoying me that there was so little space and I didn't want to just set it to a static 24 rows.
💙💙💙
Image
Image


User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 5th, 2016, 2:15 pm

https://raw.githubusercontent.com/TheVa ... ria/NPC.cs

Oh god. I'm no expert coder, especially when it comes to C# but most of what happens here should happen in switch statements (maybe there's an even better way, as I said I'm no expert. I just know this way would at least be better) which would at least leave it in a readable state and, from what I know of switch statements, would be more efficient than all those nested if checks. Some of those nested ifs are so indented they even wrap lines on a 2560px wide monitor.
💙💙💙
Image
Image


User avatar
chridd
Posts: 204
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » March 5th, 2016, 3:40 pm

Alice wrote:maybe there's an even better way
Arrays.

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 5th, 2016, 3:59 pm

chridd wrote:Arrays.

That's a good point, that would be even simpler, wouldn't it?

I was looking around after reading that and found instructions to decompile it myself so I was looking at the most recent version of the items file. Instead of doing something like storing localization stuff in files it's all for some reason hard coded into the game. There's these monstrous if checks for each language the game supports followed by another set of grossly nested if loops for item modifier prefixes.

Ie: (only including one prefix here for brevity)

Code: Select all

public string AffixName_Old()
{
   string text = "";
   if (Lang.lang <= 1)
   {
      if (this.prefix == 1)
      {
         text = "Large";
      }
   }
   else if (Lang.lang == 2)
   {
      if (this.prefix == 1)
      {
         text = "Gross";
      }
   }
}

And it does this for five different languages. (There's 83 different prefixes btw.) Again, I'm no expert, but this one I do have a bit of knowledge on weirdly enough. It'd be better to store all the text for each language into a file for that language. Then you just include a handler that loads the proper language on game start/whenever you change the language in the settings and after that you would just need to code the entire section one single time instead since the strings it's trying to use would automatically be the localized version.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 7th, 2016, 1:22 am

I've been actually putting some real effort into learning Android development over the past while. It's kind of a lot to take in but I'm enjoying it. Right now I've played around by making an app that basically just displays quotes.
I started by following this guide which kinda has the basics of the Quote displaying, but I've expanded upon it with the ability to add quotes, background colors, and some animation touches. There's a few things I don't quite understand about Android development, like how to make things backwards compatible. There's a super neat built-in method called createCircularReveal, which is what I use for the background ripple effect, but it requires API 21 or higher, so right now my app only works on Lollipop or later.
Anyway, here's a video of the app:
https://www.youtube.com/watch?v=-7nIJuP9Irs

And you can view the source code at
http://quotr.haydenblai.se
(also there's that domain I was talking about buying recently.)
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 7th, 2016, 2:32 am

Android development is Java isn't it? Never actually messed with that at all.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 7th, 2016, 2:48 am

Yeah, it is. I actually originally started learning programming with Java specifically for this reason, but when I went to university they only taught C++ and now I'm probably better with that. But most of the syntax is the same anyway.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 7th, 2016, 3:35 am

From what I've seen most languages that are based on C's structure tend to be quite similar in general. Knowing C++ isn't a bad thing though either. While Android is Java, C++ is still the most commonly used programming language. Java does have the strength though of being more portable between systems since it runs in a virtual machine.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 10th, 2016, 7:24 pm

Been having fun learning Android development.
Here's a video of the most recent build:
https://www.youtube.com/watch?v=TAWHTcAqMDM

Notable changes since last time:
  • I found a third-party library that handles the ripple animation so I was able to lower the API minimum from 21 (Lollipop) to 16 (Jellybean)
  • Horizontal layout (not shown in the video because aspect ratios) with proper rotation handling
  • Animation interpolator on the ripple effect
  • Pull up and push down animations for the Add Quote screen
  • Back button on the top left of Add Quote screen
  • Custom toolbar
  • Converted all the drawables to vectors (this was a recent addition to Android as far as I know, and it's great cause before you needed like 5 folders for different DPIs)
  • Did some back-end fixing on the algorithms to select random colors which ensures that 2 adjacent quotes don't have matching colors, so you always get to see the nice ripple animation. This also applies to the color refresh.
  • (my favorite) swapped out the temporary "change colors" button to a refresh button on toolbar with a custom animation. I love how that turned out
And some other general code cleanup.
I'm still not quite sure the direction I want to take with this app. Ideally I'd like to publish it on the Play Store at some point but I need for it to actually do something interesting. I'm thinking I might add some kind of Twitter integration to display Tweets?
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 18th, 2016, 12:39 pm

Man I hate dealing with javascript sometimes.

Code: Select all

function splitText(string, width) {
   "use strict";
   console.log("string: " + string);
   console.log("length: " + string.length);
   var tmp = "", out = [];
   for (var i = 0; i < string.length; i++) {
      //console.log(string.charAt[i]);
      if (ctx.measureText(tmp).width + ctx.measureText(string.charAt(i)).width < width && string.charAt(i) !== "\n" && string.charAt(i) !== "\t") {
         tmp += string.charAt(i);
      } else if (string.charAt(i) == "\t") {
         tmp += "    ";
      } else {
         out[out.length] = tmp;
      tmp = "";
      }
      console.log(out[i]);
   }
   out[out.length] = tmp;
   return out;
}

This function is being fed a string of text and a pixel width. Its purpose is to return an array splitting that string according to the pixel width of the characters so it can be used to wrap lines for a canvas object. It worked fine up until I randomly started having with the charAt command. For some reason it's always undefined now.
Image
The first one is logging the string to the console, second one is the length of the string in characters, third set should be returning the output. (Though I should probably be logging my tmp variable instead but that's beside the point because either way it should not be returning undefined values constantly.)

I'm really not sure what even happened because it was working perfectly fine earlier. Now depending on what I change I either get this undefined crap constantly or I get random errors where it tries to claim charAt isn't a function when it definitely is.
💙💙💙
Image
Image


User avatar
chridd
Posts: 204
Joined: December 25th, 2014, 9:20 pm
Location: the internet
Contact:

Re: Programming Thread

Postby chridd » March 18th, 2016, 2:03 pm

Alice wrote:

Code: Select all

      //console.log(string.charAt[i]);
I think you mean string.charAt(i), or equivalently, string[ i ]
(technically, charAt[ i ] could have a value, because you can add array elements to any object, including functions, but adding array elements to charAt would just be confusing)

Code: Select all

      console.log(out[i]);
I think you're using the wrong index there.
or I get random errors where it tries to claim charAt isn't a function when it definitely is.
Sounds like the thing you're trying to call charAt on isn't a string.

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 18th, 2016, 2:15 pm

chridd wrote:
Alice wrote:

Code: Select all

      //console.log(string.charAt[i]);
I think you mean string.charAt(i), or equivalently, string[ i ]
(technically, charAt[ i ] could have a value, because you can add array elements to any object, including functions, but adding array elements to charAt would just be confusing)

Code: Select all

      console.log(out[i]);

I think you're using the wrong index there.
or I get random errors where it tries to claim charAt isn't a function when it definitely is.

Sounds like the thing you're trying to call charAt on isn't a string.

Whoops, you're right on that charAt[i] stuff. I didn't even notice that issue. I guess I'll add that to the list of random characters to be hitting when trying to do ()s. There've been several times recently where I've tried to enclose the conditions for an if loop inside curly braces too for some random reason.

For the charAt not being a function thing I really don't see how it could be having that issue. The

Code: Select all

console.log("string: " + string);

output is definitely always a string yet I occasionally get that issue. There was a point where it could end up being an array instead, due to this very function in fact, but I fixed that so there's no point that the value getting fed into the function should ever be anything other than a string.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 19th, 2016, 4:32 am

I've been doing so much web development for this kind of absurdly large assignment I got in my web development class where we had to learn PHP and SQL basically on our own and build an entire website from scratch and it's such a mess at this point I just get sad looking at the code.
I need to learn how to properly format my php code and whatnot cause the website I made has such ugly code it's insane.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 20th, 2016, 6:08 am


Finally I found a way to handle this. Now if I can figure out whatever the issue is with my word wrap function I can continue with this project. (Well if the text rendering scales too though I think it will.)

Instructions for use: Right click+drag to pan the view and mousewheel zooms in and out. This was a surprisingly massive pain in the ass to figure out but it turns out that zooming and translating entire views is one of the few things (unlike word wrap or even measuring the vertical height of a line of text...) I needed the canvas to do that's actually supported with a default command.

Edit: Actually I just noticed an issue with panning I overlooked. The origin for it resets each time which is not what I want to happen.

Edit2: I think I got it. That was actually simpler than I thought it'd be.

Edit3: And now it feels less clunky. Turns out that I had my mouse coordinate difference code in a slightly wrong spot. If was inside my panning check instead of outside it where it should be.
Ashan wrote:I've been doing so much web development for this kind of absurdly large assignment I got in my web development class where we had to learn PHP and SQL basically on our own and build an entire website from scratch and it's such a mess at this point I just get sad looking at the code.
I need to learn how to properly format my php code and whatnot cause the website I made has such ugly code it's insane.

Formatting your code should generally be the same for any language really. Doing a larger project though with stuff you're unfamiliar with can be a huge mess.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 20th, 2016, 4:24 pm

Alice wrote:
Ashan wrote:I've been doing so much web development for this kind of absurdly large assignment I got in my web development class where we had to learn PHP and SQL basically on our own and build an entire website from scratch and it's such a mess at this point I just get sad looking at the code.
I need to learn how to properly format my php code and whatnot cause the website I made has such ugly code it's insane.

Formatting your code should generally be the same for any language really. Doing a larger project though with stuff you're unfamiliar with can be a huge mess.

I find it's extra hard to do with markup languages like HTML though, and becomes even more of a hard-to-manage monstrosity when you add PHP into the mix, and you have like, an if block that's spanning an entire page and it's hard to see the start and end since it's got a jumble of HTML between it.
Image
► Show Spoiler

User avatar
Alice
⦂☽
Posts: 3732
Joined: December 23rd, 2014, 10:47 pm
Location: Wonderland
Contact:

Re: Programming Thread

Postby Alice » March 21st, 2016, 2:41 am

Ashan wrote:I find it's extra hard to do with markup languages like HTML though, and becomes even more of a hard-to-manage monstrosity when you add PHP into the mix, and you have like, an if block that's spanning an entire page and it's hard to see the start and end since it's got a jumble of HTML between it.

Oh yeah, definitely. Though it also depends on what exactly you're doing. Sometimes handling things a bit differently can help. For example on that userscript I made I'm injecting some html and a stylesheet into the page. These things are stored in a variable. Before I learned that the newest javascript revision has string templates which makes it a lot easier to format stuff like that I was stuck with dealing with it in one of two clunky ways.

Code: Select all

var whatever = '<div>\
other elements\
' + anothervariable + '\
</div>';

or

Code: Select all

var whatever = '<div>' +
'other elements' +
anothervariable +
'</div>';

The new way that ecma6 supports is this instead:

Code: Select all

var whatever = `<div>
other elements
${anothervariable}
</div>`;

Fairly bad examples since those are all pretty small but the point I'm trying to make is simply that you might try looking into other ways of handling things. You might come across a way that works better for you.
💙💙💙
Image
Image


User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 21st, 2016, 3:56 pm

Terry Davis is streaming on YouTube right now. Was it Tova that mentioned him a while back?
https://www.youtube.com/watch?v=hxyiafJ7CeQ

Tovarisch Red Yoshi wrote:ping
Image
► Show Spoiler

User avatar
Ashan
Posts: 487
Joined: February 15th, 2015, 1:33 am
Location: Saskatchewan
Contact:

Re: Programming Thread

Postby Ashan » March 21st, 2016, 4:22 pm

He's legitimately scary.
He was talking about how god is completely just, and then he started talking about how his doctor told him he thinks 1000ms ahead, and then he started calling him a retard and the n-word.
There's also some weirdo in chat that said, and I quote:
if it's a literal nigger id be kind of scared, what with blacks getting into med school more easily from AFFIRMATIVE ACTION.


edit: He hopped onto Reddit for a second, and I managed to get a glimpse of his username. He's absolutely insane.
https://www.reddit.com/user/templeosv402
Image
► Show Spoiler


Return to “General Perversion”

Who is online

Users browsing this forum: No registered users and 3 guests